* Shallow clone an object * * @param object - The object to be cloned. * @returns A new object that copies the original.
(object: T)
| 175 | * @returns A new object that copies the original. |
| 176 | */ |
| 177 | function clone<T extends Record<string, any>>(object: T): T { |
| 178 | const newObject = create(null); |
| 179 | |
| 180 | for (const [property, value] of entries(object)) { |
| 181 | const isPropertyExist = objectHasOwnProperty(object, property); |
| 182 | |
| 183 | if (isPropertyExist) { |
| 184 | if (arrayIsArray(value)) { |
| 185 | newObject[property] = cleanArray(value); |
| 186 | } else if ( |
| 187 | value && |
| 188 | typeof value === 'object' && |
| 189 | value.constructor === Object |
| 190 | ) { |
| 191 | newObject[property] = clone(value); |
| 192 | } else { |
| 193 | newObject[property] = value; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | return newObject; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Copy a flat lookup table |
no test coverage detected