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

garbage-patch

v0.1.1

Published

cute implementation of json-pointer and json-patch

Downloads

3

Readme

JSON Patch is a neat idea for describing changes to JSON -

... the idea is that you can clean-up muddled parts of a codebase, by making data-things explicit.

it is pretty simple, and sort of beautiful.

Does the world need another json-patch implementation? no.

did I make one, and is it crappy? yes.

import { get, make, apply } = from 'garbage-patch'

let json = {
  foo: ['bar', 'baz'],
}

// apply clean changes to the json
let patch = { op: 'add', path: '/foo/-', value: 'bob' }
apply(patch, json)
/* {
  foo: ['bar', 'baz', 'bob'],
} */

// make a valid json pointer
let ptr = make(['foo', 1])
// '/foo/1'

// use it to get some data
let result = get(ptr, json)
// 'baz'

there is nothing too-clever going on. the min.js build is 2.5kb.


these methods take inspiration from the pointer spec and the patch-spec but have some differences:

  1. get() is more-forgiving if you give it a path '/foo/typo/9/wrong' that doesn't exist, it will just say - 'haha! no problem! undefined! ✌'

    also, a trailing slash is allowed - /foo/

    also, a leading slash is optional - foo/1

    if you prefer, you can use an array as a pointer, too:

let json = { foo: [null, 'found'] }
let found = get(['foo', 1], json) 
// 'found'
  1. apply() changes in-place (mutable) -and before you think 'hey this guy has never seen that Rich Hickey talk' - I have seen that one. most javascript implementations call .clone() over and over.

  2. - sneaky-append - an 'add' patch on an array doesn't need an index, and assumes a push on an array:

let json = { foo: [1] }
// according to the spec, this must be '/foo/[index]', which seemed annoying
let patch = { op: 'add', path: '/foo', value: 2 }
json = apply(patch, json) //normally this would be an arror
// {foo: [1, 2]}
  1. - sneaky-splat - an 'add' patch on an object doesn't need an key, if the value is also an object:
let json = { foo: { bar: true } }
let patch = { op: 'add', path: '/foo', value: { baz: true } }
apply(patch, json) //this would normally be an error, i think.
// { foo: { bar: true, baz: true } }

API

  • make(props) - create a JSON pointer from a list of properties
  • get(pntr,json) - use a pointer to get data out of JSON
  • apply(patch, json) - make modifications to a JSON object (in place)

all the different build formats are in ./builds there's a get only build, that is < 1kb:

import { get } from 'garbage-patch'
const get = require('garbage-patch/get')

See Also

MIT