Skip to content

Commit

Permalink
feat(data-point/reducer-path): Add a custom name to ReducerPath#body …
Browse files Browse the repository at this point in the history
…functions (#193)

closes #182
  • Loading branch information
raingerber authored and acatl committed Jan 31, 2018
1 parent a704568 commit 6aab266
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
11 changes: 6 additions & 5 deletions packages/data-point/lib/reducer-types/reducer-path/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports.mapFromAccumulatorValue = mapFromAccumulatorValue
* @returns {Function}
*/
function getPathReducerFunction (jsonPath, asCollection) {
if (jsonPath === '$' || _.isEmpty(jsonPath)) {
if (jsonPath === '$') {
return getAccumulatorValue
}

Expand All @@ -101,15 +101,16 @@ module.exports.getPathReducerFunction = getPathReducerFunction

/**
* @param {Function} createReducer
* @param {string} source
* @param {string} source - must begin with $
* @return {reducer}
*/
function create (createReducer, source) {
const reducer = new ReducerPath()

reducer.asCollection = source.slice(-2) === '[]'
reducer.name = (source.substr(1) || '$').replace(/\[]$/, '')
const value = source.replace(/\[]$/, '')
reducer.name = value.substr(1) || '$'
reducer.asCollection = source.endsWith('[]')
reducer.body = getPathReducerFunction(reducer.name, reducer.asCollection)
Object.defineProperty(reducer.body, 'name', { value })

return reducer
}
Expand Down
23 changes: 16 additions & 7 deletions packages/data-point/lib/reducer-types/reducer-path/factory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ describe('ReducerPath getters', () => {

describe('reducer/reducer-path#getPathReducerFunction', () => {
it('should always return a function', () => {
expect(factory.getPathReducerFunction()).toBeInstanceOf(Function)
expect(factory.getPathReducerFunction('')).toBeInstanceOf(Function)
expect(factory.getPathReducerFunction('.')).toBeInstanceOf(Function)
expect(factory.getPathReducerFunction('..')).toBeInstanceOf(Function)
Expand All @@ -63,36 +62,46 @@ describe('reducer/reducer-path#getPathReducerFunction', () => {
})

describe('reducer/reducer-path#create', () => {
it('basic path', () => {
const reducer = factory.create(createReducer, '$a')
it('empty path', () => {
const reducer = factory.create(createReducer, '$')
expect(reducer.type).toBe('ReducerPath')
expect(reducer.name).toBe('a')
expect(reducer.name).toBe('$')
expect(reducer.asCollection).toBe(false)
expect(reducer.body).toBeInstanceOf(Function)
expect(reducer.body.name).toBe('$')
})

it('empty path', () => {
const reducer = factory.create(createReducer, '$')
it('basic path', () => {
const reducer = factory.create(createReducer, '$a')
expect(reducer.type).toBe('ReducerPath')
expect(reducer.name).toBe('$')
expect(reducer.name).toBe('a')
expect(reducer.asCollection).toBe(false)
expect(reducer.body).toBeInstanceOf(Function)
expect(reducer.body.name).toBe('$a')
})

it('compound path', () => {
const reducer = factory.create(createReducer, '$foo.bar')
expect(reducer.name).toBe('foo.bar')
expect(reducer.asCollection).toBe(false)
expect(reducer.body).toBeInstanceOf(Function)
expect(reducer.body.name).toBe('$foo.bar')
})

it('compound path', () => {
const reducer = factory.create(createReducer, '$foo.bar[0]')
expect(reducer.name).toBe('foo.bar[0]')
expect(reducer.asCollection).toBe(false)
expect(reducer.body).toBeInstanceOf(Function)
expect(reducer.body.name).toBe('$foo.bar[0]')
})

it('path with asCollection', () => {
const reducer = factory.create(createReducer, '$foo.bar[]')
expect(reducer.name).toBe('foo.bar')
expect(reducer.asCollection).toBe(true)
expect(reducer.body).toBeInstanceOf(Function)
expect(reducer.body.name).toBe('$foo.bar')
})
})

Expand Down

0 comments on commit 6aab266

Please sign in to comment.