Skip to content

Commit

Permalink
feat: 🎸 add assignSymbols
Browse files Browse the repository at this point in the history
  • Loading branch information
l246804 committed Aug 15, 2023
1 parent 11f1041 commit 7bf2fea
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/assignSymbols/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isObject } from 'lodash-unified'
import symbolsOf from '../symbolsOf'

/**
* 合并符号属性到目标对象上
*
* @example
* ```ts
* const obj = {}
*
* const obj1 = {}
* obj1[Symbol('key1')] = 1
*
* const obj2 = {}
* obj2[Symbol('key2')] = 2
*
* assignSymbols(obj, obj1, obj2)
* obj // => { [Symbol('key1')]: 1, [Symbol('key2')]: 2 }
* ```
*/
export default function assignSymbols<T extends object>(o: T, ...sources): T {
if (isObject(o)) {
for (const source of sources) {
const symbols = symbolsOf(source || {})
for (const syb of symbols) o[syb] = source[syb]
}
}
return o
}

0 comments on commit 7bf2fea

Please sign in to comment.