Skip to content
This repository has been archived by the owner on Jun 17, 2023. It is now read-only.

Commit

Permalink
fix: only object type roots should be pathed
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Apr 10, 2017
1 parent 22abf2a commit 55cc16d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
9 changes: 8 additions & 1 deletion __tests__/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@ export default {
name: "Cat B",
owner: 2
}
}
},

arrayInRoot: [
1, 2
],

stringInRoot: "1",
numberInRoot: 2
}
11 changes: 10 additions & 1 deletion __tests__/general.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const join = repath({
}
})

const {users, dogs} = join(data)
const {users, dogs, arrayInRoot, stringInRoot, numberInRoot} = join(data)

test('users[1].dog should match dogs[1]', () => {
expect(users[1].dog).toMatchObject(_.omit(dogs[1], ["owner", "friends"]))
Expand Down Expand Up @@ -73,4 +73,13 @@ test('Properties not defined in schema should remain unchanged', () => {

test('Null properties should not be changed', () => {
expect(users[1].nullValue).toBe(null)
})

test('Array type values in schema roots should not be touched', () => {
expect(arrayInRoot).toEqual([1, 2])
})

test('String or number type values in schema roots should not be touched', () => {
expect(stringInRoot).toBe("1")
expect(numberInRoot).toBe(2)
})
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ export default (config: Config) => (data: Object, limiter: string | Array<string
/* Parse the top level of all entities to replace
* all relationships with getters.
* */
return mapValues(data, (entities, root) =>
mapValues(entities, applyGetters(root))
)
return mapValues(data, (entities, root) => {
if (typeof entities === 'object' && !Array.isArray(entities)) {
return mapValues(entities, applyGetters(root))
}

return entities
})
}

0 comments on commit 55cc16d

Please sign in to comment.