/add-new-key-value

add a new key/value to an JS Object

Primary LanguageJavaScriptISC LicenseISC

add-new-key-value

add a new key/value to an JS Object

Build StatusCode Coverage 100%ISC License

API

var addKeyValue = require('add-new-key-value')

addKeyValue({JS Object}, {key:string}, {value})

the JS Object should valid as well the key or will return undefined

addKeyValue.strict({JS Object}, {key:string}, {value})

the same as the above method but if the value is undefined will ignore the creation on the new property for the given object

note: will overwrite existing keys

Usage

var addKeyValue = require('add-new-key-value')

var pkg = require('./package.json')

// case 1
addKeyValue(pkg, 'version', '1.0.0')
console.log(pkg.version)// should return '1.0.0'

addKeyValue(pkg, 'version')
console.log(pkg.version)// property should exist and return undefined

// case 2
var newPkg = addKeyValue(pkg, 'version', '1.0.0')
console.log(newPkg.version)// should return '1.0.0'

// case 3 - with strict
var newPkg = addKeyValue.strict(pkg, 'version', null)
console.log(newPkg.version)// should return null

var newPkg = addKeyValue.strict(pkg, 'version', '1.0.0')
console.log(newPkg.version)// should return '1.0.0'

// case 4 - with strict
var newPkg = addKeyValue.strict(pkg, 'version')
console.log(newPkg.version)// property should not exist and return undefined

Development

this projet has been set up with a precommit that forces you to follow a code style, no jshint issues and 100% of code coverage before commit

to run test

npm test

to run jshint

npm run jshint

to run code style

npm run code-style

to run check code coverage

npm run check-coverage

to open the code coverage report

npm run open-coverage