extension-methods-js
This package is inspired by C#'s extension methods.
This package provides a function that will bind an extension type to whatever target type you want those methods to be on.
Table of Contents
- Installation
- Basic Usage
Installation:
npm i extension-methods-js
Basic Usage:
import { bindExtensions } from 'extension-methods-js';
declare global {
interface Array<T> {
clear(): void;
}
interface Set<T> {
pipe(action: (item: T) => void): void;
}
}
class ArrayExtensions {
public static clear<TSource>(arr: TSource[]): void {
arr.length = 0;
}
}
class SetExtensions {
public static pipe<TSource>(set: TSource[], action: (item: TSource) => void): void {
for (const item of set) {
action(item);
}
}
}
bindExtensions([[ArrayExtensions, Array], [SetExtensions, Set]]);
const arr = [1, 2, 3];
arr.clear();
console.log(arr);
const set = new Set([1, 2, 3]);
set.pipe(x => console.log(x));