Package Exports
- indexable-array
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (indexable-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Indexable Array
Extended native JavaScript Array which provides indexed lookup similar to native Map.
Installation
npm install indexable-array
Description
indexable-array
extends native JavaScript array and provides indexed lookup features similar to native Map
by using
Proxy
for shallow change detection.
Synopsis
import IndexableArray, { Self } from "indexable-array";
const users = new IndexableArray({ id: 23, name: "George" }, { id: 92, name: "George" }).addIndex("name", Self);
const otherUsers = new IndexableArray({ id: 12, name: "Hans" }, { id: 18, name: "Tanja" }).addIndex("name").addSelfIndex();
Array.isArray(users); // true
users.getIndex("George"); // 1
users.get("George"); // Get first George: { id: 23, name: "George"}
users.get("George", { fromIndex: 1 }); // Get first George starting from index 1: { id: 23, name: "George"}
users.getAllIndexes("George"); // [0, 1]
// Replace George with Henry
const newUser = { id: 21, name: "Henry" };
users[0] = newUser;
users.getIndex(newUser); // 0 - It is possible to index whole object by { selfIndex: true } option.
// Add another Henry
users.splice(1, 1, { id: 34, name: "Henry" });
users.getAllIndexes("Henry"); // [0, 1];
// You may want to disable en re-enable index for heavy updates for performance reasons.
users.disableIndex(); // Disable index before heavy updates.
// ... many many many updates here
users.enableIndex(); // Index is recreated from scratch.
// Do NOT change deeply nested values in indexed fields.
// users[0].name = "DON'T DO THIS"; // WRONG: Sub fields (i.e. [0]."name") of the array is not watched, so index does not get updated.
// To change nested values use `set()`
users.set(0, "name", "OK"); // Index updated.
// or (not preferred because of expensive index creation for a small update)
users.disableIndex();
users[0].name = "Prefer set()";
users.enableIndex(); // Index is recreated from scratch.
Details
- Written in TypeScript.
- Is a native
Array
(Array.isArray(indexableArray) === true
), so supports all array features. - 100% test coverage.
- Tracks all shallow changes via by using
Proxy
- Limited support for updating deep properties via
set()
method. - Uses
map
to index for very fast lookups. - Uses binary search for updates for faster index update.
- Disables and recreates index from scratch automatically for heavy update operations like
splice
if above threshold.. - Indexing may be disabled and re-enabled for heavy update operations manually.
API
IndexableArray
Extended native array class to access array elements by fast key lookups using binary search. Used for storing objects.
- IndexableArray
- .addIndex(keys) ⇒
this
- .addSelfIndex() ⇒
this
- .getIndex(value, [options]) ⇒
number
- .getAllIndexes(value, [options]) ⇒
Array.<number>
- .get(value, [options]) ⇒
Object
|undefined
- .getAll(value, [options]) ⇒
Array.<number>
- .has(value, [options]) ⇒
boolean
- .set(position, path, value) ⇒
void
- .disableIndex()
- .enableIndex()
- .addIndex(keys) ⇒
indexableArray.addIndex(keys) ⇒ this
Adds given keys to the index.
Returns: this
-
- This object.
Param | Type | Description |
---|---|---|
keys | string , Self |
List of keys to add to index. |
indexableArray.addSelfIndex() ⇒ this
Adds
Self
(whole object) to index.
Returns: this
-
- This object.
indexableArray.getIndex(value, [options]) ⇒ number
Returns the first index at which a given indexed value can be found in the array, or -1 if it is not present.
Returns: number
-
- The first index of the element in the array; -1 if not found.
Param | Type | Description |
---|---|---|
value | * |
Indexed value to search for. |
[options] | Object |
Options |
[options.key] | string |
Index field to look for value. Default lookup field is used if no key is provided. |
[options.fromIndex] | number |
The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array |
indexableArray.getAllIndexes(value, [options]) ⇒ Array.<number>
Returns all indexes at which a given indexed value can be found in the array, or empty array if it is not present.
Returns: Array.<number>
-
- All indexes of the element in the array; Empty array if not found.
Param | Type | Description |
---|---|---|
value | * |
Indexed value to search for. |
[options] | Object |
Options |
[options.key] | string |
Index field to look for value. Default lookup field is used if no key is provided. |
indexableArray.get(value, [options]) ⇒ Object
| undefined
Returns the first item at which a given indexed value can be found in the array, or
undefined
if it is not present.
Returns: Object
| undefined
-
- The first item with given indexed value in the array;
undefined
if not found.
Param | Type | Description |
---|---|---|
value | * |
Indexed value to search for. |
[options] | Object |
Options |
[options.key] | string |
Index field to look for value. Default lookup field is used if no key is provided. |
[options.fromIndex] | number |
The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array |
indexableArray.getAll(value, [options]) ⇒ Array.<number>
Returns all items at which a given indexed value can be found in the array, or empty array if it is not present.
Returns: Array.<number>
-
- All items with given indexed value in the array; Empty array if not found.
Param | Type | Description |
---|---|---|
value | * |
Indexed value to search for. |
[options] | Object |
Options |
[options.key] | string |
Index field to look for value. Default lookup field is used if no key is provided. |
indexableArray.has(value, [options]) ⇒ boolean
Determines whether an array includes a certain indexed value among its entries' keys, returning true or false as appropriate.
Returns: boolean
-
- True if indexed value is found among array's entries' keys.
Param | Type | Description |
---|---|---|
value | * |
Indexed value to search for. |
[options] | Object |
Options |
[options.key] | string |
Index field to look for value. Default lookup field is used if no key is provided. |
[options.fromIndex] | number |
The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array |
indexableArray.set(position, path, value) ⇒ void
Sets value at path of the object, which is one of the entires of array. To update fields of the objects, this method should be used. Otherwise index cannot be updated, because sub fileds are not tracked for chage detection.
Param | Type | Description |
---|---|---|
position | number |
Index of the item to be changed. |
path | string |
Item's path where value to be changed at. |
value | * |
New value to be assigned. |
Example
indexedArray[0].name = "DON'T DO THIS"; // WRONG: Sub fields (i.e. [0]."name") of the array is not watched, so index does not get updated.
indexedArray.set(0, "name", "OK"); // Index updated.
indexableArray.disableIndex()
Disables indexing of the array. It may be used to disable temporarily
- to do heavy updates for performance reasons,
- to do operations in sub fields. If indexing is not needed anymore, it is suggested to create a new native non-extended array and copy values into it for avoiding performance penalty of proxy array used in this library.
See: {IndexedArray#enableIndex} method.
Example
indexedArray.disableIndex();
indexedArray[0].name = "THIS IS OK NOW";
indexedArray.enableIndex(); // Index is recreated from scratch.
indexableArray.enableIndex()
Enables indexing and recreates index from scratch.
See: {IndexedArray#disableIndex} method.