@udt/parser-utils 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +18 -14
- package/coverage/clover.xml +56 -50
- package/coverage/coverage-final.json +3 -3
- package/coverage/extractProperties.ts.html +8 -8
- package/coverage/index.html +22 -22
- package/coverage/index.ts.html +17 -11
- package/coverage/isJsonObject.ts.html +1 -1
- package/coverage/parseData.ts.html +74 -47
- package/dist/extractProperties.d.ts +4 -4
- package/dist/extractProperties.js +3 -3
- package/dist/index.js +2 -0
- package/dist/parseData.d.ts +23 -23
- package/dist/parseData.js +19 -12
- package/package.json +1 -1
- package/src/extractProperties.test.ts +2 -2
- package/src/extractProperties.ts +7 -7
- package/src/index.ts +2 -0
- package/src/parseData.test.ts +33 -36
- package/src/parseData.ts +50 -41
|
@@ -25,14 +25,14 @@
|
|
|
25
25
|
<div class='fl pad1y space-right2'>
|
|
26
26
|
<span class="strong">100% </span>
|
|
27
27
|
<span class="quiet">Statements</span>
|
|
28
|
-
<span class='fraction'>
|
|
28
|
+
<span class='fraction'>77/77</span>
|
|
29
29
|
</div>
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
<div class='fl pad1y space-right2'>
|
|
33
33
|
<span class="strong">100% </span>
|
|
34
34
|
<span class="quiet">Branches</span>
|
|
35
|
-
<span class='fraction'>
|
|
35
|
+
<span class='fraction'>18/18</span>
|
|
36
36
|
</div>
|
|
37
37
|
|
|
38
38
|
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
<div class='fl pad1y space-right2'>
|
|
47
47
|
<span class="strong">100% </span>
|
|
48
48
|
<span class="quiet">Lines</span>
|
|
49
|
-
<span class='fraction'>
|
|
49
|
+
<span class='fraction'>77/77</span>
|
|
50
50
|
</div>
|
|
51
51
|
|
|
52
52
|
|
|
@@ -334,7 +334,17 @@
|
|
|
334
334
|
<a name='L269'></a><a href='#L269'>269</a>
|
|
335
335
|
<a name='L270'></a><a href='#L270'>270</a>
|
|
336
336
|
<a name='L271'></a><a href='#L271'>271</a>
|
|
337
|
-
<a name='L272'></a><a href='#L272'>272</a
|
|
337
|
+
<a name='L272'></a><a href='#L272'>272</a>
|
|
338
|
+
<a name='L273'></a><a href='#L273'>273</a>
|
|
339
|
+
<a name='L274'></a><a href='#L274'>274</a>
|
|
340
|
+
<a name='L275'></a><a href='#L275'>275</a>
|
|
341
|
+
<a name='L276'></a><a href='#L276'>276</a>
|
|
342
|
+
<a name='L277'></a><a href='#L277'>277</a>
|
|
343
|
+
<a name='L278'></a><a href='#L278'>278</a>
|
|
344
|
+
<a name='L279'></a><a href='#L279'>279</a>
|
|
345
|
+
<a name='L280'></a><a href='#L280'>280</a>
|
|
346
|
+
<a name='L281'></a><a href='#L281'>281</a></td><td class="line-coverage quiet"><span class="cline-any cline-yes">1x</span>
|
|
347
|
+
<span class="cline-any cline-yes">1x</span>
|
|
338
348
|
<span class="cline-any cline-neutral"> </span>
|
|
339
349
|
<span class="cline-any cline-neutral"> </span>
|
|
340
350
|
<span class="cline-any cline-neutral"> </span>
|
|
@@ -540,6 +550,7 @@
|
|
|
540
550
|
<span class="cline-any cline-yes">69x</span>
|
|
541
551
|
<span class="cline-any cline-yes">69x</span>
|
|
542
552
|
<span class="cline-any cline-yes">69x</span>
|
|
553
|
+
<span class="cline-any cline-yes">69x</span>
|
|
543
554
|
<span class="cline-any cline-neutral"> </span>
|
|
544
555
|
<span class="cline-any cline-yes">69x</span>
|
|
545
556
|
<span class="cline-any cline-yes">72x</span>
|
|
@@ -554,13 +565,20 @@
|
|
|
554
565
|
<span class="cline-any cline-yes">34x</span>
|
|
555
566
|
<span class="cline-any cline-yes">34x</span>
|
|
556
567
|
<span class="cline-any cline-yes">34x</span>
|
|
568
|
+
<span class="cline-any cline-neutral"> </span>
|
|
569
|
+
<span class="cline-any cline-yes">34x</span>
|
|
570
|
+
<span class="cline-any cline-yes">34x</span>
|
|
557
571
|
<span class="cline-any cline-yes">34x</span>
|
|
558
572
|
<span class="cline-any cline-yes">34x</span>
|
|
559
573
|
<span class="cline-any cline-yes">34x</span>
|
|
560
|
-
<span class="cline-any cline-neutral"> </span>
|
|
561
574
|
<span class="cline-any cline-yes">34x</span>
|
|
562
575
|
<span class="cline-any cline-neutral"> </span>
|
|
563
576
|
<span class="cline-any cline-yes">34x</span>
|
|
577
|
+
<span class="cline-any cline-yes">34x</span>
|
|
578
|
+
<span class="cline-any cline-yes">6x</span>
|
|
579
|
+
<span class="cline-any cline-yes">2x</span>
|
|
580
|
+
<span class="cline-any cline-yes">2x</span>
|
|
581
|
+
<span class="cline-any cline-yes">34x</span>
|
|
564
582
|
<span class="cline-any cline-yes">2x</span>
|
|
565
583
|
<span class="cline-any cline-yes">2x</span>
|
|
566
584
|
<span class="cline-any cline-neutral"> </span>
|
|
@@ -646,12 +664,14 @@ export type ParseDesignTokenDataFn<ParsedDesignToken, T> = (
|
|
|
646
664
|
|
|
647
665
|
/**
|
|
648
666
|
* A function that adds a parsed group or design token
|
|
649
|
-
* as a child of
|
|
667
|
+
* as a child of the given parsed group.
|
|
650
668
|
*
|
|
669
|
+
* @param parent The parent group to add a child to
|
|
651
670
|
* @param name The name of the child group or design token
|
|
652
|
-
* @param child The group or
|
|
671
|
+
* @param child The group or design token to add
|
|
653
672
|
*/
|
|
654
673
|
export type AddChildFn<ParsedGroup, ParsedDesignToken> = (
|
|
674
|
+
parent: ParsedGroup,
|
|
655
675
|
name: string,
|
|
656
676
|
child: ParsedGroup | ParsedDesignToken
|
|
657
677
|
) => void;
|
|
@@ -659,25 +679,15 @@ export type AddChildFn<ParsedGroup, ParsedDesignToken> = (
|
|
|
659
679
|
/**
|
|
660
680
|
* The return value of a `ParseGroupDataFn`.
|
|
661
681
|
*/
|
|
662
|
-
export interface ParseGroupResult<ParsedGroup,
|
|
682
|
+
export interface ParseGroupResult<ParsedGroup, T> {
|
|
663
683
|
/**
|
|
664
684
|
* The parsed representation of the group.
|
|
665
685
|
*
|
|
666
|
-
* May be
|
|
667
|
-
*
|
|
668
|
-
*
|
|
686
|
+
* May be omitted if there is no useful result to
|
|
687
|
+
* return and we only need to pass along context
|
|
688
|
+
* data.
|
|
669
689
|
*/
|
|
670
|
-
group
|
|
671
|
-
|
|
672
|
-
/**
|
|
673
|
-
* Optional function that will add other parsed groups
|
|
674
|
-
* or design tokens as children of this parsed group.
|
|
675
|
-
*
|
|
676
|
-
* Intended for cases where the parsed representation
|
|
677
|
-
* of a group needs to contain its children. If not
|
|
678
|
-
* needed, this property can be omitted.
|
|
679
|
-
*/
|
|
680
|
-
addChild?: AddChildFn<ParsedGroup, ParsedDesignToken>;
|
|
690
|
+
group?: ParsedGroup;
|
|
681
691
|
|
|
682
692
|
/**
|
|
683
693
|
* Optional context data to be passed into the
|
|
@@ -703,15 +713,14 @@ export interface ParseGroupResult<ParsedGroup, ParsedDesignToken, T> {
|
|
|
703
713
|
* parsed the group containing this group.
|
|
704
714
|
*
|
|
705
715
|
* @returns The parsed representation of the group and,
|
|
706
|
-
* optionally,
|
|
707
|
-
*
|
|
708
|
-
* pass down when child data is parsed.
|
|
716
|
+
* optionally, some context data to pass down
|
|
717
|
+
* when child data is parsed.
|
|
709
718
|
*/
|
|
710
|
-
export type ParseGroupDataFn<ParsedGroup,
|
|
719
|
+
export type ParseGroupDataFn<ParsedGroup, T> = (
|
|
711
720
|
data: PlainObject,
|
|
712
721
|
path: string[],
|
|
713
722
|
contextFromParent?: T
|
|
714
|
-
) => ParseGroupResult<ParsedGroup,
|
|
723
|
+
) => ParseGroupResult<ParsedGroup, T>;
|
|
715
724
|
|
|
716
725
|
export interface ParserConfig<ParsedDesignToken, ParsedGroup, T> {
|
|
717
726
|
/**
|
|
@@ -739,7 +748,7 @@ export interface ParserConfig<ParsedDesignToken, ParsedGroup, T> {
|
|
|
739
748
|
* path, and should parse that data into whatever structure
|
|
740
749
|
* is desired.
|
|
741
750
|
*/
|
|
742
|
-
parseGroupData
|
|
751
|
+
parseGroupData?: ParseGroupDataFn<ParsedGroup, T>;
|
|
743
752
|
|
|
744
753
|
/**
|
|
745
754
|
* Function which is called for each design token
|
|
@@ -750,6 +759,16 @@ export interface ParserConfig<ParsedDesignToken, ParsedGroup, T> {
|
|
|
750
759
|
* desired.
|
|
751
760
|
*/
|
|
752
761
|
parseDesignTokenData: ParseDesignTokenDataFn<ParsedDesignToken, T>;
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Optional function that will add parsed groups
|
|
765
|
+
* or design tokens as children of another parsed group.
|
|
766
|
+
*
|
|
767
|
+
* Intended for cases where the parsed representation
|
|
768
|
+
* of a group needs to contain its children. If not
|
|
769
|
+
* needed, this property can be omitted.
|
|
770
|
+
*/
|
|
771
|
+
addChildToGroup?: AddChildFn<ParsedGroup, ParsedDesignToken>;
|
|
753
772
|
}
|
|
754
773
|
|
|
755
774
|
/**
|
|
@@ -799,8 +818,8 @@ function parseDataImpl<ParsedDesignToken, ParsedGroup, T>(
|
|
|
799
818
|
config: ParserConfig<ParsedDesignToken, ParsedGroup, T>,
|
|
800
819
|
contextFromParent?: T,
|
|
801
820
|
path: string[] = [],
|
|
802
|
-
|
|
803
|
-
): ParsedDesignToken | ParsedGroup {
|
|
821
|
+
parentGroup?: ParsedGroup
|
|
822
|
+
): ParsedDesignToken | ParsedGroup | undefined {
|
|
804
823
|
if (!isPlainObject(data)) {
|
|
805
824
|
throw new InvalidDataError(path, data);
|
|
806
825
|
}
|
|
@@ -810,38 +829,46 @@ function parseDataImpl<ParsedDesignToken, ParsedGroup, T>(
|
|
|
810
829
|
groupPropsToExtract,
|
|
811
830
|
parseGroupData,
|
|
812
831
|
parseDesignTokenData,
|
|
832
|
+
addChildToGroup,
|
|
813
833
|
} = config;
|
|
814
834
|
|
|
815
|
-
let groupOrToken: ParsedGroup | ParsedDesignToken;
|
|
835
|
+
let groupOrToken: ParsedGroup | ParsedDesignToken | undefined = undefined;
|
|
816
836
|
if (isDesignTokenData(data)) {
|
|
817
837
|
// looks like a token
|
|
818
838
|
groupOrToken = parseDesignTokenData(data, path, contextFromParent);
|
|
819
|
-
if (
|
|
820
|
-
|
|
839
|
+
if (addChildToGroup && path.length > 0 && parentGroup !== undefined) {
|
|
840
|
+
addChildToGroup(parentGroup, path[path.length - 1], groupOrToken);
|
|
821
841
|
}
|
|
822
842
|
} else {
|
|
823
843
|
// must be a group
|
|
824
|
-
const { extracted: groupData,
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
groupData,
|
|
828
|
-
path,
|
|
829
|
-
contextFromParent
|
|
844
|
+
const { extracted: groupData, rest: children } = extractProperties(
|
|
845
|
+
data,
|
|
846
|
+
groupPropsToExtract
|
|
830
847
|
);
|
|
831
848
|
|
|
832
|
-
|
|
849
|
+
let contextForChildren: T | undefined;
|
|
850
|
+
if (parseGroupData) {
|
|
851
|
+
const parseResult = parseGroupData(groupData, path, contextFromParent);
|
|
852
|
+
contextForChildren = parseResult.contextForChildren;
|
|
853
|
+
groupOrToken = parseResult.group;
|
|
854
|
+
}
|
|
833
855
|
|
|
834
|
-
if (
|
|
835
|
-
|
|
856
|
+
if (
|
|
857
|
+
addChildToGroup &&
|
|
858
|
+
path.length > 0 &&
|
|
859
|
+
parentGroup !== undefined &&
|
|
860
|
+
groupOrToken !== undefined
|
|
861
|
+
) {
|
|
862
|
+
addChildToGroup(parentGroup, path[path.length - 1], groupOrToken);
|
|
836
863
|
}
|
|
837
864
|
|
|
838
|
-
for (const childName
|
|
865
|
+
for (const childName in children) {
|
|
839
866
|
parseDataImpl(
|
|
840
|
-
|
|
867
|
+
children[childName],
|
|
841
868
|
config,
|
|
842
869
|
contextForChildren,
|
|
843
870
|
[...path, childName],
|
|
844
|
-
|
|
871
|
+
groupOrToken
|
|
845
872
|
);
|
|
846
873
|
}
|
|
847
874
|
}
|
|
@@ -873,7 +900,7 @@ export function parseData<ParsedDesignToken, ParsedGroup, T>(
|
|
|
873
900
|
data: unknown,
|
|
874
901
|
config: ParserConfig<ParsedDesignToken, ParsedGroup, T>,
|
|
875
902
|
contextFromParent?: T
|
|
876
|
-
): ParsedDesignToken | ParsedGroup {
|
|
903
|
+
): ParsedDesignToken | ParsedGroup | undefined {
|
|
877
904
|
return parseDataImpl(data, config, contextFromParent);
|
|
878
905
|
}
|
|
879
906
|
</pre></td></tr></table></pre>
|
|
@@ -883,7 +910,7 @@ export function parseData<ParsedDesignToken, ParsedGroup, T>(
|
|
|
883
910
|
<div class='footer quiet pad2 space-top1 center small'>
|
|
884
911
|
Code coverage generated by
|
|
885
912
|
<a href="https://istanbul.js.org/" target="_blank" rel="noopener noreferrer">istanbul</a>
|
|
886
|
-
at 2024-11-
|
|
913
|
+
at 2024-11-28T23:35:17.447Z
|
|
887
914
|
</div>
|
|
888
915
|
<script src="prettify.js"></script>
|
|
889
916
|
<script>
|
|
@@ -16,15 +16,15 @@ import { type PlainObject } from "./isJsonObject.js";
|
|
|
16
16
|
* and their values, and an array of all property
|
|
17
17
|
* names of the input object that were not extracted.
|
|
18
18
|
*/
|
|
19
|
-
export declare function extractProperties(object: PlainObject, propsToExtract: (string | RegExp)[]): {
|
|
19
|
+
export declare function extractProperties(object: PlainObject, propsToExtract: readonly (string | RegExp)[]): {
|
|
20
20
|
/**
|
|
21
21
|
* Object containg the extract properties
|
|
22
22
|
* and their respective values.
|
|
23
23
|
*/
|
|
24
24
|
extracted: PlainObject;
|
|
25
25
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
26
|
+
* Object containing the remaining, unextracted
|
|
27
|
+
* properties and their respective values.
|
|
28
28
|
*/
|
|
29
|
-
|
|
29
|
+
rest: PlainObject;
|
|
30
30
|
};
|
|
@@ -19,18 +19,18 @@ export function extractProperties(object, propsToExtract) {
|
|
|
19
19
|
const propNamesToExtract = propsToExtract.filter((prop) => typeof prop === "string");
|
|
20
20
|
const propRegexesToExtract = propsToExtract.filter((prop) => prop instanceof RegExp);
|
|
21
21
|
const extracted = {};
|
|
22
|
-
const
|
|
22
|
+
const rest = {};
|
|
23
23
|
Object.getOwnPropertyNames(object).forEach((prop) => {
|
|
24
24
|
if (propNamesToExtract.some((propNameToExtract) => propNameToExtract === prop) ||
|
|
25
25
|
propRegexesToExtract.some((propRegexToExtract) => propRegexToExtract.test(prop))) {
|
|
26
26
|
extracted[prop] = object[prop];
|
|
27
27
|
}
|
|
28
28
|
else {
|
|
29
|
-
|
|
29
|
+
rest[prop] = object[prop];
|
|
30
30
|
}
|
|
31
31
|
});
|
|
32
32
|
return {
|
|
33
33
|
extracted,
|
|
34
|
-
|
|
34
|
+
rest,
|
|
35
35
|
};
|
|
36
36
|
}
|
package/dist/index.js
CHANGED
package/dist/parseData.d.ts
CHANGED
|
@@ -31,33 +31,25 @@ export type IsDesignTokenDataFn = (data: PlainObject) => boolean;
|
|
|
31
31
|
export type ParseDesignTokenDataFn<ParsedDesignToken, T> = (data: PlainObject, path: string[], contextFromParent?: T) => ParsedDesignToken;
|
|
32
32
|
/**
|
|
33
33
|
* A function that adds a parsed group or design token
|
|
34
|
-
* as a child of
|
|
34
|
+
* as a child of the given parsed group.
|
|
35
35
|
*
|
|
36
|
+
* @param parent The parent group to add a child to
|
|
36
37
|
* @param name The name of the child group or design token
|
|
37
|
-
* @param child The group or
|
|
38
|
+
* @param child The group or design token to add
|
|
38
39
|
*/
|
|
39
|
-
export type AddChildFn<ParsedGroup, ParsedDesignToken> = (name: string, child: ParsedGroup | ParsedDesignToken) => void;
|
|
40
|
+
export type AddChildFn<ParsedGroup, ParsedDesignToken> = (parent: ParsedGroup, name: string, child: ParsedGroup | ParsedDesignToken) => void;
|
|
40
41
|
/**
|
|
41
42
|
* The return value of a `ParseGroupDataFn`.
|
|
42
43
|
*/
|
|
43
|
-
export interface ParseGroupResult<ParsedGroup,
|
|
44
|
+
export interface ParseGroupResult<ParsedGroup, T> {
|
|
44
45
|
/**
|
|
45
46
|
* The parsed representation of the group.
|
|
46
47
|
*
|
|
47
|
-
* May be
|
|
48
|
-
*
|
|
49
|
-
*
|
|
48
|
+
* May be omitted if there is no useful result to
|
|
49
|
+
* return and we only need to pass along context
|
|
50
|
+
* data.
|
|
50
51
|
*/
|
|
51
|
-
group
|
|
52
|
-
/**
|
|
53
|
-
* Optional function that will add other parsed groups
|
|
54
|
-
* or design tokens as children of this parsed group.
|
|
55
|
-
*
|
|
56
|
-
* Intended for cases where the parsed representation
|
|
57
|
-
* of a group needs to contain its children. If not
|
|
58
|
-
* needed, this property can be omitted.
|
|
59
|
-
*/
|
|
60
|
-
addChild?: AddChildFn<ParsedGroup, ParsedDesignToken>;
|
|
52
|
+
group?: ParsedGroup;
|
|
61
53
|
/**
|
|
62
54
|
* Optional context data to be passed into the
|
|
63
55
|
* `parseGroupData()` and `parseDesignTokenData()` calls
|
|
@@ -81,11 +73,10 @@ export interface ParseGroupResult<ParsedGroup, ParsedDesignToken, T> {
|
|
|
81
73
|
* parsed the group containing this group.
|
|
82
74
|
*
|
|
83
75
|
* @returns The parsed representation of the group and,
|
|
84
|
-
* optionally,
|
|
85
|
-
*
|
|
86
|
-
* pass down when child data is parsed.
|
|
76
|
+
* optionally, some context data to pass down
|
|
77
|
+
* when child data is parsed.
|
|
87
78
|
*/
|
|
88
|
-
export type ParseGroupDataFn<ParsedGroup,
|
|
79
|
+
export type ParseGroupDataFn<ParsedGroup, T> = (data: PlainObject, path: string[], contextFromParent?: T) => ParseGroupResult<ParsedGroup, T>;
|
|
89
80
|
export interface ParserConfig<ParsedDesignToken, ParsedGroup, T> {
|
|
90
81
|
/**
|
|
91
82
|
* A function that determines whether an object in the input
|
|
@@ -110,7 +101,7 @@ export interface ParserConfig<ParsedDesignToken, ParsedGroup, T> {
|
|
|
110
101
|
* path, and should parse that data into whatever structure
|
|
111
102
|
* is desired.
|
|
112
103
|
*/
|
|
113
|
-
parseGroupData
|
|
104
|
+
parseGroupData?: ParseGroupDataFn<ParsedGroup, T>;
|
|
114
105
|
/**
|
|
115
106
|
* Function which is called for each design token
|
|
116
107
|
*data object that is encountered.
|
|
@@ -120,6 +111,15 @@ export interface ParserConfig<ParsedDesignToken, ParsedGroup, T> {
|
|
|
120
111
|
* desired.
|
|
121
112
|
*/
|
|
122
113
|
parseDesignTokenData: ParseDesignTokenDataFn<ParsedDesignToken, T>;
|
|
114
|
+
/**
|
|
115
|
+
* Optional function that will add parsed groups
|
|
116
|
+
* or design tokens as children of another parsed group.
|
|
117
|
+
*
|
|
118
|
+
* Intended for cases where the parsed representation
|
|
119
|
+
* of a group needs to contain its children. If not
|
|
120
|
+
* needed, this property can be omitted.
|
|
121
|
+
*/
|
|
122
|
+
addChildToGroup?: AddChildFn<ParsedGroup, ParsedDesignToken>;
|
|
123
123
|
}
|
|
124
124
|
/**
|
|
125
125
|
* Thrown when `parseData()` encounters group or design token
|
|
@@ -156,4 +156,4 @@ export declare class InvalidDataError extends Error {
|
|
|
156
156
|
* or group parser function call.
|
|
157
157
|
* @returns The outermost parsed group or design token
|
|
158
158
|
*/
|
|
159
|
-
export declare function parseData<ParsedDesignToken, ParsedGroup, T>(data: unknown, config: ParserConfig<ParsedDesignToken, ParsedGroup, T>, contextFromParent?: T): ParsedDesignToken | ParsedGroup;
|
|
159
|
+
export declare function parseData<ParsedDesignToken, ParsedGroup, T>(data: unknown, config: ParserConfig<ParsedDesignToken, ParsedGroup, T>, contextFromParent?: T): ParsedDesignToken | ParsedGroup | undefined;
|
package/dist/parseData.js
CHANGED
|
@@ -35,29 +35,36 @@ export class InvalidDataError extends Error {
|
|
|
35
35
|
* to the parent group.
|
|
36
36
|
* @returns The parsed design token or group.
|
|
37
37
|
*/
|
|
38
|
-
function parseDataImpl(data, config, contextFromParent, path = [],
|
|
38
|
+
function parseDataImpl(data, config, contextFromParent, path = [], parentGroup) {
|
|
39
39
|
if (!isPlainObject(data)) {
|
|
40
40
|
throw new InvalidDataError(path, data);
|
|
41
41
|
}
|
|
42
|
-
const { isDesignTokenData, groupPropsToExtract, parseGroupData, parseDesignTokenData, } = config;
|
|
43
|
-
let groupOrToken;
|
|
42
|
+
const { isDesignTokenData, groupPropsToExtract, parseGroupData, parseDesignTokenData, addChildToGroup, } = config;
|
|
43
|
+
let groupOrToken = undefined;
|
|
44
44
|
if (isDesignTokenData(data)) {
|
|
45
45
|
// looks like a token
|
|
46
46
|
groupOrToken = parseDesignTokenData(data, path, contextFromParent);
|
|
47
|
-
if (
|
|
48
|
-
|
|
47
|
+
if (addChildToGroup && path.length > 0 && parentGroup !== undefined) {
|
|
48
|
+
addChildToGroup(parentGroup, path[path.length - 1], groupOrToken);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
else {
|
|
52
52
|
// must be a group
|
|
53
|
-
const { extracted: groupData,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
53
|
+
const { extracted: groupData, rest: children } = extractProperties(data, groupPropsToExtract);
|
|
54
|
+
let contextForChildren;
|
|
55
|
+
if (parseGroupData) {
|
|
56
|
+
const parseResult = parseGroupData(groupData, path, contextFromParent);
|
|
57
|
+
contextForChildren = parseResult.contextForChildren;
|
|
58
|
+
groupOrToken = parseResult.group;
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
if (addChildToGroup &&
|
|
61
|
+
path.length > 0 &&
|
|
62
|
+
parentGroup !== undefined &&
|
|
63
|
+
groupOrToken !== undefined) {
|
|
64
|
+
addChildToGroup(parentGroup, path[path.length - 1], groupOrToken);
|
|
65
|
+
}
|
|
66
|
+
for (const childName in children) {
|
|
67
|
+
parseDataImpl(children[childName], config, contextForChildren, [...path, childName], groupOrToken);
|
|
61
68
|
}
|
|
62
69
|
}
|
|
63
70
|
return groupOrToken;
|
package/package.json
CHANGED
|
@@ -18,12 +18,12 @@ describe("extractProperties()", () => {
|
|
|
18
18
|
expect(extractResult.extracted).toStrictEqual({ bar: 13, baz: 666 });
|
|
19
19
|
});
|
|
20
20
|
|
|
21
|
-
it("returns
|
|
21
|
+
it("returns non-extracted properties", () => {
|
|
22
22
|
const extractResult = extractProperties(
|
|
23
23
|
{ foo: 42, bar: 13, baz: 666, quux: 0 },
|
|
24
24
|
["foo", "baz"]
|
|
25
25
|
);
|
|
26
|
-
expect(extractResult.
|
|
26
|
+
expect(extractResult.rest).toStrictEqual({ bar: 13, quux: 0 });
|
|
27
27
|
});
|
|
28
28
|
|
|
29
29
|
it("ignores props to extract that are not present in input object", () => {
|
package/src/extractProperties.ts
CHANGED
|
@@ -19,7 +19,7 @@ import { type PlainObject } from "./isJsonObject.js";
|
|
|
19
19
|
*/
|
|
20
20
|
export function extractProperties(
|
|
21
21
|
object: PlainObject,
|
|
22
|
-
propsToExtract: (string | RegExp)[]
|
|
22
|
+
propsToExtract: readonly (string | RegExp)[]
|
|
23
23
|
): {
|
|
24
24
|
/**
|
|
25
25
|
* Object containg the extract properties
|
|
@@ -28,10 +28,10 @@ export function extractProperties(
|
|
|
28
28
|
extracted: PlainObject;
|
|
29
29
|
|
|
30
30
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
31
|
+
* Object containing the remaining, unextracted
|
|
32
|
+
* properties and their respective values.
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
rest: PlainObject;
|
|
35
35
|
} {
|
|
36
36
|
const propNamesToExtract = propsToExtract.filter(
|
|
37
37
|
(prop) => typeof prop === "string"
|
|
@@ -41,7 +41,7 @@ export function extractProperties(
|
|
|
41
41
|
);
|
|
42
42
|
|
|
43
43
|
const extracted: PlainObject = {};
|
|
44
|
-
const
|
|
44
|
+
const rest: PlainObject = {};
|
|
45
45
|
Object.getOwnPropertyNames(object).forEach((prop) => {
|
|
46
46
|
if (
|
|
47
47
|
propNamesToExtract.some(
|
|
@@ -53,12 +53,12 @@ export function extractProperties(
|
|
|
53
53
|
) {
|
|
54
54
|
extracted[prop] = object[prop];
|
|
55
55
|
} else {
|
|
56
|
-
|
|
56
|
+
rest[prop] = object[prop];
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
return {
|
|
61
61
|
extracted,
|
|
62
|
-
|
|
62
|
+
rest,
|
|
63
63
|
};
|
|
64
64
|
}
|