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

immutable-json-pointer

v1.1.0

Published

JSON Pointer implementation for Node.js

Downloads

18

Readme

immutable-json-pointer

JSON Pointer (RFC6901) implementation for Node.js

Mutate a copy of data without changing the original source.

Install

// NPM
$ npm install immutable-json-pointer

// yarn v2
$ yarn add immutable-json-pointer

Usage

get

import { get } from "immutable-json-pointer";
// For example, given the JSON document
const json = {
  foo: ["bar", "baz"],
  "": 0,
  "a/b": 1,
  "c%d": 2,
  "e^f": 3,
  "g|h": 4,
  "i\\j": 5,
  'k"l': 6,
  " ": 7,
  "m~n": 8,
};
//   The following JSON strings evaluate to the accompanying values:
expect(get(json, "")).toBe(json); // the whole document
expect(get(json, "/foo")).toStrictEqual(["bar", "baz"]);
expect(get(json, "/foo/0")).toBe("bar");
expect(get(json, "/")).toBe(0);
expect(get(json, "/a~1b")).toBe(1);
expect(get(json, "/c%d")).toBe(2);
expect(get(json, "/e^f")).toBe(3);
expect(get(json, "/g|h")).toBe(4);
expect(get(json, "/i\\j")).toBe(5);
expect(get(json, '/k"l')).toBe(6);
expect(get(json, "/ ")).toBe(7);
expect(get(json, "/m~0n")).toBe(8);

set

import { set } from "immutable-json-pointer";
// For example, given the JSON document
const json = {
  foo: ["bar", "baz"],
  hoge: {
    fuga: "piyo",
  },
};
const newJson = set(json, "/foo/-", "boss");

expect(newJson === json).toBeFalsy();
expect(newJson.hoge === json.hoge).toBeTruthy();
expect(newJson.foo === json.foo).toBeFalsy();
expect(newJson.foo[2]).toBe("boss");

has

import { has } from "immutable-json-pointer";
const json = {
  value: 1,
};
has(json, "/value"); // true
has(json, "/item"); // false

read

import { read } from "immutable-json-pointer";
const json = {
  value: 1,
};
read(json, "/value"); // 1
read(json, "/item"); // undefined

remove

import { remove } from "immutable-json-pointer";
const json = {
  unused: 0,
  value: 1,
};
const newJson = remove(json, "/unused");
console.log(newJSON); // { value: 1 }
console.log(json); // { unused: 0, value: 1 }

dict

import { dict } from "immutable-json-pointer";
const json = {
  item: 0,
  value: 1,
};
const dic = dict(json);
console.log(dic); // { '/item': 0, '/value': 1 }

compile

import { compile } from "immutable-json-pointer";
console.log(compile(["a", "b", "c"])); // "/a/b/c"

transform

import { transform } from "immutable-json-pointer";
const json = {
  value: 0,
};
const nextValue = 1;
const json2 = transform(json, "/value", (prev, path) => {
  if (prev != nextValue) {
    return nextValue;
  }
  return prev;
});
console.log(json); // { "value": 0 }
console.log(json2); // { "value": 1 }

chain

import { chain } from "immutable-json-pointer";
const doc = {
  foo: {
    bar: {
      baz: 1,
    },
  },
};
const operation = chain<typeof doc>(
  (doc) => set(doc, "/foo/bar/baz", 2),
  (doc) => set(doc, "/foo/bar/baz", 3),
  (doc) => set(doc, "/foo/bar/baz", 4)
);
const result = operation(doc);
console.log(result); // { foo:{ bar: { baz: 4 } } }