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

json-pointer-relational

v1.0.7

Published

A library for resolving JSON Pointers with extended relational support

Downloads

401

Readme

json-pointer-relational

A lightweight JS JSON-Pointer library that provides methods for getting and setting members of a JSON document, with support for relational JSON-Pointers.

Testing Playground

You can test the functionality of this library at the JSON-Pointer-Relational Playground.

The playground isn't pretty, but it should demonstrate the capabilities of this library.

Funtionality

This library adheres to the rules for interpretting a json-pointer as laid out by the RFC 6901 proposed standard, as well as the additional relative json-pointer suggestion as laid out by Relative JSON Pointer proposal.

All examples and rules defined in these documents should be expected to function with the same behavior.

Methods

getByPointer

Params

  • pointer (pointers) : type: string | string[] - A string representing a JSON Pointer. You may supply an array of strings, where each string represents a full JSON Pointer. Each JSON Pointer will be evaluated from the ending reference of the JSON Pointer before it in this array.
  • obj : type: Record<string, any> - A JSON compatible object. This object is expected to be JSON compatible.

Return

type: any - The result will be the value of the final reference resolved from each pointer. Therefore, the return type could be anything. [^note]: Attempting to retrieve a non-existent value past the end of an array will result in an error.

setByPointer

Params

  • value : type: any - The value to be set at the final reference point reached from resolving all supplied JSON Pointers.
  • pointer (pointers) : type: string | string[] - A string representing a JSON Pointer. You may supply an array of strings, where each string represents a full JSON Pointer. Each JSON Pointer will be evaluated from the ending reference of the JSON Pointer before it in this array.
  • obj : type: Record<string, any> - A JSON compatible object. This object is expected to be JSON compatible.

Return

type: any - The result will be the previous value of the final referencer point reached by resolving all supplied JSON Pointers. [^note]: Unlike getByPointer, you may use a JSON Pointer that resolves to the non-existent index at the end of an array.

getReferenceByPointer

[^note]: For internal use - or a more detailed response

Params

  • pointer (pointers) : type: string | string[] - A string representing a JSON Pointer. You may supply an array of strings, where each string represents a full JSON Pointer. Each JSON Pointer will be evaluated from the ending reference of the JSON Pointer before it in this array.
  • obj : type: Record<string, any> - A JSON compatible object. This object is expected to be JSON compatible.
  • tree (optional) : type: RefPoint[] - For internal use. This array represents the path this method has traveled throughout the data tree to arrive at its current node.

Return

type: RefPoint - A detailed reference to the resolved point in the data tree.

Types

RefPoint

Members

  • obj : type: any - A reference to the represented point within the data tree.
  • key : type: string - The member name of this point within the parent object.
  • normalizedPath : type: string - A direct path to this node within the data tree. This might not be the same path that was taken to reach this node, but should represent the most direct path.
  • parent : type: RefPoint | null - A RefPoint of the immediate parent node, if available. If a $ref was resolved, this will represent the immediate parent of the object resolved from the $ref.

Examples

Let's begin with the example object:

const jsonObj = {
    "foo": ["bar", "baz"],
    "highly": {
        "nested": {
            "objects": true
        }
    }
}

From here, we could navigate to "baz" in the document with the JSON Pointer /foo/1 or #/foo/1.

console.log(getByPointer('/foo/1', jsonObj));
// baz

We could specific another JSON-Pointer that includes a relative prefix in order to begin navigating the document starting from the reference that our last JSON-Pointer resolved to. Let's say we are on "baz", but we want to access the index before us in our parent array. We could supply 0-1 to get a result of "bar".

console.log(getByPointer(['/foo/1', '0-1'], jsonObj))
// bar

If we were on "baz" and wanted to know the index of the reference we occupied within the parent array, we could ask for the index with #.

console.log(getByPointer(['/foo/1', '#'], jsonObj))
// 1

If we were on "baz" and wanted to know the member name of the parent array, we could navigate upward to the parent and ask for its key.

console.log(getByPointer(['/foo/1', '1#'], jsonObj))
// foo

From any point in the tree, we can use our relative prefix to navigate to any other location.

console.log(getByPointer(['/foo/1', '2/highly/nested/objects'], jsonObj))
// true

JSON-Pointer functionality is the same for setting a value.

console.log(getByPointer('/highly/nested/objects', jsonObj))
// true
setByPointer(false, '/highly/nested/objects', jsonObj)
console.log(getByPointer('/highly/nested/objects', jsonObj))
// false

We can also use setByPointer to create keys that previously did not exist. [^note]: You can only set a new key within an already existing node in the tree. You cannot create new nested nodes with a JSON Pointer alone, though you can set an object to a new key in an existing node.

console.log(getByPointer('/highly', jsonObj))
// {"nested":{"objects":true}}
setByPointer('bar', '/highly/foo', jsonObj)
console.log(getByPointer('/highly', jsonObj))
// {"nested":{"objects":true},"foo":"bar"}

You may also use setByPointer to append values to the end of an array using the - operator.

console.log(getByPointer('/foo', jsonObj))
// ["bar","baz"]
setByPointer('added', ['/foo/1', '1/-'], jsonObj)
console.log(getByPointer('/foo', jsonObj))
// ["bar","baz","added"]