✂ 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
] )
collection
Object
- the collection 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
Removes fields from an object using dot (.) and not (!) notation
dotprune.circular
( collection
[, replacement
] )
collection
Object
- the collection in which to replace circular references.- [
replacement="[Circular]"
]*
- The value to use as a replacement. Ifreplacement
is 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
)
collection
Object
- the collection to perform a deep pluck on.path
String
- 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
] )
target
Object
- the object to merge into.source
Object
- the object to merge from.sourceN
Object
- 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
] )
value
Object
- value to stringify.replacer
Function
- a function who's output will replace the value.space
String
- spacing.
Similar to JSON.stringify but with support for functions, dates, regular expressions, etc...
Basic Prune Example
JavaScript
var dotprune = ; // define a javascript object with sub-propertiesvar 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 propertiesvar prunedObject = dotprune; // format the output and print it to the consolevar prettyObject = JSON;console;
Output
"id": 1 "name": "Jack Shepard" "groups": "name": "survivors" "station": "name": "swan" "name": "chosen ones" "station": "name": "temple"
Basic Circular Example
JavaScript
var dotprune = ; var main = name: 'main' obj1: name: 'main' ; mainobj1main = main;mainobj2 = main; var main2 = name: 'main2' obj: main; var obj = dotprune; console;
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 = ; // define a javascript object with sub-propertiesvar 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;// > [ 'survivors', 'chosen ones' ] console;// > [ 'swan' ] console;// > [ 'swan', 'temple' ]
Tools
Created with Nodeclipse (Eclipse Marketplace, site)
Nodeclipse is free open-source project that grows with your contributions.