@drm2/dictionary.js
v0.1.0
Published
A sortable Dictionary data structure for JavaScript (or Node).
Downloads
3
Maintainers
Readme
dictionary.js
A sortable Dictionary data structure for JavaScript (or Node).
installation
You can use NPM to install this package:
npm install --save @drm2/dictionary.js
usage
basic (as a map)
// include the package
var Dict = require('@drm2/dictionary.js');
var dict = new Dict();
dict.set('hello', 'world');
dict.set('cat', 'dog');
dict.set('rick', 'morty');
var message = dict.get('hello'); // returns 'world'
sorting (with the built in functions)
// using the same details from above
dict.sortByKeyAsc();
var message = dict.getAll();
This value of message
will be:
[
{ key: 'cat', value: 'dog' },
{ key: 'hello', value: 'world' },
{ key: 'rick', value: 'morty' }
]
sorting (implementing a custom function)
The sort method expects a compare function just like Array.prototype.sort
.
// using the same details from above
dict.sort(function (a, b)
{
// sort by key ascending, and if they are equal, sort by value ascending
if (a.key === b.key)
{
if (a.value === b.value) return 0;
return a.value < b.value ? -1 : 1;
}
return a.key < b.key ? -1 : 1;
});