UNPKG

16.7 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3/*-----------------------------------------------------------------------------
4| Copyright (c) 2014-2017, PhosphorJS Contributors
5|
6| Distributed under the terms of the BSD 3-Clause License.
7|
8| The full license is in the file LICENSE, distributed with this software.
9|----------------------------------------------------------------------------*/
10/**
11 * The namespace for JSON-specific functions.
12 */
13var 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 (first[key] !== undefined && !(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 (second[key] !== undefined && !(key in first)) {
136 return false;
137 }
138 }
139 // Compare the values for equality.
140 for (var key in first) {
141 // Get the values.
142 var firstValue = first[key];
143 var secondValue = second[key];
144 // If both are undefined, ignore the key.
145 if (firstValue === undefined && secondValue === undefined) {
146 continue;
147 }
148 // If only one value is undefined, the objects are not equal.
149 if (firstValue === undefined || secondValue === undefined) {
150 return false;
151 }
152 // Compare the values.
153 if (!deepEqual(firstValue, secondValue)) {
154 return false;
155 }
156 }
157 // At this point, the objects are equal.
158 return true;
159 }
160 /**
161 * Create a deep copy of a JSON array.
162 */
163 function deepArrayCopy(value) {
164 var result = new Array(value.length);
165 for (var i = 0, n = value.length; i < n; ++i) {
166 result[i] = deepCopy(value[i]);
167 }
168 return result;
169 }
170 /**
171 * Create a deep copy of a JSON object.
172 */
173 function deepObjectCopy(value) {
174 var result = {};
175 for (var key in value) {
176 // Ignore undefined values.
177 var subvalue = value[key];
178 if (subvalue === undefined) {
179 continue;
180 }
181 result[key] = deepCopy(subvalue);
182 }
183 return result;
184 }
185})(JSONExt || (JSONExt = {}));
186
187// Copyright (c) Jupyter Development Team.
188// Distributed under the terms of the Modified BSD License.
189/*-----------------------------------------------------------------------------
190| Copyright (c) 2014-2017, PhosphorJS Contributors
191|
192| Distributed under the terms of the BSD 3-Clause License.
193|
194| The full license is in the file LICENSE, distributed with this software.
195|----------------------------------------------------------------------------*/
196/**
197 * An object which stores MIME data for general application use.
198 *
199 * #### Notes
200 * This class does not attempt to enforce "correctness" of MIME types
201 * and their associated data. Since this class is designed to transfer
202 * arbitrary data and objects within the same application, it assumes
203 * that the user provides correct and accurate data.
204 */
205var MimeData = /** @class */ (function () {
206 function MimeData() {
207 this._types = [];
208 this._values = [];
209 }
210 /**
211 * Get an array of the MIME types contained within the dataset.
212 *
213 * @returns A new array of the MIME types, in order of insertion.
214 */
215 MimeData.prototype.types = function () {
216 return this._types.slice();
217 };
218 /**
219 * Test whether the dataset has an entry for the given type.
220 *
221 * @param mime - The MIME type of interest.
222 *
223 * @returns `true` if the dataset contains a value for the given
224 * MIME type, `false` otherwise.
225 */
226 MimeData.prototype.hasData = function (mime) {
227 return this._types.indexOf(mime) !== -1;
228 };
229 /**
230 * Get the data value for the given MIME type.
231 *
232 * @param mime - The MIME type of interest.
233 *
234 * @returns The value for the given MIME type, or `undefined` if
235 * the dataset does not contain a value for the type.
236 */
237 MimeData.prototype.getData = function (mime) {
238 var i = this._types.indexOf(mime);
239 return i !== -1 ? this._values[i] : undefined;
240 };
241 /**
242 * Set the data value for the given MIME type.
243 *
244 * @param mime - The MIME type of interest.
245 *
246 * @param data - The data value for the given MIME type.
247 *
248 * #### Notes
249 * This will overwrite any previous entry for the MIME type.
250 */
251 MimeData.prototype.setData = function (mime, data) {
252 this.clearData(mime);
253 this._types.push(mime);
254 this._values.push(data);
255 };
256 /**
257 * Remove the data entry for the given MIME type.
258 *
259 * @param mime - The MIME type of interest.
260 *
261 * #### Notes
262 * This is a no-op if there is no entry for the given MIME type.
263 */
264 MimeData.prototype.clearData = function (mime) {
265 var i = this._types.indexOf(mime);
266 if (i !== -1) {
267 this._types.splice(i, 1);
268 this._values.splice(i, 1);
269 }
270 };
271 /**
272 * Remove all data entries from the dataset.
273 */
274 MimeData.prototype.clear = function () {
275 this._types.length = 0;
276 this._values.length = 0;
277 };
278 return MimeData;
279}());
280
281// Copyright (c) Jupyter Development Team.
282// Distributed under the terms of the Modified BSD License.
283/*-----------------------------------------------------------------------------
284| Copyright (c) 2014-2017, PhosphorJS Contributors
285|
286| Distributed under the terms of the BSD 3-Clause License.
287|
288| The full license is in the file LICENSE, distributed with this software.
289|----------------------------------------------------------------------------*/
290/**
291 * A class which wraps a promise into a delegate object.
292 *
293 * #### Notes
294 * This class is useful when the logic to resolve or reject a promise
295 * cannot be defined at the point where the promise is created.
296 */
297var PromiseDelegate = /** @class */ (function () {
298 /**
299 * Construct a new promise delegate.
300 */
301 function PromiseDelegate() {
302 var _this = this;
303 this.promise = new Promise(function (resolve, reject) {
304 _this._resolve = resolve;
305 _this._reject = reject;
306 });
307 }
308 /**
309 * Resolve the wrapped promise with the given value.
310 *
311 * @param value - The value to use for resolving the promise.
312 */
313 PromiseDelegate.prototype.resolve = function (value) {
314 var resolve = this._resolve;
315 resolve(value);
316 };
317 /**
318 * Reject the wrapped promise with the given value.
319 *
320 * @reason - The reason for rejecting the promise.
321 */
322 PromiseDelegate.prototype.reject = function (reason) {
323 var reject = this._reject;
324 reject(reason);
325 };
326 return PromiseDelegate;
327}());
328
329// Copyright (c) Jupyter Development Team.
330// Distributed under the terms of the Modified BSD License.
331/*-----------------------------------------------------------------------------
332| Copyright (c) 2014-2017, PhosphorJS Contributors
333|
334| Distributed under the terms of the BSD 3-Clause License.
335|
336| The full license is in the file LICENSE, distributed with this software.
337|----------------------------------------------------------------------------*/
338/**
339 * A runtime object which captures compile-time type information.
340 *
341 * #### Notes
342 * A token captures the compile-time type of an interface or class in
343 * an object which can be used at runtime in a type-safe fashion.
344 */
345var Token = /** @class */ (function () {
346 /**
347 * Construct a new token.
348 *
349 * @param name - A human readable name for the token.
350 */
351 function Token(name) {
352 this.name = name;
353 this._tokenStructuralPropertyT = null;
354 }
355 return Token;
356}());
357
358// Copyright (c) Jupyter Development Team.
359// Distributed under the terms of the Modified BSD License.
360/*-----------------------------------------------------------------------------
361| Copyright (c) 2014-2017, PhosphorJS Contributors
362|
363| Distributed under the terms of the BSD 3-Clause License.
364|
365| The full license is in the file LICENSE, distributed with this software.
366|----------------------------------------------------------------------------*/
367// Fallback
368function fallbackRandomValues(buffer) {
369 var value = 0;
370 for (var i = 0, n = buffer.length; i < n; ++i) {
371 if (i % 4 === 0) {
372 value = (Math.random() * 0xffffffff) >>> 0;
373 }
374 buffer[i] = value & 0xff;
375 value >>>= 8;
376 }
377}
378
379// Copyright (c) Jupyter Development Team.
380/**
381 * The namespace for random number related functionality.
382 */
383var Random;
384(function (Random) {
385 /**
386 * A function which generates random bytes.
387 *
388 * @param buffer - The `Uint8Array` to fill with random bytes.
389 *
390 * #### Notes
391 * A cryptographically strong random number generator will be used if
392 * available. Otherwise, `Math.random` will be used as a fallback for
393 * randomness.
394 *
395 * The following RNGs are supported, listed in order of precedence:
396 * - `window.crypto.getRandomValues`
397 * - `window.msCrypto.getRandomValues`
398 * - `require('crypto').randomFillSync
399 * - `require('crypto').randomBytes
400 * - `Math.random`
401 */
402 Random.getRandomValues = (function () {
403 // Look up the crypto module if available.
404 var crypto = (typeof require !== 'undefined' && require('crypto')) || null;
405 // Node 7+
406 if (crypto && typeof crypto.randomFillSync === 'function') {
407 return function getRandomValues(buffer) {
408 return crypto.randomFillSync(buffer);
409 };
410 }
411 // Node 0.10+
412 if (crypto && typeof crypto.randomBytes === 'function') {
413 return function getRandomValues(buffer) {
414 var bytes = crypto.randomBytes(buffer.length);
415 for (var i = 0, n = bytes.length; i < n; ++i) {
416 buffer[i] = bytes[i];
417 }
418 };
419 }
420 // Fallback
421 return fallbackRandomValues;
422 })();
423})(Random || (Random = {}));
424
425// Copyright (c) Jupyter Development Team.
426// Distributed under the terms of the Modified BSD License.
427/*-----------------------------------------------------------------------------
428| Copyright (c) 2014-2017, PhosphorJS Contributors
429|
430| Distributed under the terms of the BSD 3-Clause License.
431|
432| The full license is in the file LICENSE, distributed with this software.
433|----------------------------------------------------------------------------*/
434/**
435 * A function which creates a function that generates UUID v4 identifiers.
436 *
437 * @returns A new function that creates a UUID v4 string.
438 *
439 * #### Notes
440 * This implementation complies with RFC 4122.
441 *
442 * This uses `Random.getRandomValues()` for random bytes, which in
443 * turn will use the underlying `crypto` module of the platform if
444 * it is available. The fallback for randomness is `Math.random`.
445 */
446function uuid4Factory(getRandomValues) {
447 // Create a 16 byte array to hold the random values.
448 var bytes = new Uint8Array(16);
449 // Create a look up table from bytes to hex strings.
450 var lut = new Array(256);
451 // Pad the single character hex digits with a leading zero.
452 for (var i = 0; i < 16; ++i) {
453 lut[i] = '0' + i.toString(16);
454 }
455 // Populate the rest of the hex digits.
456 for (var i = 16; i < 256; ++i) {
457 lut[i] = i.toString(16);
458 }
459 // Return a function which generates the UUID.
460 return function uuid4() {
461 // Get a new batch of random values.
462 getRandomValues(bytes);
463 // Set the UUID version number to 4.
464 bytes[6] = 0x40 | (bytes[6] & 0x0f);
465 // Set the clock sequence bit to the RFC spec.
466 bytes[8] = 0x80 | (bytes[8] & 0x3f);
467 // Assemble the UUID string.
468 return (lut[bytes[0]] +
469 lut[bytes[1]] +
470 lut[bytes[2]] +
471 lut[bytes[3]] +
472 '-' +
473 lut[bytes[4]] +
474 lut[bytes[5]] +
475 '-' +
476 lut[bytes[6]] +
477 lut[bytes[7]] +
478 '-' +
479 lut[bytes[8]] +
480 lut[bytes[9]] +
481 '-' +
482 lut[bytes[10]] +
483 lut[bytes[11]] +
484 lut[bytes[12]] +
485 lut[bytes[13]] +
486 lut[bytes[14]] +
487 lut[bytes[15]]);
488 };
489}
490
491// Copyright (c) Jupyter Development Team.
492/**
493 * The namespace for UUID related functionality.
494 */
495var UUID;
496(function (UUID) {
497 /**
498 * A function which generates UUID v4 identifiers.
499 *
500 * @returns A new UUID v4 string.
501 *
502 * #### Notes
503 * This implementation complies with RFC 4122.
504 *
505 * This uses `Random.getRandomValues()` for random bytes, which in
506 * turn will use the underlying `crypto` module of the platform if
507 * it is available. The fallback for randomness is `Math.random`.
508 */
509 UUID.uuid4 = uuid4Factory(Random.getRandomValues);
510})(UUID || (UUID = {}));
511
512export { JSONExt, MimeData, PromiseDelegate, Random, Token, UUID };
513//# sourceMappingURL=index.node.es6.js.map