npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

associative

v1.0.4

Published

Dictionary implementation based on JavaScript object (associative) arrays

Downloads

3

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