UNPKG

18.9 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.lumino_coreutils = {}));
5}(this, (function (exports) { 'use strict';
6
7 // Copyright (c) Jupyter Development Team.
8 // Distributed under the terms of the Modified BSD License.
9 /*-----------------------------------------------------------------------------
10 | Copyright (c) 2014-2017, PhosphorJS Contributors
11 |
12 | Distributed under the terms of the BSD 3-Clause License.
13 |
14 | The full license is in the file LICENSE, distributed with this software.
15 |----------------------------------------------------------------------------*/
16 /**
17 * The namespace for JSON-specific functions.
18 */
19 exports.JSONExt = void 0;
20 (function (JSONExt) {
21 /**
22 * A shared frozen empty JSONObject
23 */
24 JSONExt.emptyObject = Object.freeze({});
25 /**
26 * A shared frozen empty JSONArray
27 */
28 JSONExt.emptyArray = Object.freeze([]);
29 /**
30 * Test whether a JSON value is a primitive.
31 *
32 * @param value - The JSON value of interest.
33 *
34 * @returns `true` if the value is a primitive,`false` otherwise.
35 */
36 function isPrimitive(value) {
37 return (value === null ||
38 typeof value === 'boolean' ||
39 typeof value === 'number' ||
40 typeof value === 'string');
41 }
42 JSONExt.isPrimitive = isPrimitive;
43 function isArray(value) {
44 return Array.isArray(value);
45 }
46 JSONExt.isArray = isArray;
47 function isObject(value) {
48 return !isPrimitive(value) && !isArray(value);
49 }
50 JSONExt.isObject = isObject;
51 /**
52 * Compare two JSON values for deep equality.
53 *
54 * @param first - The first JSON value of interest.
55 *
56 * @param second - The second JSON value of interest.
57 *
58 * @returns `true` if the values are equivalent, `false` otherwise.
59 */
60 function deepEqual(first, second) {
61 // Check referential and primitive equality first.
62 if (first === second) {
63 return true;
64 }
65 // If one is a primitive, the `===` check ruled out the other.
66 if (isPrimitive(first) || isPrimitive(second)) {
67 return false;
68 }
69 // Test whether they are arrays.
70 var a1 = isArray(first);
71 var a2 = isArray(second);
72 // Bail if the types are different.
73 if (a1 !== a2) {
74 return false;
75 }
76 // If they are both arrays, compare them.
77 if (a1 && a2) {
78 return deepArrayEqual(first, second);
79 }
80 // At this point, they must both be objects.
81 return deepObjectEqual(first, second);
82 }
83 JSONExt.deepEqual = deepEqual;
84 /**
85 * Create a deep copy of a JSON value.
86 *
87 * @param value - The JSON value to copy.
88 *
89 * @returns A deep copy of the given JSON value.
90 */
91 function deepCopy(value) {
92 // Do nothing for primitive values.
93 if (isPrimitive(value)) {
94 return value;
95 }
96 // Deep copy an array.
97 if (isArray(value)) {
98 return deepArrayCopy(value);
99 }
100 // Deep copy an object.
101 return deepObjectCopy(value);
102 }
103 JSONExt.deepCopy = deepCopy;
104 /**
105 * Compare two JSON arrays for deep equality.
106 */
107 function deepArrayEqual(first, second) {
108 // Check referential equality first.
109 if (first === second) {
110 return true;
111 }
112 // Test the arrays for equal length.
113 if (first.length !== second.length) {
114 return false;
115 }
116 // Compare the values for equality.
117 for (var i = 0, n = first.length; i < n; ++i) {
118 if (!deepEqual(first[i], second[i])) {
119 return false;
120 }
121 }
122 // At this point, the arrays are equal.
123 return true;
124 }
125 /**
126 * Compare two JSON objects for deep equality.
127 */
128 function deepObjectEqual(first, second) {
129 // Check referential equality first.
130 if (first === second) {
131 return true;
132 }
133 // Check for the first object's keys in the second object.
134 for (var key in first) {
135 if (first[key] !== undefined && !(key in second)) {
136 return false;
137 }
138 }
139 // Check for the second object's keys in the first object.
140 for (var key in second) {
141 if (second[key] !== undefined && !(key in first)) {
142 return false;
143 }
144 }
145 // Compare the values for equality.
146 for (var key in first) {
147 // Get the values.
148 var firstValue = first[key];
149 var secondValue = second[key];
150 // If both are undefined, ignore the key.
151 if (firstValue === undefined && secondValue === undefined) {
152 continue;
153 }
154 // If only one value is undefined, the objects are not equal.
155 if (firstValue === undefined || secondValue === undefined) {
156 return false;
157 }
158 // Compare the values.
159 if (!deepEqual(firstValue, secondValue)) {
160 return false;
161 }
162 }
163 // At this point, the objects are equal.
164 return true;
165 }
166 /**
167 * Create a deep copy of a JSON array.
168 */
169 function deepArrayCopy(value) {
170 var result = new Array(value.length);
171 for (var i = 0, n = value.length; i < n; ++i) {
172 result[i] = deepCopy(value[i]);
173 }
174 return result;
175 }
176 /**
177 * Create a deep copy of a JSON object.
178 */
179 function deepObjectCopy(value) {
180 var result = {};
181 for (var key in value) {
182 // Ignore undefined values.
183 var subvalue = value[key];
184 if (subvalue === undefined) {
185 continue;
186 }
187 result[key] = deepCopy(subvalue);
188 }
189 return result;
190 }
191 })(exports.JSONExt || (exports.JSONExt = {}));
192
193 // Copyright (c) Jupyter Development Team.
194 // Distributed under the terms of the Modified BSD License.
195 /*-----------------------------------------------------------------------------
196 | Copyright (c) 2014-2017, PhosphorJS Contributors
197 |
198 | Distributed under the terms of the BSD 3-Clause License.
199 |
200 | The full license is in the file LICENSE, distributed with this software.
201 |----------------------------------------------------------------------------*/
202 /**
203 * An object which stores MIME data for general application use.
204 *
205 * #### Notes
206 * This class does not attempt to enforce "correctness" of MIME types
207 * and their associated data. Since this class is designed to transfer
208 * arbitrary data and objects within the same application, it assumes
209 * that the user provides correct and accurate data.
210 */
211 var MimeData = /** @class */ (function () {
212 function MimeData() {
213 this._types = [];
214 this._values = [];
215 }
216 /**
217 * Get an array of the MIME types contained within the dataset.
218 *
219 * @returns A new array of the MIME types, in order of insertion.
220 */
221 MimeData.prototype.types = function () {
222 return this._types.slice();
223 };
224 /**
225 * Test whether the dataset has an entry for the given type.
226 *
227 * @param mime - The MIME type of interest.
228 *
229 * @returns `true` if the dataset contains a value for the given
230 * MIME type, `false` otherwise.
231 */
232 MimeData.prototype.hasData = function (mime) {
233 return this._types.indexOf(mime) !== -1;
234 };
235 /**
236 * Get the data value for the given MIME type.
237 *
238 * @param mime - The MIME type of interest.
239 *
240 * @returns The value for the given MIME type, or `undefined` if
241 * the dataset does not contain a value for the type.
242 */
243 MimeData.prototype.getData = function (mime) {
244 var i = this._types.indexOf(mime);
245 return i !== -1 ? this._values[i] : undefined;
246 };
247 /**
248 * Set the data value for the given MIME type.
249 *
250 * @param mime - The MIME type of interest.
251 *
252 * @param data - The data value for the given MIME type.
253 *
254 * #### Notes
255 * This will overwrite any previous entry for the MIME type.
256 */
257 MimeData.prototype.setData = function (mime, data) {
258 this.clearData(mime);
259 this._types.push(mime);
260 this._values.push(data);
261 };
262 /**
263 * Remove the data entry for the given MIME type.
264 *
265 * @param mime - The MIME type of interest.
266 *
267 * #### Notes
268 * This is a no-op if there is no entry for the given MIME type.
269 */
270 MimeData.prototype.clearData = function (mime) {
271 var i = this._types.indexOf(mime);
272 if (i !== -1) {
273 this._types.splice(i, 1);
274 this._values.splice(i, 1);
275 }
276 };
277 /**
278 * Remove all data entries from the dataset.
279 */
280 MimeData.prototype.clear = function () {
281 this._types.length = 0;
282 this._values.length = 0;
283 };
284 return MimeData;
285 }());
286
287 // Copyright (c) Jupyter Development Team.
288 // Distributed under the terms of the Modified BSD License.
289 /*-----------------------------------------------------------------------------
290 | Copyright (c) 2014-2017, PhosphorJS Contributors
291 |
292 | Distributed under the terms of the BSD 3-Clause License.
293 |
294 | The full license is in the file LICENSE, distributed with this software.
295 |----------------------------------------------------------------------------*/
296 /**
297 * A class which wraps a promise into a delegate object.
298 *
299 * #### Notes
300 * This class is useful when the logic to resolve or reject a promise
301 * cannot be defined at the point where the promise is created.
302 */
303 var PromiseDelegate = /** @class */ (function () {
304 /**
305 * Construct a new promise delegate.
306 */
307 function PromiseDelegate() {
308 var _this = this;
309 this.promise = new Promise(function (resolve, reject) {
310 _this._resolve = resolve;
311 _this._reject = reject;
312 });
313 }
314 /**
315 * Resolve the wrapped promise with the given value.
316 *
317 * @param value - The value to use for resolving the promise.
318 */
319 PromiseDelegate.prototype.resolve = function (value) {
320 var resolve = this._resolve;
321 resolve(value);
322 };
323 /**
324 * Reject the wrapped promise with the given value.
325 *
326 * @reason - The reason for rejecting the promise.
327 */
328 PromiseDelegate.prototype.reject = function (reason) {
329 var reject = this._reject;
330 reject(reason);
331 };
332 return PromiseDelegate;
333 }());
334
335 // Copyright (c) Jupyter Development Team.
336 // Distributed under the terms of the Modified BSD License.
337 /*-----------------------------------------------------------------------------
338 | Copyright (c) 2014-2017, PhosphorJS Contributors
339 |
340 | Distributed under the terms of the BSD 3-Clause License.
341 |
342 | The full license is in the file LICENSE, distributed with this software.
343 |----------------------------------------------------------------------------*/
344 /**
345 * A runtime object which captures compile-time type information.
346 *
347 * #### Notes
348 * A token captures the compile-time type of an interface or class in
349 * an object which can be used at runtime in a type-safe fashion.
350 */
351 var Token = /** @class */ (function () {
352 /**
353 * Construct a new token.
354 *
355 * @param name - A human readable name for the token.
356 */
357 function Token(name) {
358 this.name = name;
359 this._tokenStructuralPropertyT = null;
360 }
361 return Token;
362 }());
363
364 // Copyright (c) Jupyter Development Team.
365 // Distributed under the terms of the Modified BSD License.
366 /*-----------------------------------------------------------------------------
367 | Copyright (c) 2014-2017, PhosphorJS Contributors
368 |
369 | Distributed under the terms of the BSD 3-Clause License.
370 |
371 | The full license is in the file LICENSE, distributed with this software.
372 |----------------------------------------------------------------------------*/
373 // Fallback
374 function fallbackRandomValues(buffer) {
375 var value = 0;
376 for (var i = 0, n = buffer.length; i < n; ++i) {
377 if (i % 4 === 0) {
378 value = (Math.random() * 0xffffffff) >>> 0;
379 }
380 buffer[i] = value & 0xff;
381 value >>>= 8;
382 }
383 }
384
385 // Copyright (c) Jupyter Development Team.
386 /**
387 * The namespace for random number related functionality.
388 */
389 exports.Random = void 0;
390 (function (Random) {
391 /**
392 * A function which generates random bytes.
393 *
394 * @param buffer - The `Uint8Array` to fill with random bytes.
395 *
396 * #### Notes
397 * A cryptographically strong random number generator will be used if
398 * available. Otherwise, `Math.random` will be used as a fallback for
399 * randomness.
400 *
401 * The following RNGs are supported, listed in order of precedence:
402 * - `window.crypto.getRandomValues`
403 * - `window.msCrypto.getRandomValues`
404 * - `require('crypto').randomFillSync
405 * - `require('crypto').randomBytes
406 * - `Math.random`
407 */
408 Random.getRandomValues = (function () {
409 // Look up the crypto module if available.
410 var crypto = (typeof window !== 'undefined' && (window.crypto || window.msCrypto)) ||
411 null;
412 // Modern browsers and IE 11
413 if (crypto && typeof crypto.getRandomValues === 'function') {
414 return function getRandomValues(buffer) {
415 return crypto.getRandomValues(buffer);
416 };
417 }
418 // Fallback
419 return fallbackRandomValues;
420 })();
421 })(exports.Random || (exports.Random = {}));
422
423 // Copyright (c) Jupyter Development Team.
424 // Distributed under the terms of the Modified BSD License.
425 /*-----------------------------------------------------------------------------
426 | Copyright (c) 2014-2017, PhosphorJS Contributors
427 |
428 | Distributed under the terms of the BSD 3-Clause License.
429 |
430 | The full license is in the file LICENSE, distributed with this software.
431 |----------------------------------------------------------------------------*/
432 /**
433 * A function which creates a function that generates UUID v4 identifiers.
434 *
435 * @returns A new function that creates a UUID v4 string.
436 *
437 * #### Notes
438 * This implementation complies with RFC 4122.
439 *
440 * This uses `Random.getRandomValues()` for random bytes, which in
441 * turn will use the underlying `crypto` module of the platform if
442 * it is available. The fallback for randomness is `Math.random`.
443 */
444 function uuid4Factory(getRandomValues) {
445 // Create a 16 byte array to hold the random values.
446 var bytes = new Uint8Array(16);
447 // Create a look up table from bytes to hex strings.
448 var lut = new Array(256);
449 // Pad the single character hex digits with a leading zero.
450 for (var i = 0; i < 16; ++i) {
451 lut[i] = '0' + i.toString(16);
452 }
453 // Populate the rest of the hex digits.
454 for (var i = 16; i < 256; ++i) {
455 lut[i] = i.toString(16);
456 }
457 // Return a function which generates the UUID.
458 return function uuid4() {
459 // Get a new batch of random values.
460 getRandomValues(bytes);
461 // Set the UUID version number to 4.
462 bytes[6] = 0x40 | (bytes[6] & 0x0f);
463 // Set the clock sequence bit to the RFC spec.
464 bytes[8] = 0x80 | (bytes[8] & 0x3f);
465 // Assemble the UUID string.
466 return (lut[bytes[0]] +
467 lut[bytes[1]] +
468 lut[bytes[2]] +
469 lut[bytes[3]] +
470 '-' +
471 lut[bytes[4]] +
472 lut[bytes[5]] +
473 '-' +
474 lut[bytes[6]] +
475 lut[bytes[7]] +
476 '-' +
477 lut[bytes[8]] +
478 lut[bytes[9]] +
479 '-' +
480 lut[bytes[10]] +
481 lut[bytes[11]] +
482 lut[bytes[12]] +
483 lut[bytes[13]] +
484 lut[bytes[14]] +
485 lut[bytes[15]]);
486 };
487 }
488
489 // Copyright (c) Jupyter Development Team.
490 /**
491 * The namespace for UUID related functionality.
492 */
493 exports.UUID = void 0;
494 (function (UUID) {
495 /**
496 * A function which generates UUID v4 identifiers.
497 *
498 * @returns A new UUID v4 string.
499 *
500 * #### Notes
501 * This implementation complies with RFC 4122.
502 *
503 * This uses `Random.getRandomValues()` for random bytes, which in
504 * turn will use the underlying `crypto` module of the platform if
505 * it is available. The fallback for randomness is `Math.random`.
506 */
507 UUID.uuid4 = uuid4Factory(exports.Random.getRandomValues);
508 })(exports.UUID || (exports.UUID = {}));
509
510 exports.MimeData = MimeData;
511 exports.PromiseDelegate = PromiseDelegate;
512 exports.Token = Token;
513
514 Object.defineProperty(exports, '__esModule', { value: true });
515
516})));
517//# sourceMappingURL=index.js.map