@macrof/shared
Version:
React MicroFrontend Shared, Typescript, Webpack 5, ModuleFederation
30 lines (29 loc) • 1.2 kB
JavaScript
import isPlainObject from 'lodash/isPlainObject';
import isObjectLike from 'lodash/isObjectLike';
const isFilledObject = (value) => isPlainObject(value) && Object.keys(value).length > 0;
const isEmptyObject = (value) => isPlainObject(value) && Object.keys(value).length === 0;
const plainToNew = (TypeName, value, ...args) => {
if (value instanceof TypeName || isEmptyObject(value) || (Array.isArray(value) && value.length === 0)) {
return value;
}
if (Array.isArray(value) && value[0] instanceof TypeName) {
return value.map((x) => x);
}
if (Array.isArray(value) && isPlainObject(value[0])) {
return value.map((x) => new TypeName(x, ...args));
}
if (isFilledObject(value)) {
return new TypeName(value, ...args);
}
if (Array.isArray(value) && isObjectLike(value[0])) {
return value.map((x) => new TypeName(x, ...args));
}
return undefined;
};
const includedPath = (first, second) => {
if (first && second && first.length > 1 && second.length > 1) {
return first.includes(second) || second.includes(first);
}
return false;
};
export { isFilledObject, isEmptyObject, plainToNew, includedPath };