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

obj-path-expression-parser

v0.0.2

Published

Parser for path expressions

Downloads

16

Readme

obj-path-expression-parser

A path expression is a string that matches a one or more values in an object. This package exposes a generator "expressionParser" that with the given path expression and object, returns paths matching the expression.

Example:

const expressionParser = require('obj-path-expression-parser');
const iter = expressionParser('hello.world', { hello: { world: 1 } });
Array.from(iter); // [['hello', 'world']]

The first argument "hello.world" is the path expression, the second is the object where the match happens. Only paths existing in the corresponding objects are returned!

const iter = expressionParser('hello.world', {});
Array.from(iter); // [] no matches in the object!

Path expression syntax

A path expression is formed by multiple path "fragments". A fragment is a string that tries to match an attribute:

hello.world

"hello" is a fragment matching the attribute "hello", and "world" matches the attribute "world" of the object inside "hello". Fragments can be separated by dots of wrapped in square brackets. The following is equivalent to "hello.world":

[hello][world]

The difference is that between square brackets, you can use any character you need, including square brackets if escaped with "". You can mix the fragment of the 2 types:

[hello]world

Globbing

Every fragment can use globbing to match attributes: This matches any item contained in the "hello" or "world" attributes:

[hello|world][*]

The globbing library used is "micromatch". Check out for further details. The library supports escaping using "".

Slice syntax

If the target item is an array, you can set a fragment to filter a specific slice of the array. If your object contains:

{ items: ['a', 'b', 'c', 'd'] }

You can specify the beginning and the end of the slice. It behaves in the same way as Array.prototype.slice:

items[:] // every item

Expands to:

  • ['items', 0]
  • ['items', 1]
  • ['items', 2]
  • ['items', 3]
items[1:] // every item except the first

Expands to:

  • ['items', 1]
  • ['items', 2]
  • ['items', 3]
items[:2] // from the first to the second

Expands to:

  • ['items', 0]
  • ['items', 1]
items[:2] // from the first to the second

Expands to:

  • ['items', 0]
  • ['items', 1]
items[1:-1] // from the first to the one before the last one

Expands to:

  • ['items', 1]
  • ['items', 2]

Multiple expressions

You can separate multiple path expressions with a comma:

a.b,x.y

returns the following paths (if they exist in the object):

[ ['a', 'b'], ['x', 'y']]

Nested Expression

A nested expression is a path expression wrapped in round parenthesis, that is part of another path expression:

(products,services)items.prices

That expands to:

[
  ['products', 'items', 'prices'],
  ['services', 'items', 'prices']
]

Multiple levels of nesting are allowed:

(products.items,services(subscriptions,transactions))prices

That expands to:

[
  ['products', 'items', 'prices'],
  ['services', 'subscriptions', 'prices']
  ['services', 'transactions', 'prices']
]

Custom fragments

If you need to perform a more complex filtering you can use a custom fragment. A custom fragment is wrapped in curly braces and calls a custom function with an argument:

numbers[:]{mod 3}

In this case the function is "mod" and the argument is a string "3". You need to pass the custom function to the expressionParser function:

const customFunctions = {
  mod: (path, funcArgument, parent) => {
    const n = parseInt(funcArgument, 10)
    return parent % n === 0 ? [path] : []
  }
};

const iter = pathExpressionParser('numbers[:]{mod 3}',
  { numbers: [0, 1, 2, 3, 4, 5, 6, 7, 8] }, customFunctions);

The custom function takes as arguments:

  • the current path being evaluated
  • the argument ("3" in our example)
  • the value where the current path is pointing

The function should return an iterable with the valid paths.