-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into fix_existOrFail
- Loading branch information
Showing
48 changed files
with
716 additions
and
255 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
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,64 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { merge } from './merge'; | ||
|
||
describe('merge', () => { | ||
test('empty objects', () => expect(merge({}, {})).toEqual({})); | ||
|
||
test('basic', () => { | ||
expect(merge({}, { a: 1 })).toEqual({ a: 1 }); | ||
expect(merge({ a: 0 }, {})).toEqual({ a: 0 }); | ||
expect(merge({ a: 0 }, { a: 1 })).toEqual({ a: 1 }); | ||
}); | ||
|
||
test('undefined', () => { | ||
expect(merge({ a: undefined }, { a: 1 })).toEqual({ a: 1 }); | ||
expect(merge({ a: 0 }, { a: undefined })).toEqual({ a: 0 }); | ||
expect(merge({ a: undefined }, { a: undefined })).toEqual({}); | ||
expect(merge({ a: void 0 }, { a: void 0 })).toEqual({}); | ||
}); | ||
|
||
test('null', () => { | ||
expect(merge({ a: null }, { a: 1 })).toEqual({ a: 1 }); | ||
expect(merge({ a: 0 }, { a: null })).toEqual({ a: null }); | ||
expect(merge({ a: null }, { a: null })).toEqual({ a: null }); | ||
}); | ||
|
||
test('arrays', () => { | ||
expect(merge({ b: [0] }, { b: [2] })).toEqual({ b: [2] }); | ||
expect(merge({ b: [0, 1] }, { b: [2] })).toEqual({ b: [2] }); | ||
expect(merge({ b: [0] }, { b: [2, 3] })).toEqual({ b: [2, 3] }); | ||
expect(merge({ b: [] }, { b: [2] })).toEqual({ b: [2] }); | ||
expect(merge({ b: [0] }, { b: [] })).toEqual({ b: [] }); | ||
}); | ||
|
||
test('nested objects', () => { | ||
expect(merge({ top: { a: 0, b: 0 } }, { top: { a: 1, c: 1 } })).toEqual({ | ||
top: { a: 1, b: 0, c: 1 }, | ||
}); | ||
expect(merge({ top: { a: 0, b: 0 } }, { top: [0, 1] })).toEqual({ top: [0, 1] }); | ||
}); | ||
|
||
test('multiple objects', () => { | ||
expect(merge({}, { a: 1 }, { a: 2 })).toEqual({ a: 2 }); | ||
expect(merge({ a: 0 }, {}, {})).toEqual({ a: 0 }); | ||
expect(merge({ a: 0 }, { a: 1 }, {})).toEqual({ a: 1 }); | ||
}); | ||
}); |
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,85 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/** | ||
* Deeply merges two objects, omitting undefined values, and not deeply merging Arrays. | ||
* | ||
* @remarks | ||
* Should behave identically to lodash.merge, however it will not merge Array values like lodash does. | ||
* Any properties with `undefined` values on both objects will be ommitted from the returned object. | ||
*/ | ||
export function merge<TBase extends Record<string, any>, TSource1 extends Record<string, any>>( | ||
baseObj: TBase, | ||
source1: TSource1 | ||
): TBase & TSource1; | ||
export function merge< | ||
TBase extends Record<string, any>, | ||
TSource1 extends Record<string, any>, | ||
TSource2 extends Record<string, any> | ||
>(baseObj: TBase, overrideObj: TSource1, overrideObj2: TSource2): TBase & TSource1 & TSource2; | ||
export function merge< | ||
TBase extends Record<string, any>, | ||
TSource1 extends Record<string, any>, | ||
TSource2 extends Record<string, any>, | ||
TSource3 extends Record<string, any> | ||
>( | ||
baseObj: TBase, | ||
overrideObj: TSource1, | ||
overrideObj2: TSource2 | ||
): TBase & TSource1 & TSource2 & TSource3; | ||
export function merge<TReturn extends Record<string, any>>( | ||
baseObj: Record<string, any>, | ||
...sources: Array<Record<string, any>> | ||
): TReturn { | ||
const firstSource = sources[0]; | ||
if (firstSource === undefined) { | ||
return baseObj as TReturn; | ||
} | ||
|
||
return sources | ||
.slice(1) | ||
.reduce( | ||
(merged, nextSource) => mergeObjects(merged, nextSource), | ||
mergeObjects(baseObj, firstSource) | ||
) as TReturn; | ||
} | ||
|
||
const isMergable = (obj: any) => typeof obj === 'object' && obj !== null && !Array.isArray(obj); | ||
|
||
const mergeObjects = <T extends Record<string, any>, U extends Record<string, any>>( | ||
baseObj: T, | ||
overrideObj: U | ||
): T & U => | ||
[...new Set([...Object.keys(baseObj), ...Object.keys(overrideObj)])].reduce( | ||
(merged, key) => { | ||
const baseVal = baseObj[key]; | ||
const overrideVal = overrideObj[key]; | ||
|
||
if (isMergable(baseVal) && isMergable(overrideVal)) { | ||
merged[key] = mergeObjects(baseVal, overrideVal); | ||
} else if (overrideVal !== undefined) { | ||
merged[key] = overrideVal; | ||
} else if (baseVal !== undefined) { | ||
merged[key] = baseVal; | ||
} | ||
|
||
return merged; | ||
}, | ||
{} as any | ||
); |
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
Oops, something went wrong.