npm

npm’s 2019 JavaScript ecosystem survey analysis is now available! Get your copy here »

function-accessor

0.1.0 • Public • Published

function-accessor

Higher-Order accessor functions.

Build Status Code Climate js-standard-style

npm install function-accessor --save

You can also use Duo, Bower or download the files manually.

npm stats

npm NPM downloads David

Usage

var accessor = require('function-accessor')

Features

  • Automatic method chaining on object setters.
  • Initial (default) value support.
  • Input validation on setters.
  • Read-only getter support.

API

accessor()
var assert = require('assert')
 
var latitude = accessor()
 
latitude(32.7229)
assert.equal(latitude(), 32.7229)
//=> undefined
 
latitude(45.49428)
assert.equal(latitude(), 45.49428)
//=> undefined
accessor({ init: … })
var assert = require('assert')
 
var latitude = accessor({
  init: 32.7229
})
 
assert.equal(latitude(), 32.7229)
//=> undefined
 
latitude(45.49428)
assert.equal(latitude(), 45.49428)
//=> undefined
accessor({ check: Function })
var assert = require('assert')
var isNumber = require('util').isNumber
 
var latitude = accessor({
  check: isNumber
})
 
latitude(45.49428)
assert.equal(latitude(), 45.49428)
//=> undefined
 
latitude('this is not a number!!')
//=> RangeError
accessor({ check: RegExp })
var assert = require('assert')
 
var pin = accessor({
  check: /^\d{4}$/
})
 
latitude(1234)
assert.equal(pin(), 1234)
//=> undefined
 
pin('123')
//=> RangeError
accessor({ readonly: true })
var latitude = accessor({
  init: 32.7229,
  readonly: true
})
 
assert.equal(latitude(), 32.7229)
//=> undefined
 
latitude(45.49428)
//=> TypeError
accessor({ self: this })
var assert = require('assert')
 
var address = {
  street: accessor({
    self: address
  }),
 
  city: accessor({
    self: address
  }),
 
  state: accessor({
    self: address
  }),
 
  zip: accessor({
    self: address
  }),
 
  country: accessor({
    self: address
  })
}
 
address
  .street('200 E Main Street')
  .city('Phoenix')
  .state('AZ')
  .zip('85123')
  .country('USA')
 
assert.equal(address.country(), 'USA')
//=> undefined

Contributing

SEE: contributing.md

Alternatives / Inspiration

Licenses

GitHub license

install

npm i function-accessor

Downloadsweekly downloads

2

version

0.1.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability