A powerful options object parser utility, a.k.a. Options Specialist.
Follow the project on Twitter for updates and check out the contribution section if you're looking to contribute (issues, PRs, improve examples & docs, fix typos, discuss ideas, etc.).
- 1.: Easy & minimal options object parsing
- 2.: A better & more robust alternative to Object.assign
- 3.: Get every possible option without checking key existences via recursive Proxy
npm install optionist --save
const optionist = require('optionist')
// with a dedicated defaults object --------------------------------------------
const DEFAULTS = {
value: 7,
flag: true,
callback: null
/* etc. */
}
function testFuncion1 (options) {
options = optionist(options, DEFAULTS)
this._value = options.value
this._flag = options.flag
this._callback = options.callback
}
testFunction1({ value: 42 })
// with inline defaults --------------------------------------------------------
function testFuncion2 (options) {
options = optionist(options, {
value: 7,
flag: true,
callback: null
/* etc. */
})
this._value = options.value
this._flag = options.flag
this._callback = options.callback
}
testFunction2({ value: 42 })
// with underscored properties (e.g.: this._value = 42) ------------------------
const underscore = require('optionist/assign/underscore')
function testFuncion1 (options) {
// the properties will be set automatically based on the options
// that are merged with the passed options and with the default options
underscore(options, DEFAULTS)
/*
the following properties will be set automatically
based on the options and default options:
- this._value
- this._flag
- this._callback
*/
this._value = options.value
this._flag = options.flag
this._callback = options.callback
}
testFunction1({ value: 42 })
const optionist = require('optionist')
// with a dedicated defaults object --------------------------------------------
const DEFAULTS = {
value: 7,
flag: true,
callback: null
/* etc. */
}
function testFuncion1 (options) {
options = optionist(options, DEFAULTS)
this._value = options.value
this._flag = options.flag
this._callback = options.callback
}
testFunction1({ value: 42 })
// with inline defaults --------------------------------------------------------
function testFuncion2 (options) {
options = optionist(options, {
value: 7,
flag: true,
callback: null
/* etc. */
})
this._value = options.value
this._flag = options.flag
this._callback = options.callback
}
testFunction2({ value: 42 })
Any contribution is appreciated. To get going, check out the contribution guidelines. Thank you and have fun!