Comparing version 0.1.5 to 0.2.0
@@ -18,3 +18,3 @@ { | ||
"main": "patrun.js", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"scripts": { | ||
@@ -33,9 +33,10 @@ "test": "./node_modules/.bin/jasmine-node ./test", | ||
"devDependencies": { | ||
"jasmine-node": "~1.11.0", | ||
"jshint": "~2.3.0" | ||
"jasmine-node": "~1.14.3", | ||
"jshint": "~2.5.1", | ||
"benchmark": "~1.0.0" | ||
}, | ||
"dependencies": { | ||
"underscore": "~1.5.2", | ||
"gex": "~0.1.3" | ||
"underscore": "~1.6.0", | ||
"gex": "~0.1.4" | ||
} | ||
} |
/* Copyright (c) 2013 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("underscore")}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(){var r={},i={};return r.noConflict=function(){return n.patrun=e,r},r.add=function(n,e){for(var u,o=t.keys(n).sort(),s=i,d=0;d<o.length;d++){var f=o[d],v=n[f];if(null!==v&&void 0!==v)if(u=s.v,u&&f==s.k)s=u[v]||(u[v]={});else if(s.k)if(f<s.k){var l=s.v,a=s.s;s.v={},s.s={k:s.k,v:l,s:a},s.k=f,s=s.v[v]={}}else u=s.v,s=s.s||(s.s={}),d--;else s.k=f,s.v={},s=s.v[v]={}}return void 0!==e&&s&&(s.d=e),r},r.findexact=function(n){return r.find(n,!0)},r.find=function(n,e){var r,u=i,o=null,s=[],d={};do{if(r=u.k,u.v){var f=u.v[n[r]];f?(d[r]=!0,u.s&&s.push(u.s),o=f.d||null,u=f):u=u.s}else u=null;null==u&&null===o&&0<s.length&&(u=s.pop())}while(u);return null===o&&0===t.keys(n).length&&void 0!==i.d&&(o=i.d),e&&t.keys(d).length!=t.keys(n).length&&(o=null),o},r.remove=function(n){var e,r=i,t=null,u=[];do if(e=r.k,r.v){var o=r.v[n[e]];o?(u.push({km:r,v:n[e]}),t=o.d,r=o):r=r.s}else r=null;while(r);if(void 0!==t){var s=u[u.length-1];s&&s.km&&s.km.v&&delete s.km.v[s.v].d}},r.findall=function(n){function e(r,i,o,s){if(r.v){var d,f,v,l=r.k,a=u(n?n[l]:"*");for(var p in r.v)if(f=t.extend({},i),v=t.extend({},o),a.on(p)){var c=t.clone(f);c[l]=p,delete v[l],d=r.v[p],0===t.keys(v).length&&d&&d.d?s.push({match:c,data:d.d}):d&&d.v&&e(d,t.extend({},c),t.extend({},v),s)}d=r.s,d&&e(d,t.extend({},f),t.extend({},v),s)}}var r=[];return e(i,{},t.extend({},n),r),r},r.toString=function(n,e){function r(n,e){for(var r=0;e>r;r++)n.push(" ")}function u(e,i,s,d){var f;if(void 0!==e.d&&(r(i,s),i.push(n(e.d)),o.push(d.join(", ")+" -> "+n(e.d))),e.k&&(i.push("\n"),r(i,s),i.push(e.k+":")),e.v){s++;for(var v in e.v)i.push("\n"),r(i,s),i.push(v+" ->"),f=t.clone(d),f.push(e.k+"="+v),u(e.v[v],i,s+1,f);e.s&&(i.push("\n"),r(i,s),i.push("* ->"),f=t.clone(d),u(e.s,i,s+1,f))}}n=t.isFunction(n)?n:function(n){return t.isFunction(n)?"<"+n.name+">":"<"+n+">"},e=t.isBoolean(arguments[0])?arguments[0]:e,e=void 0===e?!1:e;var o=[],s=[];return u(i,s,0,[]),e?s.join(""):o.join("\n")},r.inspect=r.toString,r.toJSON=function(n){return JSON.stringify(i,function(n,e){return t.isFunction(e)?"[Function]":e},n)},r};"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._,u=n.gex;if("undefined"==typeof t){if(!r)throw new Error("patrun requires underscore, see http://underscorejs.org");t=require("underscore")}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,l=t.keys(n).sort(),d=o,f=0;f<l.length;f++){var v=l[f],a=n[v];if(null!==a&&void 0!==a)if(s=d.v,s&&v==d.k)d=s[a]||(s[a]={});else if(d.k)if(v<d.k){var c=d.v,p=d.s;d.v={},d.s={k:d.k,v:c,s:p},d.k=v,d=d.v[a]={}}else s=d.v,d=d.s||(d.s={}),f--;else d.k=v,d.v={},d=d.v[a]={}}return void 0!==e&&d&&(d.d=e,u&&(d.f=t.isFunction(u)?u:u.find,d.r=t.isFunction(u.remove)?u.remove:void 0)),i},i.findexact=function(n){return i.find(n,!0)},i.find=function(n,e){var r=o,u=null,s=null,l=null,d=[],f={},v=t.keys(n).length;do{if(l=r.k,r.v){var a=r.v[n[l]];a?(f[l]=!0,r.s&&d.push(r.s),u=null==a.d?null:a.d,s=a.f,r=a):r=r.s}else r=null;null==r&&null===u&&0<d.length&&(r=d.pop())}while(r);return null===u&&0===v&&void 0!==o.d&&(u=o.d,s=o.f),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 l=s.km.v[s.v];(!l.r||l.r(n,l.d))&&delete l.d}}},i.list=function(n){function e(r,i,o,s){if(r.v){var l,d,f,v=r.k,a=u(n?n[v]:"*");for(var c in r.v)if(d=t.extend({},i),f=t.extend({},o),a.on(c)){var p=t.clone(d);p[v]=c,delete f[v],l=r.v[c],0===t.keys(f).length&&l&&l.d?s.push({match:p,data:l.d,find:l.f}):l&&l.v&&e(l,t.extend({},p),t.extend({},f),s)}l=r.s,l&&e(l,t.extend({},d),t.extend({},f),s)}}var r=[];return e(o,{},t.extend({},n),r),r},i.toString=function(n,e){function r(n,e){for(var r=0;e>r;r++)n.push(" ")}function u(e,o,s,l){var d;if(void 0!==e.d&&(r(o,s),o.push(n(e.d)),i.push(l.join(", ")+" -> "+n(e.d))),e.k&&(o.push("\n"),r(o,s),o.push(e.k+":")),e.v){s++;for(var f in e.v)o.push("\n"),r(o,s),o.push(f+" ->"),d=t.clone(l),d.push(e.k+"="+f),u(e.v[f],o,s+1,d);e.s&&(o.push("\n"),r(o,s),o.push("* ->"),d=t.clone(l),u(e.s,o,s+1,d))}}n=t.isFunction(n)?n:function(n){return t.isFunction(n)?"<"+n.name+">":"<"+n+">"},e=t.isBoolean(arguments[0])?arguments[0]:e,e=void 0===e?!1:e;var i=[],s=[];return u(o,s,0,[]),e?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); | ||
//# sourceMappingURL=patrun-min.map |
@@ -30,3 +30,3 @@ /* Copyright (c) 2013 Richard Rodger, MIT License, https://github.com/rjrodger/patrun */ | ||
var patrun = root.patrun = function() { | ||
var patrun = root.patrun = function( custom ) { | ||
var self = {} | ||
@@ -44,2 +44,10 @@ | ||
self.add = function( pat, data ) { | ||
pat = _.clone(pat) | ||
var customizer | ||
if( custom ) { | ||
// can modify pat | ||
customizer = custom.call(self,pat,data) | ||
} | ||
var keys = _.keys(pat).sort() | ||
@@ -62,3 +70,5 @@ | ||
keymap.k = key | ||
keymap.v = {} | ||
keymap = keymap.v[val] = {} | ||
@@ -71,3 +81,2 @@ } | ||
keymap.v = {} | ||
//keymap.v[''] = {k:keymap.k,v:curvalmap} | ||
keymap.s = {k:keymap.k,v:curv,s:curs} | ||
@@ -80,3 +89,2 @@ | ||
valmap = keymap.v | ||
//keymap = valmap[''] || (valmap['']={}) | ||
keymap = keymap.s || (keymap.s = {}) | ||
@@ -90,2 +98,6 @@ i-- | ||
keymap.d = data | ||
if( customizer ) { | ||
keymap.f = _.isFunction(customizer) ? customizer : customizer.find | ||
keymap.r = _.isFunction(customizer.remove) ? customizer.remove : void 0 | ||
} | ||
} | ||
@@ -101,8 +113,11 @@ | ||
self.find = function( pat, exact ) { | ||
var keymap = top | ||
var data = null | ||
var key | ||
var stars = [] | ||
var keymap = top | ||
var data = null | ||
var finalfind = null | ||
var key = null | ||
var stars = [] | ||
var foundkeys = {} | ||
var patlen = _.keys(pat).length | ||
@@ -121,4 +136,5 @@ do { | ||
data = nextkeymap.d || null | ||
keymap = nextkeymap | ||
data = null == nextkeymap.d ? null : nextkeymap.d | ||
finalfind = nextkeymap.f | ||
keymap = nextkeymap | ||
} | ||
@@ -140,10 +156,15 @@ else { | ||
// special case for default with no properties | ||
if( null === data && 0 === _.keys(pat).length && void 0 !== top.d ) { | ||
data = top.d | ||
if( null === data && 0 === patlen && void 0 !== top.d ) { | ||
data = top.d | ||
finalfind = top.f | ||
} | ||
if( exact && _.keys(foundkeys).length != _.keys(pat).length ) { | ||
if( exact && _.keys(foundkeys).length != patlen ) { | ||
data = null | ||
} | ||
if( finalfind ) { | ||
data = finalfind.call(self,pat,data) | ||
} | ||
return data | ||
@@ -153,2 +174,3 @@ } | ||
self.remove = function( pat ) { | ||
@@ -183,4 +205,6 @@ var keymap = top | ||
if( part && part.km && part.km.v ) { | ||
//delete part.km.v[part.v] | ||
delete part.km.v[part.v].d | ||
var point = part.km.v[part.v] | ||
if( !point.r || point.r(pat,point.d) ) { | ||
delete point.d | ||
} | ||
} | ||
@@ -193,3 +217,3 @@ } | ||
// values can be veratim, glob, or array of globs | ||
self.findall = function( pat ) { | ||
self.list = function( pat ) { | ||
function descend(keymap,match,missing,acc) { | ||
@@ -214,3 +238,3 @@ | ||
if( 0 === _.keys(itermissing).length && nextkeymap && nextkeymap.d ) { | ||
acc.push({match:valitermatch,data:nextkeymap.d}) | ||
acc.push({match:valitermatch,data:nextkeymap.d,find:nextkeymap.f}) | ||
} | ||
@@ -217,0 +241,0 @@ else if( nextkeymap && nextkeymap.v ) { |
@@ -29,2 +29,3 @@ # patrun | ||
### Support | ||
@@ -36,5 +37,5 @@ | ||
Current Version: 0.1.5 | ||
Current Version: 0.2.0 | ||
Tested on: Node.js 0.10.22, Chrome 31, Firefox 25 | ||
Tested on: Node.js 0.10.26, Chrome 35, Firefox 29, Safari 5.1.10, Opera 12.16 | ||
@@ -44,2 +45,3 @@ [![Build Status](https://travis-ci.org/rjrodger/patrun.png?branch=master)](https://travis-ci.org/rjrodger/patrun) | ||
### Quick example | ||
@@ -206,7 +208,74 @@ | ||
# Customization | ||
You can customize the way that data is stored. For example, you might want to add a constant property to each pattern. | ||
To do this, you provide a custom function when you create the _patrun_ object: | ||
```JavaScript | ||
var alwaysAddFoo = patrun( function(pat){ | ||
pat.foo = true | ||
}) | ||
alwaysAddFoo.add( {a:1}, "bar" ) | ||
alwaysAddFoo.find( {a:1} ) // nothing! | ||
alwaysAddFoo.find( {a:1,foo:true} ) // == "bar" | ||
``` | ||
Your custom function can also return a modifer function for found | ||
data, and optionally a modifier for removing data. | ||
Here's a example that modifies found data: | ||
```JavaScript | ||
var upperify = patrun( function(pat){ | ||
return function(args,data) { | ||
return (''+data).toUpperCase() | ||
} | ||
}) | ||
upperify.add( {a:1}, "bar" ) | ||
upperify.find( {a:1} ) // BAR | ||
``` | ||
Finally, here's an example that allows you to add multiple matches for a given pattern: | ||
```JavaScript | ||
var many = patrun( function(pat,data){ | ||
var items = this.find(pat,true) || [] | ||
items.push(data) | ||
return { | ||
find: function(args,data){ | ||
return 0 < items.length ? items : null | ||
}, | ||
remove: function(args,data){ | ||
items.pop() | ||
return 0 == items.length; | ||
} | ||
} | ||
}) | ||
many.add( {a:1}, 'A' ) | ||
many.add( {a:1}, 'B' ) | ||
many.add( {b:1}, 'C' ) | ||
many.find( {a:1} ) // [ 'A', 'B' ] | ||
many.find( {b:1} ) // [ 'C' ] | ||
many.remove( {a:1} ) | ||
many.find( {a:1} ) // [ 'A' ] | ||
many.remove( {b:1} ) | ||
many.find( {b:1} ) // null | ||
``` | ||
# API | ||
## patrun() | ||
## patrun( custom ) | ||
Generates a new pattern matcher instance. | ||
Generates a new pattern matcher instance. Optionally provide a customisation function. | ||
@@ -224,3 +293,3 @@ | ||
## .findall( {...pattern...} ) | ||
## .list( {...pattern...} ) | ||
@@ -236,3 +305,3 @@ Return the list of matches for this pattern. You can use wildcards for property values. | ||
// finds nothing: [] | ||
console.log( pm.findall({a:1}) ) | ||
console.log( pm.list({a:1}) ) | ||
@@ -242,9 +311,9 @@ // finds: | ||
// { match: { a: '1', b: '2' }, data: 'B2' } ] | ||
console.log( pm.findall({a:1,b:'*'}) ) | ||
console.log( pm.list({a:1,b:'*'}) ) | ||
``` | ||
If you provide no pattern argument at all, findall will list all patterns that have been added. | ||
If you provide no pattern argument at all, _list_ will list all patterns that have been added. | ||
```JavaScript | ||
// finds everything | ||
console.log( pm.findall() ) | ||
console.log( pm.list() ) | ||
``` | ||
@@ -251,0 +320,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
26284
274
336
3
- Removedunderscore@1.5.2(transitive)
Updatedgex@~0.1.4
Updatedunderscore@~1.6.0