@particle/safe-add-prop
v3.0.2
Published
safely set and get properties on foriegn objects
Downloads
1,164
Maintainers
Keywords
Readme
@particle/safe-add-prop
safely set and get properties on foriegn objects
Installation
npm install @particle/safe-add-prop --save
API
@particle/safe-add-prop
safeAddProp.setProp(obj, keypath, value) ⇒ object
Add a property to a foreign object without exposing data to other consumers
Kind: static method of @particle/safe-add-prop
Returns: object - The internal object which received your property
| Param | Type | Description | | --- | --- | --- | | obj | object | The object to modify | | keypath | string | array | The key path of the property to set | | value | * | The value to set |
Example
// basic
const other = require('other').create();
const keypath = 'foo.bar'; // or use an array: `['foo', 'bar'];`
const value = true;
setProp(other, keypath, value);
module.exports = other;
// in middleware
module.exports = (req, res, next) => {
setProp(req, 'start', Date.now());
next();
}
safeAddProp.getProp(obj, keypath) ⇒ *
Get a property on a foreign object which you set previously
Kind: static method of @particle/safe-add-prop
Returns: * - The resolved value
| Param | Type | Description | | --- | --- | --- | | obj | object | The object to query | | keypath | string | array | The key path of the property to get |
Example
const other = require('./my-customized-other');
const keypath = 'foo.bar'; // or use an array: `['foo', 'bar'];`
const value = getProp(other, keypath);
NOTE: Unfortunately, docs have a nasty habit of falling out of date. When in doubt, check usage in tests