Comparing version 0.4.4 to 0.5.0
@@ -18,3 +18,3 @@ { | ||
"main": "patrun.js", | ||
"version": "0.4.4", | ||
"version": "0.5.0", | ||
"scripts": { | ||
@@ -33,3 +33,3 @@ "test": "./test.sh", | ||
"lodash": "3.10.0", | ||
"gex": "0.2.1" | ||
"gex": "0.2.2" | ||
}, | ||
@@ -36,0 +36,0 @@ "devDependencies": { |
/* Copyright (c) 2013-2015 Richard Rodger, MIT License, https://github.com/rjrodger/patrun */ | ||
(function(){"use strict";var n=this,e=n.patrun,r="undefined"!=typeof require,t=n._,u=n.gex;if("undefined"==typeof t){if(!r)throw new Error("patrun requires underscore, see http://underscorejs.org");t=require("lodash")}if("undefined"==typeof u){if(!r)throw new Error("patrun requires gex, see https://github.com/rjrodger/gex");u=require("gex")}var i=n.patrun=function(r){var i={},o={};return i.noConflict=function(){return n.patrun=e,i},i.add=function(n,e){n=t.clone(n);var u;r&&(u=r.call(i,n,e));for(var s,d=t.keys(n).sort(),l=o,f=0;f<d.length;f++){var v=d[f],a=n[v];if(null!==a&&void 0!==a)if(s=l.v,s&&v==l.k)l=s[a]||(s[a]={});else if(l.k)if(v<l.k){var c=l.v,p=l.s;l.v={},l.s={k:l.k,v:c,s:p},l.k=v,l=l.v[a]={}}else s=l.v,l=l.s||(l.s={}),f--;else l.k=v,l.v={},l=l.v[a]={}}return void 0!==e&&l&&(l.d=e,u&&(l.f=t.isFunction(u)?u:u.find,l.r=t.isFunction(u.remove)?u.remove:void 0)),i},i.findexact=function(n){return i.find(n,!0)},i.find=function(n,e){if(null==n)return null;var r=o,u=void 0===o.d?null:o.d,s=o.f,d=null,l=[],f={},v=t.keys(n).length;do{if(d=r.k,r.v){var a=r.v[n[d]];a?(f[d]=!0,r.s&&l.push(r.s),u=void 0===a.d?null:a.d,s=a.f,r=a):r=r.s}else r=null;null==r&&null==u&&0<l.length&&(r=l.pop())}while(r);return e&&t.keys(f).length!=v&&(u=null),s&&(u=s.call(i,n,u)),u},i.remove=function(n){var e,r=o,t=null,u=[];do if(e=r.k,r.v){var i=r.v[n[e]];i?(u.push({km:r,v:n[e]}),t=i.d,r=i):r=r.s}else r=null;while(r);if(void 0!==t){var s=u[u.length-1];if(s&&s.km&&s.km.v){var d=s.km.v[s.v];(!d.r||d.r(n,d.d))&&delete d.d}}},i.list=function(n,e){function r(i,o,s,d){if(i.v){var l,f=i.k,v=u(n?null==n[f]?e?null:"*":n[f]:"*"),a=t.extend({},o),c=t.extend({},s);for(var p in i.v)if(v.on(p)){var h=t.clone(a);h[f]=p;var g=t.extend({},c);delete g[f],l=i.v[p],0===t.keys(g).length&&l&&l.d&&d.push({match:h,data:l.d,find:l.f}),l&&l.v&&r(l,t.extend({},h),t.extend({},g),d)}l=i.s,l&&r(l,t.extend({},a),t.extend({},c),d)}}var i=[];return o.d&&i.push({match:{},data:o.d,find:o.f}),r(o,{},t.extend({},n),i),i},i.toString=function(){function n(n,e){for(var r=0;e>r;r++)n.push(" ")}function e(r,o,s,d){var l;if(void 0!==r.d&&(n(o,s),o.push(u(r.d)),i.push(d.join(", ")+" -> "+u(r.d))),r.k&&(o.push("\n"),n(o,s),o.push(r.k+":")),r.v){s++;for(var f in r.v)o.push("\n"),n(o,s),o.push(f+" ->"),l=t.clone(d),l.push(r.k+"="+f),e(r.v[f],o,s+1,l);r.s&&(o.push("\n"),n(o,s),o.push("* ->"),l=t.clone(d),e(r.s,o,s+1,l))}}var r=t.isBoolean(arguments[0])?arguments[0]:!!arguments[1],u=t.isFunction(arguments[0])?arguments[0]:function(n){return t.isFunction(n)?"<"+n.name+">":"<"+n+">"},i=[],s=[];return e(o,s,0,[]),r?s.join(""):i.join("\n")},i.inspect=i.toString,i.toJSON=function(n){return JSON.stringify(o,function(n,e){return t.isFunction(e)?"[Function]":e},n)},i};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=i),exports.patrun=i):n.patrun=i}).call(this); | ||
(function(){"use strict";var n=this,e=n.patrun,r="undefined"!=typeof require,t=n._||r&&require("lodash");if(!t)throw new Error("patrun requires underscore, see http://underscorejs.org");var u=n.gex||r&&require("gex");if(!u)throw new Error("patrun requires gex, see https://github.com/rjrodger/gex");var i=n.patrun=function(r){function i(n,e,r){if(r){var t=n.g=n.g||{},u=t[e]=t[e]||[];u.push(r),u.sort(function(n,e){return n.val$<e.val$})}}r=r||{};var o={},s={};return o.top=s,o.noConflict=function(){return n.patrun=e,o},o.add=function(n,e){n=t.clone(n);var l=t.isFunction(r)?r.call(o,n,e):null,a=t.keys(n),v=[],f=[];a.forEach(function(e){var t=n[e];null!=t&&(t=String(t),n[e]=t,(r.gex&&t.match(/[\*\?]/)?f:v).push(e))}),v=v.sort(),f=f.sort(),a=v.concat(f);for(var d,c=s,h=(require("util").inspect(n),0);h<a.length;h++){var p=a[h],g=n[p],k=r.gex&&g.match(/[\*\?]/)?u(g):null;k&&(k.val$=g);var m=(k?"1":"0")+"~",x=m+p;if(d=c.v,d&&x==c.sk)i(c,p,k),c=d[g]||(d[g]={});else if(c.k)if(x<c.sk){var y=c.s,F=c.g;c.s={k:c.k,sk:c.sk,v:c.v},y&&(c.s.s=y),F&&(c.s.g=F),c.g&&(c.g={}),i(c,p,k),c.k=p,c.sk=x,c.v={},c=c.v[g]={}}else d=c.v,c=c.s||(c.s={}),h--;else i(c,p,k),c.k=p,c.sk=x,c.v={},c=c.v[g]={}}return void 0!==e&&c&&(c.d=e,l&&(c.f=t.isFunction(l)?l:l.find,c.r=t.isFunction(l.remove)?l.remove:void 0)),o},o.findexact=function(n){return o.find(n,!0)},o.find=function(n,e){if(null==n)return null;var u=s,i=void 0===s.d?null:s.d,l=s.f,a=null,v=[],f={},d=t.keys(n).length;do{if(a=u.k,u.v){var c=n[a],h=u.v[c];if(!h&&r.gex&&u.g&&u.g[a])for(var p=u.g[a],g=0;g<p.length;g++)if(null!=p[g].on(c)){h=u.v[p[g].val$];break}h?(f[a]=!0,u.s&&v.push(u.s),i=void 0===h.d?null:h.d,l=h.f,u=h):u=u.s}else u=null;null==u&&null==i&&0<v.length&&(u=v.pop())}while(u);return e&&t.keys(f).length!=d&&(i=null),l&&(i=l.call(o,n,i)),i},o.remove=function(n){var e,r=s,t=null,u=[];do if(e=r.k,r.v){var i=r.v[n[e]];i?(u.push({km:r,v:n[e]}),t=i.d,r=i):r=r.s}else r=null;while(r);if(void 0!==t){var o=u[u.length-1];if(o&&o.km&&o.km.v){var l=o.km.v[o.v];(!l.r||l.r(n,l.d))&&delete l.d}}},o.list=function(n,e){function r(i,o,s,l){if(i.v){var a,v=i.k,f=u(n?null==n[v]?e?null:"*":n[v]:"*"),d=t.extend({},o),c=t.extend({},s);for(var h in i.v)if(f.on(h)){var p=t.clone(d);p[v]=h;var g=t.extend({},c);delete g[v],a=i.v[h],0===t.keys(g).length&&a&&a.d&&l.push({match:p,data:a.d,find:a.f}),a&&a.v&&r(a,t.extend({},p),t.extend({},g),l)}a=i.s,a&&r(a,t.extend({},d),t.extend({},c),l)}}var i=[];return s.d&&i.push({match:{},data:s.d,find:s.f}),r(s,{},t.extend({},n),i),i},o.toString=function(){function n(n,e){for(var r=0;e>r;r++)n.push(" ")}function e(r,o,s,l){var a;if(void 0!==r.d&&(o.push(" "+u(r.d)),i.push(l.join(", ")+" -> "+u(r.d))),r.k&&(o.push("\n"),n(o,s),o.push(r.k+":")),r.v){s++;var v=t.keys(r.v),f=t.filter(v,function(n){return!n.match(/[\*\?]/)}),d=t.filter(v,function(n){return n.match(/[\*\?]/)});f.sort(),d.sort(),v=f.concat(d);for(var c=0;c<v.length;c++){var h=v[c];o.push("\n"),n(o,s),o.push(h+" ->"),a=t.clone(l),a.push(r.k+"="+h),e(r.v[h],o,s+1,a)}r.s&&(o.push("\n"),n(o,s),o.push("|"),a=t.clone(l),e(r.s,o,s+1,a))}}var r=t.isBoolean(arguments[0])?arguments[0]:!!arguments[1],u=t.isFunction(arguments[0])?arguments[0]:function(n){return t.isFunction(n)?"<"+n.name+">":"<"+n+">"},i=[],o=[];return e(s,o,0,[]),r?o.join(""):i.join("\n")},o.inspect=o.toString,o.toJSON=function(n){return JSON.stringify(s,function(n,e){return t.isFunction(e)?"[Function]":e},n)},o};"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=i),exports.patrun=i):n.patrun=i}).call(this); | ||
//# sourceMappingURL=patrun-min.map |
149
patrun.js
@@ -7,31 +7,23 @@ /* Copyright (c) 2013-2015 Richard Rodger, MIT License, https://github.com/rjrodger/patrun */ | ||
var root = this | ||
var root = this | ||
var previous_patrun = root.patrun | ||
var has_require = typeof require !== 'undefined' | ||
var has_require = typeof require !== 'undefined' | ||
var _ = root._ | ||
var gex = root.gex | ||
var _ = root._ || has_require && require('lodash') | ||
if( !_ ) | ||
throw new Error('patrun requires underscore, see http://underscorejs.org') | ||
if( typeof _ === 'undefined' ) { | ||
if( has_require ) { | ||
_ = require('lodash') | ||
} | ||
else throw new Error('patrun requires underscore, see http://underscorejs.org'); | ||
} | ||
var gex = root.gex || has_require && require('gex') | ||
if( !gex ) | ||
throw new Error('patrun requires gex, see https://github.com/rjrodger/gex') | ||
if( typeof gex === 'undefined' ) { | ||
if( has_require ) { | ||
gex = require('gex') | ||
} | ||
else throw new Error('patrun requires gex, see https://github.com/rjrodger/gex') | ||
} | ||
var patrun = root.patrun = function( custom ) { | ||
custom = custom || {} | ||
var patrun = root.patrun = function( custom ) { | ||
var self = {} | ||
var top = {} | ||
var top = {} | ||
self.noConflict = function() { | ||
@@ -46,26 +38,59 @@ root.patrun = previous_patrun; | ||
var customizer | ||
if( custom ) { | ||
// can modify pat | ||
customizer = custom.call(self,pat,data) | ||
} | ||
var customizer = _.isFunction(custom) ? custom.call(self,pat,data) : null | ||
var keys = _.keys(pat).sort() | ||
var keymap = top | ||
var valmap | ||
var keys = _.keys(pat), plains = [], gexers = [] | ||
keys.forEach(function(key){ | ||
var val = pat[key] | ||
if( null == val ) return; | ||
val = String(val) | ||
pat[key] = val; | ||
(( custom.gex && val.match(/[\*\?]/) ) ? gexers : plains ).push(key) | ||
}) | ||
plains = plains.sort() | ||
gexers = gexers.sort() | ||
keys = plains.concat(gexers) | ||
var keymap = top, valmap | ||
for( var i = 0; i < keys.length; i++ ) { | ||
var key = keys[i] | ||
var val = pat[key] | ||
if( null === val || void 0 === val ) continue; | ||
var gexer = ( custom.gex && val.match(/[\*\?]/) ) ? gex(val) : null | ||
if( gexer ) gexer.val$ = val | ||
var sort_prefix = (gexer?'1':'0')+'~' | ||
var sort_key = sort_prefix+key | ||
valmap = keymap.v | ||
if( valmap && key == keymap.k) { | ||
if( valmap && sort_key == keymap.sk ) { | ||
add_gexer( keymap, key, gexer ) | ||
keymap = valmap[val] || (valmap[val]={}) | ||
} | ||
else if( !keymap.k ) { | ||
add_gexer( keymap, key, gexer ) | ||
keymap.k = key | ||
keymap.sk = sort_key | ||
keymap.v = {} | ||
keymap = keymap.v[val] = {} | ||
} | ||
else if( sort_key < keymap.sk ) { | ||
var s = keymap.s, g = keymap.g | ||
keymap.s = {k:keymap.k,sk:keymap.sk,v:keymap.v} | ||
if( s ) keymap.s.s = s | ||
if( g ) keymap.s.g = g | ||
if( keymap.g ) keymap.g = {} | ||
add_gexer( keymap, key, gexer ) | ||
keymap.k = key | ||
keymap.sk = sort_key | ||
keymap.v = {} | ||
@@ -76,16 +101,5 @@ | ||
else { | ||
if( key < keymap.k ) { | ||
var curv = keymap.v | ||
var curs = keymap.s | ||
keymap.v = {} | ||
keymap.s = {k:keymap.k,v:curv,s:curs} | ||
keymap.k = key | ||
keymap = keymap.v[val] = {} | ||
} | ||
else { | ||
valmap = keymap.v | ||
keymap = keymap.s || (keymap.s = {}) | ||
i-- | ||
} | ||
valmap = keymap.v | ||
keymap = keymap.s || (keymap.s = {}) | ||
i-- | ||
} | ||
@@ -101,3 +115,3 @@ } | ||
} | ||
return self | ||
@@ -107,2 +121,14 @@ } | ||
function add_gexer( keymap, key, gexer ) { | ||
if( !gexer ) return; | ||
var g = keymap.g = keymap.g || {} | ||
var ga = g[key] = g[key] || [] | ||
ga.push( gexer ) | ||
ga.sort( function(a,b) { | ||
return a.val$ < b.val$ | ||
}) | ||
} | ||
self.findexact = function( pat ) { | ||
@@ -128,3 +154,15 @@ return self.find( pat, true ) | ||
if( keymap.v ) { | ||
var nextkeymap = keymap.v[pat[key]] | ||
var val = pat[key] | ||
var nextkeymap = keymap.v[val] | ||
if( !nextkeymap && custom.gex && keymap.g && keymap.g[key] ) { | ||
var ga = keymap.g[key] | ||
for( var gi = 0; gi < ga.length; gi++ ) { | ||
if( null != ga[gi].on(val) ) { | ||
nextkeymap = keymap.v[ga[gi].val$] | ||
break; | ||
} | ||
} | ||
} | ||
if( nextkeymap ) { | ||
@@ -239,3 +277,3 @@ foundkeys[key]=true | ||
data:nextkeymap.d, | ||
find:nextkeymap.f | ||
find:nextkeymap.f, | ||
}) | ||
@@ -271,3 +309,3 @@ } | ||
data:top.d, | ||
find:top.f | ||
find:top.f, | ||
}) | ||
@@ -302,4 +340,3 @@ } | ||
if( void 0 !== n.d ) { | ||
indent(o,d) | ||
o.push(dstr(n.d)) | ||
o.push(' '+dstr(n.d)) | ||
@@ -315,3 +352,11 @@ str.push( vs.join(', ')+' -> '+dstr(n.d)) | ||
d++ | ||
for( var p in n.v ) { | ||
var pa = _.keys(n.v) | ||
var pal = _.filter(pa,function(x){ return !x.match(/[\*\?]/) }) | ||
var pas = _.filter(pa,function(x){ return x.match(/[\*\?]/) }) | ||
pal.sort() | ||
pas.sort() | ||
pa = pal.concat(pas) | ||
for( var pi = 0; pi < pa.length; pi++ ) { | ||
var p = pa[pi] | ||
o.push('\n') | ||
@@ -330,3 +375,3 @@ indent(o,d) | ||
indent(o,d) | ||
o.push( '* ->') | ||
o.push( '|') | ||
@@ -333,0 +378,0 @@ vsc = _.clone(vs) |
@@ -38,3 +38,3 @@ # patrun | ||
Current Version: 0.4.4 | ||
Current Version: 0.5.0 | ||
@@ -224,2 +224,31 @@ Tested on: node 0.10, 0.11, 0.12, iojs, Chrome 43, Safari 7, Firefox 38 | ||
# Glob matching | ||
You can also have glob matching using | ||
[gex](https://github.com/rjrodger/gex). Create a new patrun instance | ||
with: | ||
`js | ||
var glob_patterns = patrun({gex:true}) | ||
` | ||
This extends the rules with glob matching: | ||
* `a:0` -> `'A'` | ||
* `a:*` -> `'AA'` | ||
* `b:1,c:x*y` -> `'BC'` | ||
So that you can do this: | ||
* `a:0` -> `'A'` as exact match | ||
* `a:1` -> `'AA'` as glob match `a:*` | ||
* `b:1,c:xhy` -> `'BC'` as exact `b:1` and glob `c:x*y` | ||
As always, more specific matches win, and matches are disambiguated | ||
using alphanumeric sorting, so it doesn't matter what order you add | ||
them. Exact matches are considered more specific than globs. See the | ||
tests for an [example | ||
(multi-gex)](https://github.com/rjrodger/patrun/blob/master/test/patrun.spec.js#L741). | ||
# Customization | ||
@@ -226,0 +255,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32326
325
400