|
1 | | -import { Attributes, isAttributes } from "../resources/attributes.js"; |
2 | | -import { ResourceIdentifier } from "../resources/resource-identifier.js"; |
3 | | -import { ResourceObject } from "../resources/resource-object.js"; |
| 1 | +import { Attributes, isAttributes } from '../resources/attributes.js' |
| 2 | +import { ResourceIdentifier } from '../resources/resourceIdentifier.js' |
| 3 | +import { ResourceObject } from '../resources/resourceObject.js' |
4 | 4 |
|
5 | | -export type DeattributedResourceObject = ResourceIdentifier & Attributes; |
| 5 | +export type DeattributedResourceObject = ResourceIdentifier & Attributes |
6 | 6 |
|
7 | 7 | // Write a function that hoists the attributes of a given object to the top level |
8 | | -export function deattribute(data: ResourceObject): DeattributedResourceObject; |
9 | | -export function deattribute(data: ResourceObject[]): DeattributedResourceObject[]; |
10 | | -export function deattribute(data: ResourceObject | ResourceObject[]): DeattributedResourceObject | DeattributedResourceObject[] { |
11 | | - return isResourceObjectArray(data) ? data.map(_deattribute) : _deattribute(data); |
| 8 | +export function deattribute(data: ResourceObject): DeattributedResourceObject |
| 9 | +export function deattribute( |
| 10 | + data: ResourceObject[] |
| 11 | +): DeattributedResourceObject[] |
| 12 | +export function deattribute( |
| 13 | + data: ResourceObject | ResourceObject[] |
| 14 | +): DeattributedResourceObject | DeattributedResourceObject[] { |
| 15 | + return isResourceObjectArray(data) |
| 16 | + ? data.map(_deattribute) |
| 17 | + : _deattribute(data) |
12 | 18 | } |
13 | 19 |
|
14 | 20 | function _deattribute(data: ResourceObject): DeattributedResourceObject { |
| 21 | + // FIXME: what is the best behaviour when given an invalid attributes key? |
| 22 | + // 1. (Current) the same invalid object is returned. |
| 23 | + // a. This results in deattribute returning potentially invalid DeattributedResourceObjects |
| 24 | + // 2. the object is modified, and has the invalid key removed |
| 25 | + // a. This would guarantee valid returns, but will also change the current default behaviour. |
| 26 | + // 3. the object is not touched, and an error is thrown |
| 27 | + // a. this would function closer to how JSON.parse does, throwing errors when unexpected input is given |
| 28 | + // |
| 29 | + // This should not be an issue for projects using typescript natively, since the compiler will warn when passing |
| 30 | + // objects with mismatched types to deattribute |
| 31 | + if (!isAttributes(data.attributes)) return data as DeattributedResourceObject |
| 32 | + |
15 | 33 | const output = { |
16 | 34 | ...data, |
17 | 35 | ...data.attributes |
18 | 36 | } |
19 | 37 |
|
20 | 38 | if (output.attributes === data.attributes) delete output.attributes |
21 | | - return output; |
| 39 | + return output |
22 | 40 | } |
23 | 41 |
|
24 | | -function isResourceObjectArray(obj: ResourceObject | ResourceObject[]): obj is ResourceObject[] { |
25 | | - return Array.isArray(obj) |
| 42 | +function isResourceObjectArray( |
| 43 | + object: ResourceObject | ResourceObject[] |
| 44 | +): object is ResourceObject[] { |
| 45 | + return Array.isArray(object) |
26 | 46 | } |
0 commit comments