🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

fantasy-lenses

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fantasy-lenses

Composable, immutable getters and setters.

latest
Source
npmnpm
Version
0.1.1
Version published
Maintainers
1
Created
Source

Fantasy Lenses

Lenses are composable, immutable getters and setters. Composable in that they allow updating of nested data structures. Immutable in that the setters return copies of the whole data structure.

Examples

Nested updating

var person = {
        name: "Brian McKenna",
        location: {
            number: 1006,
            street: "Pearl St",
            postcode: 80302
        }
    },
    objectLens = require('fantasy-lenses').Lens.objectLens,
    locationLens = objectLens('location'),
    numberLens = objectLens('number'),
    store = locationLens.andThen(numberLens).run(person);

console.log(store.get());
// 1006

console.log(store.set(1007));
// { name: 'Brian McKenna',
//   location: { number: 1007, street: 'Pearl St', postcode: 80302 } }

Accessing optional fields

var data = [{
        name: "First record",
        config: {
            type: 2
        }
    }, {
        description: "Hello world",
        config: {
            type: 3
        }
    }, {
        name: "Third record"
    }],
    objectLens = require('fantasy-lenses').PartialLens.objectLens,
    configLens = objectLens('config'),
    typeLens = objectLens('type'),
    configTypeLens = configLens.andThen(typeLens);

console.log(data.filter(function(o) {
    return configTypeLens.run(o).fold(
        function(store) {
            // Get the field's content
            return store.get() == 2;
        },
        function() {
            // Didn't find field
            return false;
        }
    );
}));
// [ { name: 'First record', config: { type: 2 } } ]

Keywords

fantasyland

FAQs

Package last updated on 15 Aug 2013

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