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

zefyr

v0.0.1-alpha.21

Published

Configurable and typesafe monkey patching for TypeScript

Downloads

45

Readme

Zefyr

Configurable and typesafe monkey patching for TypeScript.

import 'zefyr/patches';

// blueAndGreen :: ['green', 'blue']
const blueAndGreen = words('red green blue').without('red');
console.log(blueAndGreen); // ['green', 'blue']

new Date().format('yyyy-MM-dd'); // => '2023-09-01'
((3).days + (2).hours).ago() // => Date(2023-08-29T21:00:00.000Z)

const arr = [1, 2, true, 'a'];
arr.last; // => 'a'
const wrongResult = [1, 2, false].sum(); // Throws an error at runtime. wrongResult :: never
const rightResult = [1, 2, 3].sum(); // => 6. rightResult :: number

const arr2 = [1, 2, undefined, 4, null, undefined]; // arr2 :: (number | null | undefined)[]
const arr3 = arr2.compact(); // arr3 :: number[]

(54321).digits[0]; // => 1
(54321).digits(7)[1]; // => 6 (in 7-base)
-10.5.isPositive(); // => false

const obj = { a: 1, b: 2 };
const keys = Object.strictKeys(obj);
for (const key of keys) {
  console.log(obj[key]); // No type error in TypeScript
}

'hello'.reverse(); // => 'olleh'
const light = 'red' as 'red' | 'green' | 'blue' | '';
if (light.isNotEmpty()) {
  const light2 = light; // light2 :: 'red' | 'green' | 'blue'
  // ...
}

for (const n of range(3)) {
  console.log(n); // 0, 1, 2
}

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 patches for Array
import 'zefyr/patches/Number'; // Import patches for Number
import 'zefyr/patches/Date/format'; // Import patches for only Date#format

If you dislike the Monkey Patching pattern, you can also import the patches as functions.

import { sum } from 'zefyr/Array'; // Note that `patches` is not included in the path
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';

// Declarations are required to ensure type safety
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()); // => 84
hello(); // => 'Hello, world!'