API Docs for: 3.11.0-git
Show:

IndexedMap Class

An ordered, indexed hash map data structure. Like Y.Map and Array got together and had a beautiful baby.

Provides constant time key-based lookups as well as numerical index-based lookups, splicing, and other array-like functionality. Keys may be any value, including objects.

The Y.IndexedMap class extends Y.Map. See that module's documentation for more details on Map fundamentals.

Constructor

IndexedMap

(
  • [entries]
  • [options]
)

Parameters:

  • [entries] Array[] | Map optional

    Array 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 optional

    Options.

    • [autoStamp=false] Boolean optional

      If true, objects used as keys will be automatically stamped with a unique id as the value of the property defined by the objectIdName option ("_yuid" by default) if that property isn't already set. This will result in much faster lookups for object keys.

    • [objectIdName="_yuid"] String optional

      Name 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.

Methods

_defaultSortFn

(
  • a
  • b
)
Number protected

Default sort comparator used when sort() is called without a callback.

Parameters:

  • a Array

    First entry to compare.

  • b Array

    Second entry to compare.

Returns:

Number:

Result of the comparison.

_indexMapKey

(
  • index
  • key
)
protected

Inherited from Map: src/sm-map/js/map.js:399

Indexes the given key to enable faster lookups.

Parameters:

  • index Number

    Numerical index of the key in the internal _mapKeys array.

  • key Mixed

    Key to index.

_indexOfKey

(
  • key
)
Number protected

Inherited from Map: src/sm-map/js/map.js:430

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 Mixed

    Key to look up.

Returns:

Number:

Index of the entry with the given key, or -1 if not found.

_isSafeKey

(
  • key
)
Boolean protected

Inherited from Map: src/sm-map/js/map.js:488

Returns true if the given string key is safe to use in environments that don't support Object.create().

Parameters:

Returns:

Boolean:

true if the key is safe.

_reindexMap

() protected

Inherited from Map: src/sm-map/js/map.js:501

Reindexes all the keys in this map.

_removeMapEntry

(
  • index
)
protected

Inherited from Map: src/sm-map/js/map.js:520

Removes the entry at the given index from internal arrays.

This method does not update the size property.

Parameters:

  • index Number

    Index of the entry to remove.

_sameValueZero

(
  • a
  • b
)
Boolean protected

Inherited from Map: src/sm-map/js/map.js:541

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 Mixed

    First value to compare.

  • b Mixed

    Second value to compare.

Returns:

Boolean:

true if a and b are the same value, false otherwise.

_updateMapSize

() protected

Inherited from Map: src/sm-map/js/map.js:564

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

Inherited from Map: src/sm-map/js/map.js:166

Deletes all entries from this map.

concat

(
  • [maps*]
)
Map

Inherited from Map: src/sm-map/js/map.js:182

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:

  • [maps*] Array[] | Map optional

    Zero or more maps or entry arrays to concatenate into the resulting map.

Returns:

Map:

New map containing the concatenated values of this map and all arguments.

delete

()

Inherited from Map: src/sm-map/js/map.js:576

Alias for remove().

each

(
  • callback
  • [thisObj]
)
chainable

Inherited from Map: src/sm-map/js/map.js:207

Executes the given callback function on each entry in this map.

To halt iteration early, return false from the callback.

Parameters:

  • callback Function

    Callback function.

    • value Mixed

      Value being iterated.

    • key Mixed

      Key being iterated.

    • map Map

      Reference to this map.

  • [thisObj] Object optional

    this object to use when calling callback.

entries

() Array

Inherited from Map: src/sm-map/js/map.js:236

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:

Array of entries.

entryAt

(
  • index
)
Array | Undefined

Returns the entry at the given index, or undefined if not found.

An entry is an array with two items, the first being a key and the second a value associated with that key.

Parameters:

  • index Number

    Index to look up.

Returns:

Array | Undefined:

Entry at the given index, or undefined if not found.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
]);

map.entryAt(1); // => ['b', 'battlestar galactica']
map.entryAt(5); // => undefined

forEach

()

Inherited from Map: src/sm-map/js/map.js:584

Alias for each().

get

(
  • key
  • [defaultValue]
)
Mixed

Inherited from Map: src/sm-map/js/map.js:256

Returns the value associated with the given key, or default if the key isn't found.

Parameters:

  • key Mixed

    Key to look up.

  • [defaultValue] Mixed optional

    Default value to return if key isn't found.

Returns:

Mixed:

Value associated with the given key, or default if the key isn't found.

has

(
  • key
)
Boolean

Inherited from Map: src/sm-map/js/map.js:271

Returns true if key exists in this map, false otherwise.

Parameters:

  • key Mixed

    Key to look up.

Returns:

Boolean:

true if key exists in this map, false otherwise.

indexOfKey

(
  • key
)
Number protected

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 Mixed

    Key to look up.

Returns:

Number:

Index of the entry with the given key, or -1 if not found.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
]);

map.indexOfKey('b'); // => 1
map.indexOfKey('z'); // => -1

keyAt

(
  • index
)
Mixed

Returns the key at the given index, or undefined if not found.

Parameters:

  • index Number

    Index to look up.

Returns:

Mixed:

Key at the given index, or undefined if not found.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
]);

map.keyAt(0); // => 'a'
map.keyAt(5); // => undefined

keys

() Array

Inherited from Map: src/sm-map/js/map.js:282

Returns an array of all the keys in this map.

Returns:

Array:

Array of keys.

merge

(
  • maps
)
chainable

Inherited from Map: src/sm-map/js/map.js:292

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']]);

Parameters:

  • maps Array[] | Map multiple

    One or more maps or entry arrays to merge into this map.

pop

() Array

Removes the last entry from this map and returns it.

Returns:

Array:

Entry that was removed, or undefined if this map is empty.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
]);

map.pop(); // => ['b', 'battlestar galactica']
map.pop(); // => ['a', 'airwolf']
map.pop(); // => undefined

push

(
  • entry
)
Number

Appends the given entry or entries to the end of this map and returns the map's new size.

Any entries that already exist in this map with the same keys as the new entries will be removed to make way for the new entries, regardless of their position. If this map has been sorted, you may need to re-sort it after appending new entries.

Parameters:

  • entry Array multiple

    One or more entries to append to this map.

Returns:

Number:

New size of this map.

Example:

var map = new Y.IndexedMap();

map.push(
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
); // => 2

map.get('a'); // => 'airwolf'

remove

(
  • key
)
Boolean

Inherited from Map: src/sm-map/js/map.js:337

Deletes the entry with the given key.

Parameters:

  • key Mixed

    Key to delete.

Returns:

Boolean:

true if the key existed and was deleted, false otherwise.

set

(
  • key
  • value
)
chainable

Inherited from Map: src/sm-map/js/map.js:358

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 Mixed

    Key to set.

  • value Mixed

    Value to set.

shift

() Array

Removes the first entry from this map and returns it. This will cause the indices of subsequent entries in the map to be adjusted.

Returns:

Array:

Entry that was removed, or undefined if this map is empty.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
]);

map.shift(); // => ['a', 'airwolf']
map.shift(); // => ['b', 'battlestar galactica']
map.shift(); // => undefined

slice

(
  • [begin]
  • [end]
)
IndexedMap

Returns a new IndexedMap containing a shallow copy of a portion of this map.

The returned map will be created using the same options and constructor as this map.

This method does not remove any entries or modify this map in any way.

Parameters:

  • [begin] Number optional

    Zero-based index at which to begin extracting entries. If begin is negative, it will be treated as relative to the end of the map. If begin is omitted, it defaults to 0.

  • [end] Number optional

    Zero-based index at which to end extraction. slice() extracts entries up to but not including end. If end is negative, it will be treated as relative to the end of the map. If _end is omitted, all elements between begin and the end of the map will be extracted.

Returns:

IndexedMap:

New IndexedMap containing a shallow copy of the specified portion of this map.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['m', 'manimal'],
    ['s', 'space: 1999']
]);

map.slice(1, 2);
// => new IndexedMap containing ['m', 'manimal']

map.slice(1);
// => new IndexedMap containing ['m', 'manimal'] and ['s', 'space: 1999']

map.slice(-1);
// => new IndexedMap containing ['s', 'space: 1999']

map.slice(0, -1);
// => new IndexedMap containing ['a', 'airwolf'] and ['m', 'manimal']

map.slice();
// => new IndexedMap containing all three entries (effectively a copy)

sort

(
  • [callback]
)
chainable

Sorts the entries in this map.

If a callback is not given, entries will be sorted by converting their values to strings and comparing the values in lexicographic order (a simple ASCII sort).

If a callback is given, entries will be sorted according to the return value of the callback. The two entries (a and b) passed to the callback are sorted based on the value the callback returns.

The callback should return -1 (or any negative number) to sort a before b, 0 if a and b are equal, 1 (or any positive number) to sort b before a.

Parameters:

  • [callback] Function optional

    Sort comparator callback. Receives two entriess as arguments, and should return an integer indicating whether the first is less than (-1), equal to (0), or greater than (1) the second.

    • a Array

      First entry to compare. An entry is an array in which the first element is the key and the second element is the value.

    • b Array

      Second entry to compare.

Example:

var map = new Y.IndexedMap([
    [0, 'manimal'],
    [1, 'space: 1999'],
    [2, 'airwolf']
]);

// ASCII sort by string values (the default).
map.sort();

map.entries();
// => [
//     [2, 'airwolf'],
//     [0, 'manimal'],
//     [1, 'space: 1999']
// ]

// Numerical sort by key (a custom sort).
map.sort(function (a, b) {
    return a[0] - b[0];
});

map.entries();
// => [
//     [0, 'manimal'],
//     [1, 'space: 1999']
//     [2, 'airwolf'],
// ]

// Reverse ASCII sort by value (a custom sort).
map.sort(function (a, b) {
    var aValue = a[1].toString(),
        bValue = b[1].toString();

    if (bValue === aValue) {
        return 0;
    }

    return bValue < aValue ? -1 : 1;
});

map.entries();
// => [
//     [1, 'space: 1999']
//     [0, 'manimal'],
//     [2, 'airwolf'],
// ]

splice

(
  • start
  • [count]
  • [entry*]
)
Array[]

Changes the contents of this map, adding new entries while removing old entries.

If one or more new entries are specified, they will be added to this map starting at start.

Any entries that already exist in this map with the same keys as the new entries will be removed to make way for the new entries, regardless of their position. If this map has been sorted, you may need to re-sort it after splicing in new entries.

Parameters:

  • start Number

    Index at which to start changing this map. A negative value will result in a position relative to the end of the map.

  • [count] Number optional

    Number of entries to remove, starting at start. If count is 0, no entries will be removed. If no count parameter is given, all entries after start will be removed.

  • [entry*] Array optional

    Zero or more new entries to add to this map. Each entry must be an array in which the first element is the key and the second element is the value to add.

Returns:

Array[]:

An array of entries that were removed, if any.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['m', 'manimal'],
    ['s', 'space: 1999']
]);

// Insert 'battlestar galactica' between 'airwolf' and 'manimal'.
map.splice(1, 0, ['b', 'battlestar galactica']); // => []

// Remove 'manimal'.
map.splice(2, 1); // => [['m', 'manimal']]

// Insert 'mission: impossible' and 'star trek' after
// battlestar galactica. Since 'star trek' reuses the 's' key, the
// 'space: 1999' entry will be removed.
map.splice(2, 0, ['m', 'mission: impossible'], ['s', 'star trek']);
// => [['s', 'space: 1999']]

// Remove everything after 'airwolf', because who needs those shows
// when you've got a show about a badass helicopter?
map.splice(1);
// => [['b', 'battlestar galactica'], ['m', 'mission: impossible'],
//     ['s', 'star trek']]

toJSON

() Array

Inherited from Map: src/sm-map/js/map.js:592

Returns a JSON-serializable representation of this map.

This is effectively an alias for entries(), but could be overridden to return a customized representation.

Returns:

Array:

JSON-serializable array of entries in this map.

unshift

(
  • entry
)
Number

Prepends the given entry or entries to the beginning of this map and returns the map's new size.

Any entries that already exist in this map with the same keys as the new entries will be removed to make way for the new entries, regardless of their position. If this map has been sorted, you may need to re-sort it after prepending new entries.

Parameters:

  • entry Array multiple

    One or more entries to append to this map.

Returns:

Number:

New size of this map.

Example:

var map = new Y.IndexedMap([
    ['m', 'manimal'],
    ['s', 'space: 1999']
]);

map.unshift(
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
); // => 2

map.entries();
// => [
//     ['a', 'airwolf'],
//     ['b', 'battlestar galactica'],
//     ['m', 'manimal'],
//     ['s', 'space: 1999']
// ]

valueAt

(
  • index
)
Mixed

Returns the value at the given index, or undefined if not found.

Parameters:

  • index Number

    Index to look up.

Returns:

Mixed:

Value at the given index, or undefined if not found.

Example:

var map = new Y.IndexedMap([
    ['a', 'airwolf'],
    ['b', 'battlestar galactica']
]);

map.valueAt(0); // => 'airwolf'
map.valueAt(5); // => undefined

values

() Array

Inherited from Map: src/sm-map/js/map.js:387

Returns an array of all the values in this map.

Returns:

Array:

Array of values.

Properties

_isIndexStale

Boolean protected

Inherited from Map: src/sm-map/js/map.js:110

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

Inherited from Map: src/sm-map/js/map.js:131

Internal index mapping string keys to their indices in the _mapKeys array.

_mapKeys

Array protected

Inherited from Map: src/sm-map/js/map.js:124

Internal array of the keys in this map.

_mapObjectIndices

Object protected

Inherited from Map: src/sm-map/js/map.js:138

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.

_mapOptions

Object protected

Inherited from Map: src/sm-map/js/map.js:147

Options that affect the functionality of this map.

_mapValues

Array protected

Inherited from Map: src/sm-map/js/map.js:157

Internal array of the values in this map.

size

Number

Inherited from Map: src/sm-map/js/map.js:100

The number of entries in this map.

Default: 0