-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from ajthinking/nested-merge
Nested merge
- Loading branch information
Showing
6 changed files
with
81 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { merge } from './merge'; | ||
|
||
it('merges flat objects', () => { | ||
const first = { a: 1 }; | ||
const second = { a: 2, b: 3 }; | ||
|
||
expect(merge(first, second)).toEqual({ a: 2, b: 3 }); | ||
}); | ||
|
||
it('merges nested objects', () => { | ||
const first = { a: { b: 1, c: 2 } }; | ||
const second = { a: { b: 'new' } }; | ||
|
||
expect(merge(first, second)).toEqual({ a: { b: 'new', c: 2 } }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
type Obj = { | ||
[key: string]: any | ||
} | ||
|
||
export const merge = (first: Obj, second: Obj) => { | ||
// Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties | ||
for (const key of Object.keys(second)) { | ||
if (second[key] instanceof Object) | ||
Object.assign(second[key], merge(first[key], second[key])); | ||
} | ||
// Join `target` and modified `source` | ||
Object.assign(first || {}, second); | ||
return first; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { multiline } from './multiline'; | ||
|
||
|
||
it('removes first line when it is empty', () => { | ||
const result = multiline` | ||
content | ||
`; | ||
|
||
expect(result).toEqual('content\n'); | ||
}); | ||
|
||
it('bases indentation on the first content line', () => { | ||
const result = multiline` | ||
heading | ||
p1 | ||
p2 | ||
`; | ||
|
||
expect(result).toEqual('heading\n p1\n p2\n'); | ||
}); | ||
|
||
it('throws if it gets content on first line', () => { | ||
expect(() => multiline`content-on-first-line`).toThrow(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters