extensions-ts
v0.0.11
Published
A modular collection of TypeScript extensions for various entities like arrays and objects
Downloads
7
Maintainers
Readme
extensions-ts
Contemplative and deep exploration of extending entities in TypeScript, inspired by Swift-like extensions.
Read about extensions here: Introducing extensions to TypeScript.
Installation
Install the package using npm:
npm install extensions-ts # or yarn add extensions-ts or pnpm add extensions-ts
Usage
To use a specific function from extensions-ts
, directly import the extension you need from its file:
import { Arrays } from 'extensions-ts/Array.extension.js';
const uniqueItems = Arrays.withoutDuplicates([1, 2, 2, 3, 4, 4, 5]);
console.log(uniqueItems); // Output: [1, 2, 3, 4, 5]
Overview
extensions-ts
is a TypeScript library offering a collection of functions designed extend other entities. Inspired by Swift-like extensions, this library aims to provide elegant approaches to solving common programming problems in TypeScript.
While extensions-ts provides extensions itself, the main goal of this repository is to inspire developers to embrace the pattern and extend their own entities (as an alternative to OOP-based solutions).
If you are into FP, then this technique will sound familiar as it's functions exported under a namespace with some restrictions in the structure of these functions (for discoverability reasons).
Available Extensions
export declare namespace Arrays {
function withoutDuplicates<T, K>(array: T[], keyFn?: (item: T) => K): T[];
function groupBy<T, K extends keyof T>(array: T[], key: K | ((obj: T) => string)): Record<string, T[]>;
function compactMap<T, U>(array: T[], mapper: (element: T) => U | null): U[];
function findMap<T, U>(array: T[], mapper: (element: T) => U | null): U | null;
function intersection<T>(array1: T[], array2: T[]): T[];
function zip<T, U>(array: T[], array2: U[]): Array<[T, U, number]>;
function partition<T, M>(xs: T[], pred: (arg0: T) => boolean, transformer: (arg0: T) => M): [M[], M[]];
function maxBy<T>(array: T[], fn: (item: T, index: number) => number): T | undefined;
function minBy<T>(array: T[], iteratee: (value: T) => number): T | undefined;
}
export declare namespace Numbers {
function maxValue(): number;
function minValue(): number;
function random(min: number, max: number): number;
}
export declare namespace Objects {
function isEmpty(obj: any): boolean;
}
export declare namespace Strings {
function capitalizeFirstLetter(word: string): string;
}
Rationale
The general guidelines for extending an entity are:
- Extensions should be placed in their own file, typically
<entity>.extension.ts
, although this can vary at the implementer’s discretion. (e.g.,Array.extension.ts
) - Extensions are inherently linked to the existence of the entity they extend (without embodying an entity themselves).
- An entity should be represented by a type from the type system.
- Extensions are named as the pluralized version of the entity extended.
- Extension functions can either:
- receive the extended entity as their first parameter, along with any additional parameters.
- Compute a value (i.e:
Numbers.maxValue()
)
- Extensions can call other extensions when appropriate, modelling behaviors as extensions wherever possible.
- Extensions should be pure, ensuring that every function is a transformation without side effects.
- Extensions can utilize properties or functions previously defined in the entity they extend.
These guidelines ensure that extensions are self-contained, isolated, shareable, and testable by definition.
For a deeper dive into the reasoning behind the design of extensions-ts
, check out my blog post: Introducing extensions to TypeScript.
While extensions-ts provides extensions themselves, the main goal of this repository is to inspire developers to embrace and extend their own entities (as an alternative to OOP-based solutions).
Tests
To run the tests for extensions-ts
, follow these steps:
npm install
npm run test
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
License
Distributed under the MIT License. See LICENSE
file for more information.