Chainable Object
Create objects with chainable properties
Allows the creation of D3-style chainable objects.
Usage
Creating chainable objects
var chainable = ; // create chainable objectvar instance = ; // now use it //retrieve a valueconsole; //outputs: "a value for qux" //set a value directlyinstance; //sets the value of `qux` to "other value"console; //outputs: "other value" //set a nested value through the use of an objectinstance; //sets the value of `qux` to "yet another value"console; //outputs: "yet another value"
Processing of values
If you need to process the value before it's set, you can provide a processor function:
// create chainable objectvar instance = ; instance;console; //outputs: "BURN MOTHERFUCKER, BURN !!!"
v1.2.0 and up
Processors receive the name of the property as a second argument:
var instance = ;instance; //output: 'name: foo'
Combining processors and initial values
Just wrap 'm in an Array. The processor always goes first!
// create chainable objectvar instance = ; console; //outputs: "initial value"instance;console; //outputs: "BURN MOTHERFUCKER, BURN !!!"
CAVEAT: if you want to set a function or an object as an initial value, you HAVE TO use the Array syntax:
// create chainable objectvar instance = ; console; //outputs: "[Function]"console; //outputs: "10"
Existing objects and prototypes
You can mix chainable properties into an already existing object:
var instance = { console; } // mixin; console; //outputs: "bar"console; //outputs: "Heads will roll!"
The same applies to prototypes:
{} // mixin; var instance = ;console; //outputs: "Too drunk to fuck"
Setting multiple root values at once
v1.1.0 and up
Nested objects can be updated by passing a vanilla object to the setters:
instance = ; //set a nested value through the use of an objectinstance; //sets the value of `bar` to "yet another value"console; //outputs: "yet another value"
However, what if you'd want to set the value of foo
and the other root properties qux
and baz
? chainable-object
adds a values
accessor especially for this purpose:
instance;console; //outputs: 'b'
You can use it to retrieve a vanilla object with all the values too (aliased to toObject
, since that's what it really does):
console;//outputs: foo: bar: 'a' qux: 'b' baz: 'c'
get
and set
v1.2.0 and up
Another way to access the properties (with a bit more leeway) is to use get
and set
:
var instance = ; instance; // creates a 'foo' methodconsole; // output: 900console; // output: 900
get
allows you to pass default values, in case the property has not been set yet:
var instance = ; var value = instance;console; // outputs: 'a default value for foo'
get
will not create a property, i.e. this will throw:
var instance = ;instance;instance; // throws: TypeError: instance.foo is not a function
Installation and other shizzle
Use bower
or npm
to install chainable-object
. You can use it on the front- and backend, but for clientside you'll need webpack
or browserify to get it going. We'll have none of that global variable shizzle.
Dependencies: lodash
If you don't want to pull in lodash
entirely or you prefer underscore
you can configure webpack to swap lodash
with Your Preference (TM).
//file: webpack.config.js or some suchresolve: alias: "lodash": "underscore/underscore.min"
ATM chainable-object
uses the following lodash functions: isObject
, isArray
, isFunction
and each
, i.e. just replace "underscore/underscore.min" in the above example with the location of your file where you provide those functions.
License
MIT