trim-keys
v0.0.4
Published
Keep or remove certain keys from an object.
Downloads
11
Readme
trim-keys
Keep or remove certain keys from an object.
Installation
npm install trim-keys
Environment Support
trim-keys has been tested in Node, IE9+, Chrome, Firefox, and Opera.
Usage
// CommonJS
var trim = require('trim-keys');
// AMD
require(['trim-keys'], function(trim) { ... });
// Script Tag
var trim = window.trim;
API
trim(obj, keyMap)
obj
type: Object
The object to trim. This object will be modified, so if you want to retain your original object, you'll need to pass in a copy of that object.
keyMap
type: Object
This object defines what properties to keep, or what properties to remove. It takes the following form:
{ field1: <boolean>, field2: <boolean> ... }
The <boolean>
value can be any of the following:
1
ortrue
to include the field.0
orfalse
to exclude the field.
A keyMap
cannot contain both include and exclude specifications. An error will be thrown if all of the keys are not the same value.
Example
var trim = require('trim-keys');
var person = { name: 'John Doe', age: 40, height: { ft: 5, in: 10 } };
trim(person, { name: 1 })
// person === { name: 'John Doe' }
var trim = require('trim-keys');
var person = { name: 'John Doe', age: 40, height: { ft: 5, in: 10 } };
trim(person, { age: 0, height: { in: 0 } });
// person === { name: 'John Doe', height: { ft: 5 } }