Skip to content

Commit

Permalink
[update] Continue to refine
Browse files Browse the repository at this point in the history
  • Loading branch information
nhunzaker committed May 28, 2015
1 parent 7001385 commit a55cfe6
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 20 deletions.
6 changes: 2 additions & 4 deletions karma.conf.js
Expand Up @@ -6,17 +6,16 @@ module.exports = function(config) {
frameworks: [ 'mocha', 'sinon-chai' ],

autoWatchBatchDelay: 400,
logLevel: config.LOG_ERROR,

files: [
'src/**/__tests__/*.js*'
],

preprocessors: {
'src/**/__tests__/*.js*' : [ 'webpack' ]
'src/**/__tests__/*.js*' : ['webpack', 'sourcemap']
},

reporters: [ 'nyan', 'coverage' ],
reporters: [ 'progress', 'coverage' ],

coverageReporter: {
type: 'html',
Expand All @@ -26,7 +25,6 @@ module.exports = function(config) {

webpack: {
devtool : 'inline-source-map',

module: {
loaders: [{
test : /\.jsx*$/,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -33,8 +33,8 @@
"karma-coverage": "~0.3",
"karma-firefox-launcher": "~0.1",
"karma-mocha": "~0.1",
"karma-nyan-reporter": "~0.0",
"karma-sinon-chai": "~0.3",
"karma-sourcemap-loader": "^0.3.4",
"karma-webpack": "~1.5"
},
"dependencies": {
Expand Down
23 changes: 10 additions & 13 deletions src/Foliage.js
Expand Up @@ -5,16 +5,18 @@
* @param {Object} state - The initial state of the instance
*/

let assoc = require('./assoc')
let dissoc = require('./dissoc')
let getIn = require('./get')
let Diode = require('diode')
let assoc = require('./assoc')
let dissoc = require('./dissoc')
let getIn = require('./get')
let Diode = require('diode')
let keysOf = require('./keys')
let valuesOf = require('./values')

function Foliage (state) {
Diode(this)

this._path = []
this._root = this
this._path = []
this._root = this

this.commit(state)
}
Expand Down Expand Up @@ -73,16 +75,11 @@ Foliage.prototype = {
},

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

values() {
// An anonymous function is used here instead of
// calling `this.get` directly because we have no
// fallback value.
return this.keys().map(function(key) {
return this.get(key)
}, this)
return valuesOf(this.valueOf())
},

valueOf() {
Expand Down
38 changes: 36 additions & 2 deletions src/__tests__/enumeration.test.js
@@ -1,8 +1,12 @@
import Foliage from '../Foliage'
import Foliage from '../Foliage'
import Immutable from 'immutable'

describe('Enumeration', function() {

describe('Foliage', function() {
let array = [1, 2, 3, 4 ]
let object = { a: 1, b: 2, c: 3, d: 4 }
let map = Immutable.Map(object)
let list = Immutable.Set(array)

let tests = new Foliage([
[ 'map', (i => i + 1) ],
Expand Down Expand Up @@ -31,6 +35,20 @@ describe('Foliage', function() {
plant[method](...args).should.eql(expected)
})

it ('works at the root level with maps', function() {
let plant = new Foliage(map)
let expected = plant.values()[method](...args)

plant[method](...args).should.eql(expected)
})

it ('works at the root level with sets', function() {
let plant = new Foliage(map)
let expected = plant.values()[method](...args)

plant[method](...args).should.eql(expected)
})

it ('works at a sub-level with arrays', function() {
let plant = new Foliage({ body: array })
let query = plant.refine('body')
Expand All @@ -47,6 +65,22 @@ describe('Foliage', function() {
query[method](...args).should.eql(expected)
})

it ('works at a sub-level with sets', function() {
let plant = new Foliage({ body: set })
let query = plant.refine('body')
let expected = array[method](...args)

query[method](...args).should.eql(expected)
})

it ('works at a sub-level level with maps', function() {
let plant = new Foliage({ body : map })
let query = plant.refine('body')
let expected = query.values()[method](...args)

query[method](...args).should.eql(expected)
})

})
})

Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/keys.test.js
@@ -0,0 +1,23 @@
import keys from '../keys'
import { Map, List } from 'immutable'

describe.only('Keys', function() {

it ('works for objects', function() {
keys({ a: 1, b: 2, c: 3 }).should.eql(['a', 'b', 'c'])
})

it ('works for arrays', function() {
keys([ 'a', 'b', 'c' ]).should.eql([ '0', '1', '2' ])
})

it.only ('works for maps', function() {
let sample = Map({ a: 1, b: 2, c: 3 })
keys(sample).should.eql([ 'a', 'b', 'c'])
})

it ('works for lists', function() {
keys(List([1, 2, 3])).should.eql([0, 1, 2])
})

})
23 changes: 23 additions & 0 deletions src/__tests__/values.test.js
@@ -0,0 +1,23 @@
import values from '../values'
import { Map, Set } from 'immutable'

describe('Values', function() {

it ('works for objects', function() {
values({ a: 1, b: 2, c: 3 }).should.eql([1,2,3])
})

it ('works for arrays', function() {
values([ 1, 2, 3 ]).should.eql([1,2,3])
})

it ('works for maps', function() {
let sample = Map({ a: 1, b: 2, c: 3 })
values(sample).should.eql([ 1, 2, 3])
})

it ('works for sets', function() {
values(Set([1,2,3])).should.eql([1,2,3])
})

})
6 changes: 6 additions & 0 deletions src/keys.js
@@ -0,0 +1,6 @@
let isImmutable = require('./isImmutable')

module.exports = function(obj) {
return isImmutable(obj) ? obj.map((value, key) => key).toArray()
: Object.keys(obj || {})
}
9 changes: 9 additions & 0 deletions src/values.js
@@ -0,0 +1,9 @@
let get = require('./get')
let isImmutable = require('./isImmutable')
let keys = require('./keys')

module.exports = function(obj) {
// An anonymous function is used here instead of calling
// `get` directly because there is no fallback value.
return keys(obj).map(key => get(obj, [ key ]))
}

0 comments on commit a55cfe6

Please sign in to comment.