0.2.0 • Published 6 years ago

funsenal v0.2.0

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

funsenal

An opinionated function programming arsenal. It is not a general purpose fp lib that offers the things you would expect from lodash or Ramda.

This is an experiment to make fp easier with the recurring patterns.

to start

npm i funsenal

examples

import {maps} from "funsenal"

suite("maps Tests", () => {
    before(() => {
        chai.should();
    });

    test("collect should be able to collect turples", () => {
        maps.collect([['hello', 1], ["world", 2]]).should.deep.equal({
            hello: 1,
            world: 2
        })
    });

    test("collect should be able to collect key-val pairs", () => {
        maps.collect([{ key: 'hello', val: 1 }, { key: 'world', val: 2 }]).should.deep.equal({
            hello: 1,
            world: 2
        })
    });

    test("pickMap should return a map with picked keys and applied mapping", () => {
        maps.pickMap({ hello: i => i + 2 }, { hello: 1, world: 2 }).should.deep.equal({
            hello: 3
        })
    });

    test("specMap should return a map that matches the spec by applying the mappings to the input", () => {
        maps.specMap({ greeting: m => m.world }, { hello: 1, world: 2 }).should.deep.equal({
            greeting: 2
        })
    });

    test("mergeMap should first apply the mappings and merge the output", () => {
        maps.mergeMap({ hello: 1, world: 2 },
            i => { return { hi: i.hello } },
            j => { return { oldWorld: j.world, newWorld:j.world + 1 } }).should.deep.equal({
                hi: 1,
                oldWorld: 2,
                newWorld: 3
            })
    });
});