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

json8-pointer

v1.0.6

Published

JSON Pointer toolkit for JavaScript

Downloads

36,752

Readme

JSON8 Pointer

Introduction

JSON Pointer RFC 6901 toolkit for JavaScript.

See also JSON8 Patch for more methods to work with JSON pointers.


Getting started

npm install json8-pointer


const pointer = require("json8-pointer");

Methods

find

Use a JSON Pointer to find a value in a JSON document. Returns undefined if the value cannot be found.

var doc = { foo: { bar: "foobar" } };

pointer.find(doc, "/foo/bar");
// "foobar"

pointer.find(doc, "/bar/foo");
// undefined

context

Returns the target parent and target property of a pointer.

var doc = { foo: { bar: "foobar" } };

pointer.context(doc, "/foo/bar");
// ['bar', doc.foo]

encode

Takes an array of unescaped tokens (see decode) and return a JSON Pointer string.

pointer.encode(["foo", "bar", "hello"]);
// '/foo/bar/hello'

pointer.encode(["foo", "a/b"]);
// '/foo/a~1b'

You can specify a different separator than the default /.

pointer.encode(["foo", "bar", "hello"], ".");
// '.foo.bar.hello'

serialize

Alias for the encode method.

escape

Escape a single token for use in JSON Pointer.

pointer.escape("a/b");
// 'a~1b'

You can specify a different separator than the default /.

pointer.escape("a.b", ".");
// 'a~1b'

decode

Takes a JSON Pointer string and return an array of unescaped tokens.

pointer.decode("/foo/bar/hello");
// ['foo', 'bar', 'hello'];

pointer.decode("/foo/a~1b");
// ['foo', 'a/b']

You can specify a different separator than the default /.

pointer.decode(".foo.bar.hello", ".");
// ['foo', 'bar', 'hello'];

prototype pollution

decode will throw with an error if prototype pollution is attempted.

parse

Alias for the decode method.

unescape

Unescape a single token see escape.

pointer.unescape("a~1b");
// 'a/b'

You can specify a different separator than the default /.

pointer.unescape("a~1b", ".");
// 'a/b'

join

Join a base pointer and tokens;

pointer.join("/foo", ["bar"]);
pointer.join(["foo"], "bar");
pointer.join("", ["foo", "bar"]);
pointer.join([], ["foo", "bar"]);
// `/foo/bar`

You can specify a different separator than the default /.

pointer.join("/foo", ["bar"], ".");
// `.foo.bar`

index

demo/playground

The index method returns an object with all values indexed by pointers.

pointer.index("foo"); // {'': 'foo'}

pointer.index(["hello", "earth"]);
//  {
//    '': ['hello', 'earth'],
//    '/0': 'hello',
//    '/1': 'earth'
//  }

dict

demo/playground

Just like index but only indexes primitives.

pointer.dict(['hello', 'earth'])
//  {
//    '/0': 'hello',
//    '/1': 'earth'
//  }

pointer.dict({'foo', 'bar'})
//  {
//    '/foo': 'bar'
//  }

flatten

demo/playground

The flatten method works like a flat version of index.

pointer.flatten(["hello", "earth"]);
//  {
//    '': [],
//    '/0': 'hello',
//    '/1': 'earth'
//  }

unflatten

demo/playground

The unflatten method takes an flattened object and returns a deep JSON document.

pointer.unflatten({ "": "foo" }); // 'foo'

pointer.unflatten({
  "": [],
  "/0": "hello",
  "/1": "earth",
}); // ['hello', 'earth']

compile

The compile method takes a pointer and returns a function that accept a document and returns the value at the location of the pointer.

const getAge = pointer.compile("/age");

getAge({ age: 22 }); // 22