1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export type JSONPrimitive = boolean | number | string | null;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export type JSONValue = JSONPrimitive | JSONObject | JSONArray;
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export interface JSONObject {
|
25 | [key: string]: JSONValue;
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export interface JSONArray extends Array<JSONValue> {}
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | export interface ReadonlyJSONObject {
|
37 | readonly [key: string]: ReadonlyJSONValue;
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | export interface ReadonlyJSONArray extends ReadonlyArray<ReadonlyJSONValue> {}
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | export type ReadonlyJSONValue =
|
49 | | JSONPrimitive
|
50 | | ReadonlyJSONObject
|
51 | | ReadonlyJSONArray;
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | export type PartialJSONValue =
|
59 | | JSONPrimitive
|
60 | | PartialJSONObject
|
61 | | PartialJSONArray;
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | export interface PartialJSONObject {
|
69 | [key: string]: PartialJSONValue | undefined;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | export interface PartialJSONArray extends Array<PartialJSONValue> {}
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | export interface ReadonlyPartialJSONObject {
|
85 | readonly [key: string]: ReadonlyPartialJSONValue | undefined;
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | export interface ReadonlyPartialJSONArray
|
94 | extends ReadonlyArray<ReadonlyPartialJSONValue> {}
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | export type ReadonlyPartialJSONValue =
|
102 | | JSONPrimitive
|
103 | | ReadonlyPartialJSONObject
|
104 | | ReadonlyPartialJSONArray;
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | export namespace JSONExt {
|
110 | |
111 |
|
112 |
|
113 | export const emptyObject = Object.freeze({}) as ReadonlyJSONObject;
|
114 |
|
115 | |
116 |
|
117 |
|
118 | export const emptyArray = Object.freeze([]) as ReadonlyJSONArray;
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | export function isPrimitive(
|
128 | value: ReadonlyPartialJSONValue
|
129 | ): value is JSONPrimitive {
|
130 | return (
|
131 | value === null ||
|
132 | typeof value === 'boolean' ||
|
133 | typeof value === 'number' ||
|
134 | typeof value === 'string'
|
135 | );
|
136 | }
|
137 |
|
138 | |
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | export function isArray(value: JSONValue): value is JSONArray;
|
146 | export function isArray(value: ReadonlyJSONValue): value is ReadonlyJSONArray;
|
147 | export function isArray(value: PartialJSONValue): value is PartialJSONArray;
|
148 | export function isArray(
|
149 | value: ReadonlyPartialJSONValue
|
150 | ): value is ReadonlyPartialJSONArray;
|
151 | export function isArray(value: ReadonlyPartialJSONValue): boolean {
|
152 | return Array.isArray(value);
|
153 | }
|
154 |
|
155 | |
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | export function isObject(value: JSONValue): value is JSONObject;
|
163 | export function isObject(
|
164 | value: ReadonlyJSONValue
|
165 | ): value is ReadonlyJSONObject;
|
166 | export function isObject(value: PartialJSONValue): value is PartialJSONObject;
|
167 | export function isObject(
|
168 | value: ReadonlyPartialJSONValue
|
169 | ): value is ReadonlyPartialJSONObject;
|
170 | export function isObject(value: ReadonlyPartialJSONValue): boolean {
|
171 | return !isPrimitive(value) && !isArray(value);
|
172 | }
|
173 |
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | export function deepEqual(
|
184 | first: ReadonlyPartialJSONValue,
|
185 | second: ReadonlyPartialJSONValue
|
186 | ): boolean {
|
187 |
|
188 | if (first === second) {
|
189 | return true;
|
190 | }
|
191 |
|
192 |
|
193 | if (isPrimitive(first) || isPrimitive(second)) {
|
194 | return false;
|
195 | }
|
196 |
|
197 |
|
198 | let a1 = isArray(first);
|
199 | let a2 = isArray(second);
|
200 |
|
201 |
|
202 | if (a1 !== a2) {
|
203 | return false;
|
204 | }
|
205 |
|
206 |
|
207 | if (a1 && a2) {
|
208 | return deepArrayEqual(
|
209 | first as ReadonlyPartialJSONArray,
|
210 | second as ReadonlyPartialJSONArray
|
211 | );
|
212 | }
|
213 |
|
214 |
|
215 | return deepObjectEqual(
|
216 | first as ReadonlyPartialJSONObject,
|
217 | second as ReadonlyPartialJSONObject
|
218 | );
|
219 | }
|
220 |
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | export function deepCopy<T extends ReadonlyPartialJSONValue>(value: T): T {
|
229 |
|
230 | if (isPrimitive(value)) {
|
231 | return value;
|
232 | }
|
233 |
|
234 |
|
235 | if (isArray(value)) {
|
236 | return deepArrayCopy(value);
|
237 | }
|
238 |
|
239 |
|
240 | return deepObjectCopy(value);
|
241 | }
|
242 |
|
243 | |
244 |
|
245 |
|
246 | function deepArrayEqual(
|
247 | first: ReadonlyPartialJSONArray,
|
248 | second: ReadonlyPartialJSONArray
|
249 | ): boolean {
|
250 |
|
251 | if (first === second) {
|
252 | return true;
|
253 | }
|
254 |
|
255 |
|
256 | if (first.length !== second.length) {
|
257 | return false;
|
258 | }
|
259 |
|
260 |
|
261 | for (let i = 0, n = first.length; i < n; ++i) {
|
262 | if (!deepEqual(first[i], second[i])) {
|
263 | return false;
|
264 | }
|
265 | }
|
266 |
|
267 |
|
268 | return true;
|
269 | }
|
270 |
|
271 | |
272 |
|
273 |
|
274 | function deepObjectEqual(
|
275 | first: ReadonlyPartialJSONObject,
|
276 | second: ReadonlyPartialJSONObject
|
277 | ): boolean {
|
278 |
|
279 | if (first === second) {
|
280 | return true;
|
281 | }
|
282 |
|
283 |
|
284 | for (let key in first) {
|
285 | if (first[key] !== undefined && !(key in second)) {
|
286 | return false;
|
287 | }
|
288 | }
|
289 |
|
290 |
|
291 | for (let key in second) {
|
292 | if (second[key] !== undefined && !(key in first)) {
|
293 | return false;
|
294 | }
|
295 | }
|
296 |
|
297 |
|
298 | for (let key in first) {
|
299 |
|
300 | let firstValue = first[key];
|
301 | let secondValue = second[key];
|
302 |
|
303 |
|
304 | if (firstValue === undefined && secondValue === undefined) {
|
305 | continue;
|
306 | }
|
307 |
|
308 |
|
309 | if (firstValue === undefined || secondValue === undefined) {
|
310 | return false;
|
311 | }
|
312 |
|
313 |
|
314 | if (!deepEqual(firstValue, secondValue)) {
|
315 | return false;
|
316 | }
|
317 | }
|
318 |
|
319 |
|
320 | return true;
|
321 | }
|
322 |
|
323 | |
324 |
|
325 |
|
326 | function deepArrayCopy(value: any): any {
|
327 | let result = new Array<any>(value.length);
|
328 | for (let i = 0, n = value.length; i < n; ++i) {
|
329 | result[i] = deepCopy(value[i]);
|
330 | }
|
331 | return result;
|
332 | }
|
333 |
|
334 | |
335 |
|
336 |
|
337 | function deepObjectCopy(value: any): any {
|
338 | let result: any = {};
|
339 | for (let key in value) {
|
340 |
|
341 | let subvalue = value[key];
|
342 | if (subvalue === undefined) {
|
343 | continue;
|
344 | }
|
345 | result[key] = deepCopy(subvalue);
|
346 | }
|
347 | return result;
|
348 | }
|
349 | }
|