New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

associative

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

associative

Dictionary implementation based on JavaScript object (associative) arrays

latest
Source
npmnpm
Version
1.0.4
Version published
Maintainers
1
Created
Source

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

Keywords

dictionary

FAQs

Package last updated on 27 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts