{"id":16658826,"url":"https://github.com/danielrbradley/collection-fns","last_synced_at":"2025-04-09T18:33:26.807Z","repository":{"id":57203160,"uuid":"140888945","full_name":"danielrbradley/collection-fns","owner":"danielrbradley","description":"Functions for working with built-in collection types","archived":false,"fork":false,"pushed_at":"2020-05-11T20:24:28.000Z","size":504,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-03-23T20:37:08.673Z","etag":null,"topics":["array","collections","iterable","javascript","javascript-library","map","set","typescript","typescript-library"],"latest_commit_sha":null,"homepage":"https://www.danielbradley.net/collection-fns/","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/danielrbradley.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"code-of-conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-13T20:11:10.000Z","updated_at":"2021-10-19T04:45:54.000Z","dependencies_parsed_at":"2022-09-16T15:11:05.281Z","dependency_job_id":null,"html_url":"https://github.com/danielrbradley/collection-fns","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielrbradley%2Fcollection-fns","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielrbradley%2Fcollection-fns/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielrbradley%2Fcollection-fns/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielrbradley%2Fcollection-fns/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielrbradley","download_url":"https://codeload.github.com/danielrbradley/collection-fns/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247640509,"owners_count":20971553,"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":["array","collections","iterable","javascript","javascript-library","map","set","typescript","typescript-library"],"created_at":"2024-10-12T10:06:52.070Z","updated_at":"2025-04-09T18:33:26.788Z","avatar_url":"https://github.com/danielrbradley.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Collection Functions\n\n[![npm version](https://badge.fury.io/js/collection-fns.svg)](https://badge.fury.io/js/collection-fns)\n[![GitHub issues](https://img.shields.io/github/issues/danielrbradley/collection-fns.svg)](https://github.com/danielrbradley/collection-fns/issues)\n[![TypeDoc docs](https://img.shields.io/badge/TypeDoc-docs-lightgrey.svg)](https://www.danielbradley.net/collection-fns/)\n[![Travis](https://img.shields.io/travis/danielrbradley/collection-fns.svg)](https://travis-ci.org/danielrbradley/collection-fns)\n[![Coveralls](https://img.shields.io/coveralls/danielrbradley/collection-fns.svg)](https://coveralls.io/github/danielrbradley/collection-fns)\n[![Dev Dependencies](https://david-dm.org/danielrbradley/collection-fns/dev-status.svg)](https://david-dm.org/danielrbradley/collection-fns?type=dev) \n[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)\n\nReally simple functions for working with built-in collection types, inspired by F#'s collection modules design.\n\n### Features\n\n- Modules for iterables, arrays, maps and sets.\n- Composable functional design with full type-safety when used with TypeScript.\n\n## Installation\n\nAdd package using NPM or yarn\n```bash\nnpm i --save collection-fns\n```\n```bash\nyarn add collection-fns\n```\n\nYou can import the top level modules directly:\n\n```javascript\nimport { Iterables, Arrays, Sets, Maps, pipe } from 'collection-fns'\n```\n\nAdditionally, you can import the specific module functions from `dist/lib/[module]`:\n\n```javascript\nimport { groupBy } from 'collection-fns/dist/lib/iterables'\n```\n\n## Examples\n\nCalculating primes lazily with iterators:\n```javascript\nimport { pipe } from 'collection-fns'\nimport { init, map, filter, count } from 'collection-fns/dist/lib/iterables';\n\nconst primes = pipe(\n  init({ from: 1, to: 100 }),\n  map(x =\u003e ({\n    x,\n    factors: pipe(\n      init({ from: 1, to: x }),\n      filter(y =\u003e x % y === 0)\n    )\n  })),\n  filter(num =\u003e count(num.factors) === 2),\n  map(num =\u003e num.x)\n)\n\nfor (const prime of primes) {\n  console.log(prime)\n}\n```\n\nGrouping numbers into odd and even buckets\n```javascript\nimport { pipe, Maps } from 'collection-fns'\nimport { init, groupBy } from 'collection-fns/dist/lib/arrays';\n\nconst oddAndEven = pipe(\n  init({ from: 1, to: 25 }),\n  groupBy(i =\u003e i % 2 === 0 ? 'even' : 'odd'),\n  Maps.ofArray\n)\n```\n\nThis works by use of partial application, however all functional can also be called directly such as:\n\n```javascript\nIterables.map(\n  Iterables.init({ from: 1, to: 10 }),\n  x =\u003e x * x))\n```\n\n## Pipes\n\nThe `pipe()` function is a stand-in until the pipe (`|\u003e`) operator gets [implemented in ESNext](https://github.com/tc39/proposal-pipeline-operator#introduction).\n\nFor pipes of up to 26 steps, the multi-argument overloads can be used where the first argument is the initial value, and all following arguments are functions take the result of the step before and returning a new result. The result from the final step is then returned as the result of the pipe.\n\nFor longer pipes there is an alternative syntax:\n1. The pipe is started by passing `pipe(...)` a single initial value.\n2. Each `.then(...)` step in a pipe takes a callback that is passed the value from the previous step.\n3. At the end of the pipe, access the `.result` property to get the value returned from the last step.\n\nTaking the example from the proposal linked above:\n\n```javascript\nfunction doubleSay (str) {\n  return str + \", \" + str;\n}\nfunction capitalize (str) {\n  return str[0].toUpperCase() + str.substring(1);\n}\nfunction exclaim (str) {\n  return str + '!';\n}\n```\n\nThe following statements are equivalent:\n```javascript\nlet result = exclaim(capitalize(doubleSay(\"hello\")));\nresult // Prints: \"Hello, hello!\"\n\nlet result = pipe(\n  \"hello\",\n  doubleSay,\n  capitalize,\n  exclaim\n)\nresult // Prints: \"Hello, hello!\"\n\nlet result =\n  pipe(\"hello\")\n    .then(doubleSay)\n    .then(capitalize)\n    .then(exclaim)\n    .result\nresult // Prints: \"Hello, hello!\"\n```\n\n## NPM scripts\n\n - `yarn test`: Run test suite\n - `yarn start`: Run `yarn build` in watch mode\n - `yarn test:watch`: Run test suite in [interactive watch mode](http://facebook.github.io/jest/docs/cli.html#watch)\n - `yarn test:prod`: Run linting and generate coverage\n - `yarn build`: Generate bundles and typings, create docs\n - `yarn lint`: Lints code\n - `yarn commit`: Commit using conventional commit style ([husky](https://github.com/typicode/husky) will tell you to use it if you haven't :wink:)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielrbradley%2Fcollection-fns","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielrbradley%2Fcollection-fns","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielrbradley%2Fcollection-fns/lists"}