mount-expressions
Version:
A simple ultility function to make working with values, arrays and objects chainable. All values are wrappered with accesser functions that accept callbacks. Making calls, chainable and fluent.
66 lines (48 loc) • 2.05 kB
JavaScript
const mount = require('./mount');
/** ======================================
Mounting Primatives
======================================= **/
// Strings
var s = 'john doe';
m = mount( s, 'name')
.name(function( name, store, mount, prop, input ){
mount(32, 'age'), console.log( 'Welcome back ' + name );
})
.age(function(age){
console.log(age)
});
/** ======================================
Mounting Objects
======================================= **/
var o = { name: "john doe", age: 22 },
m = mount(o)
.name(function( name, store, mount, prop, input ){
console.log('the user\'s name is ' + name + ' and he is ' + store.age + ' years old' );
});
/** ======================================
Mounting Arrays
======================================= **/
var a = ['john doe', 22 ];
m = mount(a)[0](function( name, store, mount, prop, input ){
console.log('Welcome back ' + name);
})
// mapTo
var a = ['john doe', 22 ];
m = mount(a)
.mapTo({name: 0, age: 1})
.name(function(name, person){ console.log( 'hello ' + name + ', your age is ' + person.age )})
/** ======================================
Remounting Mounted Stores
======================================= **/
var o = { name: "john doe", age: 22 };
var r = mount(o).name((name, person ) => { person.name = name.toUpperCase() } );
var s = mount(r).name(function(name){ return name }); // explicit return value "JOHN DOE"
console.log(s)
/**
Helper Functions
*/
// .has() checks for the presence of a property, and conditionaly calls one of two optional callbacks.
// .hasNot() checks for the absence of a property, and conditionaly calls one of two optional callbacks.
var o = {},
m = mount(o).has('name', null, function(n,s,m) { m('John Doe', 'Name'); return s; });
m = mount(o).hasNot('age', function(){ console.log('not found!')});