Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

fantasyland/fantasy-lenses

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 } } ]

About

Composable, immutable getters and setters.

Resources

Stars

Watchers

Forks

Packages

No packages published