@microsoft/atlas-css 3.25.1 → 3.26.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.
@@ -0,0 +1,160 @@
1
+ // To parse this data:
2
+ //
3
+ // import { Convert, AtlasClassNames } from "./file";
4
+ //
5
+ // const atlasClassNames = Convert.toAtlasClassNames(json);
6
+ //
7
+ // These functions will throw an error if the JSON doesn't
8
+ // match the expected interface, even if the JSON is valid.
9
+
10
+ export interface AtlasClassNames {
11
+ }
12
+
13
+ // Converts JSON strings to/from your types
14
+ // and asserts the results of JSON.parse at runtime
15
+ export class Convert {
16
+ public static toAtlasClassNames(json: string): AtlasClassNames {
17
+ return cast(JSON.parse(json), r("AtlasClassNames"));
18
+ }
19
+
20
+ public static atlasClassNamesToJson(value: AtlasClassNames): string {
21
+ return JSON.stringify(uncast(value, r("AtlasClassNames")), null, 2);
22
+ }
23
+ }
24
+
25
+ function invalidValue(typ: any, val: any, key: any = ''): never {
26
+ if (key) {
27
+ throw Error(`Invalid value for key "${key}". Expected type ${JSON.stringify(typ)} but got ${JSON.stringify(val)}`);
28
+ }
29
+ throw Error(`Invalid value ${JSON.stringify(val)} for type ${JSON.stringify(typ)}`, );
30
+ }
31
+
32
+ function jsonToJSProps(typ: any): any {
33
+ if (typ.jsonToJS === undefined) {
34
+ const map: any = {};
35
+ typ.props.forEach((p: any) => map[p.json] = { key: p.js, typ: p.typ });
36
+ typ.jsonToJS = map;
37
+ }
38
+ return typ.jsonToJS;
39
+ }
40
+
41
+ function jsToJSONProps(typ: any): any {
42
+ if (typ.jsToJSON === undefined) {
43
+ const map: any = {};
44
+ typ.props.forEach((p: any) => map[p.js] = { key: p.json, typ: p.typ });
45
+ typ.jsToJSON = map;
46
+ }
47
+ return typ.jsToJSON;
48
+ }
49
+
50
+ function transform(val: any, typ: any, getProps: any, key: any = ''): any {
51
+ function transformPrimitive(typ: string, val: any): any {
52
+ if (typeof typ === typeof val) return val;
53
+ return invalidValue(typ, val, key);
54
+ }
55
+
56
+ function transformUnion(typs: any[], val: any): any {
57
+ // val must validate against one typ in typs
58
+ const l = typs.length;
59
+ for (let i = 0; i < l; i++) {
60
+ const typ = typs[i];
61
+ try {
62
+ return transform(val, typ, getProps);
63
+ } catch (_) {}
64
+ }
65
+ return invalidValue(typs, val);
66
+ }
67
+
68
+ function transformEnum(cases: string[], val: any): any {
69
+ if (cases.indexOf(val) !== -1) return val;
70
+ return invalidValue(cases, val);
71
+ }
72
+
73
+ function transformArray(typ: any, val: any): any {
74
+ // val must be an array with no invalid elements
75
+ if (!Array.isArray(val)) return invalidValue("array", val);
76
+ return val.map(el => transform(el, typ, getProps));
77
+ }
78
+
79
+ function transformDate(val: any): any {
80
+ if (val === null) {
81
+ return null;
82
+ }
83
+ const d = new Date(val);
84
+ if (isNaN(d.valueOf())) {
85
+ return invalidValue("Date", val);
86
+ }
87
+ return d;
88
+ }
89
+
90
+ function transformObject(props: { [k: string]: any }, additional: any, val: any): any {
91
+ if (val === null || typeof val !== "object" || Array.isArray(val)) {
92
+ return invalidValue("object", val);
93
+ }
94
+ const result: any = {};
95
+ Object.getOwnPropertyNames(props).forEach(key => {
96
+ const prop = props[key];
97
+ const v = Object.prototype.hasOwnProperty.call(val, key) ? val[key] : undefined;
98
+ result[prop.key] = transform(v, prop.typ, getProps, prop.key);
99
+ });
100
+ Object.getOwnPropertyNames(val).forEach(key => {
101
+ if (!Object.prototype.hasOwnProperty.call(props, key)) {
102
+ result[key] = transform(val[key], additional, getProps, key);
103
+ }
104
+ });
105
+ return result;
106
+ }
107
+
108
+ if (typ === "any") return val;
109
+ if (typ === null) {
110
+ if (val === null) return val;
111
+ return invalidValue(typ, val);
112
+ }
113
+ if (typ === false) return invalidValue(typ, val);
114
+ while (typeof typ === "object" && typ.ref !== undefined) {
115
+ typ = typeMap[typ.ref];
116
+ }
117
+ if (Array.isArray(typ)) return transformEnum(typ, val);
118
+ if (typeof typ === "object") {
119
+ return typ.hasOwnProperty("unionMembers") ? transformUnion(typ.unionMembers, val)
120
+ : typ.hasOwnProperty("arrayItems") ? transformArray(typ.arrayItems, val)
121
+ : typ.hasOwnProperty("props") ? transformObject(getProps(typ), typ.additional, val)
122
+ : invalidValue(typ, val);
123
+ }
124
+ // Numbers can be parsed by Date but shouldn't be.
125
+ if (typ === Date && typeof val !== "number") return transformDate(val);
126
+ return transformPrimitive(typ, val);
127
+ }
128
+
129
+ function cast<T>(val: any, typ: any): T {
130
+ return transform(val, typ, jsonToJSProps);
131
+ }
132
+
133
+ function uncast<T>(val: T, typ: any): any {
134
+ return transform(val, typ, jsToJSONProps);
135
+ }
136
+
137
+ function a(typ: any) {
138
+ return { arrayItems: typ };
139
+ }
140
+
141
+ function u(...typs: any[]) {
142
+ return { unionMembers: typs };
143
+ }
144
+
145
+ function o(props: any[], additional: any) {
146
+ return { props, additional };
147
+ }
148
+
149
+ function m(additional: any) {
150
+ return { props: [], additional };
151
+ }
152
+
153
+ function r(name: string) {
154
+ return { ref: name };
155
+ }
156
+
157
+ const typeMap: any = {
158
+ "AtlasClassNames": o([
159
+ ], false),
160
+ };