{"id":24442539,"url":"https://github.com/frandepascuali/extensions-ts","last_synced_at":"2025-04-12T21:06:28.593Z","repository":{"id":237532318,"uuid":"794662766","full_name":"FranDepascuali/extensions-ts","owner":"FranDepascuali","description":"Extending entities in TypeScript (inspired by Swift-like extensions)","archived":false,"fork":false,"pushed_at":"2024-05-03T17:09:21.000Z","size":239,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-12T21:06:24.690Z","etag":null,"topics":["arrays","extensions","objects","typescript"],"latest_commit_sha":null,"homepage":"https://depa-thoughts.vercel.app/extensions-typescript/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FranDepascuali.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-01T17:31:37.000Z","updated_at":"2024-11-04T02:40:37.000Z","dependencies_parsed_at":"2024-05-02T12:15:17.919Z","dependency_job_id":"cba8203e-6ffd-4534-aaa0-160cacbdbaee","html_url":"https://github.com/FranDepascuali/extensions-ts","commit_stats":null,"previous_names":["frandepascuali/extensions-ts","frandepascuali/extensions.ts","frandepascuali/ts-extensions"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FranDepascuali%2Fextensions-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FranDepascuali%2Fextensions-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FranDepascuali%2Fextensions-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FranDepascuali%2Fextensions-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FranDepascuali","download_url":"https://codeload.github.com/FranDepascuali/extensions-ts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248631685,"owners_count":21136562,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arrays","extensions","objects","typescript"],"created_at":"2025-01-20T21:59:19.762Z","updated_at":"2025-04-12T21:06:28.563Z","avatar_url":"https://github.com/FranDepascuali.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# extensions-ts\nContemplative and deep exploration of extending entities in TypeScript, inspired by Swift-like extensions.\n\nRead about extensions here: [Introducing extensions to TypeScript](https://depa-thoughts.vercel.app/extensions-typescript/).\n\n## Installation\nInstall the package using npm:\n```bash\n# Using npm\nnpm install extensions-ts\n\n# Using Yarn\nyarn add extensions-ts\n\n# Using pnpm\npnpm add extensions-ts\n```\n\n## Usage\n\nTo use a specific function from `extensions-ts`, directly import the extension you need from its file:\n\n```ts\nimport { Arrays } from 'extensions-ts/Array.extension.js';\n\nconst uniqueItems = Arrays.withoutDuplicates([1, 2, 2, 3, 4, 4, 5]);\nconsole.log(uniqueItems); // Output: [1, 2, 3, 4, 5]\n```\n\n## Overview\n`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.\n\nWhile 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).\n\nIf you are into FP, then this technique will sound familiar as it's functions exported under a\nnamespace with some restrictions in the structure of these functions (for discoverability reasons).\n\n## Available Extensions\n```ts\nexport declare namespace Arrays {\n    function withoutDuplicates\u003cT, K\u003e(array: T[], keyFn?: (item: T) =\u003e K): T[];\n    function groupBy\u003cT, K extends keyof T\u003e(array: T[], key: K | ((obj: T) =\u003e string)): Record\u003cstring, T[]\u003e;\n    function compactMap\u003cT, U\u003e(array: T[], mapper: (element: T) =\u003e U | null): U[];\n    function findMap\u003cT, U\u003e(array: T[], mapper: (element: T) =\u003e U | null): U | null;\n    function intersection\u003cT\u003e(array1: T[], array2: T[]): T[];\n    function zip\u003cT, U\u003e(array: T[], array2: U[]): Array\u003c[T, U, number]\u003e;\n    function partition\u003cT, M\u003e(xs: T[], pred: (arg0: T) =\u003e boolean, transformer: (arg0: T) =\u003e M): [M[], M[]];\n    function maxBy\u003cT\u003e(array: T[], fn: (item: T, index: number) =\u003e number): T | undefined;\n    function minBy\u003cT\u003e(array: T[], iteratee: (value: T) =\u003e number): T | undefined;\n}\n\nexport declare namespace Numbers {\n    function maxValue(): number;\n    function minValue(): number;\n    function random(min: number, max: number): number;\n}\n\nexport declare namespace Objects {\n    function isEmpty(obj: any): boolean;\n}\n\nexport declare namespace Strings {\n    function capitalizeFirstLetter(word: string): string;\n}\n```\n\n## Rationale\nThe general guidelines for extending an entity are:\n1. Extensions should be placed in their own file, typically `\u003centity\u003e.extension.ts`, although this can vary at the implementer’s discretion. (e.g., `Array.extension.ts`)\n2. Extensions are inherently linked to the existence of the entity they extend (without embodying an entity themselves).\n3. An entity should be represented by a type from the type system.\n4. Extensions are named as the pluralized version of the entity extended.\n5. Extension functions can either:\n   1. receive the extended entity as the first parameter, along with any additional parameters.\n   2. receive no parameter and return a value (i.e: `Numbers.maxValue()`)\n6. Extensions can call other extensions when appropriate, modelling behaviors as extensions wherever possible.\n7. Extensions should be pure, ensuring that every function is a transformation without side effects.\n8.  Extensions can utilize properties or functions previously defined in the entity they extend.\n\nThese guidelines ensure that extensions are self-contained, isolated, shareable, and testable by definition.\n\nFor a deeper dive into the reasoning behind the design of `extensions-ts`, check out my blog post: [Introducing extensions to TypeScript](https://depa-thoughts.vercel.app/extensions-typescript/).\n\n### Example\nLet's consider we want to overload array. We create an array extension `Array.extension.ts`:\n```ts\n// We're extending Array, so by convention we use the pluralized name Arrays\nexport namespace Arrays {\n    // As it's a function that belongs to an extension, it receives the entity as it's first parameter\n    export function compactMap\u003cT, U\u003e(array: T[], mapper: (element: T) =\u003e U | null): U[] {\n        ...\n    }\n\n    // Other functions belonging to the extension\n    export function intersection\u003cT\u003e(array: T[], array2: T[]): T[] {\n        ...\n    }\n\n```\n\n## Tests\n\nTo run the tests for `extensions-ts`, follow these steps:\n\n```bash\nnpm install\nnpm run test\n```\n\n## Contributing\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\n## License\n\nDistributed under the MIT License. See `LICENSE` file for more information.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrandepascuali%2Fextensions-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrandepascuali%2Fextensions-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrandepascuali%2Fextensions-ts/lists"}