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

keypath-resolve

v2.1.6

Published

Tiny library (1Kb) to resolve and manipulate keyPaths. Allows dot and bracket notation complex keypaths.

Downloads

14

Readme

logo

Keypath-resolve

NoDependencies Browser Node

Tiny and flexible library ( 1Kb gziped, 3Kb minified), for safe (any form of code evaluation is used internally) resolution and manipulation of Keypaths. Accepts keypaths formated using dot and bracket notation (or mixed).

  // simple resolution example
  Keypath.resolve( targeObj , 'myProperty.myNestedArray[3]["deepProperty"]' )

Features

  • No dependencies. Tiny and compact.
  • Accepts Keypaths in dot notation and bracket notation (or mixed)
  • Allows Global context resolutions (autodetects window/global)
  • Safe : no eval, or new Function() code evaluation
  • Extremely fast : >200,000 keypath resolutions/second (check ./tests/test.js)
  • Wide platform support :
    • Node , Chrome 49+, Firefox 47+, Edge, Opera , Safari 5.1+, Internet Explorer 11+

API Methods

The library API provides methods for common keypath operations (unfold to see syntax and usage specs) :

Keypath.resolve()


Resolves the value of the provided keypath . If the keypath does not exist in the object triggers an error.

Keypath.resolve( [contextObject ,] keypath )

  • contextObject : (optional) Object to use as target object. If omited, global object is used instead.
  • keyPath: String representing the keyPath to resolve.
    const myObj = {
        myArray : [ 
            {
                myProp : 'foo'
            }
        ]
    }
    Keypath.resolve(myObj, 'myArray[0].myProp');   
    Keypath.resolve(myObj, 'myArray.0["myProp"]');   
    Keypath.resolve(myObj, "myArray['0'].myProp");   
    // all return 'foo'

Keypath.create()


Creates the provided keypath structure, as a sequence of objects or arrays, according to the keypath string.

Keypath.create( [contextObject ,] keypath )

  • contextObject : (optional) Object to use as target object. If omited, global object is used instead.
  • keyPath: String representing the keyPath to create.
    const myObj = {}
    Keypath.create(myObj, 'myNested["myProp"]');   
    // creates the following object structure :
    // myObj.myNested.myProp , and sets the last property
    // value to undefined. Returns undefined

Keypath.assign()


Resolves the provided keypath and assigns to it the provided value. If the keypath does not exist in the object triggers an error.

Keypath.assign( [contextObject ,] keypath, value )

  • contextObject : (optional) Object to use as target object. If omited, global object is used instead.
  • keyPath: String representing the keyPath to create.
  • value : Value to set to the keypath resolution item
    const myObj = {
    	myProp : undefined
    }
    Keypath.assign(myObj, 'myProp', 'foo');   
    // Assigns 'foo' to myProp and returns foo'

Keypath.exist()


Tries to resolve the provided keypath, if succeeds returns true, if fails returns false.

Keypath.exist( [contextObject ,] keypath )

  • contextObject : (optional) Object to use as target object. If omited, global object is used instead.
  • keyPath: String representing the keyPath to create.
    const myObj = {
    	myProp : [111,222,333,444]
    }
    Keypath.exist(myObj, 'myProp[2]' );   
    // Returns true

Keypath.toArray()


Returns an array with the keys of the provided keypath. Returns false if the keypath is not properly formated

Keypath.toArray( keypath )

  • keyPath: String representing the keyPath to create.
    Keypath.exist('myProp[2].myNested["deepProp"]' );   
    // Returns ['myProp', '2','myNested','deepProp'] 

Keypath.resolveContext()


Resolves the provided keypath and returns an object containing the context of the resolution and the name of the property to retrieve the value. If the keypath cannot be resolved, triggers an error.

Keypath.resolveContext( [contextObject ,] keypath )

  • contextObject : (optional) Object to use as target object. If omited, global object is used instead.
  • keyPath: String representing the keyPath to create.
    const myObj = {
    	myNested:  {
            first: 'foo',
            second : 'bar'
        }
    }
    Keypath.resolveContext(myObj, 'myProp.myNested.first]' );   
    // Returns Object :
    // { context: {first: 'foo', second: 'bar'} , property:'first' }

Keypath.defaultContext()


Set the context to be used by default, when no context is provided to the API collection methods calls (by default: global/windowd).

Keypath.defaultContext( context )

  • context: Reference to the object to be used as default context.
    myObj= {
        myProo:'foo'
    }
    Keypath.defaultContext( myObj );   
    Keypath.resolve('myProp');
    // Returns 'foo'
    Keypath.exist('myProp');
    // Returns true
    // (...)

Global Context Resolutions

By default when the context object is omited ( in any of the API methods calls ), the resolution will be performed searching in the global object (window/global).

    global.myObj = {
        myArray : [ 
            {
                myProp : 'foo'
            }
        ]
    }
    Keypath.resolve('myObj.myArray[0].myProp');   
    // returns 'foo'

However if the default resolution context is reassigned, using Keypath.defaultContext( anyObj ) this behavior does not apply anymore until the default Context is set again to the global object (global/window) using Keypath.defaultContext( global )

Package distribution :

In browser enviroment you can include this library using the CDN ...

<script src='https://cdn.jsdelivr.net/gh/colxi/keypath-resolve@latest/src/keypath-resolve.min.js'></script>

Package can also be installed via:

$ npm install keypath-resolve --save

and available in Github :

 https://github.com/colxi/keypath-resolve

In browsers enviroment the global window.Keypath Object is created automatically. In Node you must perform a regulr module import using require to retrieve the Keypath exported Object.

Changelog

v2.1.4 Added Keypath.defaultContext method

v2.1.2 Added Keypath.toArray and improved documentation

v2.0.0 Major changes

  • New API
  • New methods and features
  • Removed local resolver (inapropiate use could lead to security issues )
  • Removed Object.prototype.resolveKeyPath ( to avoid messing with prototypes of primitives )

v1.2.2 Improved speed and implemented bracket notation

v1.2.1 Minor bufgixes, improved documentation

v1.2.0 First public release

Licence

MIT