{"id":17041414,"url":"https://github.com/davestewart/collection-fns","last_synced_at":"2026-02-11T11:31:54.468Z","repository":{"id":42864123,"uuid":"259047763","full_name":"davestewart/collection-fns","owner":"davestewart","description":"A set of flexible, type-safe functions to manipulate arrays of models","archived":false,"fork":false,"pushed_at":"2023-01-06T04:31:08.000Z","size":1074,"stargazers_count":1,"open_issues_count":11,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-23T03:03:28.109Z","etag":null,"topics":["collections","functional","functional-programming","models","typescript"],"latest_commit_sha":null,"homepage":"","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/davestewart.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}},"created_at":"2020-04-26T14:14:07.000Z","updated_at":"2022-02-17T05:18:28.000Z","dependencies_parsed_at":"2023-02-05T07:46:22.806Z","dependency_job_id":null,"html_url":"https://github.com/davestewart/collection-fns","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/davestewart/collection-fns","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davestewart%2Fcollection-fns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davestewart%2Fcollection-fns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davestewart%2Fcollection-fns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davestewart%2Fcollection-fns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davestewart","download_url":"https://codeload.github.com/davestewart/collection-fns/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davestewart%2Fcollection-fns/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29332539,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-11T06:13:03.264Z","status":"ssl_error","status_checked_at":"2026-02-11T06:12:55.843Z","response_time":97,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["collections","functional","functional-programming","models","typescript"],"created_at":"2024-10-14T09:12:21.642Z","updated_at":"2026-02-11T11:31:54.454Z","avatar_url":"https://github.com/davestewart.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Collection Fns\n\n## Abstract\n\nCollection Fns is a set of flexible, type-safe functions designed to manipulate collections of models:\n\n- a **model** is defined as an object with a common identifier such as `id`, `guid` or `someId`\n- a **collection** is defined as an `Array` of models sharing the same `id` key\n- **flexible functions** is defined that any function can applied to any collection of arbitrary models\n\nThe project has the following goals:\n\n- to provide a basic set of array collection / model manipulation functions\n- to target models by arbitrary property (defaulting to `id`)\n- to be expressive and flexible\n- to be purely functional\n- to be TypeScript native\n\nThe end result is you use simple, safe and robust helper functions to maniulate arrays of models without ever having to resort to writing repetitive, complex, fragile or error-prone array-centric code.\n\n\n## Functions\n\nNote the  \"keyed\" column below, for functions which take an optional `key` parameter, allowing you to target any model schema (defaults to `'id'`).\n\n- an `x` means the model `id` is keyed\n- an `o` means a different property is keyed\n\n### Models\n\nThese functions manage single models within a collection:\n\n| Function    | Keyed  | Description                                                  | Returns |\n| ----------- | ------ | ------------------------------------------------------------ | ------- |\n| first       | \u0026nbsp; | Get the first model in a collection                          | model   |\n| last        | \u0026nbsp; | Get the last model in a collection                           | model   |\n| has         | x      | Test if a collection has a model                             | boolean |\n| get         | x      | Get a model from a collection                                | model   |\n| getIndex    | x      | Get the index of a model in a collection                     | number  |\n| getRandom   | \u0026nbsp; | Get a random model from a collection                         | model   |\n| add         | x      | Add a model to a collection, or if it already exists, update | model   |\n| addOrMove   | x      | Add a model to a collection, or if it already exists, move it to an index | model   |\n| update      | x      | Update a model if it exists in a collection                  | model   |\n| move        | x      | Move a model in a collection to a specific index in the same or a different array | model   |\n| moveToEnd   | x      | Move a model in a collection to the end of the same or a different array | model   |\n| moveByIndex | \u0026nbsp; | Move a model in a collection from one index to another in the same or a different array | model   |\n| remove      | x      | Remove a model from a collection                             | model   |\n\n### Collections\n\nThese functions manipulate collections, offering simple lodash-like functionality:\n\n| Function | Keyed  | Description                                                  | Returns  |\n| -------- | ------ | ------------------------------------------------------------ | -------- |\n| forEach  | \u0026nbsp; | Iterate over a collection of models and call a function on each model | array    |\n| map      | \u0026nbsp; | Iterate over a collection of models, call a function on each model, and return the updated array | array    |\n| filter   | o      | Filter a collection of models by property, including matched values | array    |\n| omit     | o      | Filter a collection of models by property, omitting matched values | array    |\n| dedupe   | x      | Filter a collection of models, omitting those with duplicate ids | array    |\n| merge    | x      | Given two arrays of models, add the models not found in the first array from the second array, and return the new array | array    |\n| sort     | o      | Sort a collection of models by property                      | array    |\n| sortBy   | \u0026nbsp; | Utility function to return a sort() comparison function      | function |\n\n\n## Installation\n\nAdd this line to your project's `.npmrc`:\n\n```\n@likelylogic:registry=https://npm.pkg.github.com\n```\n\nInstall via the command line:\n\n```bash\nnpm i @likelylogic/collection-fns\n```\n\n## Usage\n\nHere are some basic examples showing the flexibility and functional nature of the library:\n\n```js\n// get the model identified by an id of 5\nget(models, 5)\n```\n\n```js\n// update the window identified by a windowId of 10 with new data \nupdate(windows, 10, data, 'windowId')\n```\n\n```js\n// move a tab identified by a tabId 15 to the 5th index in another collection\nmove(left, 15, 5, right, 'tabId')\n```\n\n```js\n// sort a collection of users by first name\nsort(users, 'firstName')\n```\n\nCheck the example files for full code:\n\n- [Basic](./examples/basic.ts) – manage arrays of arbitrary models\n- [Advanced](./examples/advanced.ts) – compose the functions into reusable collection classes\n- [Generics](./examples/generics.ts) – example of using and overriding generic type-safety\n\n\n## Generic type safety\n\nThe package's functions are [generic](https://www.typescriptlang.org/docs/handbook/generics.html#using-type-parameters-in-generic-constraints) meaning that the values you supply the function will enforce their own type checking.\n\nConsider the following; the `people` array should not be able to be updated with the wrong information:\n\n```ts\nimport { update } from '@likelylogic/collection-fns'\n\nconst people = [\n  { id: 1, name: 'tom' },\n  { id: 2, name: 'dick' },\n  { id: 3, name: 'harry' },\n]\n\nupdate(people, 2, { age: 100 }) // error! Object literal may only specify known properties, and 'age' does not exist in type 'Partial\u003c{ id: number; name: string; }\u003e'.\n```\n\nTo force an update, type the payload as `any`:\n\n```ts\nupdate(people, 2, { age: 100 } as any)\n```\n\nYou can be sure that TypeScript's got your back when shuffling models within and between collections!\n\n## Scripts\n\n - `npm run dev` - build and watch the package for changes\n\n- `npm run build` - build the package for production\n- `npm run prepare` - lint and fix, then build the package ready for publishing\n- `npm run lint` - run linting\n- `npm run lint:fix` - run linting and fix errors\n- `npm run test` - run and watch unit tests\n\n## Contributing\n\nAdding new functionality:\n\n- write code\n- write tests\n- run tests / check coverage\n- update docs\n\nPublishing:\n\n- update package version (minor or patch)\n- run scripts:\n\n```\nnpm run prepare\nnpm publish\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavestewart%2Fcollection-fns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavestewart%2Fcollection-fns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavestewart%2Fcollection-fns/lists"}