Skip to content
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

perf(utils): extract lodash.merge #45

Merged
merged 6 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ pnpm-debug.log*
*.sw?

# Coverage directory used by tools like istanbul
coverage
coverage
package-lock.json
5 changes: 5 additions & 0 deletions src/core/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ export const isString = (val: unknown): val is string =>
toTypeString(val) === '[object String]';
export const isPlainObject = (val: unknown): val is Record<string, any> =>
toTypeString(val) === '[object Object]';
export const isPlainArray = (val: unknown): val is Record<string, any> =>
toTypeString(val) === '[object Array]';

export const isObject = (val: unknown): val is Record<any, any> =>
val !== null && typeof val === 'object';

export const isSameType = (a: unknown, b: unknown): boolean =>
toTypeString(a) === toTypeString(b);

export const isPromise = (fn: unknown): fn is Promise<unknown> =>
isObject(fn) && isFunction(fn.then) && isFunction(fn.catch);

Expand Down
44 changes: 44 additions & 0 deletions src/core/utils/lodash/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { isPlainObject, isPlainArray, isObject, isSameType } from '../index';

const mergeArr = (oriArr: any[], target: any[]) => {
for (let i = 0; i < target.length; i++) {
if (
!isObject(target[i]) ||
!isSameType(oriArr[i], target[i]) ||
(!oriArr[i] && target[i])
) {
if (isObject(oriArr[i]) && isObject(target[i])) return;
oriArr[i] = target[i];
}
isPlainObject(target[i]) && mergeObj(oriArr[i], target[i]);
isPlainArray(target[i]) && mergeArr(oriArr[i], target[i]);
}
};

const mergeObj = (oriObj: object, target: object) => {
for (const key in target) {
if (
!isObject(target[key]) ||
!isSameType(oriObj[key], target[key]) ||
!(key in oriObj)
) {
if (isObject(oriObj[key]) && isObject(target[key])) return;
oriObj[key] = target[key];
}
isPlainObject(target[key]) && mergeObj(oriObj[key], target[key]);
isPlainArray(target[key]) && mergeArr(oriObj[key], target[key]);
}
};

function merge(origin: object, ...other: object[]) {
const result = Object.assign({}, origin);
if (!other.length) return result;

for (let i = 0; i < other.length; i++) {
mergeObj(result, other[i]);
}

return result;
}

export default merge;