|
| 1 | +/** |
| 2 | + * Checks if a value is a plain object. |
| 3 | + * |
| 4 | + * @param {*} obj - The value to check. |
| 5 | + * @returns {boolean} True if the value is a plain object, false otherwise. |
| 6 | + */ |
| 7 | +const isObject = obj => { |
| 8 | + if (typeof obj !== 'object' || obj === null) { |
| 9 | + return false |
| 10 | + } |
| 11 | + |
| 12 | + // Objects with null prototype are considered plain objects (jQuery compatible) |
| 13 | + if (Object.getPrototypeOf(obj) === null) { |
| 14 | + return true |
| 15 | + } |
| 16 | + |
| 17 | + let proto = obj |
| 18 | + |
| 19 | + while (Object.getPrototypeOf(proto) !== null) { |
| 20 | + proto = Object.getPrototypeOf(proto) |
| 21 | + } |
| 22 | + |
| 23 | + return Object.getPrototypeOf(obj) === proto |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Merges the contents of two or more objects together into the first object. |
| 28 | + * This is a re-implementation of jQuery's extend function. |
| 29 | + * Based on: https://github.com/jquery/jquery/blob/3.6.2/src/core.js#L132 |
| 30 | + * |
| 31 | + * @param {boolean|Object} [deep=false] - If true, the merge becomes recursive (deep copy). |
| 32 | + * @param {Object} target - The object to extend. |
| 33 | + * @param {...Object} objects - The objects to merge into the target. |
| 34 | + * @returns {Object} The extended target object. |
| 35 | + */ |
| 36 | +const extend = (...args) => { |
| 37 | + let target = args[0] || {} |
| 38 | + let i = 1 |
| 39 | + let deep = false |
| 40 | + let clone |
| 41 | + |
| 42 | + // Handle a deep copy situation |
| 43 | + if (typeof target === 'boolean') { |
| 44 | + deep = target |
| 45 | + |
| 46 | + // Skip the boolean and the target |
| 47 | + target = args[i] || {} |
| 48 | + i++ |
| 49 | + } |
| 50 | + |
| 51 | + // Handle case when target is a string or something (possible in deep copy) |
| 52 | + if (typeof target !== 'object' && typeof target !== 'function') { |
| 53 | + target = {} |
| 54 | + } |
| 55 | + |
| 56 | + for (; i < args.length; i++) { |
| 57 | + const options = args[i] |
| 58 | + |
| 59 | + // Ignore undefined/null values |
| 60 | + if (typeof options === 'undefined' || options === null) { |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + // Extend the base object |
| 65 | + // eslint-disable-next-line guard-for-in |
| 66 | + for (const name in options) { |
| 67 | + const copy = options[name] |
| 68 | + |
| 69 | + // Prevent Object.prototype pollution |
| 70 | + // Prevent never-ending loop |
| 71 | + if (name === '__proto__' || target === copy) { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + const copyIsArray = Array.isArray(copy) |
| 76 | + |
| 77 | + // Recurse if we're merging plain objects or arrays |
| 78 | + if (deep && copy && (isObject(copy) || copyIsArray)) { |
| 79 | + const src = target[name] |
| 80 | + |
| 81 | + if (copyIsArray && Array.isArray(src)) { |
| 82 | + if (src.every(it => !isObject(it) && !Array.isArray(it))) { |
| 83 | + target[name] = copy.slice() |
| 84 | + continue |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if (copyIsArray && !Array.isArray(src)) { |
| 89 | + clone = [] |
| 90 | + } else if (!copyIsArray && !isObject(src)) { |
| 91 | + clone = {} |
| 92 | + } else { |
| 93 | + clone = src |
| 94 | + } |
| 95 | + |
| 96 | + // Never move original objects, clone them |
| 97 | + target[name] = extend(deep, clone, copy) |
| 98 | + |
| 99 | + // Don't bring in undefined values |
| 100 | + } else if (copy !== undefined) { |
| 101 | + target[name] = copy |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return target |
| 107 | +} |
| 108 | + |
1 | 109 | const compareObjects = (objectA, objectB, compareLength) => { |
2 | 110 | const aKeys = Object.keys(objectA) |
3 | 111 | const bKeys = Object.keys(objectB) |
@@ -178,6 +286,7 @@ const toRaw = proxy => { |
178 | 286 |
|
179 | 287 | export { |
180 | 288 | compareObjects, |
| 289 | + extend, |
181 | 290 | findByParam, |
182 | 291 | getDocumentClickEvent, |
183 | 292 | removeDiacritics, |
|
0 commit comments