parth 4.2.3 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,14 +6,12 @@
6
6
  [todo](#todo) -
7
7
  [why](#why)
8
8
 
9
- [![build][badge-build]][x-travis]
10
-
11
9
  ## sample
12
10
 
13
11
  ```js
14
- var Parth = require('parth');
12
+ import Parth from 'parth';
15
13
 
16
- var parth = new Parth();
14
+ const parth = new Parth();
17
15
  var props = {handle: function(){}};
18
16
 
19
17
  parth.set('(get|post) /:page/:view', props)
@@ -37,11 +35,11 @@ parth.set('(get|post) /:page/:view', props)
37
35
 
38
36
  ## documentation
39
37
 
40
- The `module.exports` a `Parth` constructor
38
+ Parth exports a `Parth` class (default export)
41
39
 
42
- ````js
43
- var Parth = require('parth');
44
- ````
40
+ ```js
41
+ import Parth from 'parth';
42
+ ```
45
43
 
46
44
  which can take the options below
47
45
 
@@ -55,7 +53,7 @@ _options_ type `object`, can be
55
53
  example:
56
54
 
57
55
  ```js
58
- var parth = new Parth({ defaultRE: /[^\s\/?#]+/ });
56
+ const parth = new Parth({ defaultRE: /[^\s\/?#]+/ });
59
57
 
60
58
  parth.set('/page/:view') // no regex given after ":view"
61
59
  .get('/page/10/?query=here')
@@ -124,11 +122,13 @@ _return_
124
122
  > NOTE: the returned object is a deep copy of the original `options`
125
123
  > given in `parth.set` to avoid mutation
126
124
 
127
- ### parth properties
125
+ ### TypeScript
128
126
 
129
- - `parth.store`: all paths set for match are here
130
- - `parth.regex`: array of carefully ordered regexes
131
- - `parth.regex.master`: regex aggregating all learned
127
+ Parth is written in TypeScript and ships with type definitions. Import types as needed:
128
+
129
+ ```ts
130
+ import Parth, { ParthOptions, ParthResult } from 'parth';
131
+ ```
132
132
 
133
133
  ## why
134
134
 
@@ -142,45 +142,23 @@ With [npm](http://npmjs.org)
142
142
 
143
143
  ### examples
144
144
 
145
- Run the [`example.js`](example.js) file.
145
+ Run the [`example.ts`](example.ts) file: `npx tsx example.ts` (or `npm run dist` then run with Node after changing the import to `./dist`).
146
146
 
147
147
  ### test
148
148
 
149
149
  npm test
150
150
 
151
151
  ```
152
- ➜ parth (master) ✓ npm t
153
- parth
154
- paths
155
- object
156
- raw object paths
157
- ✓ unix paths
158
- raw unix paths
159
- urls
160
- ✓ raw urls
161
- ✓ urls: querystring is stripped
162
- ✓ urls: hash is stripped
163
- ✓ urls: parameters are not mistaken as querystrings
164
- ✓ space separated paths
165
- ✓ raw, space separated paths
166
- ✓ unix, object and url paths together
167
- ✓ raw: unix, object and urls paths together
168
- params
169
- ✓ can be given as a string regex
170
- ✓ will contain all parameter keys at _
171
- ✓ parameter values should be at params
172
- notFound
173
- ✓ should be false for perfect match
174
- ✓ should have what is left of the path
175
-
176
-
177
- 18 passing (16ms)
152
+ ➜ parth npm test
153
+ PASS test/paths.test.ts
154
+ PASS test/params.test.ts
155
+ PASS test/options.test.ts
156
+ PASS test/notFound.test.ts
157
+
158
+ Test Suites: 4 passed, 4 total
159
+ Tests: 20 passed, 20 total
178
160
  ```
179
161
 
180
- ### todo
181
-
182
- - [ ] set support for regexp input
183
-
184
162
  ### license
185
163
 
186
164
  The MIT License (MIT)
@@ -194,7 +172,5 @@ The above copyright notice and this permission notice shall be included in all c
194
172
  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.
195
173
 
196
174
  [x-npm]: https://npmjs.org/package/parth
197
- [x-travis]: https://travis-ci.org/stringparser/parth/builds
198
- [badge-build]: http://img.shields.io/travis/stringparser/parth/master.svg?style=flat-square
199
175
  [badge-version]: http://img.shields.io/npm/v/parth.svg?style=flat-square
200
176
  [badge-downloads]: http://img.shields.io/npm/dm/parth.svg?style=flat-square
@@ -0,0 +1,10 @@
1
+ import type { ParthOptions, ParthResult, PathOpt } from './types';
2
+ export type { ParthOptions, ParthResult };
3
+ export declare class Parth {
4
+ #private;
5
+ constructor(options?: ParthOptions);
6
+ set(path: string, opt?: Partial<PathOpt>): this;
7
+ get(path: string): ParthResult | null;
8
+ }
9
+ export default Parth;
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,OAAO,EAGR,MAAM,SAAS,CAAC;AAEjB,YAAY,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC;AAO1C,qBAAa,KAAK;;gBAIJ,OAAO,CAAC,EAAE,YAAY;IAUlC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI;IA8D/C,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI;CAwCtC;AAED,eAAe,KAAK,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
36
+ if (kind === "m") throw new TypeError("Private method is not writable");
37
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
38
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
39
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
40
+ };
41
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
42
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
43
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
44
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
45
+ };
46
+ var _Parth_store, _Parth_regex;
47
+ Object.defineProperty(exports, "__esModule", { value: true });
48
+ exports.Parth = void 0;
49
+ const _ = __importStar(require("./lib"));
50
+ const qshRE = /[?#][^/\s]*/g;
51
+ const depthRE = /((^|[/?#.\s]+)[(:\w])/g;
52
+ const paramRE = /:([-\w]+)(\([^\s]+?[)][?)]*)?/g;
53
+ const noParamRE = /(^|[/.=\s]+)(\(.+?\)+)/g;
54
+ class Parth {
55
+ constructor(options) {
56
+ _Parth_store.set(this, {});
57
+ _Parth_regex.set(this, void 0);
58
+ __classPrivateFieldSet(this, _Parth_regex, [], "f");
59
+ __classPrivateFieldGet(this, _Parth_regex, "f").master = new RegExp('(?:[])');
60
+ __classPrivateFieldGet(this, _Parth_regex, "f").defaultRE = /[^?#./\s]+/;
61
+ if (options?.defaultRE) {
62
+ __classPrivateFieldGet(this, _Parth_regex, "f").defaultRE = options.defaultRE;
63
+ }
64
+ }
65
+ set(path, opt) {
66
+ if (typeof path !== 'string') {
67
+ return this;
68
+ }
69
+ const o = _.cloneDeep(opt || {});
70
+ o.path = path.replace(/\s+/g, ' ').trim();
71
+ if (__classPrivateFieldGet(this, _Parth_store, "f")[o.path]) {
72
+ Object.assign(__classPrivateFieldGet(this, _Parth_store, "f")[o.path], o);
73
+ return this;
74
+ }
75
+ __classPrivateFieldGet(this, _Parth_regex, "f").push((__classPrivateFieldGet(this, _Parth_store, "f")[o.path] = o));
76
+ let index = -1;
77
+ const defaultRE = '(' + __classPrivateFieldGet(this, _Parth_regex, "f").defaultRE.source + ')';
78
+ o.stem = o.path.replace(noParamRE, function ($0, $1, $2) {
79
+ return $1 + ':' + (++index) + $2;
80
+ });
81
+ const url = (o.stem.match(/[^\s]*\/\S*/) || [null]).pop();
82
+ const qsh = _.getQueryString(url);
83
+ if (url && !qsh) {
84
+ o.stem = o.stem.replace(url, url.replace(/\/$/, '$1') + ':qs(?:\\/)?(' + qshRE.source + ')?');
85
+ }
86
+ o.depth = o.stem.split(depthRE).length || -1;
87
+ o.regex = new RegExp('^' +
88
+ o
89
+ .stem.replace(/\S+/g, function (s) {
90
+ return s.replace(paramRE, function ($0, $1, $2) {
91
+ return $2 || defaultRE;
92
+ });
93
+ })
94
+ .replace(/[^?( )+*$]+(?=\(|$)/g, function escapeRegExp($0) {
95
+ return $0.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
96
+ }));
97
+ __classPrivateFieldGet(this, _Parth_regex, "f").sort(function (x, y) {
98
+ return y.depth - x.depth || y.stem.localeCompare(x.stem);
99
+ });
100
+ __classPrivateFieldGet(this, _Parth_regex, "f").master = new RegExp('(' +
101
+ __classPrivateFieldGet(this, _Parth_regex, "f")
102
+ .map(function (el) {
103
+ return el.regex.source.replace(/\((?=[^?])/g, '(?:');
104
+ })
105
+ .join(')|(') +
106
+ ')');
107
+ return this;
108
+ }
109
+ get(path) {
110
+ if (typeof path !== 'string') {
111
+ return null;
112
+ }
113
+ path = path.replace(/\s+/g, ' ').trim();
114
+ if (__classPrivateFieldGet(this, _Parth_store, "f")[path]) {
115
+ return Object.assign(_.cloneDeep(__classPrivateFieldGet(this, _Parth_store, "f")[path]), {
116
+ match: path,
117
+ notFound: '',
118
+ params: {},
119
+ });
120
+ }
121
+ const found = __classPrivateFieldGet(this, _Parth_regex, "f").master.exec(path);
122
+ if (!found) {
123
+ return null;
124
+ }
125
+ const o = {
126
+ match: found.shift(),
127
+ params: {},
128
+ notFound: '',
129
+ };
130
+ const matchIndex = found.indexOf(o.match);
131
+ const foundEntry = __classPrivateFieldGet(this, _Parth_regex, "f")[matchIndex];
132
+ o.notFound = path.slice(o.match.length);
133
+ let paramIndex = -1;
134
+ const params = foundEntry.regex.exec(path).slice(1);
135
+ foundEntry.stem.replace(paramRE, function ($0, $1) {
136
+ o.params[$1] = params[++paramIndex];
137
+ return $0;
138
+ });
139
+ return Object.assign(_.cloneDeep(foundEntry), o);
140
+ }
141
+ }
142
+ exports.Parth = Parth;
143
+ _Parth_store = new WeakMap(), _Parth_regex = new WeakMap();
144
+ exports.default = Parth;
145
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yCAA2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,EAAE,CAAC;YACX,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,CAAC,GAAgB;YACrB,KAAK,EAAE,KAAK,CAAC,KAAK,EAAG;YACrB,MAAM,EAAE,EAAE;YACV,QAAQ,EAAE,EAAE;SACb,CAAC;QAEF,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAC1C,MAAM,UAAU,GAAG,uBAAA,IAAI,oBAAO,CAAC,UAAU,CAAC,CAAC;QAC3C,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QAExC,IAAI,UAAU,GAAG,CAAC,CAAC,CAAC;QACpB,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAErD,UAAU,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE;YAC/C,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,EAAE,UAAU,CAAC,CAAC;YACpC,OAAO,EAAE,CAAC;QACZ,CAAC,CAAC,CAAC;QAEH,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,CAAC;CACF;AApHD,sBAoHC;;AAED,kBAAe,KAAK,CAAC"}
package/dist/lib.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import cloneDeep from 'lodash.clonedeep';
2
+ export { cloneDeep };
3
+ export declare function getQueryString(url: string | null | undefined): string | null;
4
+ //# sourceMappingURL=lib.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.d.ts","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,kBAAkB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,CAAC;AAErB,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAY5E"}
package/dist/lib.js ADDED
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.cloneDeep = void 0;
7
+ exports.getQueryString = getQueryString;
8
+ const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
9
+ exports.cloneDeep = lodash_clonedeep_1.default;
10
+ function getQueryString(url) {
11
+ if (!url) {
12
+ return null;
13
+ }
14
+ const index = url.indexOf('?');
15
+ if (index > -1 && url.charAt(index + 1) !== ':') {
16
+ return url.substring(index);
17
+ }
18
+ else {
19
+ return null;
20
+ }
21
+ }
22
+ //# sourceMappingURL=lib.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lib.js","sourceRoot":"","sources":["../src/lib.ts"],"names":[],"mappings":";;;;;;AAIA,wCAYC;AAhBD,wEAAyC;AAEhC,oBAFF,0BAAS,CAEE;AAElB,SAAgB,cAAc,CAAC,GAA8B;IAC3D,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAE/B,IAAI,KAAK,GAAG,CAAC,CAAC,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC;QAChD,OAAO,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;SAAM,CAAC;QACN,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
package/package.json CHANGED
@@ -1,13 +1,9 @@
1
1
  {
2
2
  "name": "parth",
3
- "main": "index.js",
4
- "version": "4.2.3",
3
+ "main": "dist/index.js",
4
+ "version": "5.0.0",
5
5
  "engines": {
6
- "node": ">= 0.10.0"
7
- },
8
- "dependencies": {
9
- "lodash.clone": "^3.0.3",
10
- "lodash.merge": "^3.3.2"
6
+ "node": ">= 14"
11
7
  },
12
8
  "author": "Javier Carrillo",
13
9
  "description": "path to regex madness",
@@ -17,13 +13,25 @@
17
13
  "url": "https://github.com/stringparser/parth/issues"
18
14
  },
19
15
  "scripts": {
20
- "lint": "eslint lib *.js",
21
- "test": "npm run lint && mocha test"
16
+ "lint": "eslint src test example.ts --fix",
17
+ "test": "jest",
18
+ "posttest": "npm run verify:publish",
19
+ "dist": "tsc",
20
+ "prepublishOnly": "npm run dist",
21
+ "verify:publish": "bash test/verify-publish/test.sh"
22
22
  },
23
23
  "devDependencies": {
24
- "mocha": "*",
25
- "should": "*",
26
- "eslint": "*"
24
+ "@types/jest": "30.0.0",
25
+ "@types/lodash.clonedeep": "4.5.8",
26
+ "@types/node": "25.2.1",
27
+ "eslint": "^9.39.2",
28
+ "jest": "30.2.0",
29
+ "ts-jest": "29.4.6",
30
+ "typescript": "5.9.3",
31
+ "typescript-eslint": "^8.54.0"
32
+ },
33
+ "dependencies": {
34
+ "lodash.clonedeep": "4.5.0"
27
35
  },
28
36
  "keywords": [
29
37
  "url",
@@ -34,5 +42,9 @@
34
42
  "params",
35
43
  "cached"
36
44
  ],
37
- "license": "MIT"
45
+ "license": "MIT",
46
+ "types": "dist/index.d.ts",
47
+ "files": [
48
+ "dist"
49
+ ]
38
50
  }
package/.editorconfig DELETED
@@ -1,14 +0,0 @@
1
-
2
- # editorconfig.org
3
- root = true
4
-
5
- [*]
6
- indent_style = space
7
- indent_size = 2
8
- end_of_line = lf
9
- charset = utf-8
10
- trim_trailing_whitespace = true
11
- insert_final_newline = true
12
-
13
- [*.md]
14
- trim_trailing_whitespace = false
package/.eslintrc DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "env": {
3
- "node": true,
4
- "mocha": true
5
- },
6
- "rules": {
7
- "strict": 2,
8
- "quotes": [2, "single", "avoid-escape"],
9
- "indent": ["error", 2, {"SwitchCase": 1}],
10
- "eol-last": 2,
11
- "no-shadow": 2,
12
- "dot-notation": 2,
13
- "dot-location": [2, "property"],
14
- "comma-dangle": [2, "never"],
15
- "no-unused-vars": 2,
16
- "no-multi-spaces": 2,
17
- "no-process-exit": 0,
18
- "consistent-return": 2,
19
- "no-use-before-define": 0,
20
- "no-underscore-dangle": 0,
21
- "no-unused-expressions": 2
22
- }
23
- }
package/.npmignore DELETED
@@ -1 +0,0 @@
1
- node_modules
package/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - "0.10"
4
- - "4"
5
- - "stable"
6
- notifications:
7
- email: true
package/example.js DELETED
@@ -1,69 +0,0 @@
1
- 'use strict';
2
-
3
- var input, result;
4
- var parth = require('./.')();
5
-
6
- // #set
7
- input = [
8
- '/',
9
- '/page',
10
- '/page/:number(\\d+)',
11
- 'get /',
12
- 'get /page',
13
- 'get /:page',
14
- 'get :page/:view',
15
- 'get /:page/:view',
16
- 'get /page/:number(\\d+)',
17
- 'get /:page(\\w+)/number',
18
- 'get /:page(\\w+)/:number(\\d+)',
19
- '(get|post) /page/number',
20
- '(get|post) /:page(\\w+)/number',
21
- '(get|post) /:page(\\w+)/:Number(\\d+)',
22
- 'get /page/number',
23
- '(get|post) /:page(\\w+)/:view([^.\\/\\s]+)',
24
- '1', '2', '1 2',
25
- 'obj.path',
26
- 'obj.:path(\\S+).:number(\\d+)',
27
- 'obj.:number(\\d+).:path(\\S+)',
28
- 'obj.path.:here',
29
- 'obj.(prop|path).:here',
30
- ':obj.(method|prop).:here'
31
-
32
- ];
33
-
34
- input.forEach(function(stem){
35
- parth.set(stem);
36
- });
37
-
38
- // #get
39
- input = [
40
- '1',
41
- '2',
42
- '1 2',
43
- 'obj.path.10',
44
- 'obj.10.prop',
45
- 'obj.10.10',
46
- 'array.method.prop',
47
- 'get weekend/baby?query=string#hash user.10.beers',
48
- 'get /weekend/baby?query=string#hash user.10.beers now',
49
- 'get /user/view/#hash',
50
- 'post /user/page/photo?query=name&path=tree#hash'
51
- ];
52
-
53
- console.log(' -- parth.get -- ');
54
- input.forEach(function(stem, index){
55
- result = parth.get(stem);
56
- console.log(' input =', stem);
57
- console.log('result =', result);
58
- console.log((input[index + 1] ? ' -- ' : '' ));
59
- });
60
-
61
- if(process.argv.indexOf('-l') < 0){ return; }
62
- Object.keys(parth).forEach(function(prop){
63
- console.log(parth[prop]);
64
- console.log(' --\n');
65
- });
66
-
67
- parth.regex.forEach(function(re){
68
- console.log(re.stem);
69
- });
package/index.js DELETED
@@ -1,115 +0,0 @@
1
- 'use strict';
2
-
3
- var util = require('./lib/util');
4
-
5
- exports = module.exports = Parth;
6
-
7
- function Parth(options){
8
- if(!(this instanceof Parth)){
9
- return new Parth(options);
10
- }
11
-
12
- this.store = {};
13
- this.regex = [];
14
- this.regex.master = new RegExp('(?:[])');
15
- this.regex.defaultRE = /[^?#./\s]+/;
16
-
17
- if(options){
18
- this.regex.defaultRE = options.defaultRE || this.regex.defaultRE;
19
- }
20
- }
21
-
22
- var qshRE = /[?#][^\/\s]*/g;
23
- var depthRE = /((^|[/?#.\s]+)[(:\w])/g;
24
- var paramRE = /:([-\w]+)(\([^\s]+?[)][?)]*)?/g;
25
- var noParamRE = /(^|[/.=\s]+)(\(.+?\)+)/g
26
-
27
- Parth.prototype.set = function(path, opt){
28
- if(typeof path !== 'string'){
29
- return this;
30
- }
31
-
32
- var o = util.clone(opt || {}, true);
33
- o.path = path.replace(/\s+/g, ' ').trim();
34
-
35
- if(this.store[o.path]){
36
- util.merge(this.store[o.path], o);
37
- return this;
38
- }
39
- this.regex.push(this.store[o.path] = o);
40
-
41
- var index = -1;
42
- var defaultRE = '(' + this.regex.defaultRE.source + ')';
43
-
44
- o.stem = o.path.replace(noParamRE, function($0, $1, $2){
45
- return $1 + ':' + (++index) + $2;
46
- });
47
-
48
- var url = (o.stem.match(/[^\s]*\/\S*/) || [null]).pop();
49
- var qsh = util.getQueryString(url);
50
-
51
- if(url && !qsh){
52
- o.stem = o.stem.replace(url, url.replace(/\/$/, '$1') +
53
- ':qs(?:\\/)?(' + qshRE.source + ')?'
54
- );
55
- }
56
-
57
- o.depth = o.stem.split(depthRE).length || -1;
58
- o.regex = new RegExp('^' +
59
- o.stem.replace(/\S+/g, function(s){
60
- return s.replace(paramRE, function($0, $1, $2){
61
- return ($2 || defaultRE);
62
- });
63
- }).replace(/[^?( )+*$]+(?=\(|$)/g, function escapeRegExp($0){
64
- return $0.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
65
- })
66
- );
67
-
68
- // order regexes according to depth (# of separation tokes) or,
69
- // if that fails, use localCompare
70
-
71
- this.regex.sort(function(x, y){
72
- return (y.depth - x.depth) || y.stem.localeCompare(x.stem);
73
- });
74
-
75
- // sum up all learned (void all groups and make a giant regex)
76
- this.regex.master = new RegExp( '(' +
77
- this.regex.map(function voidRegExp(el){
78
- return el.regex.source.replace(/\((?=[^?])/g, '(?:');
79
- }).join(')|(') + ')'
80
- );
81
-
82
- return this;
83
- };
84
-
85
- Parth.prototype.get = function(path){
86
- if(typeof path !== 'string'){
87
- return null;
88
- }
89
-
90
- path = path.replace(/\s+/g, ' ').trim();
91
-
92
- if(this.store[path]){
93
- return util.merge(util.clone(this.store[path], true), {
94
- match: path,
95
- notFound: ''
96
- });
97
- }
98
-
99
- var found = this.regex.master.exec(path);
100
- if(!found){ return null; }
101
-
102
- var o = {match: found.shift(), params: {}};
103
-
104
- found = this.regex[found.indexOf(o.match)];
105
- o.notFound = path.slice(o.match.length);
106
-
107
- var index = -1;
108
- var params = found.regex.exec(path).slice(1);
109
-
110
- found.stem.replace(paramRE, function($0, $1){
111
- o.params[$1] = params[++index];
112
- });
113
-
114
- return util.merge(util.clone(found, true), o);
115
- };
package/lib/util.js DELETED
@@ -1,22 +0,0 @@
1
- 'use strict';
2
-
3
- exports = module.exports = {};
4
-
5
- // dependencies
6
- //
7
- exports.clone = require('lodash.clone');
8
- exports.merge = require('lodash.merge');
9
-
10
- // assorted
11
- //
12
- exports.getQueryString = function(url){
13
- if(!url){ return null; }
14
-
15
- var index = url.indexOf('?');
16
-
17
- if(index > -1 && url.charAt(index + 1) !== ':'){
18
- return url.substring(index);
19
- } else {
20
- return null;
21
- }
22
- };
package/test/index.js DELETED
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- var path = require('path');
4
- var pack = require('../');
5
-
6
- require('should');
7
-
8
- [
9
- 'paths.js',
10
- 'params.js',
11
- 'options.js',
12
- 'notFound.js'
13
- ].forEach(function(file){
14
- if(file === 'index.js'){ return; }
15
- var suite = path.basename(file, path.extname(file));
16
- describe(suite, function(){
17
- require('./' + file)(pack);
18
- });
19
- });
package/test/notFound.js DELETED
@@ -1,24 +0,0 @@
1
- 'use strict';
2
-
3
- var path, stems, result;
4
-
5
- module.exports = function(Parth){
6
- var parth = new Parth();
7
-
8
- it('should be empty string for perfect match', function(){
9
- stems = ':method(get) /hello/:there';
10
- path = 'get /hello/awesome?query#hash';
11
-
12
- result = parth.set(stems).get(path);
13
- result.should.not.be.eql(null);
14
- result.notFound.should.be.eql('');
15
- });
16
-
17
- it('should have what is left of the path', function(){
18
- stems = ':method(get) /hello/:there';
19
- path = 'get /hello/awesome/human';
20
- result = parth.set(stems).get(path);
21
- result.should.not.be.eql(null);
22
- result.notFound.should.be.eql('human');
23
- });
24
- };
package/test/options.js DELETED
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- var path, stems, result;
4
-
5
- module.exports = function(Parth){
6
-
7
- it('default regex can be changed using options', function(){
8
- var parth = new Parth({
9
- defaultRE: /\S+/
10
- });
11
-
12
- stems = 'do :src :dest';
13
- path = 'do /src/**/*.js /dest/';
14
-
15
- result = parth.set(stems).get(path);
16
- result.notFound.should.be.eql('');
17
- result.regex.source.should.match(/[\\]+S\+/);
18
- });
19
- };
package/test/params.js DELETED
@@ -1,59 +0,0 @@
1
- 'use strict';
2
-
3
- var stem, path, result;
4
-
5
- module.exports = function(Parth){
6
- var parth = new Parth();
7
-
8
- it('can be given as part of the input string', function(){
9
- stem = 'post /:number(\\d+)';
10
- path = 'post /1';
11
- result = parth.set(stem).get(path);
12
- result.should.have.properties({
13
- params: {
14
- number: '1'
15
- }
16
- });
17
- });
18
-
19
- it('may not contain labels', function(){
20
- stem = '(get|post) /hello/:there/:you';
21
- path = 'post /hello/awesome/10';
22
- result = parth.set(stem).get(path);
23
- result.should.have.properties({
24
- params: {
25
- '0': 'post',
26
- you: '10',
27
- there: 'awesome'
28
- }
29
- });
30
- });
31
-
32
- it('query fragment is setup by default for an url', function(){
33
- stem = '(get|post) /hello/:there/:you';
34
- path = 'post /hello/awesome/10?query=string#here';
35
- result = parth.set(stem).get(path);
36
- result.should.have.properties({
37
- params: {
38
- '0': 'post',
39
- you: '10',
40
- there: 'awesome',
41
- qs: '?query=string#here'
42
- }
43
- });
44
- });
45
-
46
- it('querystring may contain parameters', function(){
47
- stem = 'post /:page\\/?:query(\\?[^/#\\s]+)?:fragment(#[^?\\s]+)?';
48
- path = 'post /page?query=here#hash';
49
- result = parth.set(stem).get(path);
50
-
51
- result.should.have.properties({
52
- params: {
53
- page: 'page',
54
- query: '?query=here',
55
- fragment: '#hash'
56
- }
57
- });
58
- });
59
- };
package/test/paths.js DELETED
@@ -1,91 +0,0 @@
1
- 'use strict';
2
-
3
- var path, match;
4
-
5
- function check(result) {
6
- result.should.have.properties({
7
- path: path,
8
- match: match
9
- });
10
- }
11
-
12
- module.exports = function(Parth){
13
- var parth = new Parth();
14
-
15
- it('object', function(){
16
- path = 'hello.:there';
17
- match = 'hello.awesome';
18
- check(parth.set(path).get(match));
19
- });
20
-
21
- it('raw object paths', function(){
22
- match = path = 'hello.there';
23
- check(parth.set(path).get(match));
24
- });
25
-
26
- it('unix paths', function(){
27
- path = '/hello/:there/:you';
28
- match = '/hello/awesome/human';
29
- check(parth.set(path).get(match));
30
- });
31
-
32
- it('raw unix paths', function(){
33
- path = '/hello/there/you';
34
- match = '/hello/there/you?here';
35
- check(parth.set(path).get(match));
36
- });
37
-
38
- it('urls', function(){
39
- path = '/hello/:there';
40
- match = '/hello/awesome/?query';
41
- check(parth.set(path).get(match));
42
- });
43
-
44
- it('raw urls', function(){
45
- path = '/hello/there';
46
- match = '/hello/there/?query';
47
- check(parth.set(path).get(match));
48
- });
49
-
50
- it('urls: querystring is stripped', function(){
51
- path = 'get page.thing /hello/there';
52
- match = 'get page.thing /hello/there/?query';
53
- check(parth.set(path).get(match));
54
- });
55
-
56
- it('urls: hash is stripped', function(){
57
- path = 'get page.thing /hello/there';
58
- match = 'get page.thing /hello/there#hello';
59
- check(parth.set(path).get(match));
60
- });
61
-
62
- it('urls: parameters are not mistaken as querystrings', function(){
63
- path = 'get page.thing /hello/:here(?:\\w+you)';
64
- match = 'get page.thing /hello/helloyou';
65
- check(parth.set(path).get(match));
66
- });
67
-
68
- it('space separated paths', function(){
69
- path = 'you are an :there :you';
70
- match = 'you are an awesome human';
71
- check(parth.set(path).get(match));
72
- });
73
-
74
- it('raw, space separated paths', function(){
75
- match = path = 'you are an there you';
76
- check(parth.set(path).get(match));
77
- });
78
-
79
- it('unix, object and url paths together', function(){
80
- path = 'get page.:thing /hello/:there';
81
- match = 'get page.data /hello/awesome/?query';
82
- check(parth.set(path).get(match));
83
- });
84
-
85
- it('raw: unix, object and urls paths together', function(){
86
- path = 'get page.thing /hello/there';
87
- match = 'get page.thing /hello/there/?query';
88
- check(parth.set(path).get(match));
89
- });
90
-
91
- };