Skip to content

Commit

Permalink
fix(createOrApplyPath): fixed an issue with correct object mapping. a…
Browse files Browse the repository at this point in the history
…dded test
  • Loading branch information
BowlingX committed Dec 12, 2019
1 parent 10c430e commit 7e76fe6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
33 changes: 32 additions & 1 deletion src/lib/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import expect from 'expect'
import { Patch } from 'immer'
import { serializers } from '../serializers'
import { DEFAULT_NAMESPACE } from '../store'
import { createQueriesFromPatch, flattenConfig, pm } from '../utils'
import {
createOrApplyPath,
createQueriesFromPatch,
flattenConfig,
pm
} from '../utils'

describe('utils', () => {
describe('flattenConfig', () => {
Expand All @@ -27,6 +32,32 @@ describe('utils', () => {
})
})

describe('createOrApplyPath', () => {
it('should map an object', () => {
const object = {
some: {
path: 'test'
},
somewhere: {
else: {
deep: 'xyz'
}
}
}

createOrApplyPath(object, ['some', 'path'], 'new value')

expect(object).toEqual({
some: { path: 'new value' },
somewhere: {
else: {
deep: 'xyz'
}
}
})
})
})

describe('createQueriesFromPatch', () => {
const config = {
parameter: pm('p', serializers.string)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export const pm = (name: string, serializer: Serializer) => (): Parameter => ({
serializer
})

const createOrApplyPath = (
export const createOrApplyPath = (
obj: GenericObject,
path: readonly string[],
value = null
value: any = null
) => {
let current = obj
let thisPath: ReadonlyArray<string> = [...path]
while (path.length > 1) {
while (thisPath.length > 1) {
const [head, ...tail] = thisPath
thisPath = tail
if (current[head] === undefined) {
Expand Down

0 comments on commit 7e76fe6

Please sign in to comment.