enmap
Enhanced Map structure with additional utility methods.
Collection ⇐ Map
A enhanced Map structure with additional utility methods.
Can be made persistent
Kind: global class
Extends: Map
- Collection ⇐
Map
- .init() ⇒
Void
- .validateName() ⇒
boolean
- .close()
- .set(key, val) ⇒
Map
- .delete(key, bulk)
- .purge() ⇒
Promise
- .array() ⇒
Array
- .keyArray() ⇒
Array
- .first([count]) ⇒
*
| Array.<*>
- .firstKey([count]) ⇒
*
| Array.<*>
- .last([count]) ⇒
*
| Array.<*>
- .lastKey([count]) ⇒
*
| Array.<*>
- .random([count]) ⇒
*
| Array.<*>
- .randomKey([count]) ⇒
*
| Array.<*>
- .findAll(prop, value) ⇒
Array
- .find(propOrFn, [value]) ⇒
*
- .findKey(propOrFn, [value]) ⇒
*
- .exists(prop, value) ⇒
boolean
- .filter(fn, [thisArg]) ⇒
Collection
- .filterArray(fn, [thisArg]) ⇒
Array
- .map(fn, [thisArg]) ⇒
Array
- .some(fn, [thisArg]) ⇒
boolean
- .every(fn, [thisArg]) ⇒
boolean
- .reduce(fn, [initialValue]) ⇒
*
- .clone() ⇒
Collection
- .concat(...collections) ⇒
Collection
- .deleteAll() ⇒
Array.<Promise>
- .equals(collection) ⇒
boolean
- .sort([compareFunction]) ⇒
Collection
collection.init() ⇒ Void
Internal method called on persistent Enmaps to load data from the underlying database.
Kind: instance method of Collection
collection.validateName() ⇒ boolean
Internal method used to validate persistent enmap names (valid Windows filenames);
Kind: instance method of Collection
Returns: boolean
- Indicates whether the name is valid.
collection.close()
Shuts down the underlying persistent enmap database.
Kind: instance method of Collection
collection.set(key, val) ⇒ Map
Kind: instance method of Collection
Returns: Map
- The EnMap object.
Param | Type | Description |
---|
key | * | Required. The key of the element to add to the EnMap object. If the EnMap is persistent this value MUST be a string or number. |
val | * | Required. The value of the element to add to the EnMap object. If the EnMap is persistent this value MUST be stringifiable as JSON. |
collection.delete(key, bulk)
Kind: instance method of Collection
Param | Type | Default | Description |
---|
key | * | | Required. The key of the element to delete from the EnMap object. |
bulk | boolean | false | Internal property used by the purge method. |
collection.purge() ⇒ Promise
Completely deletes all keys from an EnMap, including persistent data.
Kind: instance method of Collection
collection.array() ⇒ Array
Creates an ordered array of the values of this collection, and caches it internally.
The array will only be reconstructed if an item is added to or removed from the collection,
or if you change the length of the array itself. If you don't want this caching behaviour,
use Array.from(collection.values())
instead.
Kind: instance method of Collection
collection.keyArray() ⇒ Array
Creates an ordered array of the keys of this collection, and caches it internally.
The array will only be reconstructed if an item is added to or removed from the collection,
or if you change the length of the array itself. If you don't want this caching behaviour,
use Array.from(collection.keys())
instead.
Kind: instance method of Collection
collection.first([count]) ⇒ *
| Array.<*>
Obtains the first value(s) in this collection.
Kind: instance method of Collection
Returns: *
| Array.<*>
- The single value if count
is undefined,
or an array of values of count
length
Param | Type | Description |
---|
[count] | number | Number of values to obtain from the beginning |
collection.firstKey([count]) ⇒ *
| Array.<*>
Obtains the first key(s) in this collection.
Kind: instance method of Collection
Returns: *
| Array.<*>
- The single key if count
is undefined,
or an array of keys of count
length
Param | Type | Description |
---|
[count] | number | Number of keys to obtain from the beginning |
collection.last([count]) ⇒ *
| Array.<*>
Obtains the last value(s) in this collection. This relies on array,
and thus the caching mechanism applies here as well.
Kind: instance method of Collection
Returns: *
| Array.<*>
- The single value if count
is undefined,
or an array of values of count
length
Param | Type | Description |
---|
[count] | number | Number of values to obtain from the end |
collection.lastKey([count]) ⇒ *
| Array.<*>
Obtains the last key(s) in this collection. This relies on keyArray,
and thus the caching mechanism applies here as well.
Kind: instance method of Collection
Returns: *
| Array.<*>
- The single key if count
is undefined,
or an array of keys of count
length
Param | Type | Description |
---|
[count] | number | Number of keys to obtain from the end |
collection.random([count]) ⇒ *
| Array.<*>
Obtains random value(s) from this collection. This relies on array,
and thus the caching mechanism applies here as well.
Kind: instance method of Collection
Returns: *
| Array.<*>
- The single value if count
is undefined,
or an array of values of count
length
Param | Type | Description |
---|
[count] | number | Number of values to obtain randomly |
collection.randomKey([count]) ⇒ *
| Array.<*>
Obtains random key(s) from this collection. This relies on keyArray,
and thus the caching mechanism applies here as well.
Kind: instance method of Collection
Returns: *
| Array.<*>
- The single key if count
is undefined,
or an array of keys of count
length
Param | Type | Description |
---|
[count] | number | Number of keys to obtain randomly |
collection.findAll(prop, value) ⇒ Array
Searches for all items where their specified property's value is identical to the given value
(item[prop] === value
).
Kind: instance method of Collection
Param | Type | Description |
---|
prop | string | The property to test against |
value | * | The expected value |
Example
collection.findAll('username', 'Bob');
collection.find(propOrFn, [value]) ⇒ *
Searches for a single item where its specified property's value is identical to the given value
(item[prop] === value
), or the given function returns a truthy value. In the latter case, this is identical to
Array.find().
All collections used in Discord.js are mapped using their id
property, and if you want to find by id you
should use the get
method. See
MDN for details.
Kind: instance method of Collection
Param | Type | Description |
---|
propOrFn | string | function | The property to test against, or the function to test with |
[value] | * | The expected value - only applicable and required if using a property for the first argument |
Example
collection.find('username', 'Bob');
Example
collection.find(val => val.username === 'Bob');
collection.findKey(propOrFn, [value]) ⇒ *
Searches for the key of a single item where its specified property's value is identical to the given value
(item[prop] === value
), or the given function returns a truthy value. In the latter case, this is identical to
Array.findIndex().
Kind: instance method of Collection
Param | Type | Description |
---|
propOrFn | string | function | The property to test against, or the function to test with |
[value] | * | The expected value - only applicable and required if using a property for the first argument |
Example
collection.findKey('username', 'Bob');
Example
collection.findKey(val => val.username === 'Bob');
collection.exists(prop, value) ⇒ boolean
Searches for the existence of a single item where its specified property's value is identical to the given value
(item[prop] === value
).
Do not use this to check for an item by its ID. Instead, use collection.has(id)
. See
MDN for details.
Kind: instance method of Collection
Param | Type | Description |
---|
prop | string | The property to test against |
value | * | The expected value |
Example
if (collection.exists('username', 'Bob')) {
console.log('user here!');
}
collection.filter(fn, [thisArg]) ⇒ Collection
Identical to
Array.filter(),
but returns a Collection instead of an Array.
Kind: instance method of Collection
Param | Type | Description |
---|
fn | function | Function used to test (should return a boolean) |
[thisArg] | Object | Value to use as this when executing function |
collection.filterArray(fn, [thisArg]) ⇒ Array
Identical to
Array.filter().
Kind: instance method of Collection
Param | Type | Description |
---|
fn | function | Function used to test (should return a boolean) |
[thisArg] | Object | Value to use as this when executing function |
collection.map(fn, [thisArg]) ⇒ Array
Identical to
Array.map().
Kind: instance method of Collection
Param | Type | Description |
---|
fn | function | Function that produces an element of the new array, taking three arguments |
[thisArg] | * | Value to use as this when executing function |
collection.some(fn, [thisArg]) ⇒ boolean
Identical to
Array.some().
Kind: instance method of Collection
Param | Type | Description |
---|
fn | function | Function used to test (should return a boolean) |
[thisArg] | Object | Value to use as this when executing function |
collection.every(fn, [thisArg]) ⇒ boolean
Identical to
Array.every().
Kind: instance method of Collection
Param | Type | Description |
---|
fn | function | Function used to test (should return a boolean) |
[thisArg] | Object | Value to use as this when executing function |
collection.reduce(fn, [initialValue]) ⇒ *
Identical to
Array.reduce().
Kind: instance method of Collection
Param | Type | Description |
---|
fn | function | Function used to reduce, taking four arguments; accumulator , currentValue , currentKey , and collection |
[initialValue] | * | Starting value for the accumulator |
collection.clone() ⇒ Collection
Creates an identical shallow copy of this collection.
Kind: instance method of Collection
Example
const newColl = someColl.clone();
collection.concat(...collections) ⇒ Collection
Combines this collection with others into a new collection. None of the source collections are modified.
Kind: instance method of Collection
Param | Type | Description |
---|
...collections | Collection | Collections to merge |
Example
const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
collection.deleteAll() ⇒ Array.<Promise>
Calls the delete()
method on all items that have it.
Kind: instance method of Collection
collection.equals(collection) ⇒ boolean
Checks if this collection shares identical key-value pairings with another.
This is different to checking for equality using equal-signs, because
the collections may be different objects, but contain the same data.
Kind: instance method of Collection
Returns: boolean
- Whether the collections have identical contents
Param | Type | Description |
---|
collection | Collection | Collection to compare with |
collection.sort([compareFunction]) ⇒ Collection
The sort() method sorts the elements of a collection in place and returns the collection.
The sort is not necessarily stable. The default sort order is according to string Unicode code points.
Kind: instance method of Collection
Param | Type | Description |
---|
[compareFunction] | function | Specifies a function that defines the sort order. if omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element. |