{"id":18582090,"url":"https://github.com/lorefnon/fluent-piper","last_synced_at":"2025-04-10T11:35:57.863Z","repository":{"id":46934761,"uuid":"192649208","full_name":"lorefnon/fluent-piper","owner":"lorefnon","description":"Type-safe left-to-right functional pipeline composition","archived":false,"fork":false,"pushed_at":"2024-02-17T07:27:23.000Z","size":1663,"stargazers_count":18,"open_issues_count":16,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T00:47:56.898Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lorefnon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"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":"2019-06-19T03:08:01.000Z","updated_at":"2025-03-19T02:10:56.000Z","dependencies_parsed_at":"2024-10-24T02:57:11.671Z","dependency_job_id":null,"html_url":"https://github.com/lorefnon/fluent-piper","commit_stats":null,"previous_names":["ts-delight/pipe"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Ffluent-piper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Ffluent-piper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Ffluent-piper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lorefnon%2Ffluent-piper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lorefnon","download_url":"https://codeload.github.com/lorefnon/fluent-piper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247704515,"owners_count":20982292,"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-11-07T00:09:10.341Z","updated_at":"2025-04-10T11:35:52.834Z","avatar_url":"https://github.com/lorefnon.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fluent-piper\n\nHave you see code like `dispatch(intUserEvent(await findActiveUser(flatten(ids.map((it) =\u003e it.split(\",\"))))))` and felt: **wow, that's ugly**.\n\nSuch code is hard to read is because to understand it you need to first unwrap it inside out. The FP world has had a solution for this for quite some time: [functional pipelines](https://fsharpforfunandprofit.com/pipeline/).\n\nThis library brings a similar approach to typescript: You can now refactor the above code to be: \n\n```ts\nawait pipe(ids)\n    .thru(ids =\u003e ids.map(it =\u003e it.split(\",\")))\n    .thru(flatten)\n    .thru(findActiveUser)\n    .await()\n    .thru(initUserEvent)\n    .thru(dispatch)\n    .value\n```\n\nThis is longer, but also easier to read because we can follow the logic top -\u003e down, left -\u003e right in natural reading order.\n\nAlso, unlike many similar javascript libraries, this is fully type-safe and does not impose any limitations on the number of steps your chain can have.\n\n## Installation\n\npnpm (recommended):\n\n```\npnpm install fluent-piper\n```\n\nnpm: \n\n```\nnpm install fluent-piper\n```\n\n## Usage - Eager evaluation\n\nWe pass an initial value to pipe and chain steps using `.thru` and finally call `.value` to get the result.\n\n```typescript\nimport {pipe} from \"fluent-piper\";\n\nconst result = pipe(10).thru((i: number) =\u003e i + 1).thru((i: number) =\u003e `Result: ${i}`).value;\n//                   |           ^            |          ^\n//                   |___________|            |__________|----- Types of Subsequent output -\u003e input pairs must match\n//                         |\n//                         Initial input must match input of first step\n//\n// result: string = \"Result: 11\"\n//         ^        ^\n//         |        |__ Result of left-to-right composition\n//         |\n//         |__ Output type inferred from output type of last step\n//\n```\n\nIn above usage, steps are eagerly evaluated - every step passed to `.thru` gets immediately executed.\n\n### With async steps\n\nWe can use `.await()` to ensure that next step receives resolved value instead of a promise\n\n```typescript\nimport {pipe} from \"fluent-piper\";\n\nconst result = pipe(10)\n    .thru(async (i: number) =\u003e i + 1)\n    .await()\n    .thru((i: number) =\u003e `Result: ${i}`)\n        // ^--- Not a promise\n    .value;\n// result: Promise\u003cstring\u003e\n```\n\n## Advanced usage - Lazy Pipelines\n\nThere is a lazy API that offers few more features at the cost of higher overhead. The lazy API builds up a chain of thunks that get executed when the final `.value` is called.\nUntil `.value` is invoked, nothing gets executed.\n\n### Catching exceptions\n\nIf some of the steps can throw, we can use `.catch` to handle them within the pipeline and chain subsequent steps\n\n```typescript\nimport {pipe} from \"fluent-piper\"\n\nconst departmentName = await pipe.lazy({ id: 1})\n    .thru(fetchUser)\n    .await()\n    .thru(fetchDepartment)\n    .await()\n    .catch(e =\u003e {\n        // Catch errors thrown from previous steps (sync/async)\n        console.error(e);\n        return null;\n    })\n    .thru(dept =\u003e dept?.name)\n    .value;\n```\n\n### Bailing early\n\nA more [railway oriented](https://fsharpforfunandprofit.com/rop/) approach to handle early exits in a type-safe manner is available through `.bailIf`:\n\n```typescript\nimport {pipe} from \"fluent-piper\"\n\nconst departmentInfo = await pipe.lazy({ id: 1})\n    .thru(fetchUser)\n    .await()\n    .bailIf(\n        user =\u003e !user.departmentId, // Decide if to bail\n        user =\u003e ({ type: \"unassigned\" as const }) // Value to bail with\n    )\n    .thru(fetchDepartment) // We will reach here only if we didn't bail above\n    .await()\n    .bailIf(\n        isPublic, // (dept) =\u003e boolean\n        dept =\u003e ({ type: \"classified\" as const })\n    )\n    .thru(dept =\u003e ({ type: \"public\" as const, name: dept.name })) // Only if dept is public\n    .value; // Promise\u003c{ type: \"unassigned\" } | { type: \"classified\" } | { type: \"public\", name: string }\u003e\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florefnon%2Ffluent-piper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Florefnon%2Ffluent-piper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Florefnon%2Ffluent-piper/lists"}