associative
v1.0.4
Published
Dictionary implementation based on JavaScript object (associative) arrays
Downloads
3
Maintainers
Readme
associative.js
Dictionary class based on JavaScript object (associative) arrays
API
declare namespace Associative { type Func = (currentValue: any, key: string, index?: number) => any type Predicate = (currentValue: any, key: string, index?: number) => boolean type Accumulator = (accumulatedValue: any, currentValue: any, index?: number) => any
class Dictionary {
constructor(source?: Object | any[]);
hasKey(key): boolean;
get(key): Object;
put(key, value);
remove(key);
keys(): string[];
values(): any[];
entries(): { key: string, value: any }[];
getObject(): Object;
count(): number;
select(func): Dictionary;
where(func): Dictionary;
accumulate(initial, func);
forAll(func);
sum(func): number;
avg(func): number;
max(func): number;
min(func): number;
take(count): Dictionary;
skip(count): Dictionary;
toString();
}
}
Example
const Dictionary = require("associative")// as typeof Associative.Dictionary; const consoleX = require("console-x");
var dict = new Dictionary({ foo: 'fooo', bar: 'baar' });
consoleX.notify("Key 'foo' exists?", dict.hasKey('foo')); // true consoleX.notify("Key 'zen' exists?", dict.hasKey('zen')); // false
consoleX.notify("Value of key 'foo'", dict.get('foo')); // "fooo" consoleX.notify("Value of key 'baar'", dict.get('baar')); // undefined
consoleX.warn("put 4 into key 'four'") dict.put("four", 4); // new key is "4" consoleX.notify("Value of key 'four'", dict.get("four")); // "four"
consoleX.warn("Put 'seven' into key 7") dict.put(7, "seven"); consoleX.notify("Value of key 7", dict.get(7)); // 7
consoleX.notify("Dictionary Keys", dict.keys()); // [foo, bar, 4, 7]
consoleX.warn("Remove nonexistent key 'test'") dict.remove("test"); // key does not exist, nothing removed consoleX.notify("Dictionary Keys", dict.keys()); // [foo, bar, 4, seven]
consoleX.notify("Dictionary Values", dict.values()); // seven,fooo,baar,4
consoleX.warn("Remove key 'four'")
dict.remove('four'); // key "four" removed
consoleX.notify("Dictionary Entries", JSON.stringify(dict.entries())); // [{"key":"7","value":"seven"},{"key":"foo","value":"fooo"},{"key":"bar","value":"baar"}]
consoleX.notify("Entries count", dict.count()); //
consoleX.notify(as object
, JSON.stringify(dict.getObject())); // {"7":"seven","foo":"fooo","bar":"baar"}
consoleX.notify("Select value lengths", dict.select((item) => item.length).toString()); consoleX.notify("Where value length > 4", dict.where((item) => item.length > 4).toString()); consoleX.notify("Accumulate concat", dict.accumulate('', (currentAccumulation, newItem) => currentAccumulation + newItem)); consoleX.notify("Sum", dict.sum((item) => item.length)); //b13 consoleX.notify("Avg", dict.avg((item) => item.length)); // 4.333333333333333 consoleX.notify("Min", dict.min((item) => item.length)); // 4 consoleX.notify("Max", dict.max((item) => item.length)); // 5
consoleX.notify("Take 3", dict.take(3).toString()); consoleX.notify("Skip 2", dict.skip(2).toString());
consoleX.notify("Entries count", dict.count());
Install
npm install associative --save