Map Class
An ordered hash map data structure with an interface and behavior similar to (but not exactly the same as) ECMAScript 6 Maps.
Constructor
Map
-
[entries]
-
[options]
Parameters:
-
[entries]
Array[] | Map optionalArray or Map of entries to add to this map. If an array, then each entry should itself be an array in which the first item is the key and the second item is the value for that entry.
-
[options]
Object optionalOptions.
-
[autoStamp=false]
Boolean optionalIf
true
, objects used as keys will be automatically stamped with a unique id as the value of the property defined by theobjectIdName
option ("_yuid" by default) if that property isn't already set. This will result in much faster lookups for object keys. -
[objectIdName="_yuid"]
String optionalName of a property whose string value should be used as the unique key when present on an object that's given as a key. This will significantly speed up lookups of object-based keys that define this property.
-
Item Index
Methods
Methods
_indexMapKey
-
index
-
key
Indexes the given key to enable faster lookups.
Parameters:
-
index
NumberNumerical index of the key in the internal
_mapKeys
array. -
key
MixedKey to index.
_indexOfKey
-
key
Returns the numerical index of the entry with the given key, or -1
if
not found.
This is a very efficient operation with string keys, but may be slower with non-string keys.
Parameters:
-
key
MixedKey to look up.
Returns:
Index of the entry with the given key, or -1
if not
found.
_isSafeKey
-
key
Returns true
if the given string key is safe to use in environments that
don't support Object.create()
.
Parameters:
-
key
StringKey to check.
Returns:
true
if the key is safe.
_reindexMap
()
protected
Reindexes all the keys in this map.
_removeMapEntry
-
index
Removes the entry at the given index from internal arrays.
This method does not update the size
property.
Parameters:
-
index
NumberIndex of the entry to remove.
_sameValueZero
-
a
-
b
Returns true
if the two given values are the same value, false
otherwise.
This is an implementation of the ES6 SameValueZero comparison
algorithm. It's more correct than ===
in that it considers NaN
to be the
same as NaN
.
Note that 0
and -0
are considered the same by this algorithm.
Parameters:
-
a
MixedFirst value to compare.
-
b
MixedSecond value to compare.
Returns:
true
if a and b are the same value, false
otherwise.
_updateMapSize
()
protected
Updates the value of the size
property in old browsers. In ES5 browsers
this is a noop, since the size
property is a getter.
clear
()
chainable
Deletes all entries from this map.
concat
-
[maps*]
Returns a new map comprised of this map's entries combined with those of the maps or arrays passed as arguments. Does not alter this map or those given as arguments in any way.
Entries in later (rightmost) arguments will take precedence over entries in earlier (leftmost) arguments if their keys are the same.
This method also accepts arrays of entries in lieu of actual Y.Map instances.
The returned map will be created using the same options and constructor as this map.
Parameters:
Returns:
New map containing the concatenated values of this map and all arguments.
delete
()
Alias for remove()
.
each
-
callback
-
[thisObj]
Executes the given callback function on each entry in this map.
To halt iteration early, return false
from the callback.
entries
()
Array
Returns an array of all the entries in this map. Each entry is an array with two items, the first being a key and the second a value associated with that key.
Returns:
Array of entries.
forEach
()
Alias for each()
.
get
-
key
-
[defaultValue]
Returns the value associated with the given key, or default if the key isn't found.
Parameters:
-
key
MixedKey to look up.
-
[defaultValue]
Mixed optionalDefault value to return if key isn't found.
Returns:
Value associated with the given key, or default if the key isn't found.
has
-
key
Returns true
if key exists in this map, false
otherwise.
Parameters:
-
key
MixedKey to look up.
Returns:
true
if key exists in this map, false
otherwise.
merge
-
maps
Merges the entries from one or more other maps or entry arrays into this map. Entries in later (rightmost) arguments will take precedence over entries in earlier (leftmost) arguments if their keys are the same.
This method also accepts arrays of entries in lieu of actual Y.Map instances, so the following operations have the same result:
// This...
map.merge(new Y.Map([['a', 'apple'], ['b', 'bear']]));
// ...has the same result as this...
map.merge([['a', 'apple'], ['b', 'bear']]);
remove
-
key
Deletes the entry with the given key.
Parameters:
-
key
MixedKey to delete.
Returns:
true
if the key existed and was deleted, false
otherwise.
set
-
key
-
value
Sets the value of the entry with the given key. If the key already exists, its value will be overwritten; otherwise it will be created.
The key may be any JavaScript value (including both primitives and objects), but string keys will allow fast lookups, whereas non-string keys may result in slower lookups.
Parameters:
-
key
MixedKey to set.
-
value
MixedValue to set.
Properties
_isIndexStale
Boolean
protected
Whether or not the internal key index is in need of reindexing.
Rather than reindexing immediately whenever it becomes necessary, we use this flag to allow on-demand indexing the first time an up-to-date index is actually needed.
This makes multiple remove()
operations significantly faster, at the
expense of a single reindex operation the next time a key is looked up.
_mapKeyIndices
Object
protected
Internal index mapping string keys to their indices in the _mapKeys
array.
_mapObjectIndices
Object
protected
Internal index mapping object key ids to their indices in the _mapKeys
array. This is separate from _mapKeyIndices
in order to prevent collisions
between object key ids and string keys.