-
Notifications
You must be signed in to change notification settings - Fork 881
feat: new merge function #2484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: new merge function #2484
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
20fc6cb
feat: new merge function
obecny 667f0a7
chore: updating info about lodash.merge util
obecny 467d4f8
Merge branch 'main' into merge
obecny 9114417
chore: merge branch 'main' into merge
obecny 7a7c012
Merge branch 'merge' of github.com:obecny/opentelemetry-js into merge
obecny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,174 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
/** | ||
* based on lodash in order to support esm builds without esModuleInterop. | ||
* lodash is using MIT License. | ||
**/ | ||
|
||
const objectTag = '[object Object]'; | ||
const nullTag = '[object Null]'; | ||
const undefinedTag = '[object Undefined]'; | ||
const funcProto = Function.prototype; | ||
const funcToString = funcProto.toString; | ||
const objectCtorString = funcToString.call(Object); | ||
const getPrototype = overArg(Object.getPrototypeOf, Object); | ||
const objectProto = Object.prototype; | ||
const hasOwnProperty = objectProto.hasOwnProperty; | ||
const symToStringTag = Symbol ? Symbol.toStringTag : undefined; | ||
const nativeObjectToString = objectProto.toString; | ||
|
||
/** | ||
* Creates a unary function that invokes `func` with its argument transformed. | ||
* | ||
* @private | ||
* @param {Function} func The function to wrap. | ||
* @param {Function} transform The argument transform. | ||
* @returns {Function} Returns the new function. | ||
*/ | ||
function overArg(func: Function, transform: any): any { | ||
return function(arg: any) { | ||
return func(transform(arg)); | ||
}; | ||
} | ||
|
||
/** | ||
* Checks if `value` is a plain object, that is, an object created by the | ||
* `Object` constructor or one with a `[[Prototype]]` of `null`. | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 0.8.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is a plain object, else `false`. | ||
* @example | ||
* | ||
* function Foo() { | ||
* this.a = 1; | ||
* } | ||
* | ||
* _.isPlainObject(new Foo); | ||
* // => false | ||
* | ||
* _.isPlainObject([1, 2, 3]); | ||
* // => false | ||
* | ||
* _.isPlainObject({ 'x': 0, 'y': 0 }); | ||
* // => true | ||
* | ||
* _.isPlainObject(Object.create(null)); | ||
* // => true | ||
*/ | ||
export function isPlainObject(value: any) { | ||
if (!isObjectLike(value) || baseGetTag(value) !== objectTag) { | ||
return false; | ||
} | ||
const proto = getPrototype(value); | ||
if (proto === null) { | ||
return true; | ||
} | ||
const Ctor = hasOwnProperty.call(proto, 'constructor') && proto.constructor; | ||
return typeof Ctor == 'function' && Ctor instanceof Ctor && | ||
funcToString.call(Ctor) === objectCtorString; | ||
} | ||
|
||
/** | ||
* Checks if `value` is object-like. A value is object-like if it's not `null` | ||
* and has a `typeof` result of "object". | ||
* | ||
* @static | ||
* @memberOf _ | ||
* @since 4.0.0 | ||
* @category Lang | ||
* @param {*} value The value to check. | ||
* @returns {boolean} Returns `true` if `value` is object-like, else `false`. | ||
* @example | ||
* | ||
* _.isObjectLike({}); | ||
* // => true | ||
* | ||
* _.isObjectLike([1, 2, 3]); | ||
* // => true | ||
* | ||
* _.isObjectLike(_.noop); | ||
* // => false | ||
* | ||
* _.isObjectLike(null); | ||
* // => false | ||
*/ | ||
function isObjectLike(value: any) { | ||
return value != null && typeof value == 'object'; | ||
} | ||
|
||
/** | ||
* The base implementation of `getTag` without fallbacks for buggy environments. | ||
* | ||
* @private | ||
* @param {*} value The value to query. | ||
* @returns {string} Returns the `toStringTag`. | ||
*/ | ||
function baseGetTag(value: any) { | ||
if (value == null) { | ||
return value === undefined ? undefinedTag : nullTag; | ||
} | ||
return (symToStringTag && symToStringTag in Object(value)) | ||
? getRawTag(value) | ||
: objectToString(value); | ||
} | ||
|
||
/** | ||
* A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values. | ||
* | ||
* @private | ||
* @param {*} value The value to query. | ||
* @returns {string} Returns the raw `toStringTag`. | ||
*/ | ||
function getRawTag(value: any) { | ||
const isOwn = hasOwnProperty.call(value, symToStringTag as any), | ||
tag = value[symToStringTag as any]; | ||
let unmasked = false; | ||
|
||
try { | ||
value[symToStringTag as any] = undefined; | ||
unmasked = true; | ||
} catch (e) { | ||
// silence | ||
} | ||
|
||
const result = nativeObjectToString.call(value); | ||
if (unmasked) { | ||
if (isOwn) { | ||
value[symToStringTag as any] = tag; | ||
} else { | ||
delete value[symToStringTag as any]; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Converts `value` to a string using `Object.prototype.toString`. | ||
* | ||
* @private | ||
* @param {*} value The value to convert. | ||
* @returns {string} Returns the converted string. | ||
*/ | ||
function objectToString(value: any) { | ||
return nativeObjectToString.call(value); | ||
} |
This file contains hidden or 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,189 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
import { isPlainObject } from './lodash.merge'; | ||
|
||
const MAX_LEVEL = 20; | ||
|
||
interface ObjectInto { | ||
obj: any; | ||
key: string; | ||
} | ||
|
||
/** | ||
* Merges objects together | ||
* @param args - objects / values to be merged | ||
*/ | ||
export function merge(...args: any[]): any { | ||
let result: any = args.shift(); | ||
const objects: WeakMap<any, ObjectInto[]> | undefined = new WeakMap<any, ObjectInto[]>(); | ||
while (args.length > 0) { | ||
result = mergeTwoObjects(result, args.shift(), 0, objects); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function takeValue(value: any): any { | ||
if (isArray(value)) { | ||
return value.slice(); | ||
} | ||
return value; | ||
} | ||
|
||
/** | ||
* Merges two objects | ||
* @param one - first object | ||
* @param two - second object | ||
* @param level - current deep level | ||
* @param objects - objects holder that has been already referenced - to prevent | ||
* cyclic dependency | ||
*/ | ||
function mergeTwoObjects( | ||
one: any, | ||
two: any, | ||
level = 0, | ||
objects: WeakMap<any, ObjectInto[]>, | ||
): any { | ||
let result: any; | ||
if (level > MAX_LEVEL) { | ||
return undefined; | ||
} | ||
level++; | ||
if (isPrimitive(one) || isPrimitive(two) || isFunction(two)) { | ||
result = takeValue(two); | ||
} else if (isArray(one)) { | ||
result = one.slice(); | ||
if (isArray(two)) { | ||
for (let i = 0, j = two.length; i < j; i++) { | ||
result.push(takeValue(two[i])); | ||
} | ||
} else if (isObject(two)) { | ||
const keys = Object.keys(two); | ||
for (let i = 0, j = keys.length; i < j; i++) { | ||
const key = keys[i]; | ||
result[key] = takeValue(two[key]); | ||
} | ||
} | ||
} else if (isObject(one)) { | ||
if (isObject(two)) { | ||
if (!shouldMerge(one, two)) { | ||
return two; | ||
} | ||
result = Object.assign({}, one); | ||
const keys = Object.keys(two); | ||
|
||
for (let i = 0, j = keys.length; i < j; i++) { | ||
const key = keys[i]; | ||
const twoValue = two[key]; | ||
|
||
if (isPrimitive(twoValue)) { | ||
if (typeof twoValue === 'undefined') { | ||
delete result[key]; | ||
} else { | ||
// result[key] = takeValue(twoValue); | ||
result[key] = twoValue; | ||
} | ||
} else { | ||
const obj1 = result[key]; | ||
const obj2 = twoValue; | ||
|
||
if ( | ||
wasObjectReferenced(one, key, objects) || | ||
wasObjectReferenced(two, key, objects) | ||
) { | ||
delete result[key]; | ||
} else { | ||
|
||
if (isObject(obj1) && isObject(obj2)) { | ||
const arr1 = objects.get(obj1) || []; | ||
const arr2 = objects.get(obj2) || []; | ||
arr1.push({ obj: one, key }); | ||
arr2.push({ obj: two, key }); | ||
objects.set(obj1, arr1); | ||
objects.set(obj2, arr2); | ||
} | ||
|
||
result[key] = mergeTwoObjects( | ||
result[key], | ||
twoValue, | ||
level, | ||
objects | ||
); | ||
} | ||
} | ||
} | ||
} else { | ||
result = two; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Function to check if object has been already reference | ||
* @param obj | ||
* @param key | ||
* @param objects | ||
*/ | ||
function wasObjectReferenced( | ||
obj: any, | ||
key: string, | ||
objects: WeakMap<any, ObjectInto[]>, | ||
): boolean { | ||
const arr = objects.get(obj[key]) || []; | ||
for (let i = 0, j = arr.length; i < j; i++) { | ||
const info = arr[i]; | ||
if (info.key === key && info.obj === obj) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function isArray(value: any): boolean { | ||
return Array.isArray(value); | ||
} | ||
|
||
function isFunction(value: any): boolean { | ||
return typeof value === 'function'; | ||
} | ||
|
||
function isObject(value: any): boolean { | ||
return !isPrimitive(value) && !isArray(value) && !isFunction(value) && typeof value === 'object'; | ||
} | ||
|
||
function isPrimitive(value: any): boolean { | ||
return typeof value === 'string' || | ||
typeof value === 'number' || | ||
typeof value === 'boolean' || | ||
typeof value === 'undefined' || | ||
value instanceof Date || | ||
value instanceof RegExp || | ||
value === null; | ||
} | ||
|
||
function shouldMerge(one: any, two: any): boolean { | ||
if (!isPlainObject(one) || !isPlainObject(two)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does lerna need to be in dev deps ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
he doesn't want to have to have it installed globally. The npm scripts call lerna so if you have it in dev deps they find it correctly. If not, it looks for global.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah but i meant why does it need to be commited ? We expect it to be available globally right ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you dont have lerna locally you cant compile it. Not mentioning that the lerna we are using should be compatible with the one that is in the project. This is also missing in other packages which should be added. We should not expect user to have lerna installed globally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then do we need to add it to all other packages ? I would prefer to do this in another PR