Last active
September 29, 2023 20:28
-
-
Save cfv1984/6319681685f78333d98a to your computer and use it in GitHub Desktop.
jquery extend, minus jQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var extend = function(){ | |
function isFunction(fn){ return typeof(fn) === "function" && fn.constructor === Function } | |
function isArray(ar){ return ar instanceof Array } | |
function isPlainObject(obj){ return typeof obj == 'object' && obj.constructor == Object } | |
var options, name, src, copy, copyIsArray, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false; | |
if(typeof target === "boolean"){ | |
deep = target; | |
target = arguments[i] || {}; | |
i++; | |
} | |
if(typeof target !== "object" && !isFunction(target)) target = {}; | |
if(i === length){ | |
target = this; | |
i--; | |
} | |
for(; i < length; i++){ | |
if((options = arguments[i]) != null){ | |
for(name in options){ | |
src = target[name]; | |
copy = options[name]; | |
if(target === copy) continue; | |
if(deep && copy &&(isPlainObject(copy) ||(copyIsArray = isArray(copy)))){ | |
if(!copyIsArray){ | |
copyIsArray = false; | |
clone = src && isArray(src) ? src : []; | |
} | |
else clone = src && isPlainObject(src) ? src : {}; | |
target[name] = extend(deep, clone, copy); | |
} | |
else if(copy !== undefined) target[name] = copy; | |
} | |
} | |
} | |
return target; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Extendable = boolean | Array<unknown> | Record<string, unknown>; | |
type Extendables = Array<Extendable>; | |
function extend(..._: Extendables) { | |
function isFunction(fn) { | |
return typeof fn === "function" && fn.constructor === Function; | |
} | |
function isArray(ar) { | |
return ar instanceof Array; | |
} | |
function isPlainObject(obj) { | |
return typeof obj == "object" && obj.constructor == Object; | |
} | |
let options: unknown; | |
let src: unknown; | |
let copy: unknown; | |
let copyIsArray = false; | |
let clone: unknown; | |
let target = arguments[0] || {}; | |
let i = 1; | |
let deep = false; | |
const length = arguments.length; | |
if (typeof target === "boolean") { | |
deep = target; | |
target = arguments[i] || {}; | |
i++; | |
} | |
if (typeof target !== "object" && !isFunction(target)) target = {}; | |
if (i === length) { | |
target = this; | |
i--; | |
} | |
for (; i < length; i++) { | |
if ((options = arguments[i]) != null) { | |
for (const name in options) { | |
src = target[name]; | |
copy = options[name]; | |
if (target === copy) continue; | |
if ( | |
deep && | |
copy && | |
(isPlainObject(copy) || (copyIsArray = isArray(copy))) | |
) { | |
if (!copyIsArray) { | |
copyIsArray = false; | |
clone = src && isArray(src) ? src : []; | |
} else clone = src && isPlainObject(src) ? src : {}; | |
target[name] = extend(deep, clone as Extendable, copy as Extendable); | |
} else if (copy !== undefined) target[name] = copy; | |
} | |
} | |
} | |
return target; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment