diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..ebfe143 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,23 @@ +{ + "env": { + "node": true, + "mocha": true + }, + "rules": { + "strict": 2, + "quotes": [2, "single", "avoid-escape"], + "indent": ["error", 2, {"SwitchCase": 1}], + "eol-last": 2, + "no-shadow": 2, + "dot-notation": 2, + "dot-location": [2, "property"], + "comma-dangle": [2, "never"], + "no-unused-vars": 2, + "no-multi-spaces": 2, + "no-process-exit": 0, + "consistent-return": 2, + "no-use-before-define": 0, + "no-underscore-dangle": 0, + "no-unused-expressions": 2 + } +} diff --git a/.jshintrc b/.jshintrc deleted file mode 100644 index 26fd393..0000000 --- a/.jshintrc +++ /dev/null @@ -1,21 +0,0 @@ -{ - "camelcase": true, - "curly": true, - "eqeqeq": true, - "freeze": true, - "indent": 2, - "newcap": false, - "quotmark": "single", - "maxdepth": 3, - "maxstatements": 50, - "maxlen": 100, - "eqnull": true, - "funcscope": true, - "strict": true, - "globalstrict": true, - "undef": true, - "unused": true, - "node": true, - "mocha" : true, - "laxbreak": true -} diff --git a/.travis.yml b/.travis.yml index 3f00c73..821f418 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: node_js node_js: - - "0.10" - "4" - "stable" notifications: diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 32c5601..0000000 --- a/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright (c) 2014-2015 Javier Carrillo - -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/README.md b/README.md index 468cd0b..56a3d33 100644 --- a/README.md +++ b/README.md @@ -16,21 +16,21 @@ var Parth = require('parth'); var parth = new Parth(); var props = {handle: function(){}}; -parth.set('(get|post) /:page(\\w+)/:view([^./]+)', props) - .get('post /user/page/photo?query#hash') +parth.set('(get|post) /:page/:view', props) + .get('get /weekend/baby?query=string#hash user.10.beers now') // => { + path: 'get /:page/:view', + stem: ':0(get|post) /:page/:view:qs(?:\\/?)?([?#][^\\/\s]*)?', + depth: 2, + regex: /^(get|post) \/([^?#.\/\s]+)\/([^?#.\/\s]+)(?:\/?)?([?#][^\/\s]*)?/, + match: 'get /weekend/baby?query=string#hash', handle: [Function], - path: 'post /user/page/photo', - stem: ':0(get|post) /:page(\\w+)/:view([^./]+)', - regex: /^(get|post) \/(\w+)\/([^./]+)/, - depth: 3, - notFound: '/photo', + notFound: ' user.10.beers now', params: { - '0': 'post', - _: [ '0', 'page', 'view' ], - page: 'user', - view: 'page' + page: 'weekend', + view: 'baby', + qs: '?query=string#hash' } } ``` @@ -43,19 +43,46 @@ The `module.exports` a `Parth` constructor var Parth = require('parth'); ```` -which takes no arguments +which can take the options below + ```js -var parth = new Parth(); +var parth = new Parth(options); +``` + +_options_ type `object`, can be + - `options.defaultRE` default `regex` used if none is given after the params + +example: + +```js +var parth = new Parth({ defaultRE: /[^\s\/?#]+/ }); + +parth.set('/page/:view') // no regex given after ":view" + .get('/page/10/?query=here') +// => +{ + path: '/page/:view/', + stem: '/page/:view:qs(?:\\/?)([?#][^\\/\\s]*)?', + depth: 2, + regex: /^\/page\/([^\s\/?#]+)(?:\/?)([?#][^\/\s]*)?/, + match: '/page/10/?query=here', + params: { + view: '10', + qs: '?query=here' + }, + notFound: '' +} ``` +> NOTE: the query string is separated by default and assigned to `qs`. +> This will only happen if the path given to `parth.set` has no query string + ## parth.set ```js function set(string path[, object options]) ``` -This method purpose is to sanitize the `path` given -and classify the resulting regular expression with those -previously stored. +This method job is to sanitize `path` and order it with those previously stored. _arguments_ - `path`, type `string`, path to be set @@ -93,15 +120,15 @@ _return_ > All matches are partial i.e. /^regex baby/. > Not being strict is useful for `notFound` paths. - +> > NOTE: the returned object is a deep copy of the original `options` > given in `parth.set` to avoid mutation ### parth properties - - `store`: all paths set for match are here - - `regex`: array of carefully ordered regexes - - `regex.master`: regex aggregating all learned + - `parth.store`: all paths set for match are here + - `parth.regex`: array of carefully ordered regexes + - `parth.regex.master`: regex aggregating all learned ## why @@ -156,7 +183,15 @@ parth ### license -![LICENSE](http://img.shields.io/npm/l/parth.svg?style=flat-square) +The MIT License (MIT) + +Copyright (c) 2014-present Javier Carrillo + +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. [x-npm]: https://npmjs.org/package/parth [x-travis]: https://travis-ci.org/stringparser/parth/builds diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..c419263 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-cayman \ No newline at end of file diff --git a/index.js b/index.js index a262ff7..fd35695 100644 --- a/index.js +++ b/index.js @@ -4,38 +4,25 @@ var util = require('./lib/util'); exports = module.exports = Parth; -function Parth(){ +function Parth(options){ if(!(this instanceof Parth)){ - return new Parth(); + return new Parth(options); } - this.regex = []; this.store = {}; + this.regex = []; this.regex.master = new RegExp('(?:[])'); -} - - -/** -## parth.set -```js -function set(string path[, object options]) -``` -This method purpose is to sanitize the `path` given -and classify the resulting regular expression with those -previously stored. - -arguments - - path, type `string` - - options, type `object`, to merge with this path properties + this.regex.defaultRE = /[^?#./\s]+/; -returns `this` + if(options){ + this.regex.defaultRE = options.defaultRE || this.regex.defaultRE; + } +} -> NOTE: `options` is deep cloned beforehand to avoid mutation -**/ -var qsRE = /(\/?[?#][^!=:][^\s]+)/g; +var qshRE = /[?#][^\/\s]*/g; var depthRE = /((^|[/?#.\s]+)[(:\w])/g; var paramRE = /:([-\w]+)(\([^\s]+?[)][?)]*)?/g; -var noParamRE = /(^|[/.=\s]+)(\(.+?\)+)/g; +var noParamRE = /(^|[/.=\s]+)(\(.+?\)+)/g Parth.prototype.set = function(path, opt){ if(typeof path !== 'string'){ @@ -49,47 +36,43 @@ Parth.prototype.set = function(path, opt){ util.merge(this.store[o.path], o); return this; } - this.store[o.path] = o; + this.regex.push(this.store[o.path] = o); var index = -1; + var defaultRE = '(' + this.regex.defaultRE.source + ')'; + o.stem = o.path.replace(noParamRE, function($0, $1, $2){ return $1 + ':' + (++index) + $2; }); - var url = (o.stem.match(/[^\/\s]*\/\S*/) || [null]).pop(); + var url = (o.stem.match(/[^\s]*\/\S*/) || [null]).pop(); + var qsh = util.getQueryString(url); - if(url){ - var qsh = (url.match(qsRE) || [':queryFragment(\\/?[?#][^/\\s]+)?']).pop(); - o.stem = o.stem.replace(url, url.replace(qsRE, '') + qsh); + if(url && !qsh){ + o.stem = o.stem.replace(url, url.replace(/\/$/, '$1') + + ':qs(?:\\/)?(' + qshRE.source + ')?' + ); } - o.depth = -1; - o.stem.replace(depthRE, function(){ ++o.depth; }); - + o.depth = o.stem.split(depthRE).length || -1; o.regex = new RegExp('^' + o.stem.replace(/\S+/g, function(s){ return s.replace(paramRE, function($0, $1, $2){ - return ($2 || '([^?#./\\s]+)'); + return ($2 || defaultRE); }); }).replace(/[^?( )+*$]+(?=\(|$)/g, function escapeRegExp($0){ return $0.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&'); }) ); - /** order regexes according to - * - depth (number of separation tokens - * - if that fails, use localCompare - **/ + // order regexes according to depth (# of separation tokes) or, + // if that fails, use localCompare - this.regex.push(o); this.regex.sort(function(x, y){ return (y.depth - x.depth) || y.stem.localeCompare(x.stem); }); - /** sum up all learned - * - void all groups - * - make a giant regex - **/ + // sum up all learned (void all groups and make a giant regex) this.regex.master = new RegExp( '(' + this.regex.map(function voidRegExp(el){ return el.regex.source.replace(/\((?=[^?])/g, '(?:'); @@ -99,26 +82,6 @@ Parth.prototype.set = function(path, opt){ return this; }; - -/** -## parth.get -```js -function get(string path) -``` - -Take a string, and return a clone of the store object properties - -arguments - - path, type `string` to match stored paths with - -return - - null for non-supported types or not matching path - - object with all the stored information on `parth.set` - -> NOTE: the returned object is a deep copy of the original `options` -> given in `parth.set` to avoid mutation -**/ - Parth.prototype.get = function(path){ if(typeof path !== 'string'){ return null; @@ -135,16 +98,15 @@ Parth.prototype.get = function(path){ var found = this.regex.master.exec(path); if(!found){ return null; } - var o = {}; - o.match = found.shift(); - o.notFound = path.slice(o.match.length); + var o = {match: found.shift(), params: {}}; found = this.regex[found.indexOf(o.match)]; - var params = found.regex.exec(path).slice(1); - if(params.length){ o.params = {}; } + o.notFound = path.slice(o.match.length); var index = -1; + var params = found.regex.exec(path).slice(1); + found.stem.replace(paramRE, function($0, $1){ o.params[$1] = params[++index]; }); diff --git a/lib/util.js b/lib/util.js index 1aeb85a..9438bb9 100644 --- a/lib/util.js +++ b/lib/util.js @@ -6,3 +6,17 @@ exports = module.exports = {}; // exports.clone = require('lodash.clone'); exports.merge = require('lodash.merge'); + +// assorted +// +exports.getQueryString = function(url){ + if(!url){ return null; } + + var index = url.indexOf('?'); + + if(index > -1 && url.charAt(index + 1) !== ':'){ + return url.substring(index); + } else { + return null; + } +}; diff --git a/package.json b/package.json index af13dee..4d1bd0b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "parth", "main": "index.js", - "version": "3.3.0", + "version": "4.2.3", "engines": { "node": ">= 0.10.0" }, @@ -17,13 +17,13 @@ "url": "https://github.com/stringparser/parth/issues" }, "scripts": { - "lint": "jshint lib *.js", + "lint": "eslint lib *.js", "test": "npm run lint && mocha test" }, "devDependencies": { "mocha": "*", "should": "*", - "jshint": "*" + "eslint": "*" }, "keywords": [ "url", diff --git a/readme.md b/readme.md deleted file mode 100644 index 468cd0b..0000000 --- a/readme.md +++ /dev/null @@ -1,165 +0,0 @@ -# parth [![NPM version][badge-version]][x-npm] [![downloads][badge-downloads]][x-npm] - -[documentation](#documentation) - -[examples](#examples) - -[install](#install) - -[todo](#todo) - -[why](#why) - -[![build][badge-build]][x-travis] - -## sample - -```js -var Parth = require('parth'); - -var parth = new Parth(); -var props = {handle: function(){}}; - -parth.set('(get|post) /:page(\\w+)/:view([^./]+)', props) - .get('post /user/page/photo?query#hash') -// => -{ - handle: [Function], - path: 'post /user/page/photo', - stem: ':0(get|post) /:page(\\w+)/:view([^./]+)', - regex: /^(get|post) \/(\w+)\/([^./]+)/, - depth: 3, - notFound: '/photo', - params: { - '0': 'post', - _: [ '0', 'page', 'view' ], - page: 'user', - view: 'page' - } -} -``` - -## documentation - -The `module.exports` a `Parth` constructor - -````js -var Parth = require('parth'); -```` - -which takes no arguments -```js -var parth = new Parth(); -``` - -## parth.set - -```js -function set(string path[, object options]) -``` -This method purpose is to sanitize the `path` given -and classify the resulting regular expression with those -previously stored. - -_arguments_ - - `path`, type `string`, path to be set - - `options`, type `object`, to merge with this path properties - -_returns_ `this` - -> NOTE: `options` is deep cloned beforehand to avoid mutation - -`path` can contain any number of parameters(regexes) in the form -```js - :param-label(\\regexp(?:here)) -``` -Any string matching the regular expression below qualifies as a parameter - -````js -/:([-\w]+)(\([^\s]+?[)][?)]*)?/g; -```` - -[Go to http://regexr.com/](http://regexr.com/3cuqq) and test it out. - -## parth.get -```js -function get(string path) -``` - -Take a string and return a clone of the store object properties - -_arguments_ - - `path`, type `string` to match stored paths with - -_return_ - - null for non-supported types or not matching paths - - object with all the information stored in `parth.set` - -> All matches are partial i.e. /^regex baby/. -> Not being strict is useful for `notFound` paths. - -> NOTE: the returned object is a deep copy of the original `options` -> given in `parth.set` to avoid mutation - -### parth properties - - - `store`: all paths set for match are here - - `regex`: array of carefully ordered regexes - - `regex.master`: regex aggregating all learned - -## why - -I need it for the [gulp-runtime](https://github.com/stringparser/gulp-runtime) module. - -## install - -With [npm](http://npmjs.org) - - npm install --save parth - -### examples - -Run the [`example.js`](example.js) file. - -### test - - npm test - -``` -➜ parth (master) ✓ npm t -parth - paths - ✓ object - ✓ raw object paths - ✓ unix paths - ✓ raw unix paths - ✓ urls - ✓ raw urls - ✓ urls: querystring is stripped - ✓ urls: hash is stripped - ✓ urls: parameters are not mistaken as querystrings - ✓ space separated paths - ✓ raw, space separated paths - ✓ unix, object and url paths together - ✓ raw: unix, object and urls paths together - params - ✓ can be given as a string regex - ✓ will contain all parameter keys at _ - ✓ parameter values should be at params - notFound - ✓ should be false for perfect match - ✓ should have what is left of the path - - -18 passing (16ms) -``` - -### todo - - - [ ] set support for regexp input - -### license - -![LICENSE](http://img.shields.io/npm/l/parth.svg?style=flat-square) - -[x-npm]: https://npmjs.org/package/parth -[x-travis]: https://travis-ci.org/stringparser/parth/builds -[badge-build]: http://img.shields.io/travis/stringparser/parth/master.svg?style=flat-square -[badge-version]: http://img.shields.io/npm/v/parth.svg?style=flat-square -[badge-downloads]: http://img.shields.io/npm/dm/parth.svg?style=flat-square diff --git a/test/index.js b/test/index.js index 4bef9d9..2a4c3b1 100644 --- a/test/index.js +++ b/test/index.js @@ -8,6 +8,7 @@ require('should'); [ 'paths.js', 'params.js', + 'options.js', 'notFound.js' ].forEach(function(file){ if(file === 'index.js'){ return; } diff --git a/test/notFound.js b/test/notFound.js index 959885b..067dfe5 100644 --- a/test/notFound.js +++ b/test/notFound.js @@ -19,6 +19,6 @@ module.exports = function(Parth){ path = 'get /hello/awesome/human'; result = parth.set(stems).get(path); result.should.not.be.eql(null); - result.notFound.should.be.eql('/human'); + result.notFound.should.be.eql('human'); }); }; diff --git a/test/options.js b/test/options.js new file mode 100644 index 0000000..f8fab6e --- /dev/null +++ b/test/options.js @@ -0,0 +1,19 @@ +'use strict'; + +var path, stems, result; + +module.exports = function(Parth){ + + it('default regex can be changed using options', function(){ + var parth = new Parth({ + defaultRE: /\S+/ + }); + + stems = 'do :src :dest'; + path = 'do /src/**/*.js /dest/'; + + result = parth.set(stems).get(path); + result.notFound.should.be.eql(''); + result.regex.source.should.match(/[\\]+S\+/); + }); +}; diff --git a/test/params.js b/test/params.js index 1ab0321..54e4871 100644 --- a/test/params.js +++ b/test/params.js @@ -38,7 +38,7 @@ module.exports = function(Parth){ '0': 'post', you: '10', there: 'awesome', - queryFragment: '?query=string#here' + qs: '?query=string#here' } }); }); @@ -47,6 +47,7 @@ module.exports = function(Parth){ stem = 'post /:page\\/?:query(\\?[^/#\\s]+)?:fragment(#[^?\\s]+)?'; path = 'post /page?query=here#hash'; result = parth.set(stem).get(path); + result.should.have.properties({ params: { page: 'page',