@botonic/plugin-contentful
Version:
Botonic Plugin Contentful is one of the **[available](https://github.com/hubtype/botonic/tree/master/packages)** plugins for Botonic. **[Contentful](http://www.contentful.com)** is a CMS (Content Management System) which manages contents of a great variet
85 lines • 2.87 kB
JavaScript
// eslint-disable-next-line @typescript-eslint/ban-types
export function shallowClone(obj) {
if (obj == undefined) {
return obj;
}
//https://stackoverflow.com/a/28152032/145289 create copies the methods
const clone = Object.create(obj);
// without copying prototype, some fields (maybe the enums?) are not available in object
// TODO try this let clone = Object.assign( Object.create( Object.getPrototypeOf(orig)), orig)
// from https://stackoverflow.com/a/44782052/145289
for (const prop in Object.getPrototypeOf(clone)) {
clone[prop] = Object.getPrototypeOf(clone)[prop];
}
return clone;
}
/**
* Deep copy function for TypeScript.
* @param T Generic type of target/copied value.
* @param target Target value to be copied.
* @see Source project, ts-deepcopy https://github.com/ykdr2017/ts-deepcopy
* @see Code pen https://codepen.io/erikvullings/pen/ejyBYg
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export const deepClone = (target, alreadyCloned = []) => {
if (alreadyCloned.includes(target)) {
return target;
}
alreadyCloned.push(target);
if (target === undefined) {
return target;
}
if (target instanceof Date) {
return new Date(target.getTime());
}
if (target instanceof Array) {
const cp = [];
target.forEach(v => {
cp.push(v);
});
return cp.map((n) => deepClone(n, alreadyCloned));
}
if (typeof target === 'object' && Object.keys(target).length !== 0) {
const cp = Object.assign({}, target);
Object.keys(cp).forEach(k => {
cp[k] = deepClone(cp[k], alreadyCloned);
});
return cp;
}
return target;
};
/**
* It returns a ROUGH estimation, since V8 will greatly optimize anyway
* @return the number of bytes
* Not using https://www.npmjs.com/package/object-sizeof to minimize dependencies
*/
export function roughSizeOfObject(object) {
const objectList = [];
const recurse = function (value) {
let bytes = 0;
if (typeof value === 'boolean') {
bytes = 4;
}
else if (typeof value === 'string') {
bytes = value.length * 2;
}
else if (typeof value === 'number') {
bytes = 8;
}
else if (value == null) {
// Required because typeof null == 'object'
bytes = 0;
}
else if (typeof value === 'object' && objectList.indexOf(value) === -1) {
objectList.push(value);
for (const [k, v] of Object.entries(value)) {
bytes += 8; // an assumed existence overhead
bytes += recurse(k);
bytes += recurse(v);
}
}
return bytes;
};
return recurse(object);
}
//# sourceMappingURL=objects.js.map