{"id":16957339,"url":"https://github.com/disnet/scrap-js","last_synced_at":"2025-04-11T21:51:47.323Z","repository":{"id":47847257,"uuid":"259103775","full_name":"disnet/scrap-js","owner":"disnet","description":"A declarative data type construction and manipulation library for JavaScript","archived":false,"fork":false,"pushed_at":"2022-03-26T19:46:26.000Z","size":314,"stargazers_count":11,"open_issues_count":9,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T17:49:34.054Z","etag":null,"topics":["javascript","recursion-schemes","template-literals"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/disnet.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-26T18:28:02.000Z","updated_at":"2021-05-07T15:43:07.000Z","dependencies_parsed_at":"2022-08-18T21:52:20.045Z","dependency_job_id":null,"html_url":"https://github.com/disnet/scrap-js","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disnet%2Fscrap-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disnet%2Fscrap-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disnet%2Fscrap-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/disnet%2Fscrap-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/disnet","download_url":"https://codeload.github.com/disnet/scrap-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248487733,"owners_count":21112188,"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":["javascript","recursion-schemes","template-literals"],"created_at":"2024-10-13T22:18:51.365Z","updated_at":"2025-04-11T21:51:47.297Z","avatar_url":"https://github.com/disnet.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Scrap.js\n\nScrap.js let's you declaratively define data types that come with rich manipulation and traversal functionality built right in without the boilerplate.\n\n# Project Status\n\nExperimental, expect the API to change (but will follow semver when it does).\n\n# Use\n\nInstall:\n\n```sh\nnpm install @scrap-js/scrap\n```\n\nUse:\n\n```javascript\nimport scrap from '@scrap-js/scrap';\nimport { reduceSum } from '@scrap-js/scrap/transformers';\n\nlet { Node, Leaf } = scrap`\ndata Node { left: Node | Leaf, right: Node | Leaf }\ndata Leaf { data: any }\n`;\n\nlet tree = Node(\n  Node(Leaf(1), Leaf(10)),\n  Leaf(6)\n);\n\nlet sum = reduceSum(tree,\n                    Leaf.case(({ data }) =\u003e data));\n// sum === 17;\n```\n\nNote at the moment this project uses ES modules exclusively so you'll need a recent version of node or a bundler.\n\n# Documentation\n\nScrap.js has two main components:\n\n- a declarative DSL for defining data types inside template literals\n- a recursion scheme API for performing declarative transformations over the data\n\n## Scrap DSL\n\nData types are defined within `scrap` template literals like so:\n\n```javascript\nimport scrap from '@scrap-js/scrap';\n\nlet { Pair } = scrap`\ndata Pair { left: number, right: number }\n`;\n```\n\nThe syntax `data Pair { left: number, right: number }` defines a data type called `Pair` with two fields `left` and `right` both of type `number`. The result of invoking `scrap` is an object with data constructors for all the `data` declarations within the template literal.\n\nAs the name implies, data constructors allow you to construct object from your data types. Note that data constructors are not JavaScript `class` constructors and should be invoked without the `new` keyword:\n\n```javascript\nlet p = Pair(1, 2);\np.left === 1;\np.right === 2;\n```\nNote that the order of the arguments to the constructor will match the lexical order of fields in the `data` declaration.\n\nYou can check if some value was made by a data constructor via the static `is` predicate:\n\n```javascript\nlet p = Pair(1, 2);\n\nPair.is(p) === true;\nPair.is([1, 2]) === false;\n```\n\n### Data Types\n\nA data type field can have the types:\n\n- any type: `any`\n- the JavaScript base types: `number`, `string`, `boolean`, ...\n- an Array type: `[\u003ctype\u003e]`\n- the union type: `\u003ctype 1\u003e | \u003ctype 2\u003e`\n- a custom data type defined in another `data` declaration\n\n### Mixins\n\nA `data` declaration can \"mixin\" fields from another declaration:\n\n```javascript\ndata Base { a: number }\ndata Derived { b: string, ...Base }\n```\n\nThis is the equivalent of writing:\n\n```javascript\ndata Base { a: number }\ndata Derived { b: string, a: number }\n```\n\n## Scrap API\n\nScrap.js comes with two main kinds of manipulation functions (with some variants):\n\n- `reconstruct` - take a data structure and rebuild it with (potentially) modifications\n- `reduce` - take a data structure and \"summarize\" it into a different value\n\nThese manipulation functions combo with a static function on each data constructor called `case` (described below).\n\nUsing a tree structure for our running example:\n\n```javascript\n\nimport scrap from '@scrap-js/scrap';\n\nlet { Node, Leaf } = scrap`\ndata Node { left: Node | Leaf, right: Node | Leaf }\ndata Leaf { data: number }\n`;\n\nlet tree = Node(\n  Node(Leaf(1), Leaf(10)),\n  Leaf(6)\n);\n```\n\n### `reconstruct(data, ...cases)`\n\nReconstruct `data` bottom-up, matching and transforming each data type by running `cases` over them.\n\nFor example, let's say we want to increment the number in each leaf by one:\n\n```javascript\nimport { reconstruct } from '@scrap-js/scrap/transformers.mjs';\n\nlet resultTree = reconstruct(tree,\n  Leaf.case(({ data }) =\u003e Leaf(data + 1))\n);\n```\n\n`reconstruct` will walk `tree` bottom-up and apply the function passed to `Leaf.case` to each `Leaf` object it encounters replacing the object with the result of the function application. Any non-`Leaf` objects are left alone (or reconstructed if their children were modified).\n\nAlternatively, say we want to replace all right nodes with `-1`:\n\n```javascript\nlet resultTree = reconstruct(tree,\n  Node.case(({ left, right }) =\u003e Node(left, Leaf(-1)))\n);\n```\n\nOr combining it all together:\n\n```javascript\nlet resultTree = reconstruct(tree,\n  Leaf.case(({ data }) =\u003e Leaf(data + 1)),\n  Node.case(({ left, right }) =\u003e Node(left, Leaf(-1)))\n);\n```\n\nVariants:\n\n- `reconstructTopDown` - reconstruct top-down instead of bottom-up\n- `reconstructBottomUp` - reconstruct bottom-up instead of top-down\n- `reconstruct` - an alias of `reconstructBottomUp`\n\n### `reduce(data, empty, concat, ...cases)`\n\nReduce `data` bottom-up. Run `cases` over each data type. `concat` is used to combine the results of `cases` and `empty` is used when no `cases` match a data type.\n\nAn example of summing all the numbers in a tree should be more clear:\n\n```javascript\n\nlet sum = reduce(tree, 0, (l, r) =\u003e l + r,\n                Leaf.case(({ data }) =\u003e data));\n```\n\nThe case `Leaf.case(({ data }) =\u003e data)` extracts the number from each `Leaf`. Note the type of the `case` function here is `Leaf -\u003e number` whereas when `case` is used in `reconstruct` the type is `Leaf -\u003e Leaf`.\n\nThe `concat` function is used to combine (sum) results from each `case` and the `empty` value `0` is used as the default (_whispers:_ [monoid](https://en.wikipedia.org/wiki/Monoid)).\n\n\nVariants:\n\n- `reduceSum` - like `reduce` but with pre-set `empty` as `0` and `concat` as `+`\n- `reduceConcat` - like `reduce` but pre-set `empty` as `[]` and `concat` as `Array.prototype.concat`\n\n# Why the name?\n\nFrom the excellent paper [\"Scrap your boilerplate\"](https://www.microsoft.com/en-us/research/wp-content/uploads/2003/01/hmap.pdf).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdisnet%2Fscrap-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdisnet%2Fscrap-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdisnet%2Fscrap-js/lists"}