( json: dict, toKey: string | undefined, allowPrimitive: allowPrimitive )
| 338 | } |
| 339 | |
| 340 | const possiblyCollapse = <allowPrimitive extends boolean>( |
| 341 | json: dict, |
| 342 | toKey: string | undefined, |
| 343 | allowPrimitive: allowPrimitive |
| 344 | ): [allowPrimitive] extends [false] ? dict : unknown => { |
| 345 | const collapsibleKeys = Object.keys(json) |
| 346 | if (collapsibleKeys.length === 1 && collapsibleKeys[0] === toKey) { |
| 347 | const collapsed = json[toKey] |
| 348 | |
| 349 | if (allowPrimitive) return collapsed as never |
| 350 | |
| 351 | if ( |
| 352 | // if the collapsed value is still an object |
| 353 | hasDomain(collapsed, "object") && |
| 354 | // and the JSON did not include any implied keys |
| 355 | (Object.keys(collapsed).length === 1 || Array.isArray(collapsed)) |
| 356 | ) { |
| 357 | // we can replace it with its collapsed value |
| 358 | return collapsed as never |
| 359 | } |
| 360 | } |
| 361 | return json as never |
| 362 | } |
no test coverage detected
searching dependent graphs…