8
1
Fork
You've already forked sublimation
0
No description
  • TypeScript 52.5%
  • JavaScript 47.5%
Find a file
2026年02月14日 15:35:44 +02:00
.github/workflows refactor: move unit test action to pnpm 2024年08月29日 01:18:52 +01:00
src initail 2026年02月14日 15:35:44 +02:00
test initail 2026年02月14日 15:35:44 +02:00
.gitignore Update env, replace broken ttypescript 2024年03月10日 16:06:58 +00:00
bun.lock initail 2026年02月14日 15:35:44 +02:00
LICENSE Initial commit 2022年04月17日 17:29:06 -04:00
package.json initail 2026年02月14日 15:35:44 +02:00
README.md initail 2026年02月14日 15:35:44 +02:00
tsconfig.json chore: move to tsup 2024年08月29日 00:54:22 +01:00

sublimation

A very simple JavaScript monkeypatcher library that inserts your code in both ends.

fork of spitroast

Usage

// ESM
import * as sublimation from "sublimation";
// CJS
const sublimation = require("sublimation");
const exampleObject = { testFunction: () => {} };
// Patches that run before the original function
sublimation.before("testFunction", exampleObject, (args) => { // `args` is an array of arguments passed to the original function
 console.log("Before");
 // You can modify `args` directly, or return an array to replace the original arguments
}, false); // Changing the second argument to true here would make the patch one-time, meaning it would unpatch after being called once

exampleObject.testFunction(); // logs "Before"

// Patches that run after the original function
sublimation.after("testFunction", exampleObject, (args, ret) => { // `ret` is the return value of the original function
 console.log("After");
 // You can modify `ret` directly, or return something to replace the original return value
});
// Patches that replace the original function
const unpatch = sublimation.instead("testFunction", exampleObject, (args, orig) => { // `orig` is the original function itself
 console.log("Instead");
});
// Patches inherit context from the original function, just use `this` as normal

exampleObject.testFunction(); // Patches stack - This logs "Before", "Instead", "After"

// Unpatches are as simple as calling the return value of the patch function
unpatch(); // Now if you call the function it'll log just "Before" and "After"

// You can also unpatch EVERY patch, but be careful with this!
sublimation.unpatchAll();