{"id":22093205,"url":"https://github.com/iadvize/foldable-helpers-library","last_synced_at":"2025-07-24T20:32:40.234Z","repository":{"id":42931242,"uuid":"239596879","full_name":"iadvize/foldable-helpers-library","owner":"iadvize","description":"Typescript helpers to fold on sum types","archived":false,"fork":false,"pushed_at":"2023-01-06T02:31:51.000Z","size":1921,"stargazers_count":5,"open_issues_count":33,"forks_count":1,"subscribers_count":18,"default_branch":"master","last_synced_at":"2024-11-05T17:52:41.680Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/iadvize.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null}},"created_at":"2020-02-10T19:38:21.000Z","updated_at":"2022-12-25T14:39:05.000Z","dependencies_parsed_at":"2023-02-05T03:15:40.919Z","dependency_job_id":null,"html_url":"https://github.com/iadvize/foldable-helpers-library","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":"iadvize/hello-world-javascript-library","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Ffoldable-helpers-library","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Ffoldable-helpers-library/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Ffoldable-helpers-library/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iadvize%2Ffoldable-helpers-library/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iadvize","download_url":"https://codeload.github.com/iadvize/foldable-helpers-library/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227476010,"owners_count":17779417,"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":[],"created_at":"2024-12-01T03:13:16.651Z","updated_at":"2024-12-01T03:13:17.405Z","avatar_url":"https://github.com/iadvize.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"@iadvize-oss/foldable-helpers\n====================\n![Continuous integration](https://github.com/iadvize/foldable-helpers-library/workflows/Continuous%20integration/badge.svg)\n\n\u003e Helpers to fold on things\n\nWhile we recommend using `@iadvize-oss/foldable-helpers` with Typescript, it can\nbe used in standard Javascript apps.\n\n# 💻 Usage \n\nFirst, install the library:\n\n```bash\nyarn add @iadvize-oss/foldable-helpers\n```\n\n[📖 Documentation](https://iadvize.github.io/foldable-helpers-library/)\n\n## Classic fold - `createFold`\n\nYou have a sum type and the type guards for each of the types. For example:\n\n```ts\ntype T = A | B | C;\n\nfunction isA(t: T): t is A { ... };\nfunction isB(t: T): t is B { ... };\nfunction isC(t: T): t is C { ... };\n```\n\nTo create a fold function to fold on `T`, use `createFold`\n\n```ts\nimport { pipe } from 'fp-ts/es6/pipeable';\n\nimport { createFold } from '@iadvize-oss/foldable-helpers';\n\nconst foldOnT = createFold(isA, isB, isC);\n\nconst t: T = ...;\n\npipe(\n  t,\n  foldOnT(\n    (tbis) =\u003e console.log('executed when t is A', { tbis }),\n    (tbis) =\u003e console.log('executed when t is B', { tbis }),\n    (tbis) =\u003e console.log('executed when t is C', { tbis }),\n  ),\n);\n```\n\n## Named fold - `createFoldObject`\n\nClassic fold is very useful but could become hard to read when we have more than\n3-4 types to fold on.\nBecause we don't have named parameters in JS/Typescript, we have to use\nsomething else.\n\nYou still have a sum type and the type guards for each of the types.\nFor example:\n\n```ts\ntype T = A | B | C;\n\nfunction isA(t: T): t is A { ... };\nfunction isB(t: T): t is B { ... };\nfunction isC(t: T): t is C { ... };\n```\n\nTo create a named fold function to fold on `T` without loosing readability,\nuse `createFoldObject`. You choose the name of each fold function by passing\nan object.\n\n```ts\nimport { pipe } from 'fp-ts/es6/pipeable';\n\nimport { createFoldObject } from '@iadvize-oss/foldable-helpers';\n\nconst foldOnT = createFoldObject({\n  onA: isA,\n  onB: isB,\n  onC: isC\n});\n\nconst t: T = ...;\n\npipe(\n  t,\n  foldOnT({\n    onA: (tbis) =\u003e console.log('executed when t is A', { tbis }),\n    onB: (tbis) =\u003e console.log('executed when t is B', { tbis }),\n    onC: (tbis) =\u003e console.log('executed when t is C', { tbis }),\n  }),\n);\n```\n\n## `and`\n\nWhen using fold you will probably encounter cases where a type is a combination\n(intersection) of different guards. To reduce the boilerplate having to write\neach combination by hand you can use the `and` helper.\n\n```ts\ntype A = { a: string };\ntype B = { b: number };\n\nconst isTypeA = (value: any): value is A =\u003e\n  value != null \u0026\u0026 typeof value.a === 'string';\n\nconst isTypeB = (value: any): value is B =\u003e\n  value != null \u0026\u0026 typeof value.b === 'number';\n\nconst oldIsTypeAAndB = (value: any): value is A \u0026 B =\u003e\n    isTypeA(value) \u0026\u0026 isTypeB(value);\n\n// isTypeAAndB :: (t: any) =\u003e t is A \u0026 B\nconst isTypeAAndB = and(isTypeA, isTypeB);\n```\n\n## `or`\n\nWhen using fold you will probably encounter cases where a type is part of an\nunion of different guards. To reduce the boilerplate having to write each\ncombination by hand you can use the `or` helper.\n\n```ts\ntype A = { a: string };\ntype B = { b: number };\n\nconst isTypeA = (value: any): value is A =\u003e\n  value != null \u0026\u0026 typeof value.a === 'string';\n\nconst isTypeB = (value: any): value is B =\u003e\n  value != null \u0026\u0026 typeof value.b === 'number';\n\nconst oldIsTypeAOrB = (value: any): value is A | B =\u003e\n    isTypeA(value) || isTypeB(value);\n\n// isTypeAOrB :: (t: any) =\u003e t is A | B\nconst isTypeAOrB = or(isTypeA, isTypeB);\n```\n\n## `not`\n\nWhen using createFold you **need to make sure that each guard mutually excludes\nthe others** but it can sometimes be painfull if one type depends on another,\ntherefore we let you use the `not` operator to exclude a guard\n\n```ts\ntype TypeA = { a: string };\ntype TypeB = { a: 'test' };\n\nconst isTypeA = (value: {a: unknown}): value is TypeA =\u003e typeof value.a === 'string';\nconst isTypeB = (value: TypeA): value is TypeB =\u003e value.a === 'test';\n\nconst fold = createFoldObject({\n  onTypeA: combineGuards(isTypeA, not(isTypeB)),\n  onTypeB: isTypeB,\n});\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiadvize%2Ffoldable-helpers-library","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fiadvize%2Ffoldable-helpers-library","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fiadvize%2Ffoldable-helpers-library/lists"}