ohmypatch
v0.0.1
Published
Oh-My-Patch is an advanced function patcher.
Downloads
87
Maintainers
Readme
Oh-My-Patch
Oh-My-Patch is an advanced function and property patcher for JavaScript and TypeScript. It allows you to monkey-patch functions and accessors safely, track metadata, scope patches, and even profile execution time.
Features
- Patch functions or object properties dynamically
- Support for both value properties and accessors (get/set)
- Scope-based patching for safe temporary changes
- Profile execution (call count and total time)
- Undo patches selectively or globally
- Works with JavaScript and TypeScript
Installation
npm install ohmypatchUsage
Patch a function
import { patch, unpatch } from 'ohmypatch';
function greet(name: string) {
return `Hello, ${name}`;
}
// Apply a patch
patch({ greet }, 'greet', (name: string) => `Hi, ${name}`);
console.log(greet('Alice')); // Hi, Alice
// Undo the patch
unpatch({ greet }, 'greet');
console.log(greet('Alice')); // Hello, AlicePatch an accessor
const obj = {
get value() {
return 42;
}
};
patch(obj, 'value', {
get: () => 100,
set: (v) => console.log('Trying to set:', v)
});
console.log(obj.value); // 100
obj.value = 200; // Trying to set: 200Scoped patch execution
import { withPatch } from 'ohmypatch';
withPatch(() => {
patch(obj, 'value', { get: () => 500 });
console.log(obj.value); // 500
});
// Patch automatically removed after scope ends
console.log(obj.value); // 42List active patches
import { listPatches } from 'ohmypatch';
console.log(listPatches());License
This project is licensed under the Apache-2.0 License.
