1 | ;
|
2 | /*-----------------------------------------------------------------------------
|
3 | | Copyright (c) 2014-2017, PhosphorJS Contributors
|
4 | |
|
5 | | Distributed under the terms of the BSD 3-Clause License.
|
6 | |
|
7 | | The full license is in the file LICENSE, distributed with this software.
|
8 | |----------------------------------------------------------------------------*/
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | /**
|
11 | * The namespace for JSON-specific functions.
|
12 | */
|
13 | var JSONExt;
|
14 | (function (JSONExt) {
|
15 | /**
|
16 | * A shared frozen empty JSONObject
|
17 | */
|
18 | JSONExt.emptyObject = Object.freeze({});
|
19 | /**
|
20 | * A shared frozen empty JSONArray
|
21 | */
|
22 | JSONExt.emptyArray = Object.freeze([]);
|
23 | /**
|
24 | * Test whether a JSON value is a primitive.
|
25 | *
|
26 | * @param value - The JSON value of interest.
|
27 | *
|
28 | * @returns `true` if the value is a primitive,`false` otherwise.
|
29 | */
|
30 | function isPrimitive(value) {
|
31 | return (value === null ||
|
32 | typeof value === 'boolean' ||
|
33 | typeof value === 'number' ||
|
34 | typeof value === 'string');
|
35 | }
|
36 | JSONExt.isPrimitive = isPrimitive;
|
37 | function isArray(value) {
|
38 | return Array.isArray(value);
|
39 | }
|
40 | JSONExt.isArray = isArray;
|
41 | function isObject(value) {
|
42 | return !isPrimitive(value) && !isArray(value);
|
43 | }
|
44 | JSONExt.isObject = isObject;
|
45 | /**
|
46 | * Compare two JSON values for deep equality.
|
47 | *
|
48 | * @param first - The first JSON value of interest.
|
49 | *
|
50 | * @param second - The second JSON value of interest.
|
51 | *
|
52 | * @returns `true` if the values are equivalent, `false` otherwise.
|
53 | */
|
54 | function deepEqual(first, second) {
|
55 | // Check referential and primitive equality first.
|
56 | if (first === second) {
|
57 | return true;
|
58 | }
|
59 | // If one is a primitive, the `===` check ruled out the other.
|
60 | if (isPrimitive(first) || isPrimitive(second)) {
|
61 | return false;
|
62 | }
|
63 | // Test whether they are arrays.
|
64 | var a1 = isArray(first);
|
65 | var a2 = isArray(second);
|
66 | // Bail if the types are different.
|
67 | if (a1 !== a2) {
|
68 | return false;
|
69 | }
|
70 | // If they are both arrays, compare them.
|
71 | if (a1 && a2) {
|
72 | return deepArrayEqual(first, second);
|
73 | }
|
74 | // At this point, they must both be objects.
|
75 | return deepObjectEqual(first, second);
|
76 | }
|
77 | JSONExt.deepEqual = deepEqual;
|
78 | /**
|
79 | * Create a deep copy of a JSON value.
|
80 | *
|
81 | * @param value - The JSON value to copy.
|
82 | *
|
83 | * @returns A deep copy of the given JSON value.
|
84 | */
|
85 | function deepCopy(value) {
|
86 | // Do nothing for primitive values.
|
87 | if (isPrimitive(value)) {
|
88 | return value;
|
89 | }
|
90 | // Deep copy an array.
|
91 | if (isArray(value)) {
|
92 | return deepArrayCopy(value);
|
93 | }
|
94 | // Deep copy an object.
|
95 | return deepObjectCopy(value);
|
96 | }
|
97 | JSONExt.deepCopy = deepCopy;
|
98 | /**
|
99 | * Compare two JSON arrays for deep equality.
|
100 | */
|
101 | function deepArrayEqual(first, second) {
|
102 | // Check referential equality first.
|
103 | if (first === second) {
|
104 | return true;
|
105 | }
|
106 | // Test the arrays for equal length.
|
107 | if (first.length !== second.length) {
|
108 | return false;
|
109 | }
|
110 | // Compare the values for equality.
|
111 | for (var i = 0, n = first.length; i < n; ++i) {
|
112 | if (!deepEqual(first[i], second[i])) {
|
113 | return false;
|
114 | }
|
115 | }
|
116 | // At this point, the arrays are equal.
|
117 | return true;
|
118 | }
|
119 | /**
|
120 | * Compare two JSON objects for deep equality.
|
121 | */
|
122 | function deepObjectEqual(first, second) {
|
123 | // Check referential equality first.
|
124 | if (first === second) {
|
125 | return true;
|
126 | }
|
127 | // Check for the first object's keys in the second object.
|
128 | for (var key in first) {
|
129 | if (!(key in second)) {
|
130 | return false;
|
131 | }
|
132 | }
|
133 | // Check for the second object's keys in the first object.
|
134 | for (var key in second) {
|
135 | if (!(key in first)) {
|
136 | return false;
|
137 | }
|
138 | }
|
139 | // Compare the values for equality.
|
140 | for (var key in first) {
|
141 | if (!deepEqual(first[key], second[key])) {
|
142 | return false;
|
143 | }
|
144 | }
|
145 | // At this point, the objects are equal.
|
146 | return true;
|
147 | }
|
148 | /**
|
149 | * Create a deep copy of a JSON array.
|
150 | */
|
151 | function deepArrayCopy(value) {
|
152 | var result = new Array(value.length);
|
153 | for (var i = 0, n = value.length; i < n; ++i) {
|
154 | result[i] = deepCopy(value[i]);
|
155 | }
|
156 | return result;
|
157 | }
|
158 | /**
|
159 | * Create a deep copy of a JSON object.
|
160 | */
|
161 | function deepObjectCopy(value) {
|
162 | var result = {};
|
163 | for (var key in value) {
|
164 | result[key] = deepCopy(value[key]);
|
165 | }
|
166 | return result;
|
167 | }
|
168 | })(JSONExt = exports.JSONExt || (exports.JSONExt = {}));
|