persistent-hash-trie
Advanced tools
Comparing version 0.4.0 to 0.4.1
{ | ||
"name": "persistent-hash-trie", | ||
"description": "Pure string:val storage, using structural sharing", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"main": "src/persistent-hash-trie.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -315,25 +315,32 @@ 'use strict'; | ||
// given a node, function and seed, reduce to produce results. | ||
lib.reduce = function(node, callback, seed){ | ||
try { | ||
return reduce(node, callback, { seed: seed }).seed | ||
} catch(e) { | ||
if ( e instanceof lib.reduce.Break ) return e.value; | ||
else throw e; | ||
} | ||
lib.reduce = function(node, callback, initial){ | ||
var state = reduce(node, callback, initial) | ||
return state instanceof Break ? state.value : state | ||
} | ||
var reduce = function(node, callback, state) { | ||
reduceFns[node.type](node, callback, state) | ||
return state | ||
var reduce = function(node, callback, initial) { | ||
return reduceFns[node.type](node, callback, initial) | ||
} | ||
var reduceFns = { | ||
trie: function(node, callback, state){ | ||
for ( var path in node.children ) if ( node.children.hasOwnProperty(path) ) reduce(node.children[path], callback, state) | ||
trie: function(node, callback, state) { | ||
for ( var path in node.children ) { | ||
if ( node.children.hasOwnProperty(path) ) { | ||
state = reduce(node.children[path], callback, state) | ||
} | ||
if (state instanceof Break) break | ||
} | ||
return state | ||
}, | ||
value: function(node, callback, state){ | ||
state.seed = callback(state.seed, node.value, node.key) | ||
return callback(state, node.value, node.key) | ||
}, | ||
hashmap: function(node, callback, state){ | ||
for ( var key in node.values ) if ( node.values.hasOwnProperty(key) ) reduce(node.values[key], callback, state) | ||
for ( var key in node.values ) { | ||
if ( node.values.hasOwnProperty(key) ) { | ||
state = reduce(node.values[key], callback, state) | ||
} | ||
if (state instanceof Break) break | ||
} | ||
return state | ||
} | ||
@@ -343,3 +350,6 @@ } | ||
lib.reduce.Break = function(v){ this.value = v } | ||
var Break = lib.reduce.Break = function(v) { | ||
if (!(this instanceof Break)) return new Break(v) | ||
this.value = v | ||
} | ||
@@ -346,0 +356,0 @@ |
@@ -34,3 +34,3 @@ 'use strict'; | ||
var iterate = function(){ | ||
throw new p.reduce.Break("my-return-value") | ||
return p.reduce.Break("my-return-value") | ||
} | ||
@@ -37,0 +37,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
3954
190777
80