Skip to content

Commit

Permalink
[update] Replace fetch with get, rename get to graft
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed Apr 17, 2015
1 parent 306ebfe commit 5c521ad
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 59 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.9.0

- `get` has been renamed `graft`. `fetch` is now `get`. This change is
designed to make the API feel more like an ES6 map.

## 0.8.0

- Removed `toArray` alias for `values`
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function(config) {
'src/**/__tests__/*.js*' : [ 'webpack', 'sourcemap' ]
},

reporters: isIntegration ? [ 'progress', 'coverage' ] : [ 'progress' ],
reporters: isIntegration ? [ 'progress', 'coverage' ] : [ 'spec' ],

coverageReporter: {
reporters: [
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "foliage",
"version": "0.8.0",
"version": "0.9.0",
"description": "A cursor like tree data structure.",
"main": "dist/Foliage.js",
"scripts": {
"coveralls": "CONTINUOUS_INTEGRATION=true npm test && coveralls < coverage/report-lcov/lcov.info",
"prepublish": "NODE_ENV=production webpack -p",
"prepublish": "./scripts/prepublish",
"test": "NODE_ENV=test karma start",
"test:once": "CONTINUOUS_INTEGRATION=true npm test"
},
Expand All @@ -14,7 +14,8 @@
"url": "git://github.com/vigetlabs/foliage.git"
},
"license": "MIT",
"dependencies": {},
"dependencies": {
},
"devDependencies": {
"babel": "^5.x.x",
"babel-loader": "^5.x.x",
Expand All @@ -31,6 +32,7 @@
"karma-mocha": "^0.1.10",
"karma-sinon-chai": "^0.3.0",
"karma-sourcemap-loader": "^0.3.4",
"karma-spec-reporter": "0.0.19",
"karma-webpack": "^1.3.1",
"source-map-loader": "^0.1.3",
"webpack": "^1.7.2"
Expand Down
9 changes: 9 additions & 0 deletions scripts/prepublish
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
echo
echo "Building for production..."
echo
NODE_ENV=production
webpack -p --optimize-minimize --optimize-occurence-order --optimize-dedupe

echo "gzipped, the build is:"
echo "`gzip -c dist/Foliage.js | wc -c` bytes"
15 changes: 7 additions & 8 deletions src/Foliage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ Foliage.prototype = {
this._root._state = state
},

get(key) {
return Object.create(this, {
_path : { value: this.getPath(key) }
})
get(key, fallback) {
return get(this._state, this.getPath(key), fallback)
},

set(key, value) {
Expand All @@ -41,17 +39,18 @@ Foliage.prototype = {
this.commit(dissoc(this._state, this.getPath(key)))
},

fetch(key, fallback) {
let val = this.get(key).valueOf()
return val === undefined ? fallback : val
graft(key) {
return Object.create(this, {
_path : { value: this.getPath(key) }
})
},

keys() {
return Object.keys(this.valueOf() || {})
},

values() {
return this.keys().map(this.fetch, this)
return this.keys().map(i => this.get(i))
},

valueOf() {
Expand Down
76 changes: 38 additions & 38 deletions src/__tests__/Foliage.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,14 @@ describe('Foliage', function() {
let deep = { first: { second: 2, tertiary: true } }

describe('Foliage::get', function() {

it ('returns cursor to a given pathway', function() {
let plant = new Foliage(shallow)
let query = plant.get('first')

query.getPath().should.eql(['first'])
})

it ('returns a nested given value', function() {
let plant = new Foliage(deep)
plant.get(['first', 'second']).valueOf().should.equal(2)
})

it ('nests keys from the results of previous queries', function() {
let plant = new Foliage(deep)
let first = plant.get('first')
let second = first.get('second')

second.valueOf().should.equal(2)
})

})

describe('Foliage::fetched', function() {
it ('returns a given value', function() {
let plant = new Foliage(shallow)
plant.fetch('first').should.equal(1)
plant.get('first').should.equal(1)
})

it ('returns a fallback if a path is not represented', function() {
let plant = new Foliage()
plant.fetch('first', 'fiz').should.equal('fiz')
plant.get('first', 'fiz').should.equal('fiz')
})
})

Expand All @@ -45,44 +21,44 @@ describe('Foliage', function() {
let plant = new Foliage(shallow)

plant.set('first', 'modified')
plant.get('first').valueOf().should.equal('modified')
plant.get('first').should.equal('modified')
})

it ('assumes a single argument sets the entire state', function() {
let plant = new Foliage({ fiz: 'buz'})
let query = plant.get('fiz')
let query = plant.graft('fiz')

query.set('foo')
query.valueOf().should.equal('foo')

plant.get('fiz').valueOf().should.equal('foo')
plant.get('fiz').should.equal('foo')
})

it ('sets the original state from a cursor', function() {
let plant = new Foliage(deep)
let query = plant.get('first')
let query = plant.graft('first')

query.set('second', 'modified')

plant.get([ 'first', 'second' ]).valueOf().should.equal('modified')
plant.get([ 'first', 'second' ]).should.equal('modified')
})

it ('defines pathways that have not been set yet', function() {
let plant = new Foliage()
let query = plant.get('first')
let query = plant.graft('first')

query.set('second', 'modified')

plant.get([ 'first', 'second' ]).valueOf().should.equal('modified')
plant.get([ 'first', 'second' ]).should.equal('modified')
})

it ('can set when assuming a pathway', function() {
let plant = new Foliage()
let query = plant.get('first')
let query = plant.graft('first')

query.set('second', 'modified')

plant.get([ 'first', 'second' ]).valueOf().should.equal('modified')
plant.get([ 'first', 'second' ]).should.equal('modified')
})

})
Expand All @@ -98,7 +74,7 @@ describe('Foliage', function() {

it ('removes from original state in a cursor', function() {
let plant = new Foliage(deep)
let query = plant.get('first')
let query = plant.graft('first')

query.remove('second')

Expand All @@ -118,7 +94,7 @@ describe('Foliage', function() {
})

describe('when a cursor is empty', function() {
let cursor = plant.get('fiz')
let cursor = plant.graft('fiz')

it ('returns an empty list when asking for keys', function() {
cursor.keys().should.eql([])
Expand All @@ -130,6 +106,30 @@ describe('Foliage', function() {
})
})

describe('Foliage::graft', function() {

it ('returns cursor to a given pathway', function() {
let plant = new Foliage(shallow)
let query = plant.graft('first')

query.getPath().should.eql(['first'])
})

it ('returns a nested given value', function() {
let plant = new Foliage(deep)
plant.graft(['first', 'second']).valueOf().should.equal(2)
})

it ('nests keys from the results of previous queries', function() {
let plant = new Foliage(deep)
let first = plant.graft('first')
let second = first.graft('second')

second.get().should.equal(2)
})

})

describe('Foliage::toJSON', function() {
let data = [1,2,3,4]

Expand All @@ -140,7 +140,7 @@ describe('Foliage', function() {

it ('returns the subset if it is a cursor', function() {
let plant = new Foliage({ first: data })
let query = plant.get('first')
let query = plant.graft('first')

query.toJSON().should.equal(data)
})
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/enumeration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Foliage', function() {

it ('map over cursors', function() {
let plant = new Foliage({ first: [ 1, 2, 3] })
let query = plant.get('first')
let query = plant.graft('first')

query.map(incr).should.eql([ 2, 3, 4])
})
Expand All @@ -31,7 +31,7 @@ describe('Foliage', function() {

it ('reduce over cursors', function() {
let plant = new Foliage({ first: [ 1, 2, 3] })
let query = plant.get('first')
let query = plant.graft('first')

query.reduce(sum, 0).should.eql(6)
})
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('Foliage', function() {

it ('finds over cursors', function() {
let plant = new Foliage({ first: [ 1, 2, 3, 4] })
let query = plant.get('first')
let query = plant.graft('first')

query.find(even).should.eql(2)
})
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/equality.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe('Equality', function() {
it ('compares equality across branches', function() {
let plant = new Foliage({ foo: 'bar' })

let a = plant.get('foo')
let b = plant.get('foo')
let a = plant.graft('foo')
let b = plant.graft('foo')

a.is(b).should.equal(true)
})
Expand All @@ -15,7 +15,7 @@ describe('Equality', function() {
let a = new Foliage({ foo: 'bar' })
let b = new Foliage({ foo: 'bar' })

a.get('foo').is(b.get('foo')).should.equal(true)
a.graft('foo').is(b.graft('foo')).should.equal(true)
})

})
2 changes: 1 addition & 1 deletion src/__tests__/inheritence.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Foliage', function() {
}

let oak = new Oak()
let acorn = oak.get('acorns')
let acorn = oak.graft('acorns')

acorn.test().should.equal(true)
})
Expand Down
4 changes: 2 additions & 2 deletions src/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

let has = require('./has')

module.exports = function get (obj, keys) {
module.exports = function get (obj, keys, fallback) {
let [ head, ...tail ] = keys

if (!head) {
return obj
}

if (has(obj, keys) === false) {
return undefined
return fallback
}

return tail.length ? get(obj[head], tail) : obj[head]
Expand Down

0 comments on commit 5c521ad

Please sign in to comment.