key-tree-store
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -70,4 +70,15 @@ var slice = Array.prototype.slice; | ||
// run all matches with optional context | ||
KeyTreeStore.prototype.run = function (keypath, context) { | ||
this.get(keypath).forEach(function (fn) { | ||
if (context) { | ||
fn.call(context); | ||
} else { | ||
fn(); | ||
} | ||
}); | ||
}; | ||
module.exports = KeyTreeStore; |
{ | ||
"name": "key-tree-store", | ||
"description": "Simple tool for storing/retrieving objects events based hierarchical keypaths.", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -68,2 +68,23 @@ # key-tree-store | ||
running items: | ||
```javascript | ||
// as a shortcut, there's also the `run` method | ||
// to help you run functions that match they keypath | ||
// this assumes you're storing functions, of course. | ||
var KeyTree = require('key-tree-store'); | ||
var tree = new KeyTree(); | ||
tree.add('key.path', function () { | ||
console.log('function ran!'); | ||
}); | ||
tree.run('key'); //=> function ran! | ||
// you can also optionally pass a context to run them with | ||
tree.run('key', {some: 'object'}); | ||
``` | ||
## credits | ||
@@ -70,0 +91,0 @@ |
@@ -46,1 +46,46 @@ var test = require('tape'); | ||
}); | ||
test('`run`', function (t) { | ||
var tree = new KeyTree(); | ||
var oneRan, twoRan; | ||
var oneContext, twoContext; | ||
var one = function () { | ||
oneRan = true; | ||
oneContext = this; | ||
}; | ||
var two = function () { | ||
twoRan = true; | ||
twoContext = this; | ||
}; | ||
function reset() { | ||
oneRan = false; | ||
twoRan = false; | ||
oneContext = null; | ||
twoContext = null; | ||
} | ||
reset(); | ||
tree.add('one', one); | ||
tree.add('two', two); | ||
tree.run(); | ||
t.ok(oneRan); | ||
t.ok(twoRan); | ||
reset(); | ||
tree.run('one'); | ||
t.ok(oneRan); | ||
t.notOk(twoRan); | ||
reset(); | ||
var context = {}; | ||
tree.run('one', context) | ||
t.equal(oneContext, context); | ||
t.end(); | ||
}); |
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
7003
134
97