(target, source)
| 2087 | return JSON.parse(JSON.stringify(item)); |
| 2088 | } |
| 2089 | function fillMissingFields(target, source) { |
| 2090 | // 如果 target 不存在,直接返回 source 的深拷贝 |
| 2091 | if (target === null || target === undefined) { |
| 2092 | return cloneDeep(source); |
| 2093 | } |
| 2094 | // 如果类型不同,直接替换为 source |
| 2095 | if (typeof source !== typeof target) { |
| 2096 | return cloneDeep(source); |
| 2097 | } |
| 2098 | // 如果 source 是对象 |
| 2099 | if (base.isType(source) === "object" && !Array.isArray(source)) { |
| 2100 | if (typeof target !== "object" || Array.isArray(target)) { |
| 2101 | return cloneDeep(source); |
| 2102 | } |
| 2103 | let result = { ...target }; |
| 2104 | for (let key in source) { |
| 2105 | if (!source.hasOwnProperty(key)) continue; |
| 2106 | // 跳过 default 的自动合并 |
| 2107 | if (key === "default") continue; |
| 2108 | if (key === "dir" && target[key] !== undefined) continue; |
| 2109 | if (key === "token" && target[key] !== undefined) continue; |
| 2110 | if (key === "authName" && target[key] !== undefined) continue; |
| 2111 | if (key === "authPass" && target[key] !== undefined) continue; |
| 2112 | result[key] = fillMissingFields(target[key], source[key]); |
| 2113 | } |
| 2114 | return result; |
| 2115 | } |
| 2116 | // 如果 source 是数组 |
| 2117 | if (Array.isArray(source)) { |
| 2118 | if (!Array.isArray(target)) { |
| 2119 | return cloneDeep(source); |
| 2120 | } |
| 2121 | let result = [...target]; |
| 2122 | if (source.length > 0 && base.isType(source[0]) === "object" && source[0] !== null) { |
| 2123 | let template = source[0]; |
| 2124 | // 填充字段 |
| 2125 | for (let i = 0; i < result.length; i++) { |
| 2126 | if (base.isType(result[i]) === "object" && result[i] !== null) { |
| 2127 | result[i] = fillMissingFields(result[i], template); |
| 2128 | } else { |
| 2129 | result[i] = cloneDeep(template); |
| 2130 | } |
| 2131 | } |
| 2132 | // 自动补充 default: true |
| 2133 | if ( |
| 2134 | template.default === true && |
| 2135 | !result.some(item => item && item.default === true) && |
| 2136 | result.length > 0 |
| 2137 | ) { |
| 2138 | result[0].default = true; |
| 2139 | } |
| 2140 | } |
| 2141 | return result; |
| 2142 | } |
| 2143 | // 基本类型,保留原始值 |
| 2144 | return target; |
| 2145 | } |
| 2146 | defaults.forEach(({ name, value }) => { |
no test coverage detected