# yll_deepmerge **Repository Path**: yll10243/yll_deepmerge ## Basic Information - **Project Name**: yll_deepmerge - **Description**: yll_deepmerge的npm包 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-02-15 - **Last Updated**: 2022-02-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### 使用的参考 参考test文件夹的示例调试代码 #### 一般的使用 ``` import deepmerge from "yll_deepmerge" const x = { foo: { bar: 3 }, array: [{ does: 'work', too: [1, 2, 3] }] } const y = { foo: { baz: 4 }, quux: 5, array: [{ does: 'work', too: [4, 5, 6] }, { really: 'yes' }] } const res = deepmerge(x, y) console.log(JSON.stringify(res)) //输出{"foo":{"bar":3,"baz":4},"array":[{"does":"work","too":[1,2,3]},{"does":"work","too":[4,5,6]},{"really":"yes"}],"quux":5} ``` #### merge.all(arrayOfObjects, [options]) ``` import deepmerge from "yll_deepmerge" const foobar = { foo: { bar: 3 } } const foobaz = { foo: { baz: 4 } } const bar = { bar: 'yay!' } const res = deepmerge.all([foobar, foobaz, bar]) console.log(res) // => { foo: { bar: 3, baz: 4 }, bar: 'yay!' } ``` #### Options > arrayMerge ``` import deepmerge from "yll_deepmerge" const overwriteMerge = (destinationArray: any[], sourceArray: any[], options: deepmerge.OptionsObj) => sourceArray const res = deepmerge( [1, 2, 3], [3, 2, 1], { arrayMerge: overwriteMerge } ) console.log(res) // => [3, 2, 1] ``` ``` import deepmerge from "yll_deepmerge" const combineMerge = (target: any[], source: any[], options: deepmerge.OptionsObj) => { const destination = target.slice() source.forEach((item, index) => { if (typeof destination[index] === 'undefined') { destination[index] = options.cloneUnlessOtherwiseSpecified(item, options) } else if (options.isMergeableObject(item)) { destination[index] = deepmerge(target[index], item, options) } else if (target.indexOf(item) === -1) { destination.push(item) } }) return destination } const res = deepmerge( [{ a: true }], [{ b: true }, 'ah yup'], { arrayMerge: combineMerge } ) console.log(res) //[ { a: true, b: true }, 'ah yup' ] ``` > isMergeableObject ``` import deepmerge from "yll_deepmerge" class SuperSpecial { special = 'oh yeah man totally' } const instantiatedSpecialObject = new SuperSpecial() const target = { someProperty: { cool: 'oh for sure' } } const source = { someProperty: instantiatedSpecialObject } const defaultOutput = deepmerge(target, source) console.log(defaultOutput.someProperty.cool) // => 'oh for sure' console.log(defaultOutput.someProperty.special) // => 'oh yeah man totally' console.log(defaultOutput.someProperty instanceof SuperSpecial) // => false const customMergeOutput = deepmerge(target, source, { isMergeableObject: deepmerge.isPlainObject }) console.log(customMergeOutput.someProperty.cool) // => undefined console.log(customMergeOutput.someProperty.special) // => 'oh yeah man totally' console.log(customMergeOutput.someProperty instanceof SuperSpecial) // => true ``` > customMerge ``` import deepmerge from "yll_deepmerge" const alex = { name: { first: 'Alex', last: 'Alexson' }, pets: ['Cat', 'Parrot'] } const tony = { name: { first: 'Tony', last: 'Tonison' }, pets: ['Dog'] } const mergeNames = (nameA: any, nameB: any) => `${nameA.first} and ${nameB.first}` const options = { customMerge: (key: string) => { if (key === 'name') { return mergeNames } } } const result = deepmerge(alex, tony, options) console.log(result.name) // => 'Alex and Tony' console.log(result.pets) // => ['Cat', 'Parrot', 'Dog'] ``` > clone - 默认是true,值为false|true