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

patch

v0.0.1

Published

an object patching tool

Downloads

4,206

Readme

patch

Patch is yet another jQuery.extend clone, but with a few extra quirks. It implements a protocol for "patching" objects in aribtrary ways, rather than simply "extending" objects. That is, rather than simply adding or replacing values in an object, it is possible to modify or delete existing values, too.

overview

patch takes 3 or more arguments: options, the object to modify, and the object(s) to pull modifications from. The default behavior (i.e. with an empty options parameter) should seem intuitive, it will simply create a copy of the last argument:

var patch = require('patch')

var patched = patch({}, {}, {key: "value"})
// patched = {key: "value"}

How about something a bit fancier? Let's tell patch to delete certain keys in the object being modified:

var obj = {
    hello: "world",
    name: "steve"
}
var options = {
    delete: "delete this one"
}
patch(options, obj, {hello: "delete this one"}
// => {name: "steve"}

Cool. Any key being updated to the value specified in the "delete" option is deleted instead. Note that keys that already have the "delete" value are not deleted.

What about arrays? Most extend clones simply replace values in the destination object, so if you have a large array you want to modify, you have to modify it and then pass in the whole thing. patch provides options for modifying existing arrays in-place, so you only have to pass in the new values:

var obj = {
    numbers: [1,2,3,4,5]
}
var options = {
    arrays: 'prepend'
}
patch(options, obj, {numbers: [-2, -1, 0]})
//=> {numbers: [-2, -1, 0, 1, 2, 3, 4, 5]}

Note that if the value does not exist in yet in the original object, the 'arrays' option is ignored.

Valid values for the "arrays" option are:

  • prepend - prepend the given values to the existing array
  • append - append the given values
  • replace - throw out the old array and replace it with the new one (default behavior)
  • splice - call Array.splice, passing in the new values as arguments to splice (uses Array.prototype.splice.apply(oldArr, newArr) under the hood). This allows arbirary modifications to an array - add/remove/replace arbitrary elements
  • join - tired of that pesky array? Make it into a string in one fell swoop by calling "join". Specify a separator too, if you'd like.

options

All options supported by patch:

  • deep - boolean - default false - when true, patching is recursive on child objects and arrays, creating a deep copy
  • arrays - prepend|append|replace|splice|join - default 'replace' - define patch behavior when an array already exists in the destination object
  • delete - any - default none - any value being updated to this value will be deleted rather than updated

why?

Why build yet another extend-like module? patch was designed to support partial object updates in a REST API. RFC 5789 defines the HTTP PATCH verb. Using the PATCH verb can save bandwidth compared to PUT or POST when updating resources, since a client only needs to send the updated bits. The RFC also describes some other advantages of using PATCH over other methods.

Consider a simple key-value REST API that stores JSON documents (see patchstore for an example) that supports partial updates. Some challenges come up in writing such an API:

  • what do you do with JSON documents with arbitrarily nested objects?
  • How do you handle arrays? What if there is a long array that needs only a minor modification?
  • How do you update a document, deleting a key rather than changing its value?

Using the patch module, such an API is easy to implement.