Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "stx", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "a blazing fast state manager with network sync out of the box", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -6,6 +6,7 @@ import { getString } from '../cache' | ||
const exists = {} | ||
const oBranch = branch | ||
const id = leaf.id | ||
leaf = getFromLeaves(branch, id) | ||
const subLeaves = [] | ||
while (branch) { | ||
leaf = branch.leaves[id] | ||
if (leaf && leaf.keys) { | ||
@@ -16,17 +17,14 @@ const found = leaf.keys.find(leafId => { | ||
} | ||
exists[leafId] = true | ||
exists[leafId] = getFromLeaves(oBranch, leafId) | ||
if (cb) { | ||
return cb(leaf.kBranch.leaves[leafId]) | ||
return cb(exists[leafId]) | ||
} else { | ||
subLeaves.push(leaf.kBranch.leaves[leafId]) | ||
subLeaves.push(exists[leafId]) | ||
} | ||
}) | ||
if (found) { | ||
return leaf.kBranch.leaves[found] | ||
return exists[found] | ||
} | ||
} | ||
branch = branch.inherits | ||
if (branch) { | ||
leaf = branch.leaves[id] | ||
} | ||
} | ||
@@ -64,8 +62,22 @@ return subLeaves | ||
const find = (branch, leaf, cb) => { | ||
return children(branch, leaf, subLeaf => { | ||
return Object.assign(children(branch, leaf, subLeaf => { | ||
subLeaf.branch = branch | ||
return cb(subLeaf, getString(subLeaf.key)) | ||
}), { branch }) | ||
} | ||
const reduce = (branch, leaf, cb, accumulator) => { | ||
let skipFirst = accumulator === void 0 | ||
children(branch, leaf, subLeaf => { | ||
subLeaf.branch = branch | ||
if (skipFirst) { | ||
accumulator = subLeaf | ||
skipFirst = false | ||
} else { | ||
accumulator = cb(accumulator, subLeaf, getString(subLeaf.key)) | ||
} | ||
}) | ||
return accumulator | ||
} | ||
export { children, forEach, map, filter, find } | ||
export { children, forEach, map, filter, find, reduce } |
@@ -7,3 +7,3 @@ import { Struct } from '../index' | ||
import { compute } from './compute' | ||
import { forEach, map, filter, find } from './array' | ||
import { forEach, map, filter, find, reduce } from './array' | ||
import { inspect, serialize } from './serialize' | ||
@@ -110,2 +110,10 @@ | ||
// REDUCE | ||
define(leaf, 'reduce', function (cb, accumulator) { | ||
return reduce(this.branch, this, cb, accumulator) | ||
}) | ||
define(struct, 'reduce', function (cb, accumulator) { | ||
return reduce(this, this.leaves[root], cb, accumulator) | ||
}) | ||
/* ===== LEAF ONLY API ===== */ | ||
@@ -112,0 +120,0 @@ |
@@ -6,5 +6,2 @@ import { Leaf } from '../index' | ||
const setReferenceByPath = (leaf, path, stamp, id, branch) => | ||
set(leaf, getByPath(branch, path, root, {}, stamp), stamp, id, branch) | ||
const setVal = (leaf, val, stamp, id, branch) => { | ||
@@ -19,2 +16,5 @@ if (leaf.kBranch !== branch) { | ||
const setReferenceByPath = (leaf, path, stamp, id, branch) => | ||
set(leaf, getByPath(branch, path, root, {}, stamp), stamp, id, branch) | ||
const setReference = (leaf, val, stamp, id, branch) => { | ||
@@ -73,3 +73,3 @@ const oBranch = branch | ||
} else { | ||
// TODO: handle setting array | ||
setVal(leaf, val, stamp, id, branch) | ||
} | ||
@@ -76,0 +76,0 @@ } else if (val.isLeaf) { |
@@ -231,1 +231,42 @@ const test = require('tape') | ||
}) | ||
test('array api - reduce', t => { | ||
global.debug = true | ||
const master = new Struct({ | ||
payments: { | ||
first: 5, | ||
second: 10 | ||
} | ||
}) | ||
const branch1 = master.create() | ||
branch1.get('payments').set({ | ||
third: 15 | ||
}) | ||
const branch2 = branch1.create({ | ||
payments: { | ||
first: 3, | ||
third: 7 | ||
} | ||
}) | ||
t.equals( | ||
master.get('payments').reduce((sum, amount) => sum + amount.compute(), 0), | ||
15, | ||
'master.payments.reduce() = 15' | ||
) | ||
t.equals( | ||
branch1.get('payments').reduce((sum, amount) => sum + amount.compute(), 5), | ||
35, | ||
'branch1.payments.reduce() = 35' | ||
) | ||
t.equals( | ||
branch2.get('payments').reduce((sum, amount) => sum + amount.compute(), 0), | ||
20, | ||
'branch2.payments.reduce() = 20' | ||
) | ||
t.end() | ||
}) |
const test = require('tape') | ||
const { Struct } = require('../') | ||
test('set - get', t => { | ||
test('set - get references', t => { | ||
const master = new Struct() | ||
@@ -70,3 +70,3 @@ | ||
}, | ||
'master.pointers.serialize() = good' | ||
'master.pointers.serialize() = correct' | ||
) | ||
@@ -81,3 +81,3 @@ t.same( | ||
}, | ||
'master.pointers.serialize() = good' | ||
'master.pointers.serialize() = correct' | ||
) | ||
@@ -89,2 +89,11 @@ t.same( | ||
) | ||
const branch1 = master.create({ | ||
deep: { | ||
real2: 'thing2' | ||
} | ||
}) | ||
branch1.get(['pointers', 'pointer2']).set(['@', 'deep', 'real2']) | ||
t.equals( | ||
@@ -95,3 +104,2 @@ master.get('pointers').get('pointer5', ['@', 'pointers', 'pointer1']).origin().get('real').compute(), | ||
) | ||
master.get(['pointers', 'pointer6'], ['@', 'pointers', 'pointer5']) | ||
@@ -105,3 +113,100 @@ | ||
t.equals( | ||
branch1.get(['pointers', 'pointer2']).compute(), | ||
'thing2', | ||
'branch1.pointers.pointer2.compute() = thing2' | ||
) | ||
t.same( | ||
branch1.serialize(), | ||
{ | ||
deep: { | ||
real2: 'thing2', | ||
real: 'thing' | ||
}, | ||
pointers: { | ||
pointer1: [ '@', 'deep' ], | ||
pointer2: [ '@', 'deep', 'real2' ], | ||
pointer3: [ '@', 'pointers', 'pointer1' ], | ||
pointer4: [ '@', 'pointers', 'pointer2' ], | ||
pointer5: [ '@', 'pointers', 'pointer1' ], | ||
pointer6: [ '@', 'pointers', 'pointer5' ] | ||
} | ||
}, | ||
'branch1.serialize() = correct' | ||
) | ||
t.end() | ||
}) | ||
test('set - get - arrays', t => { | ||
const master = new Struct() | ||
master.set({ | ||
deep: { | ||
real: [ 1, 2, 3 ] | ||
}, | ||
pointers: { | ||
pointer1: ['@', 'deep', 'real'], | ||
pointer2: ['@', 'pointers', 'pointer1'] | ||
} | ||
}) | ||
t.same( | ||
master.get(['pointers', 'pointer1']).compute(), | ||
[ 1, 2, 3 ], | ||
'pointers.pointer1.compute() = [ 1, 2, 3 ]' | ||
) | ||
t.same( | ||
master.get(['pointers', 'pointer2']).compute(), | ||
[ 1, 2, 3 ], | ||
'pointers.pointer2.compute() = [ 1, 2, 3 ]' | ||
) | ||
t.equals( | ||
master.inspect(), | ||
'Struct { deep, pointers }', | ||
'master.inspect() = Struct { deep, pointers }' | ||
) | ||
t.same( | ||
master.serialize(), | ||
{ | ||
deep: { | ||
real: [ 1, 2, 3 ] | ||
}, | ||
pointers: { | ||
pointer1: ['@', 'deep', 'real'], | ||
pointer2: ['@', 'pointers', 'pointer1'] | ||
} | ||
}, | ||
'master.pointers.serialize() = correct' | ||
) | ||
t.same( | ||
master.get(['deep', 'real']).inspect(), | ||
'Struct real { val: 1,2,3 }', | ||
'master.deep.real.inspect() = Struct real { val: 1,2,3 }' | ||
) | ||
const branch1 = master.create({ | ||
deep: { | ||
real: [ 3, 2, 1 ] | ||
} | ||
}) | ||
t.same( | ||
branch1.get(['pointers', 'pointer2']).compute(), | ||
[ 3, 2, 1 ], | ||
'branch1.pointers.pointer2.compute() = [ 3, 2, 1 ]' | ||
) | ||
t.same( | ||
branch1.serialize(), | ||
{ | ||
deep: { real: [ 3, 2, 1 ] }, | ||
pointers: { | ||
pointer1: [ '@', 'deep', 'real' ], | ||
pointer2: [ '@', 'pointers', 'pointer1' ] | ||
} | ||
}, | ||
'branch1.serialize() = correct' | ||
) | ||
t.end() | ||
}) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
30594
1120
0