json-keep

0.9.11 • Public • Published

npm downloads npm-issues js-standard-style GitHub license

json-keep

Returns an object with only the specified keys.
This is synchronous code, so callbacks are not not used.

Useful when you want to keep/pass only specific parts of a JSON object.
Pass the object as the first argument, and an array as the second.

If a value in the array matches a key in the object, it is returned.

Usage

var jsonKeep = require('json-keep');
obj = { 1:1, 2:2, 3:3, 4:4 }
console.log('keeping:',jsonKeep(obj,[1,5,4]));

Returns

keeping:  { '1': 1, '4': 4 }

Example Scenario

var jsonKeep = require('json-keep')
obj = { password:'5ecret!'
       ,admin:true
       ,taxID:'000-00-0000'
      }
adminFunction(jsonKeep(obj,['password','admin']),function(err,result){
// This function does something only an admin has permission to do.
// For this reason, we only pass in the password & admin status.
// This is just an example!
//
// Your function may only require a few select keys, so there is
// no need to pass in everything you have in the object you are working  
// with (which could be user input, or sensitive info from DB results).
//
// For security (or various other) reasons you use this module to refine 
// the object/data you pass along so you never have to worry about the
// possibility of this function handling irrelevant or sensitive data.
})   

Readme

Keywords

Package Sidebar

Install

npm i json-keep

Weekly Downloads

3

Version

0.9.11

License

MIT

Last publish

Collaborators

  • keithmcfly