@wilmoore/accessor
v0.1.0
Published
Higher-Order getter/setter.
Downloads
2
Maintainers
Readme
accessor
Higher-Order getter/setter functions.
npm install @wilmoore/accessor --save
npm stats
Example
basic
var assert = require('assert')
var accessor = require('@wilmoore/accessor')
var latitude = accessor(32.7229)
assert.equal(latitude(), 32.7229)
//=> undefined
latitude(45.49428)
assert.equal(latitude(), 45.49428)
//=> undefined
fluent interface
var assert = require('assert')
var accessor = require('@wilmoore/accessor')
// define
var address = {
street: accessor('', address),
city: accessor('', address),
state: accessor('', address),
zip: accessor('', address),
country: accessor('', address)
}
// set
address
.street('200 E Main Street')
.city('Phoenix')
.state('AZ')
.zip('85123')
.country('USA')
assert.equal(address.country(), 'USA')
//=> undefined
Features
- Supports method chaining.
- Supports optional default value.
API
accessor(initial, [context])
Arguments
initial: (*)
initial property value.[context]: (…Object)
Value ofthis
.
Returns
The returned accessor function returns stored value when invoked with 0 parameters. The returned accessor function stores the value passed as the first parameter.
(Function)
accessor function.val: (*)
input value.