* Shallow clone an object * * @param object - The object to be cloned. * @returns A new object that copies the original.
(object: T)
| 154 | * @returns A new object that copies the original. |
| 155 | */ |
| 156 | function clone<T extends Record<string, any>>(object: T): T { |
| 157 | const newObject = create(null); |
| 158 | |
| 159 | for (const [property, value] of entries(object)) { |
| 160 | const isPropertyExist = objectHasOwnProperty(object, property); |
| 161 | |
| 162 | if (isPropertyExist) { |
| 163 | if (Array.isArray(value)) { |
| 164 | newObject[property] = cleanArray(value); |
| 165 | } else if ( |
| 166 | value && |
| 167 | typeof value === 'object' && |
| 168 | value.constructor === Object |
| 169 | ) { |
| 170 | newObject[property] = clone(value); |
| 171 | } else { |
| 172 | newObject[property] = value; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return newObject; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * This method automatically checks if the prop is function or getter and behaves accordingly. |
no test coverage detected