UNPKG

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