|
| 1 | +import isOptionObject from 'is-plain-obj' |
| 2 | + |
| 3 | +const { hasOwnProperty } = Object.prototype |
| 4 | +const { propertyIsEnumerable } = Object |
| 5 | +/** |
| 6 | + * @param {*} object |
| 7 | + * @param {string | symbol | number} name |
| 8 | + * @param {any} value |
| 9 | + */ |
| 10 | +const defineProperty = (object, name, value) => Object.defineProperty(object, name, { |
| 11 | + value, |
| 12 | + writable: true, |
| 13 | + enumerable: true, |
| 14 | + configurable: true |
| 15 | +}) |
| 16 | + |
| 17 | +const globalThis = this |
| 18 | +const defaultMergeOptions = { |
| 19 | + concatArrays: false, |
| 20 | + ignoreUndefined: false |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {*} value |
| 25 | + * @returns {Array<string | symbol>} |
| 26 | + */ |
| 27 | +const getEnumerableOwnPropertyKeys = value => { |
| 28 | + /** @type {Array<string | symbol>} */ |
| 29 | + const keys = [] |
| 30 | + |
| 31 | + for (const key in value) { |
| 32 | + if (hasOwnProperty.call(value, key)) { |
| 33 | + keys.push(key) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /* istanbul ignore else */ |
| 38 | + if (Object.getOwnPropertySymbols) { |
| 39 | + const symbols = Object.getOwnPropertySymbols(value) |
| 40 | + |
| 41 | + for (const symbol of symbols) { |
| 42 | + if (propertyIsEnumerable.call(value, symbol)) { |
| 43 | + keys.push(symbol) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return keys |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * @param {*} value |
| 53 | + */ |
| 54 | +function clone (value) { |
| 55 | + if (Array.isArray(value)) { |
| 56 | + return cloneArray(value) |
| 57 | + } |
| 58 | + |
| 59 | + if (isOptionObject(value)) { |
| 60 | + return cloneOptionObject(value) |
| 61 | + } |
| 62 | + |
| 63 | + return value |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * @param {*} array |
| 68 | + */ |
| 69 | +function cloneArray (array) { |
| 70 | + const result = array.slice(0, 0) |
| 71 | + |
| 72 | + getEnumerableOwnPropertyKeys(array).forEach(key => { |
| 73 | + defineProperty(result, key, clone(array[key])) |
| 74 | + }) |
| 75 | + |
| 76 | + return result |
| 77 | +} |
| 78 | + |
| 79 | +/** |
| 80 | + * @param {*} object |
| 81 | + */ |
| 82 | +function cloneOptionObject (object) { |
| 83 | + const result = Object.getPrototypeOf(object) === null ? Object.create(null) : {} |
| 84 | + |
| 85 | + getEnumerableOwnPropertyKeys(object).forEach(key => { |
| 86 | + defineProperty(result, key, clone(object[key])) |
| 87 | + }) |
| 88 | + |
| 89 | + return result |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * @param {*} merged - already cloned |
| 94 | + * @param {*} source - something to merge |
| 95 | + * @param {Array<string | symbol>} keys - keys to merge |
| 96 | + * @param {object} config - Config Object |
| 97 | + * @param {boolean} [config.ignoreUndefined] - whether to ignore undefined values |
| 98 | + * @param {boolean} [config.concatArrays] - Config Object |
| 99 | + * @returns {*} cloned Object |
| 100 | + */ |
| 101 | +const mergeKeys = (merged, source, keys, config) => { |
| 102 | + keys.forEach(key => { |
| 103 | + if (typeof source[key] === 'undefined' && config.ignoreUndefined) { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + // Do not recurse into prototype chain of merged |
| 108 | + if (key in merged && merged[key] !== Object.getPrototypeOf(merged)) { |
| 109 | + defineProperty(merged, key, merge(merged[key], source[key], config)) |
| 110 | + } else { |
| 111 | + defineProperty(merged, key, clone(source[key])) |
| 112 | + } |
| 113 | + }) |
| 114 | + |
| 115 | + return merged |
| 116 | +} |
| 117 | + |
| 118 | +/** |
| 119 | + * @param {*} merged - already cloned |
| 120 | + * @param {*} source - something to merge |
| 121 | + * @param {object} config - Config Object |
| 122 | + * @returns {*} cloned Object |
| 123 | + * |
| 124 | + * see [Array.prototype.concat ( ...arguments )](http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.concat) |
| 125 | + */ |
| 126 | +const concatArrays = (merged, source, config) => { |
| 127 | + let result = merged.slice(0, 0) |
| 128 | + let resultIndex = 0; |
| 129 | + |
| 130 | + [merged, source].forEach(array => { |
| 131 | + /** @type {Array<string | symbol>} */ |
| 132 | + const indices = [] |
| 133 | + |
| 134 | + // `result.concat(array)` with cloning |
| 135 | + for (let k = 0; k < array.length; k++) { |
| 136 | + if (!hasOwnProperty.call(array, k)) { |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + indices.push(String(k)) |
| 141 | + |
| 142 | + if (array === merged) { |
| 143 | + // Already cloned |
| 144 | + defineProperty(result, resultIndex++, array[k]) |
| 145 | + } else { |
| 146 | + defineProperty(result, resultIndex++, clone(array[k])) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // Merge non-index keys |
| 151 | + result = mergeKeys(result, array, getEnumerableOwnPropertyKeys(array).filter(key => !indices.includes(key)), config) |
| 152 | + }) |
| 153 | + |
| 154 | + return result |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * @param {*} merged - already cloned |
| 159 | + * @param {*} source - something to merge |
| 160 | + * @param {object} config - Config Object |
| 161 | + * @param {boolean} [config.concatArrays] - Config Object |
| 162 | + * @returns {*} cloned Object |
| 163 | + */ |
| 164 | +function merge (merged, source, config) { |
| 165 | + if (config.concatArrays && Array.isArray(merged) && Array.isArray(source)) { |
| 166 | + return concatArrays(merged, source, config) |
| 167 | + } |
| 168 | + |
| 169 | + if (!isOptionObject(source) || !isOptionObject(merged)) { |
| 170 | + return clone(source) |
| 171 | + } |
| 172 | + |
| 173 | + return mergeKeys(merged, source, getEnumerableOwnPropertyKeys(source), config) |
| 174 | +} |
| 175 | + |
| 176 | +/** |
| 177 | + * @param {...any} options |
| 178 | + */ |
| 179 | +function mergeOptions (...options) { |
| 180 | + // @ts-expect-error this is shadowed by the container |
| 181 | + const config = merge(clone(defaultMergeOptions), (this !== globalThis && this) || {}, defaultMergeOptions) |
| 182 | + let merged = { _: {} } |
| 183 | + |
| 184 | + for (const option of options) { |
| 185 | + if (option === undefined) { |
| 186 | + continue |
| 187 | + } |
| 188 | + |
| 189 | + if (!isOptionObject(option)) { |
| 190 | + throw new TypeError('`' + option + '` is not an Option Object') |
| 191 | + } |
| 192 | + |
| 193 | + merged = merge(merged, { _: option }, config) |
| 194 | + } |
| 195 | + |
| 196 | + return merged._ |
| 197 | +} |
| 198 | + |
| 199 | +export default mergeOptions |
0 commit comments