Comparing version 0.3.1 to 0.4.0
@@ -11,13 +11,10 @@ # API Cheatsheet | ||
remove - Remove a location in a tree | ||
trunk - Return the top level reference point | ||
Events | ||
Extraction | ||
+--------------+ | ||
listen - Add a change callback to a trunk or cursor | ||
ignore - Remove a change callback | ||
Dereferencing | ||
+--------------+ | ||
values - Return an array of all immediate values | ||
keys - Return an array of all top-level keys | ||
toArray - same as `values()` | ||
toJSON - Serialize the tree into JSON | ||
valueOf - Get the underlying value represented by an instance | ||
values - Return an array of all top-level values | ||
@@ -30,4 +27,2 @@ Enumeration | ||
find - Execute find over the current location | ||
some - Execute some over the current location | ||
every - Execute every over the current location | ||
``` |
@@ -25,3 +25,3 @@ var Webpack = require('webpack') | ||
reporters: [ 'nyan', 'coverage' ], | ||
reporters: isIntegration ? [ 'progress', 'coverage' ] : [ 'progress' ], | ||
@@ -41,25 +41,17 @@ coverageReporter: { | ||
module: { | ||
loaders: [ | ||
{ | ||
test : /\.jsx*$/, | ||
exclude : /node_modules/, | ||
loader : 'babel', | ||
query : { | ||
stage: 0, | ||
loose: true, | ||
optional: ['runtime'] | ||
} | ||
}, | ||
{ | ||
test : /\.json$/, | ||
loader : 'json' | ||
loaders: [{ | ||
test : /\.jsx*$/, | ||
exclude : /node_modules/, | ||
loader : 'babel', | ||
query : { | ||
stage: 1, | ||
loose: true, | ||
optional: ['runtime'] | ||
} | ||
], | ||
postLoaders: [ | ||
{ | ||
test: /\.jsx*$/, | ||
exclude: /(__tests__|node_modules)\//, | ||
loader: 'istanbul-instrumenter' | ||
} | ||
] | ||
}], | ||
postLoaders: isIntegration ? [{ | ||
test: /\.jsx*$/, | ||
exclude: /(__tests__|node_modules)\//, | ||
loader: 'istanbul-instrumenter' | ||
}] : [] | ||
} | ||
@@ -66,0 +58,0 @@ }, |
{ | ||
"name": "foliage", | ||
"version": "0.3.1", | ||
"description": "A cursor like tree data structure on top of sprout.", | ||
"version": "0.4.0", | ||
"description": "A cursor like tree data structure.", | ||
"main": "dist/Foliage.js", | ||
@@ -17,6 +17,3 @@ "scripts": { | ||
"license": "MIT", | ||
"dependencies": { | ||
"diode": "^4.0.0", | ||
"sprout-data": "^0.2.1" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -30,3 +27,2 @@ "babel": "^5.x.x", | ||
"istanbul-instrumenter-loader": "^0.1.2", | ||
"json-loader": "^0.5.1", | ||
"karma": "^0.12.32", | ||
@@ -38,13 +34,8 @@ "karma-chrome-launcher": "^0.1.7", | ||
"karma-mocha": "^0.1.10", | ||
"karma-nyan-reporter": "0.0.60", | ||
"karma-sinon-chai": "^0.3.0", | ||
"karma-sourcemap-loader": "^0.3.4", | ||
"karma-webpack": "^1.3.1", | ||
"raw-loader": "^0.5.1", | ||
"react": ">= 0.13.1", | ||
"react-tools": ">= 0.13.1", | ||
"source-map-loader": "^0.1.3", | ||
"webpack": "^1.7.2", | ||
"webpack-dev-server": "^1.7.0" | ||
"webpack": "^1.7.2" | ||
} | ||
} |
@@ -87,37 +87,6 @@ [![NPM](https://nodei.co/npm/foliage.png?compact=true)](https://npmjs.org/package/foliage) | ||
A couple of things are going on here. First, `set` is used to modify | ||
data. It maps directly to | ||
[`sprout`'s `assoc` method](https://github.com/herrstucki/sprout#assocobj-path-value-path2-value2-). Second, | ||
both `get` and `set` accept an array of keys. When given an array, | ||
they will traverse the tree for the leaf value instead of just | ||
returning the key from the most immediate level. | ||
data. Second, both `get` and `set` accept an array of keys. When given | ||
an array, they will traverse the tree for the leaf value instead of | ||
just returning the key from the most immediate level. | ||
## Phoning home | ||
All branches have a reference to their parent. No matter how branched, | ||
the `trunk` can be found: | ||
```javascript | ||
let plant = new Foliage({ fizz: 'buzz' }) | ||
let fiz = plant.get('fiz') | ||
assert(fiz.trunk() === plant) | ||
``` | ||
## Listening to changes | ||
All points in a Foliage publish events when they change: | ||
```javascript | ||
let plant = new Foliage({ fizz: 'buzz' }) | ||
let branch = plant.get('fiz') | ||
plant.listen(() => console.log('plant changed!')) | ||
branch.listen(() => console.log('branch changed!')) | ||
branch.set('new value') | ||
// => branch changed! | ||
// => plant changed! | ||
``` | ||
## Prior art | ||
@@ -127,7 +96,8 @@ | ||
- http://omniscientjs.github.io/ | ||
- https://github.com/omniscientjs/immstruct | ||
- https://github.com/omcljs/om | ||
- http://yquem.inria.fr/~huet/PUBLIC/zip.pdf | ||
- https://github.com/dustingetz/react-cursor | ||
- https://github.com/Yomguithereal/baobab | ||
- [Sprout](https://github.com/herrstucki/sprout) | ||
- [OmnicientJS](http://omniscientjs.github.io/) | ||
- [OmnicientJS Immstruct](https://github.com/omniscientjs/immstruct) | ||
- [Om](https://github.com/omcljs/om) | ||
- [Functional Zippers](http://yquem.inria.fr/~huet/PUBLIC/zip.pdf) | ||
- [React Cursor](https://github.com/dustingetz/react-cursor) | ||
- [Baobab](https://github.com/Yomguithereal/baobab) |
@@ -1,2 +0,2 @@ | ||
import Foliage, { Cursor } from '../index' | ||
import Foliage from '../Foliage' | ||
@@ -58,31 +58,2 @@ describe('Foliage', function() { | ||
it ('triggers a change event on the cursor', function(done) { | ||
let plant = new Foliage(deep) | ||
let query = plant.get('first') | ||
query.listen(done) | ||
query.set('second', 'modified') | ||
}) | ||
it ('triggers a change event on the parent', function(done) { | ||
let plant = new Foliage(deep) | ||
let query = plant.get('first') | ||
plant.listen(done) | ||
query.set('second', 'modified') | ||
}) | ||
it ('does not trigger a change event on siblings', function(done) { | ||
let plant = new Foliage({ first: 'first', second: 'second' }) | ||
let a = plant.get('first') | ||
let b = plant.get('second') | ||
b.listen(function() { | ||
throw "Sibling should not have triggered event" | ||
}) | ||
a.listen(done) | ||
a.set('modified') | ||
}) | ||
}) | ||
@@ -109,85 +80,5 @@ | ||
describe('Foliage::map', function() { | ||
let incr = i => i + 1 | ||
it ('map over top level elements', function() { | ||
let plant = new Foliage([ 1, 2, 3]) | ||
plant.map(incr).should.eql([ 2, 3, 4]) | ||
}) | ||
it ('map over cursors', function() { | ||
let plant = new Foliage({ first: [ 1, 2, 3] }) | ||
let query = plant.get('first') | ||
query.map(incr).should.eql([ 2, 3, 4]) | ||
}) | ||
}) | ||
describe('Foliage::reduce', function() { | ||
let sum = (a, b) => a + b | ||
it ('reduce over top level elements', function() { | ||
let plant = new Foliage([ 1, 2, 3]) | ||
plant.reduce(sum, 0).should.eql(6) | ||
}) | ||
it ('reduce over cursors', function() { | ||
let plant = new Foliage({ first: [ 1, 2, 3] }) | ||
let query = plant.get('first') | ||
query.reduce(sum, 0).should.eql(6) | ||
}) | ||
}) | ||
describe('Foliage::filter', function() { | ||
let even = n => (n % 2 === 0) | ||
it ('filter out extraneous values', function() { | ||
let plant = new Foliage([ 1, 2, 3]) | ||
plant.filter(even).should.eql([2]) | ||
}) | ||
it ('filters over cursors', function() { | ||
let plant = new Foliage({ first: [ 1, 2, 3] }) | ||
let query = plant.get('first') | ||
query.filter(even).should.eql([2]) | ||
}) | ||
}) | ||
describe('Foliage::find', function() { | ||
let even = n => (n % 2 === 0) | ||
it ('returns the first answer of a filter', function() { | ||
let plant = new Foliage([ 1, 2, 3, 4]) | ||
plant.find(even).should.eql(2) | ||
}) | ||
it ('finds over cursors', function() { | ||
let plant = new Foliage({ first: [ 1, 2, 3, 4] }) | ||
let query = plant.get('first') | ||
query.find(even).should.eql(2) | ||
}) | ||
}) | ||
describe('Foliage::trunk', function() { | ||
it ('returns itself if it is the root', function() { | ||
let plant = new Foliage([ 1, 2, 3, 4]) | ||
plant.trunk().should.equal(plant) | ||
}) | ||
it ('returns its source if it is a cursor', function() { | ||
let plant = new Foliage({ first: [ 1, 2, 3, 4] }) | ||
let query = plant.get('first') | ||
query.trunk().should.equal(plant) | ||
}) | ||
}) | ||
describe('Foliage::toJSON', function() { | ||
let data = [1,2,3,4] | ||
it ('returns the source value of it is root', function() { | ||
@@ -194,0 +85,0 @@ let plant = new Foliage(data) |
@@ -28,2 +28,2 @@ /** | ||
export default enumeration | ||
module.exports = enumeration |
@@ -5,14 +5,10 @@ /** | ||
import enumeration from './enumeration' | ||
import sprout from 'sprout-data' | ||
import Diode from 'diode' | ||
let assoc = require('./assoc') | ||
let dissoc = require('./dissoc') | ||
let get = require('./get') | ||
export default Foliage | ||
function Foliage (state, keys, source) { | ||
Diode.decorate(this) | ||
this._state = state | ||
this._keys = keys ? [].concat(keys) : [] | ||
this._source = source || this | ||
function Foliage (state={}) { | ||
this._path = [] | ||
this._root = this | ||
this._state = state | ||
} | ||
@@ -22,55 +18,44 @@ | ||
state() { | ||
return this.isTrunk() ? this._state : this._source.state() | ||
getPath(key) { | ||
return key ? this._path.concat(key) : this._path | ||
}, | ||
query(key) { | ||
return key ? this._keys.concat(key) : this._keys | ||
getState() { | ||
return this._state | ||
}, | ||
get(key) { | ||
return new Foliage(this.state(), this.query(key), this._source) | ||
commit(state) { | ||
this._root._state = state | ||
}, | ||
_bubble() { | ||
this.volley() | ||
if (this.isTrunk() === false) { | ||
this._source._bubble() | ||
} | ||
get(key) { | ||
return Object.create(this, { | ||
_path : { value: this.getPath(key) } | ||
}) | ||
}, | ||
commit(state) { | ||
this.trunk()._state = state | ||
this._bubble() | ||
}, | ||
set(key, value) { | ||
let mod; | ||
if (arguments.length === 1) { | ||
mod = sprout.assoc(this.state(), this.query(), key) | ||
} else { | ||
mod = sprout.assoc(this.state(), this.query(key), value) | ||
value = arguments[0] | ||
key = undefined | ||
} | ||
this.commit(mod) | ||
this.commit(assoc(this.getState(), this.getPath(key), value)) | ||
}, | ||
remove(key) { | ||
let mod = sprout.dissoc(this.state(), this.query(key)) | ||
this.commit(mod) | ||
this.commit(dissoc(this.getState(), this.getPath(key))) | ||
}, | ||
trunk() { | ||
return this._source | ||
keys() { | ||
return Object.keys(this.valueOf()) | ||
}, | ||
isTrunk() { | ||
return this._source === this | ||
values() { | ||
let value = this.valueOf() | ||
return this.keys().map(k => value[k]) | ||
}, | ||
valueOf() { | ||
let state = this.state() | ||
return this._keys.length ? sprout.get(state, this.query()) : state | ||
return get(this.getState(), this.getPath()) | ||
}, | ||
@@ -82,3 +67,21 @@ | ||
...enumeration | ||
toArray() { | ||
return this.values() | ||
}, | ||
find() { | ||
return this.filter(...arguments)[0] | ||
} | ||
} | ||
// Add collection methods | ||
let methods = [ 'map', 'reduce', 'filter', 'forEach' ] | ||
methods.forEach(function(name) { | ||
Foliage.prototype[name] = function() { | ||
return this.toArray()[name](...arguments) | ||
} | ||
}) | ||
module.exports = Foliage |
@@ -8,3 +8,3 @@ var Webpack = require('webpack') | ||
entry: { | ||
Foliage : './src/index.js' | ||
Foliage : './src/Foliage.js' | ||
}, | ||
@@ -27,23 +27,16 @@ | ||
externals: { | ||
'diode': 'diode', | ||
'sprout-data': 'sprout-data' | ||
'diode': 'diode' | ||
}, | ||
module: { | ||
loaders: [ | ||
{ | ||
test : /\.jsx*$/, | ||
exclude : /node_modules/, | ||
loader : 'babel', | ||
query : { | ||
stage: 0, | ||
loose: true | ||
} | ||
}, | ||
{ | ||
test : /\.json$/, | ||
loader : 'json' | ||
loaders: [{ | ||
test : /\.jsx*$/, | ||
exclude : /node_modules/, | ||
loader : 'babel', | ||
query : { | ||
stage : 1, | ||
loose : true | ||
} | ||
] | ||
}] | ||
} | ||
} |
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
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
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
0
18
24
416
2
20863
102
1
- Removeddiode@^4.0.0
- Removedsprout-data@^0.2.1
- Removeddiode@4.4.0(transitive)
- Removedsprout-data@0.2.4(transitive)