-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui/conf): better handling of mutable config
- Loading branch information
Showing
12 changed files
with
294 additions
and
58 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
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,45 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { mergeDeep } from './merge-deep' | ||
|
||
describe('Normal operations', () => { | ||
it('should merge objects (1 level)', () => { | ||
expect(mergeDeep({ a: 1 }, { b: 2 })).toEqual({ a: 1, b: 2 }) | ||
expect(mergeDeep({ a: 1 }, { a: 2 })).toEqual({ a: 2 }) | ||
}) | ||
|
||
it('should merge objects (2 levels)', () => { | ||
expect(mergeDeep({ a: { b: 1 } }, { a: { c: 2 } })).toEqual({ a: { b: 1, c: 2 } }) | ||
expect(mergeDeep({ a: { b: 1 } }, { a: { b: 2 } })).toEqual({ a: { b: 2 } }) | ||
}) | ||
|
||
it('should merge objects (3 levels)', () => { | ||
expect(mergeDeep({ a: { b: { c: 1 } } }, { a: { b: { d: 2 } } })).toEqual({ a: { b: { c: 1, d: 2 } } }) | ||
expect(mergeDeep({ a: { b: { c: 1 } } }, { a: { b: { c: 2 } } })).toEqual({ a: { b: { c: 2 } } }) | ||
}) | ||
}) | ||
|
||
describe('Array merge', () => { | ||
it('should merge arrays (1 level)', () => { | ||
expect(mergeDeep({ a: [1] }, { a: [2] })).toEqual({ a: [1, 2] }) | ||
}) | ||
it('should merge arrays (2 levels)', () => { | ||
expect(mergeDeep({ a: { b: [1] } }, { a: { b: [2] } })).toEqual({ a: { b: [1, 2] } }) | ||
}) | ||
it('should merge arrays (3 levels)', () => { | ||
expect(mergeDeep({ a: { b: { c: [1] } } }, { a: { b: { c: [2] } } })).toEqual({ a: { b: { c: [1, 2] } } }) | ||
}) | ||
}) | ||
|
||
describe('Prevent in-place modify: mergeDeep(a, b)', () => { | ||
it('should not modify a', () => { | ||
const a: any = { a: 1, arr: [1, 2, 3] } | ||
mergeDeep(a, { a: 2, arr: [4, 5, 6] }) | ||
expect(a).toEqual({ a: 1, arr: [1, 2, 3] }) | ||
}) | ||
|
||
it('should not modify b', () => { | ||
const b: any = { a: 1, arr: [1, 2, 3] } | ||
mergeDeep({ a: 2, arr: [4, 5, 6] }, b) | ||
expect(b).toEqual({ a: 1, arr: [1, 2, 3] }) | ||
}) | ||
}) |
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,27 @@ | ||
/** | ||
* Performs a deep merge of objects and returns new object. | ||
* Does not modify objects (immutable) and merges arrays via concatenation. | ||
* | ||
* @param objects - Objects to merge | ||
* @returns New object with merged key/values | ||
*/ | ||
export function mergeDeep<T>(...objects: any[]): T { | ||
const isObject = (obj: any) => obj && typeof obj === "object"; | ||
|
||
return objects.reduce((prev, obj) => { | ||
Object.keys(obj ?? {}).forEach((key) => { | ||
const pVal = prev[key] | ||
const oVal = obj[key] | ||
|
||
if (Array.isArray(pVal) && Array.isArray(oVal)) { | ||
prev[key] = pVal.concat(...oVal) | ||
} else if (isObject(pVal) && isObject(oVal)) { | ||
prev[key] = mergeDeep(pVal, oVal) | ||
} else { | ||
prev[key] = oVal | ||
} | ||
}) | ||
|
||
return prev | ||
}, {}) | ||
} |
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,16 @@ | ||
import { vi } from 'vitest' | ||
|
||
// @see https://github.com/vitest-dev/vitest/issues/821 | ||
Object.defineProperty(window, 'matchMedia', { | ||
writable: true, | ||
value: vi.fn().mockImplementation(query => ({ | ||
matches: false, | ||
media: query, | ||
onchange: null, | ||
addListener: vi.fn(), // deprecated | ||
removeListener: vi.fn(), // deprecated | ||
addEventListener: vi.fn(), | ||
removeEventListener: vi.fn(), | ||
dispatchEvent: vi.fn(), | ||
})), | ||
}) |
Oops, something went wrong.