diff options
Diffstat (limited to 'alarm/node_modules/matchit')
-rw-r--r-- | alarm/node_modules/matchit/index.d.ts | 11 | ||||
-rw-r--r-- | alarm/node_modules/matchit/lib/matchit.js | 119 | ||||
-rw-r--r-- | alarm/node_modules/matchit/lib/matchit.mjs | 115 | ||||
-rw-r--r-- | alarm/node_modules/matchit/license.md | 21 | ||||
-rw-r--r-- | alarm/node_modules/matchit/package.json | 46 | ||||
-rw-r--r-- | alarm/node_modules/matchit/readme.md | 166 |
6 files changed, 478 insertions, 0 deletions
diff --git a/alarm/node_modules/matchit/index.d.ts b/alarm/node_modules/matchit/index.d.ts new file mode 100644 index 0000000..fb6c0dd --- /dev/null +++ b/alarm/node_modules/matchit/index.d.ts @@ -0,0 +1,11 @@ +export interface Segment { + old: string; + type: number; + val: string; +} + +export type Route = Segment[]; + +export function exec(url: string, match: Route): Record<string, string>; +export function match(path: string, routes: Route[]): Route; +export function parse(path: string): Route; diff --git a/alarm/node_modules/matchit/lib/matchit.js b/alarm/node_modules/matchit/lib/matchit.js new file mode 100644 index 0000000..2c76c54 --- /dev/null +++ b/alarm/node_modules/matchit/lib/matchit.js @@ -0,0 +1,119 @@ +'use strict'; + +const every = require('@arr/every'); + +const SEP = '/'; +// Types ~> static, param, any, optional +const STYPE=0, PTYPE=1, ATYPE=2, OTYPE=3; +// Char Codes ~> / : * +const SLASH=47, COLON=58, ASTER=42, QMARK=63; + +function strip(str) { + if (str === SEP) return str; + (str.charCodeAt(0) === SLASH) && (str=str.substring(1)); + var len = str.length - 1; + return str.charCodeAt(len) === SLASH ? str.substring(0, len) : str; +} + +function split(str) { + return (str=strip(str)) === SEP ? [SEP] : str.split(SEP); +} + +function isMatch(arr, obj, idx) { + idx = arr[idx]; + return (obj.val === idx && obj.type === STYPE) || (idx === SEP ? obj.type > PTYPE : obj.type !== STYPE && (idx || '').endsWith(obj.end)); +} + +function match(str, all) { + var i=0, tmp, segs=split(str), len=segs.length, l; + var fn = isMatch.bind(isMatch, segs); + + for (; i < all.length; i++) { + tmp = all[i]; + if ((l=tmp.length) === len || (l < len && tmp[l-1].type === ATYPE) || (l > len && tmp[l-1].type === OTYPE)) { + if (every(tmp, fn)) return tmp; + } + } + + return []; +} + +function parse(str) { + if (str === SEP) { + return [{ old:str, type:STYPE, val:str, end:'' }]; + } + + var c, x, t, sfx, nxt=strip(str), i=-1, j=0, len=nxt.length, out=[]; + + while (++i < len) { + c = nxt.charCodeAt(i); + + if (c === COLON) { + j = i + 1; // begining of param + t = PTYPE; // set type + x = 0; // reset mark + sfx = ''; + + while (i < len && nxt.charCodeAt(i) !== SLASH) { + c = nxt.charCodeAt(i); + if (c === QMARK) { + x=i; t=OTYPE; + } else if (c === 46 && sfx.length === 0) { + sfx = nxt.substring(x=i); + } + i++; // move on + } + + out.push({ + old: str, + type: t, + val: nxt.substring(j, x||i), + end: sfx + }); + + // shorten string & update pointers + nxt=nxt.substring(i); len-=i; i=0; + + continue; // loop + } else if (c === ASTER) { + out.push({ + old: str, + type: ATYPE, + val: nxt.substring(i), + end: '' + }); + continue; // loop + } else { + j = i; + while (i < len && nxt.charCodeAt(i) !== SLASH) { + ++i; // skip to next slash + } + out.push({ + old: str, + type: STYPE, + val: nxt.substring(j, i), + end: '' + }); + // shorten string & update pointers + nxt=nxt.substring(i); len-=i; i=j=0; + } + } + + return out; +} + +function exec(str, arr) { + var i=0, x, y, segs=split(str), out={}; + for (; i < arr.length; i++) { + x=segs[i]; y=arr[i]; + if (x === SEP) continue; + if (x !== void 0 && y.type | 2 === OTYPE) { + out[ y.val ] = x.replace(y.end, ''); + } + } + return out; +} + +exports.exec = exec; +exports.match = match; +exports.parse = parse;
\ No newline at end of file diff --git a/alarm/node_modules/matchit/lib/matchit.mjs b/alarm/node_modules/matchit/lib/matchit.mjs new file mode 100644 index 0000000..03da81e --- /dev/null +++ b/alarm/node_modules/matchit/lib/matchit.mjs @@ -0,0 +1,115 @@ +'use strict'; + +import every from '@arr/every'; + +const SEP = '/'; +// Types ~> static, param, any, optional +const STYPE=0, PTYPE=1, ATYPE=2, OTYPE=3; +// Char Codes ~> / : * +const SLASH=47, COLON=58, ASTER=42, QMARK=63; + +function strip(str) { + if (str === SEP) return str; + (str.charCodeAt(0) === SLASH) && (str=str.substring(1)); + var len = str.length - 1; + return str.charCodeAt(len) === SLASH ? str.substring(0, len) : str; +} + +function split(str) { + return (str=strip(str)) === SEP ? [SEP] : str.split(SEP); +} + +function isMatch(arr, obj, idx) { + idx = arr[idx]; + return (obj.val === idx && obj.type === STYPE) || (idx === SEP ? obj.type > PTYPE : obj.type !== STYPE && (idx || '').endsWith(obj.end)); +} + +export function match(str, all) { + var i=0, tmp, segs=split(str), len=segs.length, l; + var fn = isMatch.bind(isMatch, segs); + + for (; i < all.length; i++) { + tmp = all[i]; + if ((l=tmp.length) === len || (l < len && tmp[l-1].type === ATYPE) || (l > len && tmp[l-1].type === OTYPE)) { + if (every(tmp, fn)) return tmp; + } + } + + return []; +} + +export function parse(str) { + if (str === SEP) { + return [{ old:str, type:STYPE, val:str, end:'' }]; + } + + var c, x, t, sfx, nxt=strip(str), i=-1, j=0, len=nxt.length, out=[]; + + while (++i < len) { + c = nxt.charCodeAt(i); + + if (c === COLON) { + j = i + 1; // begining of param + t = PTYPE; // set type + x = 0; // reset mark + sfx = ''; + + while (i < len && nxt.charCodeAt(i) !== SLASH) { + c = nxt.charCodeAt(i); + if (c === QMARK) { + x=i; t=OTYPE; + } else if (c === 46 && sfx.length === 0) { + sfx = nxt.substring(x=i); + } + i++; // move on + } + + out.push({ + old: str, + type: t, + val: nxt.substring(j, x||i), + end: sfx + }); + + // shorten string & update pointers + nxt=nxt.substring(i); len-=i; i=0; + + continue; // loop + } else if (c === ASTER) { + out.push({ + old: str, + type: ATYPE, + val: nxt.substring(i), + end: '' + }); + continue; // loop + } else { + j = i; + while (i < len && nxt.charCodeAt(i) !== SLASH) { + ++i; // skip to next slash + } + out.push({ + old: str, + type: STYPE, + val: nxt.substring(j, i), + end: '' + }); + // shorten string & update pointers + nxt=nxt.substring(i); len-=i; i=j=0; + } + } + + return out; +} + +export function exec(str, arr) { + var i=0, x, y, segs=split(str), out={}; + for (; i < arr.length; i++) { + x=segs[i]; y=arr[i]; + if (x === SEP) continue; + if (x !== void 0 && y.type | 2 === OTYPE) { + out[ y.val ] = x.replace(y.end, ''); + } + } + return out; +} diff --git a/alarm/node_modules/matchit/license.md b/alarm/node_modules/matchit/license.md new file mode 100644 index 0000000..a3f96f8 --- /dev/null +++ b/alarm/node_modules/matchit/license.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Luke Edwards <luke.edwards05@gmail.com> (lukeed.com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/alarm/node_modules/matchit/package.json b/alarm/node_modules/matchit/package.json new file mode 100644 index 0000000..619a3fd --- /dev/null +++ b/alarm/node_modules/matchit/package.json @@ -0,0 +1,46 @@ +{ + "name": "matchit", + "version": "1.1.0", + "repository": "lukeed/matchit", + "description": "Quickly parse & match URLs", + "module": "lib/matchit.mjs", + "main": "lib/matchit.js", + "types": "index.d.ts", + "license": "MIT", + "files": [ + "*.d.ts", + "lib" + ], + "author": { + "name": "Luke Edwards", + "email": "luke.edwards05@gmail.com", + "url": "https://lukeed.com" + }, + "engines": { + "node": ">=6" + }, + "scripts": { + "build": "bundt", + "bench": "node bench", + "pretest": "npm run build", + "prebench": "npm run build", + "test": "tape test/*.js | tap-spec" + }, + "keywords": [ + "route", + "regexp", + "routing", + "pattern", + "match", + "parse", + "url" + ], + "dependencies": { + "@arr/every": "^1.0.0" + }, + "devDependencies": { + "bundt": "^0.3.0", + "tap-spec": "^4.1.1", + "tape": "^4.6.3" + } +} diff --git a/alarm/node_modules/matchit/readme.md b/alarm/node_modules/matchit/readme.md new file mode 100644 index 0000000..c131890 --- /dev/null +++ b/alarm/node_modules/matchit/readme.md @@ -0,0 +1,166 @@ +# matchit [![Build Status](https://travis-ci.org/lukeed/matchit.svg?branch=master)](https://travis-ci.org/lukeed/matchit) + +> Quickly parse & match URLs + +## Install + +``` +$ npm install --save matchit +``` + + +## Usage + +```js +const { exec, match, parse } = require('matchit'); + +parse('/foo/:bar/:baz?'); +//=> [ +//=> { old:'/foo/:bar', type:0, val:'foo' }, +//=> { old:'/foo/:bar', type:1, val:'bar' }, +//=> { old:'/foo/:bar', type:3, val:'baz' } +//=> ] + +const routes = ['/', '/foo', 'bar', '/baz', '/baz/:title','/bat/*'].map(parse); + +match('/', routes); +//=> [{ old:'/', type:0, val:'/' }] + +match('/foo', routes); +//=> [{ old:'/foo', type:0, val:'foo' }] + +match('/bar', routes); +//=> [{ old:'bar', type:0, val:'bar' }] + +match('/baz', routes); +//=> [{ old:'/baz', type:0, val:'baz' }] + +let a = match('/baz/hello', routes); +//=> [{...}, {...}] +let b = exec('/baz/hello', a); +//=> { title:'hello' } + +match('/bat/quz/qut', routes); +//=> [ +//=> { old:'/bat/*', type:0, val:'bat' }, +//=> { old:'/bat/*', type:2, val:'*' } +//=> ] +``` + + +## API + +### matchit.parse(route) + +Returns: `Array` + +The `route` is `split` and parsed into a "definition" array of objects. Each object ("segment") contains a `val`, `type`, and `old` key: + +* `old` — The [`route`](#route)'s original value +* `type` — An numerical representation of the segment type. + * `0` - static + * `1` - parameter + * `2` - any/wildcard + * `3` - optional param +* `val` — The current segment's value. This is either a static value of the name of a parameter + +#### route + +Type: `String` + +A single URL pattern. + +> **Note:** Input will be stripped of all leading & trailing `/` characters, so there's no need to normalize your own URLs before passing it to `parse`! + + +### matchit.match(url, routes) + +Returns: `Array` + +Returns the [`route`](#route)'s encoded definition. See [`matchit.parse`](#matchitparseroute). + +#### url + +Type: `String` + +The true URL you want to be matched. + +#### routes + +Type: `Array` + +_All_ "parsed" route definitions, via [`matchit.parse`](#matchitparseroute). + +> **Important:** Multiple routes will require an Array of `matchit.parse` outputs. + + +### matchit.exec(url, match) + +Returns: `Object` + +Returns an object an object of `key:val` pairs, as defined by your [`route`](#route) pattern. + +#### url + +Type: `String` + +The URL (`pathname`) to evaluate. + +> **Important:** This should be `pathname`s only as any `querystring`s will be included the response. + +#### match + +Type: `Array` + +The route definition to use, via [`matchit.match`](#matchitmatchurl-routes). + + +## Benchmarks + +> Running Node v10.13.0 + +``` +# Parsing + matchit x 1,489,482 ops/sec ±2.89% (97 runs sampled) + regexparam x 406,824 ops/sec ±1.38% (96 runs sampled) + path-to-regexp x 83,439 ops/sec ±0.89% (96 runs sampled) + path-to-regexp.parse x 421,266 ops/sec ±0.13% (97 runs sampled) + +# Match (index) + matchit x 132,338,546 ops/sec ±0.14% (96 runs sampled) + regexparam x 49,889,162 ops/sec ±0.21% (95 runs sampled) + path-to-regexp.exec x 7,176,721 ops/sec ±1.23% (94 runs sampled) + path-to-regexp.tokens x 102,021 ops/sec ±0.21% (96 runs sampled) + +# Match (param) + matchit x 2,700,618 ops/sec ±0.92% (95 runs sampled) + regexparam x 6,924,653 ops/sec ±0.33% (94 runs sampled) + path-to-regexp.exec x 4,715,483 ops/sec ±0.28% (96 runs sampled) + path-to-regexp.tokens x 98,182 ops/sec ±0.45% (93 runs sampled) + +# Match (optional) + matchit x 2,816,313 ops/sec ±0.64% (93 runs sampled) + regexparam x 8,437,064 ops/sec ±0.41% (93 runs sampled) + path-to-regexp.exec x 5,909,510 ops/sec ±0.22% (97 runs sampled) + path-to-regexp.tokens x 101,832 ops/sec ±0.43% (98 runs sampled) + +# Match (wildcard) + matchit x 3,409,100 ops/sec ±0.34% (98 runs sampled) + regexparam x 9,740,429 ops/sec ±0.49% (95 runs sampled) + path-to-regexp.exec x 8,740,590 ops/sec ±0.43% (89 runs sampled) + path-to-regexp.tokens x 102,109 ops/sec ±0.35% (96 runs sampled) + +# Exec + matchit x 1,558,321 ops/sec ±0.33% (96 runs sampled) + regexparam x 6,966,297 ops/sec ±0.21% (97 runs sampled) + path-to-regexp x 102,250 ops/sec ±0.45% (95 runs sampled) +``` + +## Related + +- [regexparam](https://github.com/lukeed/regexparam) - A similar (285B) utility, but relies on `RegExp` instead of String comparisons. + + +## License + +MIT © [Luke Edwards](https://lukeed.com) |