This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(targeting-map): refactored to extract a TargetingMap class to DRY…
… slot and service
- Loading branch information
Seth Yates
committed
Sep 17, 2016
1 parent
b8b1a1c
commit b60e34d
Showing
8 changed files
with
276 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Manages setting and retrieving targeting. | ||
* | ||
* @experimental | ||
*/ | ||
export default class TargetingMap { | ||
/** | ||
* Creates a new {@link TargetingMap} instance. | ||
*/ | ||
constructor() { | ||
this._targeting = {}; | ||
} | ||
|
||
/** | ||
* Sets custom targeting parameters for a given key. | ||
* | ||
* @param {string} key Targeting parameter key. | ||
* @param {string|!Array<string>} value Targeting parameter value or array of values. | ||
*/ | ||
set(key, value) { | ||
if (Array.isArray(value)) { | ||
this._targeting[key] = value; | ||
} else { | ||
this._targeting[key] = [value]; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a specific targeting parameter that has been set. | ||
* | ||
* @param {string} key The targeting key to look for. | ||
* @returns {!Array<string>} The values associated with this key, or an empty | ||
* array if there is no such key. | ||
*/ | ||
get(key) { | ||
return this._targeting[key] || []; | ||
} | ||
|
||
/** | ||
* Returns the list of all targeting keys that have been set. | ||
* | ||
* @returns {!Array<string>} Array of targeting keys. Ordering is undefined. | ||
*/ | ||
keys() { | ||
return Object.keys(this._targeting); | ||
} | ||
|
||
/** | ||
* Clears custom targeting parameters for a given key. | ||
* | ||
* @param {string} [key] Targeting parameter key. | ||
*/ | ||
clear(key) { | ||
if (key == null) { | ||
this._targeting = {}; | ||
} else { | ||
delete this._targeting[key]; | ||
} | ||
} | ||
|
||
/** | ||
* Returns the current targeting key-value dictionary. | ||
* | ||
* @returns {Object<string, Array<string>>} the targeting map. | ||
*/ | ||
all() { | ||
return Object.assign({}, this._targeting); | ||
} | ||
} |
Oops, something went wrong.