{"id":26866123,"url":"https://github.com/cordx56/jsdrink","last_synced_at":"2025-05-07T01:23:01.200Z","repository":{"id":58750731,"uuid":"532753893","full_name":"cordx56/jsdrink","owner":"cordx56","description":"Simple parser combinators library written in TypeScript","archived":false,"fork":false,"pushed_at":"2022-09-07T01:48:56.000Z","size":183,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-03T06:11:40.086Z","etag":null,"topics":["parser","parser-combinators","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/cordx56.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":"2022-09-05T05:19:53.000Z","updated_at":"2024-01-22T07:53:56.000Z","dependencies_parsed_at":"2023-01-17T22:30:25.946Z","dependency_job_id":null,"html_url":"https://github.com/cordx56/jsdrink","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cordx56%2Fjsdrink","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cordx56%2Fjsdrink/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cordx56%2Fjsdrink/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cordx56%2Fjsdrink/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cordx56","download_url":"https://codeload.github.com/cordx56/jsdrink/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252448504,"owners_count":21749490,"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":["parser","parser-combinators","typescript"],"created_at":"2025-03-31T04:53:46.226Z","updated_at":"2025-05-07T01:23:01.173Z","avatar_url":"https://github.com/cordx56.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jsdrink - simple parser combinators library\n\n[![Package version](https://img.shields.io/github/package-json/v/cordx56/jsdrink)](https://github.com/cordx56/jsdrink/blob/main/package.json)\n[![npm version](https://img.shields.io/npm/v/jsdrink)](https://www.npmjs.com/package/jsdrink)\n[![CI](https://github.com/cordx56/jsdrink/actions/workflows/ci.yml/badge.svg)](https://github.com/cordx56/jsdrink/actions)\n[![License](https://img.shields.io/github/license/cordx56/jsdrink)](https://github.com/cordx56/jsdrink/blob/main/LICENSE)\n\njsdrink is a simple parser combinators library written in TypeScript, ported from [cordx56/godrink](https://github.com/cordx56/godrink).\n\n## Feature\njsdrink is a simple, lightweight library.\njsdrink does not depend on non-standard libraries except dev-dependencies.\n\njsdrink reads input bytes or string and applies parsers in sequence and parses them into the desired structures.\n\n## Documentation\nSee [https://cordx.net/jsdrink/](https://cordx.net/jsdrink/).\n\n## Example\n### Example code\nHere is an example of a calculator.\n```typescript\nimport { ParseResult, tf, sequence, any, many0 } from \"jsdrink\";\nimport { string, space0 } from \"jsdrink/string\";\nimport { integer } from \"jsdrink/number\";\n\nconst expr = (input: string): ParseResult\u003cnumber\u003e =\u003e {\n  return tf(\n    sequence(\n      term,\n      many0(\n        tf(\n          sequence(space0, any(string(\"+\"), string(\"-\")), space0, term),\n          (parsed) =\u003e {\n            if (parsed[1] == \"+\") {\n              return parsed[3];\n            } else if (parsed[1] == \"-\") {\n              return -1 * parsed[3];\n            } else {\n              return 0;\n            }\n          }\n        )\n      )\n    ),\n    (parsed) =\u003e {\n      return parsed[0] + parsed[1].reduce((sum, elem) =\u003e sum + elem, 0);\n    }\n  )(input);\n};\n\nconst term = (input: string): ParseResult\u003cnumber\u003e =\u003e {\n  return tf(\n    sequence(\n      factor,\n      many0(\n        tf(\n          sequence(space0, any(string(\"*\"), string(\"/\")), space0, factor),\n          (parsed) =\u003e {\n            if (parsed[1] == \"*\") {\n              return parsed[3];\n            } else if (parsed[1] == \"/\") {\n              return 1 / parsed[3];\n            } else {\n              return 0;\n            }\n          }\n        )\n      )\n    ),\n    (parsed) =\u003e {\n      return parsed[0] * parsed[1].reduce((sum, elem) =\u003e sum * elem, 1);\n    }\n  )(input);\n};\n\nconst factor = (input: string): ParseResult\u003cnumber\u003e =\u003e {\n  return any(\n    integer,\n    tf(sequence(string(\"(\"), space0, expr, space0, string(\")\")), (parsed) =\u003e {\n      return parsed[2];\n    })\n  )(input);\n};\n\nconsole.log(expr(\"3 / (1 + 2) * 9\").parsed); // 9\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcordx56%2Fjsdrink","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcordx56%2Fjsdrink","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcordx56%2Fjsdrink/lists"}