# associative

> Dictionary implementation based on JavaScript object (associative) arrays

Latest version **1.0.4** (published 2017-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install associative
pnpm add associative
yarn add associative
bun add associative
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2017-02-27 |
| First published | 2016-07-20 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | James A. Prempeh |
| Maintainers | prmph |
| Keywords | dictionary, associative, array, object |

## Links

- npm: https://www.npmjs.com/package/associative
- Repository: https://github.com/prmph/associative.js
- Homepage: https://github.com/prmph/associative.js#readme
- Issues: https://github.com/prmph/associative.js/issues
- npm.io page: https://npm.io/package/associative

## Dependencies (1)

- [type-detect](https://npm.io/package/type-detect.md) ^2.0.2

## Recent versions

- 1.0.4 (latest) — 2017-02-27
- 1.0.3 — 2016-09-16
- 1.0.2 — 2016-09-16
- 1.0.1 — 2016-09-10
- 1.0.0 — 2016-07-20

## 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

---
_Source: https://npm.io/package/associative · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
