JSFiddle - React, Tailwind, and code Playground
by IceCreamYou
TypeScript
class Utils {
static deepExtend<T extends object, U>(target: T, ...sources: Array<U>): T & U {
if (typeof target === 'undefined' || target === null) {
throw new TypeError('Cannot convert undefined or null to object');
}
// This is an adaptation of the Object.assign polyfill from MDN:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
const output = Object(target),
sourceCount = sources.length,
hop = Object.prototype.hasOwnProperty;
for (let i = 0; i < sourceCount; i++) {
const source = sources[i];
if (typeof source !== 'undefined' && source !== null) {
for (const key in source) {
if (hop.call(source, key)) {
const sourceKey = source[key],
outputKey = output[key];
if (Array.isArray(sourceKey) && Array.isArray(outputKey)) {
for (let j = 0; j < sourceKey.length; j++) {
if (typeof outputKey[j] === 'object' && !(outputKey[j] instanceof Date) && !(outputKey[j] instanceof RegExp) && !(outputKey[j] instanceof Function)) {
output[key][j] = Utils.deepExtend(outputKey[j], sourceKey[j]);
}
else {
output[key][j] = sourceKey[j];
}
}
}
else if (typeof sourceKey === 'object' && !(sourceKey instanceof Date) && !(sourceKey instanceof RegExp) && !(sourceKey instanceof Function) &&
typeof outputKey === 'object' && !(outputKey instanceof Date) && !(outputKey instanceof RegExp) && !(outputKey instanceof Function)
) {
output[key] =...