UNPKG

19.4 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 let a1 = isArray(first);
71 let 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 (let 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 (let 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 (let key in second) {
141 if (second[key] !== undefined && !(key in first)) {
142 return false;
143 }
144 }
145 // Compare the values for equality.
146 for (let key in first) {
147 // Get the values.
148 let firstValue = first[key];
149 let 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 let result = new Array(value.length);
171 for (let 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 let result = {};
181 for (let key in value) {
182 // Ignore undefined values.
183 let 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 class MimeData {
212 constructor() {
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 types() {
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 hasData(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 getData(mime) {
244 let 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 setData(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 clearData(mime) {
271 let 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 clear() {
281 this._types.length = 0;
282 this._values.length = 0;
283 }
284 }
285
286 // Copyright (c) Jupyter Development Team.
287 // Distributed under the terms of the Modified BSD License.
288 /*-----------------------------------------------------------------------------
289 | Copyright (c) 2014-2017, PhosphorJS Contributors
290 |
291 | Distributed under the terms of the BSD 3-Clause License.
292 |
293 | The full license is in the file LICENSE, distributed with this software.
294 |----------------------------------------------------------------------------*/
295 /**
296 * A class which wraps a promise into a delegate object.
297 *
298 * #### Notes
299 * This class is useful when the logic to resolve or reject a promise
300 * cannot be defined at the point where the promise is created.
301 */
302 class PromiseDelegate {
303 /**
304 * Construct a new promise delegate.
305 */
306 constructor() {
307 this.promise = new Promise((resolve, reject) => {
308 this._resolve = resolve;
309 this._reject = reject;
310 });
311 }
312 /**
313 * Resolve the wrapped promise with the given value.
314 *
315 * @param value - The value to use for resolving the promise.
316 */
317 resolve(value) {
318 let resolve = this._resolve;
319 resolve(value);
320 }
321 /**
322 * Reject the wrapped promise with the given value.
323 *
324 * @reason - The reason for rejecting the promise.
325 */
326 reject(reason) {
327 let reject = this._reject;
328 reject(reason);
329 }
330 }
331
332 // Copyright (c) Jupyter Development Team.
333 // Distributed under the terms of the Modified BSD License.
334 /*-----------------------------------------------------------------------------
335 | Copyright (c) 2014-2017, PhosphorJS Contributors
336 |
337 | Distributed under the terms of the BSD 3-Clause License.
338 |
339 | The full license is in the file LICENSE, distributed with this software.
340 |----------------------------------------------------------------------------*/
341 /**
342 * A runtime object which captures compile-time type information.
343 *
344 * #### Notes
345 * A token captures the compile-time type of an interface or class in
346 * an object which can be used at runtime in a type-safe fashion.
347 */
348 class Token {
349 /**
350 * Construct a new token.
351 *
352 * @param name - A human readable name for the token.
353 * @param description - Token purpose description for documentation.
354 */
355 constructor(name, description) {
356 this.name = name;
357 this.description = description !== null && description !== void 0 ? description : '';
358 this._tokenStructuralPropertyT = null;
359 }
360 }
361
362 // Copyright (c) Jupyter Development Team.
363 // Distributed under the terms of the Modified BSD License.
364 /*-----------------------------------------------------------------------------
365 | Copyright (c) 2014-2017, PhosphorJS Contributors
366 |
367 | Distributed under the terms of the BSD 3-Clause License.
368 |
369 | The full license is in the file LICENSE, distributed with this software.
370 |----------------------------------------------------------------------------*/
371 // Fallback
372 function fallbackRandomValues(buffer) {
373 let value = 0;
374 for (let i = 0, n = buffer.length; i < n; ++i) {
375 if (i % 4 === 0) {
376 value = (Math.random() * 0xffffffff) >>> 0;
377 }
378 buffer[i] = value & 0xff;
379 value >>>= 8;
380 }
381 }
382
383 // Copyright (c) Jupyter Development Team.
384 // Distributed under the terms of the Modified BSD License.
385 /*-----------------------------------------------------------------------------
386 | Copyright (c) 2014-2017, PhosphorJS Contributors
387 |
388 | Distributed under the terms of the BSD 3-Clause License.
389 |
390 | The full license is in the file LICENSE, distributed with this software.
391 |----------------------------------------------------------------------------*/
392 /**
393 * The namespace for random number related functionality.
394 */
395 exports.Random = void 0;
396 (function (Random) {
397 /**
398 * A function which generates random bytes.
399 *
400 * @param buffer - The `Uint8Array` to fill with random bytes.
401 *
402 * #### Notes
403 * A cryptographically strong random number generator will be used if
404 * available. Otherwise, `Math.random` will be used as a fallback for
405 * randomness.
406 *
407 * The following RNGs are supported, listed in order of precedence:
408 * - `window.crypto.getRandomValues`
409 * - `window.msCrypto.getRandomValues`
410 * - `require('crypto').randomFillSync
411 * - `require('crypto').randomBytes
412 * - `Math.random`
413 */
414 Random.getRandomValues = (() => {
415 // Look up the crypto module if available.
416 const crypto = (typeof window !== 'undefined' && (window.crypto || window.msCrypto)) ||
417 null;
418 // Modern browsers and IE 11
419 if (crypto && typeof crypto.getRandomValues === 'function') {
420 return function getRandomValues(buffer) {
421 return crypto.getRandomValues(buffer);
422 };
423 }
424 // Fallback
425 return fallbackRandomValues;
426 })();
427 })(exports.Random || (exports.Random = {}));
428
429 // Copyright (c) Jupyter Development Team.
430 // Distributed under the terms of the Modified BSD License.
431 /*-----------------------------------------------------------------------------
432 | Copyright (c) 2014-2017, PhosphorJS Contributors
433 |
434 | Distributed under the terms of the BSD 3-Clause License.
435 |
436 | The full license is in the file LICENSE, distributed with this software.
437 |----------------------------------------------------------------------------*/
438 /**
439 * A function which creates a function that generates UUID v4 identifiers.
440 *
441 * @returns A new function that creates a UUID v4 string.
442 *
443 * #### Notes
444 * This implementation complies with RFC 4122.
445 *
446 * This uses `Random.getRandomValues()` for random bytes, which in
447 * turn will use the underlying `crypto` module of the platform if
448 * it is available. The fallback for randomness is `Math.random`.
449 */
450 function uuid4Factory(getRandomValues) {
451 // Create a 16 byte array to hold the random values.
452 const bytes = new Uint8Array(16);
453 // Create a look up table from bytes to hex strings.
454 const lut = new Array(256);
455 // Pad the single character hex digits with a leading zero.
456 for (let i = 0; i < 16; ++i) {
457 lut[i] = '0' + i.toString(16);
458 }
459 // Populate the rest of the hex digits.
460 for (let i = 16; i < 256; ++i) {
461 lut[i] = i.toString(16);
462 }
463 // Return a function which generates the UUID.
464 return function uuid4() {
465 // Get a new batch of random values.
466 getRandomValues(bytes);
467 // Set the UUID version number to 4.
468 bytes[6] = 0x40 | (bytes[6] & 0x0f);
469 // Set the clock sequence bit to the RFC spec.
470 bytes[8] = 0x80 | (bytes[8] & 0x3f);
471 // Assemble the UUID string.
472 return (lut[bytes[0]] +
473 lut[bytes[1]] +
474 lut[bytes[2]] +
475 lut[bytes[3]] +
476 '-' +
477 lut[bytes[4]] +
478 lut[bytes[5]] +
479 '-' +
480 lut[bytes[6]] +
481 lut[bytes[7]] +
482 '-' +
483 lut[bytes[8]] +
484 lut[bytes[9]] +
485 '-' +
486 lut[bytes[10]] +
487 lut[bytes[11]] +
488 lut[bytes[12]] +
489 lut[bytes[13]] +
490 lut[bytes[14]] +
491 lut[bytes[15]]);
492 };
493 }
494
495 // Copyright (c) Jupyter Development Team.
496 // Distributed under the terms of the Modified BSD License.
497 /*-----------------------------------------------------------------------------
498 | Copyright (c) 2014-2017, PhosphorJS Contributors
499 |
500 | Distributed under the terms of the BSD 3-Clause License.
501 |
502 | The full license is in the file LICENSE, distributed with this software.
503 |----------------------------------------------------------------------------*/
504 /**
505 * The namespace for UUID related functionality.
506 */
507 exports.UUID = void 0;
508 (function (UUID) {
509 /**
510 * A function which generates UUID v4 identifiers.
511 *
512 * @returns A new UUID v4 string.
513 *
514 * #### Notes
515 * This implementation complies with RFC 4122.
516 *
517 * This uses `Random.getRandomValues()` for random bytes, which in
518 * turn will use the underlying `crypto` module of the platform if
519 * it is available. The fallback for randomness is `Math.random`.
520 */
521 UUID.uuid4 = uuid4Factory(exports.Random.getRandomValues);
522 })(exports.UUID || (exports.UUID = {}));
523
524 exports.MimeData = MimeData;
525 exports.PromiseDelegate = PromiseDelegate;
526 exports.Token = Token;
527
528}));
529//# sourceMappingURL=index.js.map