@afoot/sorted-array / Exports / SortedArray
Maintains a sorted array of data.
Example
Instantiated with:
import {SortedArray} from SortedArray;
const arr = new SortedArray();
• new SortedArray(params?
)
Define initial data, sort algo, and perform initial sort.
Name | Type | Description |
---|---|---|
params |
SortedArrayParams |
An intitial data array or a config object. |
index.ts:87
• Private
#config: SortedArrayParams
Default settings.
index.ts:52
• Private
#data: DataSet
Instance state. Source of truth for the module.
index.ts:48
• Private
#sortCallback: SortCallback
Overrides default sorting algorithm.
index.ts:50
• get
log(): DataSet
Getter to return the sorted data.
Example
const fruit = new SortedArray(['banana', 'pineapple', 'apple']);
fruit.log; //returns ['apple','banana','pineapple']
The sorted instance data.
index.ts:182
▸ Private
#fnSortDefault(a
, b
): number
Built-in sorting algorithm.
Name | Type | Description |
---|---|---|
a |
DataItem |
Comparator to sort against. |
b |
DataItem |
Comparator to sort against. |
number
One of [0.1,-1]
to determine sort order.
index.ts:65
▸ Private
#sort(): DataSet
Applies SortCallback on the instance data.
Sorted Data DataSet
index.ts:78
▸ add(item
): DataSet
Add a new element to SortedArray.#data.
Example
const sortedArray = new SortedArray([5, 2]);
sortedArray.add(3); // returns [2,3,5]
Name | Type | Description |
---|---|---|
item |
DataItem |
Input to append to the instance data. |
Sorted Array DataSet
index.ts:118
▸ delete(index
): DataSet
Delete an element from the instance data.
Remarks
This is a departure from the return value of Array.prototype.splice()
, which returns an array of the deleted elements. Here, we keep to the pattern and return the new array.
Example
const fruit = new SortedArray(['banana', 'pineapple', 'cheese']);
fruit.delete(1); //returns ['banana','pineapple'];
Name | Type | Description |
---|---|---|
index |
number |
Sorted index of the array to delete. |
Sorted Array DataSet
index.ts:164
▸ edit(index
, item
): DataSet
Modify an existing element in SortedArray.#data.
Example
const fruit = new SortedArray(['apples','pizza','bananas']);
const output = fruit.edit(2, 'peaches'); // Index is the _sorted_ array index.
console.log(output); // returns ['apples','bananas','peaches']
Name | Type | Description |
---|---|---|
index |
number |
Sorted index of the array to update. |
item |
DataItem |
The data to replace the instance entry at that index. |
Sorted array of DataItem
index.ts:142