(wat: unknown)
| 256 | * @returns A version of `wat` which can safely be used with `Object` class methods |
| 257 | */ |
| 258 | export function objectify(wat: unknown): typeof Object { |
| 259 | let objectified; |
| 260 | switch (true) { |
| 261 | // this will catch both undefined and null |
| 262 | case wat == undefined: |
| 263 | objectified = new String(wat); |
| 264 | break; |
| 265 | |
| 266 | // Though symbols and bigints do have wrapper classes (`Symbol` and `BigInt`, respectively), for whatever reason |
| 267 | // those classes don't have constructors which can be used with the `new` keyword. We therefore need to cast each as |
| 268 | // an object in order to wrap it. |
| 269 | case typeof wat === 'symbol' || typeof wat === 'bigint': |
| 270 | objectified = Object(wat); |
| 271 | break; |
| 272 | |
| 273 | // this will catch the remaining primitives: `String`, `Number`, and `Boolean` |
| 274 | case isPrimitive(wat): |
| 275 | // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access |
| 276 | objectified = new (wat as any).constructor(wat); |
| 277 | break; |
| 278 | |
| 279 | // by process of elimination, at this point we know that `wat` must already be an object |
| 280 | default: |
| 281 | objectified = wat; |
| 282 | break; |
| 283 | } |
| 284 | return objectified; |
| 285 | } |
no test coverage detected