Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
chill-patch
Advanced tools
chill-patch enables you to add methods to JS classes safely, with none of the problems of traditional monkey-patching.
const chillPatch = require('chill-patch')
const lastFunc = arr => arr[arr.length - 1]
// `last` will be a unique Symbol
const last = chillPatch(Array, lastFunc, 'last')
const array = [1, 2, 3]
array[last]() //=> 3
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!
Another advantage of the chill-patch
API is that you can add methods to objects without using this
! So you can use off-the-shelf this
-less functions without doing anything special.
const toggleSet = require('toggle-set')
const set = new Set([1, 2, 3])
toggleSet(set, 1) // Set([2, 3])
const chillPatch = require('chill-patch')
const toggle = chillPatch(Set, toggleSet)
set[toggle](4) // Set([1, 2, 3, 4])
// 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()
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
Klass
is an ES5-style or ES2015-style classfunc
is a function with any number of argumentsoptionalDescription
is used as the description
of the symbol.Scala Implicit Conversions
Objective-C Categories
Haskell Typeclasses and Rust Traits
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!"
The Pipeline Operator is from F#, is also implemented in Elm and is similar to Clojure's threading macro.
FAQs
Stress-free monkey-patching
The npm package chill-patch receives a total of 14 weekly downloads. As such, chill-patch popularity was classified as not popular.
We found that chill-patch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.