New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

dotprune

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dotprune

Deep filter Javascript objects with dot notation

Source
npmnpm
Version
0.1.4
Version published
Maintainers
1
Created
Source

✂ dotprune ✂

dotprune is a simple tool that allows you to remove all properties from a JavaScript object that are not present in present in an array of properties. dot notation can be used to specify sub properties to keep or prefix propertied with an ! to keep everything but them. If you do not want to alter the original object, a boolean true value can be supplied as the third argument in the prune function.

dotprune can now also replace circular references

  • See the WIKI for full documentation
  • And the Change Log for what's new

Documentation

dotprune.prune( object, fields, [perserve] )

  • object Object - the object to prune.
  • fields Array - Array of paths to keep or not keep. Can use dot notation (i.e. ['!name.value'] or ['name.value', 'name.id'])
  • [perserve=false] Boolean - Create a pruned copy of the object instead of pruning it in place

dotprune.circular( object, [replacement] )

  • object Object - the object in which to replace circular references.
  • [replacement="[Circular]"] * - The value to use as a replacement. If replacement is a function it will be passed the circular object and its return value will be used as the replacement

Basic Prune Example

JavaScript
var dotprune = require('dotprune');

// define a javascript object with sub-properties
var obj = [
    {
    	id: 1,
    	name: "Jack Shepard",
    	groups: [
    	    {
    	    	id: 1,
    	    	name: "survivors",
    	    	station: {
    	    		id: 2,
    	    		name: "swan"
    	    	}
    	    },
    	    {
    	    	id: 2,
    	    	name: "chosen ones",
    	    	station: {
    	    		id: 5,
    	    		name: "temple"
    	    	}
    	    }
    	]
    }
];

// call the prune function with the object and an array of properties
var prunedObject = dotprune.prune(obj, ['id', 'name', 'groups.name', 'groups.station.name']);

// format the output and print it to the console
var prettyObject = JSON.stringify(prunedObject, null, '  ');
console.log(prettyObject);

Output
[
  {
    "id": 1,
    "name": "Jack Shepard",
    "groups": [
      {
        "name": "survivors",
        "station": {
          "name": "swan"
        }
      },
      {
        "name": "chosen ones",
        "station": {
          "name": "temple"
        }
      }
    ]
  }
]

Basic Circular Example

JavaScript
var dotprune = require('dotprune');

var main = {
    name: 'main',
    obj1: {
        name: 'main'
    }
};

main.obj1.main = main;
main.obj2 = main;

var main2 = {
    name: 'main2',
    obj: main
};

var obj = dotprune.circular([main, main2], function(obj) {
    return '[CustomCircular]';
});

console.log(JSON.stringify(obj, null, '  '));

Output
[
  {
    "name": "main",
    "obj1": {
      "name": "main",
      "main": "[CustomCircular]"
    },
    "obj2": "[CustomCircular]"
  },
  {
    "name": "main2",
    "obj": {
      "name": "main",
      "obj1": {
        "name": "main",
        "main": "[CustomCircular]"
      },
      "obj2": "[CustomCircular]"
    }
  }
]

Tools

Created with Nodeclipse (Eclipse Marketplace, site)

Nodeclipse is free open-source project that grows with your contributions.

Keywords

json

FAQs

Package last updated on 30 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts