{"_id":"@ad2302/pattern-match","_rev":"2-27211df4b4733ee0fb558bc5f599c7bf","name":"@ad2302/pattern-match","dist-tags":{"latest":"0.3.0"},"versions":{"0.3.0":{"name":"@ad2302/pattern-match","description":"A pattern-matching DSL for JSON-like trees of data.","main":"lib/index.js","module":"lib/index.mjs","version":"0.3.0","engines":{"node":">=10"},"author":{"name":"Dave Herman, Bung"},"license":"MIT","scripts":{"lint":"eslint **/*.ts","test":"tap \"tests/**/*.spec.ts\" --ts","dist":"rollup -c"},"publishConfig":{"access":"public"},"devDependencies":{"@rollup/plugin-commonjs":"^20.0.0","@rollup/plugin-json":"^4.1.0","@rollup/plugin-node-resolve":"^13.0.4","@rollup/plugin-typescript":"^8.2.5","@types/node":"^10.17.60","@types/tap":"^15.0.5","@typescript-eslint/eslint-plugin":"^5.2.0","@typescript-eslint/parser":"^5.2.0","copy-dir":"^1.3.0","eslint":"^8.1.0","eslint-config-xo":"^0.39.0","path-exists":"^4.0.0","rimraf":"^3.0.2","rollup-plugin-auto-external":"^2.0.0","rollup-plugin-typescript2":"^0.30.0","tap":"^15.0.10","ts-node":"^10.2.1","typescript":"^4.4.4"},"repository":{"type":"git","url":"git://github.com/ad2302/pattern-match.git"},"keywords":["match","pattern matching","pattern"],"gitHead":"51bfa76dbed592650ef5e54b33852ce00788b4d0","bugs":{"url":"https://github.com/ad2302/pattern-match/issues"},"homepage":"https://github.com/ad2302/pattern-match#readme","_id":"@ad2302/pattern-match@0.3.0","_nodeVersion":"16.7.0","_npmVersion":"7.20.3","dist":{"integrity":"sha512-Z3gQ8oeBAE+oV7aJwjv51UAJUjka0YtUdLBJhAvsQ9NQIAn2PwwYE53/JZl7AgoQpabY/RSSXYo89mlnAhge4g==","shasum":"3a9937489a477c23571aaa73d1203905679e5cd5","tarball":"https://registry.npmjs.org/@ad2302/pattern-match/-/pattern-match-0.3.0.tgz","fileCount":5,"unpackedSize":29016,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJhxHqYCRA9TVsSAnZWagAAe/kQAJNSq7P87x9tDOX5Dai8\n8XgDgFCyK0vV3wShIk2Y2S4hHdl8ptzoth3guCJUvrSVAvoSGP0JY7Q4u4df\nQnnzc9cxKzxGiStuyGz/79hdykgADub7HXaUlMY4KNnmuiZRcgJWHyvGd6bw\nXvkG1p+TMTIV3q9XvFqnIXhmJucJdbBw0yv2pvWGqQ1kQNVKgnM/1JkIwN6o\nzKN1CMSq/RcwQHzsdGdxP+nzDZE+wqu9LG3dis+dSvMd+L/NfUKdTy1MoMiI\nMxwgF1Gltr0rU8WYkrWrENFQs8b/0wNEL1u6vTkilTDl1QPkxaQrpmXol9Hv\nAGtU3h0Hh2Vit3/R7rs2d+gg1UHdNfwu1TYV+rVxmCIaAKuTJ2FethRIQA+S\nF2cdwV5KE/FUQcNo+RLaDUViN/kN53b1FQ5uKIxuXnDpVDdqgdpvcXSt+RYK\nt4rgPX6eMGzAWInWx8a1aJsEKli9lzwhh7zuqFargalKGVsHlpSKpuBK2Q/B\nSkDfKuIicDNRbZU21+DWsM1/Jc/7F9bUHA4oyFfG9lsvUrnEQGKeQCuRfYgK\nRSFhLg1kQ2s16sGZvFJrA3YAcnSnQGE4pT1N8pOBqW7xVa3sKC+ByLJ2s+ja\nC7C/XxtZj6HgZ4u5hzvFD0Pvyb4qxkBbVXAlLAXsdWyuJyv9v1XbTavWFDcc\nmOgW\r\n=XHMF\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGt7rQTsAwRUeyLCZNxoBdi/9s74ZwRQfl+rEHh3NYbmAiEAiEsxTx5tFltLZQlMKIY1k9WLwLTuXcIR0s+CVvFHLBw="}]},"_npmUser":{"name":"bung87","email":"crc32@qq.com"},"directories":{},"maintainers":[{"name":"bung87","email":"crc32@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/pattern-match_0.3.0_1635825602715_0.9934687406738212"},"_hasShrinkwrap":false}},"time":{"created":"2021-11-02T04:00:02.630Z","0.3.0":"2021-11-02T04:00:03.176Z","modified":"2022-04-04T11:21:02.587Z"},"maintainers":[{"name":"bung87","email":"crc32@qq.com"}],"description":"A pattern-matching DSL for JSON-like trees of data.","homepage":"https://github.com/ad2302/pattern-match#readme","keywords":["match","pattern matching","pattern"],"repository":{"type":"git","url":"git://github.com/ad2302/pattern-match.git"},"author":{"name":"Dave Herman, Bung"},"bugs":{"url":"https://github.com/ad2302/pattern-match/issues"},"license":"MIT","readme":"## pattern-match\n\nA pattern matching DSL for JavaScript. The module is a function that\ntakes an arbitrary JavaScript value and tests it against a\n*pattern*. If the match succeeds, the result is a *sub-match object*,\nwhich consists of the sub-components of the value that matched named\nsub-patterns (using the `var` pattern). If the match fails, a\n`MatchError` is thrown.\n\n## Usage\n\nHere's a simple example of using pattern matching to analyze an AST\nfor a hypothetical language:\n\n```javascript\nvar match = require('pattern-match');\n\nmatch(ast, function(when) {\n    when({\n        type: 'FunctionCall',\n        callee: match.var('callee'),\n        args: match.var('args')\n    }, function(vars) {\n        this.analyzeFunctionCall(vars.callee, vars.args);\n    }, this);\n\n    when({\n        type: 'Assignment',\n        lhs: match.var('lhs'),\n        rhs: match.var('rhs')\n    }, function(vars) {\n        this.analyzeAssignment(vars.lhs, vars.rhs);\n    }, this);\n\n    when({\n        type: 'Return',\n        arg: match.var('arg')\n    }, function(vars) {\n        this.analyzeReturn(vars.arg);\n    }, this);\n}, this);\n```\n\nThis will get sweeter in ES6 with destructuring:\n\n```javascript\nvar match = require('pattern-match');\n\nmatch(ast, function(when) {\n    when({\n        type: 'FunctionCall',\n        callee: match.var('callee'),\n        args: match.var('args')\n    }, function({ callee, args }) {\n        this.analyzeFunctionCall(callee, args);\n    }, this);\n\n    when({\n        type: 'Assignment',\n        lhs: match.var('lhs'),\n        rhs: match.var('rhs')\n    }, function({ lhs, rhs }) {\n        this.analyzeAssignment(lhs, rhs);\n    }, this);\n\n    when({\n        type: 'Return',\n        arg: match.var('arg')\n    }, function({ arg }) {\n        this.analyzeReturn(arg);\n    }, this);\n}, this);\n```\n\nAnd sweeter still with ES6 arrow-functions:\n\n```javascript\nvar match = require('pattern-match');\n\nmatch(ast, (when) => {\n    when({\n        type: 'FunctionCall',\n        callee: match.var('callee'),\n        args: match.var('args')\n    }, ({ callee, args }) => {\n        this.analyzeFunctionCall(callee, args);\n    });\n\n    when({\n        type: 'Assignment',\n        lhs: match.var('lhs'),\n        rhs: match.var('rhs')\n    }, ({ lhs, rhs }) => {\n        this.analyzeAssignment(lhs, rhs);\n    });\n\n    when({\n        type: 'Return',\n        arg: match.var('arg')\n    }, ({ arg }) => {\n        this.analyzeReturn(arg);\n    });\n});\n```\n\n\n## API\n\n### Entry points\n\n  * **match(x, body[, thisArg])**\n\nMatch `x` against a sequence of patterns, returning the result of the\nfirst successful match. The cases are provided by the `body` function:\n\n  * **body.call(thisArg, when)**\n\nProvides the cases by calling `when` in the order the cases should be\ntried. The library calls `body` with the `thisArg` provided to `match`\nas the binding of `this`.\n\n  * **when(pattern[, template[, thisArg]])**\n\nProvides the next case, consisting of a pattern an optional\ntemplate. If matching the pattern succeeds, the result is passed to\n`template` with `thisArg` bound to `this` (defaults to the global\nobject). If `template` is not provided, this case produces the\nsub-match object.\n\n  * **match(x).when(pattern[, template[, thisArg]])**\n\nMatch `x` against a single pattern. Returns the result of calling\n`template` on the sub-match object with `thisArg` (or the global\nobject by default) as the binding of `this`. If `template` is not\nprovided, returns the sub-match object.\n\n\n### Patterns\n\n  * **match.any** - matches any value.\n  * **match.primitive** - matches any primitive (non-object) value.\n  * **match.object** - matches any non-null object.\n  * **match.array** - matches anything `Array.isArray` matches.\n  * **match.function** - assumes the pattern is a boolean-valued function and matches any value for which the function returns true.\n  * **match.null** - matches the `null` value.\n  * **match.undefined** - matches the `undefined` value.\n  * **match.boolean** - matches any boolean value.\n  * **match.number** - matches any number value.\n  * **match.int32** - matches any integral number value in the range [-2^31, 2^31).\n  * **match.uint32** - matches any integral number value in the range [0, 2^32).\n  * **match.integer** - matches any integral number value, including -Infinity and Infinity.\n  * **match.finite** - matches any number value other than NaN, -Infinity, and Infinity.\n  * **match.infinite** - matches -Infinity and Infinity.\n  * **match.negative** - matches any number less than 0.\n  * **match.positive** - matches any number greater than 0.\n  * **match.nonnegative** - matches any number greater than or equal to 0 (including -0, which most of the time should just be considered 0).\n  * **match.plusZero** - matches only +0 (and not -0). *If you don't know if you need this, don't use it.*\n  * **match.minusZero** - matches only -0 (and not +0). *If you don't know if you need this, don't use it.*\n  * **match.range(low, high)** - matches any number value in the half-open range [`low`, `high`).\n  * **match.string** - matches any string value.\n  * **match.var(name[, pattern])** - matches the `pattern` (defaults to `any`) and saves the value in the sub-match object with property name `name`.\n  * **match.all(pattern, ...)** - matches if every `pattern` matches.\n  * **match.some(pattern, ...)** - matches if one `pattern` matches.\n  * **pred(testValue)** - matches any value for which `pred` returns a truthy value.\n  * **{ x1: pattern1, ..., xn: patternn }** - matches any object with property names `x1` to `xn` matching patterns `pattern1` to `patternn`, respectively. Only the own properties of the pattern are used.\n  * **[ pattern0, ..., patternn ]** - matches any object with property names 0 to n matching patterns `pattern0` to `patternn`, respectively.\n\n### Custom patterns\n\nYou can create custom patterns by extending the root pattern prototype.\n\n  * **match.pattern** - the root pattern prototype.\n\n### Match errors\n\n  * **match.MatchError** - an object extending `Error` that represents a failed pattern-match.\n      * **e.expected** - the expected pattern.\n      * **e.actual** - the actual value tested.\n","readmeFilename":"README.md"}