File tree Expand file tree Collapse file tree 1 file changed +20
-31
lines changed
packages/kitsu-core/src/components Expand file tree Collapse file tree 1 file changed +20
-31
lines changed Original file line number Diff line number Diff line change 1- interface Data {
2- id : string
3- type : string
4- attributes ?: {
5- [ key : string ] :
6- | string
7- | number
8- | boolean
9- | null
10- | undefined
11- | object
12- | object [ ]
13- | string [ ]
14- | number [ ]
15- | boolean [ ]
16- }
17- }
1+ import { Attributes , isAttributes } from "../resources/attributes.js" ;
2+ import { ResourceIdentifier } from "../resources/resource-identifier.js" ;
3+ import { ResourceObject } from "../resources/resource-object.js" ;
4+
5+ export type DeattributedResourceObject = ResourceIdentifier & Attributes ;
186
197// Write a function that hoists the attributes of a given object to the top level
20- export const deattribute = ( data : Data | Data [ ] ) : Data | Data [ ] => {
21- let output = data
22- if ( Array . isArray ( data ) ) output = data . map ( deattribute ) as Data [ ]
23- else if (
24- typeof data . attributes === 'object' &&
25- data . attributes !== null &&
26- ! Array . isArray ( data . attributes )
27- ) {
28- output = {
29- ...data ,
30- ...data . attributes
31- } as Data
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 ) ;
12+ }
3213
33- if ( output . attributes === data . attributes ) delete output . attributes
14+ function _deattribute ( data : ResourceObject ) : DeattributedResourceObject {
15+ const output = {
16+ ...data ,
17+ ...data . attributes
3418 }
3519
36- return output
20+ if ( output . attributes === data . attributes ) delete output . attributes
21+ return output ;
22+ }
23+
24+ function isResourceObjectArray ( obj : ResourceObject | ResourceObject [ ] ) : obj is ResourceObject [ ] {
25+ return Array . isArray ( obj )
3726}
You can’t perform that action at this time.
0 commit comments