Skip to content

Return an object with only whitelisted properties.

Notifications You must be signed in to change notification settings

kael-shipman/only-nested

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

only-nested

npm Travis Code Climate Code Climate Coverage

Return on the whitelisted properties of an object, similar to only, but with support for nested properties.

Installation

$ npm isntall only-nested --save

Usage

var only = require('only-nested')

var whitelist = {
  a: null,
  b: {
    x: null,
    y: null
  }
}

var obj = {
  a: 1,
  b: {
    x: 2,
    y: 3,
    z: 4
  },
  c: 5
}

only(whitelist, obj)
/*
-> {
  a: 1,
  b: {
    x: 2,
    y: 3
  }
}
*/

Arrays of Objects

The only() function also supports whitelisting keys of objects in an array. This is expressed in the whitelist by having an array with a whitelist object as the first element.

var whitelistWithArray = {
  a: [{
    x: null
  }]
}

var objWithArray = {
  a: [
    {
      x: 1,
      y: 2
    },
    {
      x: 3,
      y: 4,
      z: 5
    }
  ]
}

only(whitelistWithArray, objWithArray)
/*
-> {
  a: [
    {
      x: 1
    },
    {
      x: 3
    }
  ]
}
*/

Partial Application

If you call only() with a whitelist as the only argument, it returns a partially applied function which takes a source object as its only argument.

var onlyWhitelist = only(whitelist)
onlyWhitelist(obj)

It works great with map().

someArrayOfObjects.map(only({a: null}))

About

Return an object with only whitelisted properties.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%