npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dotprune

v0.1.10

Published

Deep filters for Javascript objects using dot notation

Downloads

805

Readme

✂ 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.

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. If replacement 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 = 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.