-
-
Notifications
You must be signed in to change notification settings - Fork 142
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 (#722)
* fix(ui): mergeDeep causes some configurations to be overridden by the default configuration * fix: config always get override by defaults Signed-off-by: Frost Ming <[email protected]> * refactor(ui/conf): better handling of mutable config * chore: fix path issue on win env * fix: mergeDeep avoid prototype pollution BREAKING CHANGE Directly referencing `artalk.conf` and `artalk.$root` is deprecated. Please use functions instead: `artalk.getConf()` and `artalk.getEl()`. --------- Signed-off-by: Frost Ming <[email protected]> Co-authored-by: syfxlin <[email protected]> Co-authored-by: Frost Ming <[email protected]>
- Loading branch information
1 parent
32a3986
commit 4c2646e
Showing
13 changed files
with
331 additions
and
50 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,65 @@ | ||
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] }) | ||
}) | ||
}) | ||
|
||
describe('Merge special types', () => { | ||
const testItem = (name: string, val: any) => { | ||
it(name, () => { | ||
expect(mergeDeep({ a: val }, { b: val })).toEqual({ a: val, b: val }) | ||
}) | ||
} | ||
|
||
const dom = document.createElement('div') | ||
testItem('should can keep dom, not deep recursion', dom) | ||
|
||
const fn = () => {} | ||
testItem('should can keep function', fn) | ||
|
||
const date = new Date() | ||
testItem('should can keep date', date) | ||
|
||
const reg = /abc/ | ||
testItem('should can keep regex', reg) | ||
}) |
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,32 @@ | ||
/** | ||
* 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" && obj.constructor === Object; | ||
|
||
return objects.reduce((prev, obj) => { | ||
Object.keys(obj ?? {}).forEach((key) => { | ||
// Avoid prototype pollution | ||
if (key === "__proto__" || key === "constructor" || key === "prototype") { | ||
return | ||
} | ||
|
||
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.