Coder Social home page Coder Social logo

node-hashish's Introduction

Hashish

Hashish is a node.js library for manipulating hash data structures. It is distilled from the finest that ruby, perl, and haskell have to offer by way of hash/map interfaces.

Hashish provides a chaining interface, where you can do:

var Hash = require('hashish');

Hash({ a : 1, b : 2, c : 3, d : 4 })
    .map(function (x) { return x * 10 })
    .filter(function (x) { return x < 30 })
    .forEach(function (x, key) {
        console.log(key + ' => ' + x);
    })
;

Output:

a => 10
b => 20

Some functions and attributes in the chaining interface are terminal, like .items or .detect(). They return values of their own instead of the chain context.

Each function in the chainable interface is also attached to Hash in chainless form:

var Hash = require('hashish');
var obj = { a : 1, b : 2, c : 3, d : 4 };

var mapped = Hash.map(obj, function (x) {
    return x * 10
});

console.dir(mapped);

Output:

{ a: 10, b: 20, c: 30, d: 40 }

In either case, the 'this' context of the function calls is the same object that the chained functions return, so you can make nested chains.

Methods

forEach(cb)

For each key/value in the hash, calls cb(value, key).

map(cb)

For each key/value in the hash, calls cb(value, key). The return value of cb is the new value at key in the resulting hash.

filter(cb)

For each key/value in the hash, calls cb(value, key). The resulting hash omits key/value pairs where cb returned a falsy value.

detect(cb)

Returns the first value in the hash for which cb(value, key) is non-falsy. Order of hashes is not well-defined so watch out for that.

reduce(cb)

Returns the accumulated value of a left-fold over the key/value pairs.

some(cb)

Returns a boolean: whether or not cb(value, key) ever returned a non-falsy value.

update(obj1, [obj2, obj3, ...])

Mutate the context hash, merging the key/value pairs from the passed objects and overwriting keys from the context hash if the current obj has keys of the same name. Falsy arguments are silently ignored.

updateAll([ obj1, obj2, ... ])

Like multi-argument update() but operate on an array directly.

merge(obj1, [obj2, obj3, ...])

Merge the key/value pairs from the passed objects into the resultant hash without modifying the context hash. Falsy arguments are silently ignored.

mergeAll([ obj1, obj2, ... ])

Like multi-argument merge() but operate on an array directly.

has(key)

Return whether the hash has a key, key.

valuesAt(keys)

Return an Array with the values at the keys from keys.

tap(cb)

Call cb with the present raw hash. This function is chainable.

extract(keys)

Filter by including only those keys in keys in the resulting hash.

exclude(keys)

Filter by excluding those keys in keys in the resulting hash.

Attributes

These are attributes in the chaining interface and functions in the Hash.xxx interface.

keys

Return all the enumerable attribute keys in the hash.

values

Return all the enumerable attribute values in the hash.

compact

Filter out values which are === undefined.

clone

Make a deep copy of the hash.

copy

Make a shallow copy of the hash.

length

Return the number of key/value pairs in the hash. Note: use Hash.size() for non-chain mode.

size

Alias for length since Hash.length is masked by Function.prototype.

See Also

See also creationix's pattern/hash, which does a similar thing except with hash inputs and array outputs.

Installation

To install with npm:

npm install hashish

To run the tests with expresso:

expresso

node-hashish's People

Contributors

felixge avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.