From 095e915ef18cdb557be09f01e5fa9e7ba2a6d19d Mon Sep 17 00:00:00 2001 From: islam ezzat Date: Tue, 10 Apr 2018 02:21:26 +0200 Subject: [PATCH] fix issue when the object has a key that is one of the path's parts (#569) --- src/utils.ts | 2 +- test/utils.test.tsx | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index 45ce211b3..25e249076 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -30,7 +30,7 @@ export function setIn(obj: any, path: string, value: any): any { for (; i < pathArray.length - 1; i++) { const currentPath: string = pathArray[i]; - let currentObj: any = obj[currentPath]; + let currentObj: any = getIn(obj, pathArray.slice(0, i + 1)); if (resVal[currentPath]) { resVal = resVal[currentPath]; diff --git a/test/utils.test.tsx b/test/utils.test.tsx index 225057f3d..de243d7a7 100644 --- a/test/utils.test.tsx +++ b/test/utils.test.tsx @@ -190,6 +190,13 @@ describe('utils', () => { expect(obj).toEqual({ x: 'y' }); expect(newObj).toEqual({ x: 'y', nested: ['value'] }); }); + + it('supports path containing key of the object', () => { + const obj = { x: 'y' }; + const newObj = setIn(obj, 'a.x.c', 'value'); + expect(obj).toEqual({ x: 'y' }); + expect(newObj).toEqual({ x: 'y', a: { x: { c: 'value' } } }); + }); }); describe('isPromise', () => {