@aplus-frontend/utils
Version:
Utils for Aplus frontend team.
49 lines (48 loc) • 1.3 kB
JavaScript
import { isArray, isObject } from "@fruits-chain/utils";
import { mergeWith, intersectionWith, isEqual, unionWith } from "lodash-es";
function upperFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function wait(ms) {
let timerId;
return new Promise((resolve) => {
timerId = setTimeout(() => {
resolve(timerId);
}, ms);
});
}
function deepMerge(source, target, mergeArrays = "replace") {
if (!target) {
return source;
}
if (!source) {
return target;
}
return mergeWith({}, source, target, (sourceValue, targetValue) => {
if (isArray(targetValue) && isArray(sourceValue)) {
switch (mergeArrays) {
case "union":
return unionWith(sourceValue, targetValue, isEqual);
case "intersection":
return intersectionWith(sourceValue, targetValue, isEqual);
case "concat":
return sourceValue.concat(targetValue);
case "replace":
return targetValue;
default:
throw new Error(
`Unknown merge array strategy: ${mergeArrays}`
);
}
}
if (isObject(targetValue) && isObject(sourceValue)) {
return deepMerge(sourceValue, targetValue, mergeArrays);
}
return void 0;
});
}
export {
deepMerge,
upperFirst,
wait
};