mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-24 23:51:20 -08:00
refactor(core): CORE-01 migrate utility contracts to TypeScript
This commit is contained in:
1 parent
e005e5f249
commit
d3c695024a
33 files changed
+1269
-305
No files matched your search
@@ -0,0 +1,35 @@
|
||||
export const def = Object.defineProperty
|
||||
|
||||
const { hasOwnProperty } = Object.prototype
|
||||
export function has(obj: object, name: PropertyKey): boolean {
|
||||
return hasOwnProperty.call(obj, name)
|
||||
}
|
||||
|
||||
export function get(obj: object, name: PropertyKey): PropertyDescriptor | undefined {
|
||||
return Object.getOwnPropertyDescriptor(obj, name)
|
||||
}
|
||||
|
||||
export function mergeDeep<T extends object[]>(...objects: T): T[number] {
|
||||
const isObject = (item: unknown): item is Record<string, unknown> => !!item && typeof item === 'object' && !Array.isArray(item)
|
||||
const result = objects.reduce<Record<string, unknown>>((prev, obj) => {
|
||||
Object.keys(obj).forEach((key) => {
|
||||
const pVal = prev[key]
|
||||
const oVal: unknown = (obj as Record<string, unknown>)[key]
|
||||
let value: unknown
|
||||
if (Array.isArray(pVal) && Array.isArray(oVal)) {
|
||||
value = pVal.concat(...oVal)
|
||||
}
|
||||
else if (isObject(pVal) && isObject(oVal)) {
|
||||
value = mergeDeep(pVal, oVal)
|
||||
}
|
||||
else {
|
||||
value = oVal
|
||||
}
|
||||
// Treat keys as data rather than invoking inherited setters such as __proto__.
|
||||
def(prev, key, { value, enumerable: true, configurable: true, writable: true })
|
||||
})
|
||||
return prev
|
||||
}, {})
|
||||
// Preserve the existing generic API; dynamic keys are handled only inside this boundary.
|
||||
return result as T[number]
|
||||
}
|
||||
Reference in new issue
Block a user