sortable-map
v0.0.17
Published
Dictionary data structure with flexible sort capabilities
Downloads
22
Maintainers
Readme
sortable-map
Dictionary data structure with flexible sort capabilities
Installation
npm i -S sortable-map
Usage
import SortableMap from 'sortable-map';
const SortableMap = require('sortable-map');
Example
const map = new SortableMap();
const key = 'foo' // Key must be a string literal
const value = 'bar; // Value can be anything
map.add(key, value);
map.has('foo'); // true
map.find('foo'); // 'bar'
map.findAll(); // [{ key: 'foo', value: 'bar' }]
map.isEmpty(); // false
map.count(); // 1
may.keys(); // [ 'foo' ]
may.values(); // [ 'bar' ]
map.has('baz'); // false
map.find('baz'); // undefined
map.delete('foo'); // 'bar'
map.count() // 0
map.add('foo', 'bar');
map.add('ham', 'egg');
map.clear();
map.count(); // 0
Sorting
By default findAll()
returns the map sorted by key ascending. If your values are objects, pass a property name (findAll('sortOrder)
) for finer sorting:
map.add('abc', { sortOrder: 20 });
map.add('xyz', { sortOrder: 0 });
map.add('nop', { sortOrder: 10 });
map.findAll(); // [{key: 'abc', value: { sortOrder: 20 }, ...];
map.findAll('sortOrder'); // [{key: 'xyz', value: { sortOrder: 0 }, ...];
Iterators
const map = new SortableMap();
map.add('foo', 'bar');
forEach(cb)
map.forEach(entry => {
entry.key; // 'foo'
entry.value; // 'bar'
));
forEachKey(cb)
map.forEachKey(key => {
key; // 'foo'
));
forEachValue(cb)
map.forEachValue(value => {
value; // 'bar'
));