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-rfc6901

v1.1.1

Published

json-pointer implementation as per rfc6901

Downloads

492

Readme

json-pointer-rfc6901

json-pointer-rfc6901 provides convenient methods for handling json pointers as defined by RFC6901

Build Status Coverage Status

Installation

Node

npm install json-pointer-rfc6901

Web

bower install json-pointer-rfc6901

Usage

node

var pointer = require('json-pointer-rfc6901');

console.log(pointer({ a: 1 }, '/a'));

web (global)

<html>
    <head>
        <script type="text/javascript" src="json-pointer-rfc6901.web.min.js"></script>
    </head>
    <body>
        <script>
            console.log(JSON.pointer({ a: 1 }, '/a');
        </script>
    </body>
</html>

web (amd)

require.config({
  paths: {
      "json-pointer-rfc6901": "json-pointer-rfc6901.web.min.js"
  }
});
require(['json-pointer-rfc6901'], function (pointer) {
    console.log(pointer({ a: 1 }, '/a');
});

API

methods

.get(object, pointer, [options]) -> value

Finds the value in object as specified by pointer.

console.log('result:', pointer.get({ a: 1 }, '/a'));
// result: 1

console.log('result:', pointer.get({ a: 1 }, '/b'));
// result: undefined

.set(object, pointer, value, [options]) -> object

Sets the location in object, specified by pointer, to value. Returns the modified object.

var obj = { a: 1 };

console.log('result:', pointer.set(obj, '/a', 2));
// result: { a: 2 }

console.log('result:', pointer.set(obj, '/b', 3));
// result: { a: 2, b: 3 }

console.log('result:', pointer.set(obj, '/c/0/a', 4));
// result: { a: 2, b: 3, c: [ { a: 4 } ] }

console.log('result:', pointer.set(obj, '/c/-/b', 5));
// result: { a: 2, b: 3, c: [ { a: 4 }, { b: 5 } ] }

.has(object, pointer, [options]) -> Boolean

Returns true iff the location, specified by pointer, exists in object.

console.log('result:', pointer.has({ a: 1 }, '/a'));
// result: true

console.log('result:', pointer.has({ a: 1 }, '/b'));
// result: false

.del(object, pointer, [options]) -> object/undefined

Removes the location, specified by pointer, from object. Returns the modified object, or undefined if the pointer is empty.

var obj = { a: 1 };

console.log('result:', pointer.del(obj, '/b'));
// result: { a: 1 }

console.log('result:', pointer.del(obj, '/a'));
// result: {}

console.log('result:', pointer.del(obj, ''));
// result: undefined

.escape(str) -> str

Escapes the given path segment as described by RFC6901.

Notably, '~''s are replaced with '~0' and '/''s are replaced with '~1'

console.log('result:', pointer.escape('abc'));
// result: abc

console.log('result:', pointer.escape('~'));
// result: ~0

console.log('result:', pointer.escape('/'));
// result: ~1

.escapeFragment(str) -> str

Escapes the given path fragment segment as described by RFC6901.

Notably, '~''s are replaced with '~0' and '/''s are replaced with '~1' and finally the string is URI encoded.

console.log('result:', pointer.escapeFragment('a b'));
// result: a%20b

.unescape(str) -> str

Un-Escapes the given path segment, reversing the actions of .escape

Notably, '~1''s are replaced with '/' and '~0''s are replaced with '~'

console.log('result:', pointer.unescape('abc'));
// result: abc

console.log('result:', pointer.unescape('~0'));
// result: ~

console.log('result:', pointer.unescape('~1'));
// result: /

.unescapeFragment(str) -> str

Un-Escapes the given path fragment segment, reversing the actions of .escapeFragment.

Notably, the string is URI decoded and then '~1''s are replaced with '/' and '~0''s are replaced with '~'.

console.log('result:', pointer.unescapeFragment('a%20b'));
// result: a b

.isPointer(str) -> Boolean

Returns true iff str is a valid json pointer value

console.log('result:', pointer.isPointer('/a'));
// result: true

.isFragment(str) -> Boolean

Returns true iff str is a valid json fragment pointer value

console.log('result:', pointer.isFragment('#/'));
// result: true

.parse(str) -> Array

Parses a json-pointer or json fragment pointer, as described by RFC901, into an array of path segments.

console.log('result:', pointer.parse('/abc'));
// result: [ 'abc' ]

console.log('result:', pointer.parse('#/abc'));
// result: [ 'abc' ]

.parsePointer(str) -> Array

Parses a json-pointer, as described by RFC901, into an array of path segments.

console.log('result:', pointer.parsePointer('/abc'));
// result: [ 'abc' ]

.parseFragment(str) -> Array

Parses a json-pointer or json fragment pointer, as described by RFC901, into an array of path segments.

console.log('result:', pointer.parseFragment('#/abc'));
// result: [ 'abc' ]

.compile(array) -> str

Converts an array of path segments into a json pointer. This method is the reverse of .parsePointer

console.log('result:', pointer.compile([ 'abc' ]));
// result: /abc

console.log('result:', pointer.compile([ '~', '/', 'abc' ]));
// result: '/~0/~1/abc'

console.log('result:', pointer.compile([ '' ]));
// result: '/'

console.log('result:', pointer.compile([]));
// result:

.compilePointer(array) -> str

Converts an array of path segments into a json pointer. This method is the reverse of .parsePointer

console.log('result:', pointer.compilePointer([ 'abc' ]));
// result: /abc

.compileFragment(array) -> str

Converts an array of path segments into a json pointer. This method is the reverse of .compileFragment

console.log('result:', pointer.compileFragment([ 'abc' ]));
// result: #/abc

pointer(object, [pointer, [value]]) -> pointer/value/object

Convenience function for choosing between .smartBind, .get, and .set, depending on the number of arguments.

var obj = { a: 1 };

console.log('result:', pointer(obj, '/a', 2));
// result: { a: 2 }

console.log('result:', pointer(obj, '/a'));
// result: 2

var bound = pointer(obj);

console.log('result:', bound.get('/a'));
// result: 2

console.log('result:', bound('/a'));
// result: 2

console.log('result:', bound.set('/a', 3));
// result: { a: 3 }

console.log('result:', bound('/a', 4));
// result: { a: 4 }

.smartBind(bindings) -> pointer

Creates a clone of the api, with ./.get/.has/.set/.del/.smartBind method signatures adjusted.

var obj = { a: 1 };

console.log('result:', pointer.smartBind({ object: obj })('/a'));
// result: 1

console.log('result:', pointer.smartBind({ object: obj }).get('/a'));
// result: 1

console.log('result:', pointer.smartBind({ pointer: '/a' }).get(obj));
// result: 1

console.log('result:', pointer.smartBind({ object: obj }).smartBind({ pointer: '/a' }).get());
// result: 1

bound.pointer(str) -> strings

get/set bound pointer value

bound.fragment(str) -> strings

get/set bound pointer value as fragment

bound.object(str) -> strings

get/set bound object

bound.options(str) -> strings

get/set bound options

.hasJsonProp(object, key) -> Boolean

Returns true iff obj contains key and obj is either an Array or an Object. Ignores the prototype chain.

Default value for options.hasProp.

.hasOwnProp(object, key) -> Boolean

Returns true iff obj contains key, disregarding the prototype chain.

.hasProp(object, key) -> Boolean

Returns true iff obj contains key, including via the prototype chain.

.getProp(object, key) -> value

Finds the given key in obj.

Default value for options.getProp.

.setProp(object, key, value) -> value

Sets the given key in obj to value.

Default value for options.setProp.

.getNotFound(object, segment, root, segments, iSegment) -> value

Returns the value to use when .get fails to locate a pointer segment.

Default value for options.getNotFound.

.setNotFound(object, segment, root, segments, iSegment) -> value

Returns the value to use when .set fails to locate a pointer segment.

Default value for options.setNotFound.

.delNotFound(object, segment, root, segments, iSegment) -> value

Performs an action when .del fails to locate a pointer segment.

Default value for options.delNotFound.

.errorNotFound(object, segment, root, segments, iSegment) -> n/a

Raises a JsonPointerError when the given pointer segment is not found.

May be used in place of the above methods via the options argument of ./.get/.set/.has/.del/.simpleBind.

try {
  console.log('result:', pointer.get({a: 1}, '/b', {getNotFound: pointer.errorNotFound}));
}
catch (ex)
{
  console.log(ex.name, ':', ex.message);
}
// exception: JsonPointerError : Unable to find json path: /b

console.log('result:', pointer.get([1,2,3], '/length'));
// result: undefined
console.log('result:', pointer.get([1,2,3], '/length', { hasProp: pointer.hasOwnProp }));
// result: 3
console.log('result:', pointer.get([1,2,3], '/length', { hasProp: pointer.hasProp }));
// result: 3

console.log('result:', pointer.get([1,2,3], '/push'));
// result: undefined
console.log('result:', pointer.get([1,2,3], '/push', { hasProp: pointer.hasOwnProp }));
// result: undefined
console.log('result:', pointer.get([1,2,3], '/push', { hasProp: pointer.hasProp }));
// result: function push() { [native code] }