dotprune v0.1.10
✂ dotprune ✂
dotprune is a simple tool that allows you to perform operations on collections of JavaScript objects. Pruning allows you to remove or keep only specific object properties using dot notation. Circular allows you to replace circular references in an object with a default string or user defined value. deepPluck allows you to use dot and array notation to pull values from deeply nested objects and collections.
- See the WIKI for full documentation
 - And the Change Log for what's new
 
Documentation
dotprune.prune( collection, fields, perserve )
collectionObject- the collection to prune.fieldsArray- Array of paths to keep or not keep. Can use dot notation (i.e.['!name.value']or['name.value', 'name.id'])perserve=false(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) - Create a pruned copy of the object instead of pruning it in place
Removes fields from an object using dot (.) and not (!) notation
dotprune.circular( collection, replacement )
collectionObject- the collection in which to replace circular references.replacement="[Circular]"(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects) - The value to use as a replacement. Ifreplacementis a function it will be passed the circular object and its return value will be used as the replacement
Replaces circular references in an object
dotprune.deepPluck( collection, path )
collectionObject- the collection to perform a deep pluck on.pathString- A string representation of the path to pluck. Array index and dot notation are supported.
Creates an array of values plucked from a collection using dot and array notation
dotprune.mergePush( target, source, ...sourceN )
targetObject- the object to merge into.sourceObject- the object to merge from.sourceNObject- additional objects to merge from.
Merges two or more objects into the first and creates a union of array values instead of overwriting them
dotprune.stringify( value[, replacer, space )
valueObject- value to stringify.replacerFunction- a function who's output will replace the value.spaceString- spacing.
Similar to JSON.stringify but with support for functions, dates, regular expressions, etc...
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]"
    }
  }
]Basic deepPluck 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"
    	    	}
    	    }
    	]
    }
];
console.log(dotprune.deepPluck(obj, 'groups.name'));
// > [ 'survivors', 'chosen ones' ]
console.log(dotprune.deepPluck(obj, 'groups[0].station.name'));
// > [ 'swan' ]
console.log(dotprune.deepPluck(obj, 'groups[station].name'));
// > [ 'swan', 'temple' ]Tools
Created with Nodeclipse (Eclipse Marketplace, site)
Nodeclipse is free open-source project that grows with your contributions.