field
Easily get, set, stub values of a field in a JavaScript object.
Why?
I got tired of doing this:
var port = cfg && cfgenv && cfgenvprod && cfgenvprodport
now...
var field = var port = field
or if you prefer ":"...
var field = var port = field
I also got tired of writing long stubs:
var stub = window: localStorage: { return 'data' } length: 1
now...
var field = var stub = {}
Installation
npm i --save field
Usage
get
Gets the property value of the object. Returns undefined
if it does not exist.
var field = var dbPort = field
set
Sets the property value of the object. Returns the old value. If the field does not exist
then it returns undefined
and creates the object chain and sets the value.
var field = var database = {} console// => undefined // will return undefined since it never existed beforefieldconsole// => 27017
Binding
var field = var bigObject = host: url: 'http://myserver.com' /* ... some big object ... */; bigObjectget = fieldgetbigObjectset = fieldset console// => 'http://myserver.com'
License
(MIT License)
Copyright 2015, JP Richardson