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

chill-patch

v1.3.1

Published

Stress-free monkey-patching

Downloads

18

Readme

http://chillestmonkey.com

chill-patch: Stress-free Monkey Patching for JavaScript

chill-patch enables you to add methods to JS classes, with none of the problems of traditional monkey-patching.

const chillPatch = require('chill-patch')
const lastFunc = arr => arr[arr.length - 1]
const array = [1, 2, 3]

// safely add a method to `Array`
const last = chillPatch(Array, lastFunc, 'last')

// call the new method!
array[last]() //=> 3

You can use chill-patch to use off-the-shelf this-less functions as methods:

// Using toggle-set without chill-patch
const toggleSet = require('toggle-set')
const set = new Set([1, 2, 3])

toggleSet(set, 1) // Set([2, 3])

// Using toggle-set with chill-patch
const toggle = chillPatch(Set, toggleSet)

set[toggle](4) // Set([1, 2, 3, 4])

Install

npm install chill-patch

Uses

  • Method-chaining-style syntax:

// can adapt functions like this:
func3(func2(func1(instance)))

// and chain them like this:

instance
    [func1]()
    [func2]()
    [func3]()

// which is very similar to method chaining
instance
    .method1()
    .method2()
    .method3()
  • testing
const chillPatch = require('chill-patch')
const should = chillPatch(Object, require('should/as-function'))
const foo = {a: 2}
foo[should]().deepEqual({a: 2}) // succeeds
foo[should]().deepEqual({a: 3}) // fails

API

chillPatch(Klass, func, optionalDescription)
  • Klass is an ES5-style or ES2015-style class
  • func is a function with any number of arguments
  • optionalDescription is used as the description of the symbol.

Why it's Safe

chill-patch is safe because the return value is a Symbol and symbols are guaranteed to be unique. That means that the only way to access the new method you created is to have access to the symbol.

The only way another programmer can get access to symbols on an object in another scope is if they are hellbent on doing so, in which case they know they are going off-roading.

When you add a property to a prototype using a symbol, it's hidden, so you can safely pass off the patched object to other parts of the codebase, without other programmers knowing its there or being affected by it.

// after the above code is run, there is no change to the `ownPropertyNames` of the patched class

Object.getOwnPropertyNames(Array.prototype) // doesn't include anything new!

Similar Tech in other Languages

Something Better

The JavaScript Pipeline Operator proposal accomplishes the same syntactic convenience more simply and elegantly. The following two expressions would be equivalent:

let result = exclaim(capitalize(doubleSay("hello")));
result //=> "Hello, hello!"

let result = "hello"
  |> doubleSay
  |> capitalize
  |> exclaim;

result //=> "Hello, hello!"

from the pipeline operator proposal

The Pipeline Operator is from F#, is also implemented in Elm and is similar to Clojure's threading macro.