Zefyr
Configurable and typesafe monkey patching for TypeScript.
import 'zefyr/patches';
const blueAndGreen = words('red green blue').without('red');
console.log(blueAndGreen);
new Date().format('yyyy-MM-dd');
((3).days + (2).hours).ago()
const arr = [1, 2, true, 'a'];
arr.last;
const wrongResult = [1, 2, false].sum();
const rightResult = [1, 2, 3].sum();
const arr2 = [1, 2, undefined, 4, null, undefined];
const arr3 = arr2.compact();
(54321).digits[0];
(54321).digits(7)[1];
-10.5.isPositive();
const obj = { a: 1, b: 2 };
const keys = Object.strictKeys(obj);
for (const key of keys) {
console.log(obj[key]);
}
'hello'.reverse();
const light = 'red' as 'red' | 'green' | 'blue' | '';
if (light.isNotEmpty()) {
const light2 = light;
}
for (const n of range(3)) {
console.log(n);
}
Note: Zefyr requires TypeScript 5.0 or above and ES2020 or later. Currently, only ES Modules are supported.
Installation
npm
npm install zefyr
yarn
yarn add zefyr
pnpm
pnpm add zefyr
Usage
The simplest way to start using Zefyr is to import zefyr/patches
in your project. It would automatically patch the global objects and prototypes, so you can use the extensions directly.
All patches are well-typed in TypeScript, as well as well-documented, you can explore them in your editor or IDE just like the built-in ones.
import 'zefyr/patches';
All patches are optional, you can import only the patches you need.
import 'zefyr/patches/Array';
import 'zefyr/patches/Number';
import 'zefyr/patches/Date/format';
If you dislike the Monkey Patching pattern, you can also import the patches as functions.
import { sum } from 'zefyr/Array';
import isNegative from 'zefyr/Number/isNegative';
Zefyr exports a patch
function for easy patching. You can create your own patches using it.
import { patch } from 'zefyr';
declare global {
interface Number {
double(): number;
}
function hello(): void;
}
patch(Number).with({ double: (n) => n * 2 });
patch(globalThis).withStatic({ hello: () => console.log('Hello, world!') });
console.log((42).double());
hello();