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

doc-path

v4.1.2

Published

A document path library for Node

Downloads

1,706,092

Readme

A Document Path Library for Node

NPM version Downloads Minzipped Size Build Status Coverage Status Typings

This module will take paths in documents which can include nested paths specified by '.'s and can evaluate the path to a value, or can set the value at that path depending on the function called.

Installation

$ npm install doc-path

Usage

let path = require('doc-path');

API

path.evaluatePath(document, key)

  • document - Object - A JSON document that will be iterated over.
  • key - String - A path to the existing key whose value will be returned.
    • Note: If your key has a dot in it (eg. a.b) then be sure to escape the dot with a blackslash (eg. a\\.b).

If the key does not exist, undefined is returned.

If the object's structure is extremely deep, then an error may be thrown if the maximum call stack size is exceeded while traversing the object.

path.evaluatePath Example:
const path = require('doc-path');

let document = {
    Make: 'Nissan',
    Model: 'Murano',
    Year: '2013',
    Specifications: {
        Mileage: '7106',
        Trim: 'S AWD'
    },
    Features: [
		{
		    feature: 'A/C',
			packages: [
				{name: 'Base'},
				{name: 'Premium'}
			]
		},
		{
		    feature: 'Radio',
			packages: [
				{name: 'Convenience'},
				{name: 'Premium'}
			]
		}
	]
};

console.log(path.evaluatePath(document, 'Make'));
// => 'Nissan'

console.log(path.evaluatePath(document, 'Specifications.Mileage'));
// => '7106'

console.log(path.evaluatePath(document, 'Features.feature'));
// => [ 'A/C', 'Radio' ]

console.log(path.evaluatePath(document, 'Features.packages.name'));
// => [ ['Base', 'Premium'], ['Convenience', 'Premium'] ]

path.setPath(document, key, value)

  • document - Object - A JSON document that will be iterated over.
  • key - String - A path to the existing key whose value will be set.
    • Note: If your key has a dot in it (eg. a.b) then be sure to escape the dot with a blackslash (eg. a\\.b).
  • value - * - The value that will be set at the given key.

If the key does not exist, then the object will be built up to have that path. If no document is provided, an error will be thrown. If the object's structure is extremely deep, then an error may be thrown if the maximum call stack size is exceeded while traversing the object.

path.setPath Example:

const path = require('doc-path');

let document = {
    Make: 'Nissan',
    Features: [
        { feature: 'A/C' }
    ]
};

console.log(path.setPath(document, 'Color.Interior', 'Tan'));
/*
   { 
   	Make: 'Nissan',
   	Features: [
   		{ feature: 'A/C' }
   	]
   	Color: { 
   		Interior: 'Tan'
   	}
   }
*/

console.log(path.setPath(document, 'StockNumber', '34567'));
/*
   { 
   	Make: 'Nissan',
   	Features: [
   		{ feature: 'A/C' }
   	]
   	Color: { 
   		Interior: 'Tan'
   	},
   	StockNumber: '34567'
   }
*/

console.log(path.setPath(document, 'Features.cost', '$0 (Standard)'));
 /*
	{ 
   	Make: 'Nissan',
   	Features: [
   		{
   			feature: 'A/C',
   			cost: '$0 (Standard)'
   		}
   	]
   	Color: { 
   		Interior: 'Tan'
   	},
   	StockNumber: '34567'
	}
 */

Tests

$ npm test

Note: This requires mocha, should, async, and underscore.

To see test coverage, please run:

$ npm run coverage

Current Coverage is:

Statements   : 100% ( 33/33 )
Branches     : 100% ( 24/24 )
Functions    : 100% ( 3/3 )
Lines        : 100% ( 29/29 )

Features

  • Supports keys with escaped . characters (as of v3.0.0)
  • Supports nested paths
    • Including keys of objects inside arrays! (as of v2.0.0)
  • Same common path specification as other programs such as MongoDB