sortable-map

0.0.17 • Public • Published

sortable-map

npm version Build Status Coverage Status Dependency Status devDependencies Status

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

Package Sidebar

Install

npm i sortable-map

Weekly Downloads

4

Version

0.0.17

License

MIT

Last publish

Collaborators

  • mikechabot