UNPKG

jupyterlab-emrys

Version:

A computational environment for Jupyter. Powered by Emrys

132 lines (131 loc) 3.72 kB
// Copyright (c) Jupyter Development Team. // Distributed under the terms of the Modified BSD License. "use strict"; /** * Test whether a JSON value is a primitive. * * @param value - The JSON value of interest. * * @returns `true` if the value is a primitive or `null`, * `false` otherwise. */ function isPrimitive(value) { return (value === null || typeof value === 'boolean' || typeof value === 'number' || typeof value === 'string'); } exports.isPrimitive = isPrimitive; /** * Test whether a JSON value is an array. * * @param value - The JSON value of interest. * * @returns `true` if the value is a an array, `false` otherwise. */ function isArray(value) { return Array.isArray(value); } exports.isArray = isArray; /** * Test whether a JSON value is an object. * * @param value - The JSON value of interest. * * @returns `true` if the value is a an object, `false` otherwise. */ function isObject(value) { return !isPrimitive(value) && !isArray(value); } exports.isObject = isObject; /** * Compare two JSON values for deep equality. * * @param first - The first JSON value of interest. * * @param second - The second JSON value of interest. * * @returns `true` if the values are equivalent, `false` otherwise. */ function deepEqual(first, second) { // Check referential and primitive equality first. if (first === second) { return true; } // If one is a primitive, the `===` check ruled out the other. if (isPrimitive(first) || isPrimitive(second)) { return false; } // Bail if either is `undefined`. if (!first || !second) { return false; } // Test whether they are arrays. var a1 = isArray(first); var a2 = isArray(second); // Bail if the types are different. if (a1 !== a2) { return false; } // If they are both arrays, compare them. if (a1 && a2) { return Private.arrayEqual(first, second); } // At this point, they must both be objects. return Private.objectEqual(first, second); } exports.deepEqual = deepEqual; /** * The namespace for the private module data. */ var Private; (function (Private) { /** * Compare two JSON arrays for deep equality. */ function arrayEqual(first, second) { // Test the arrays for equal length. if (first.length !== second.length) { return false; } // Compare the values for equality. for (var i = 0, n = first.length; i < n; ++i) { if (!deepEqual(first[i], second[i])) { return false; } } // At this point, the arrays are equal. return true; } Private.arrayEqual = arrayEqual; /** * Compare two JSON objects for deep equality. */ function objectEqual(first, second) { // Get the keys for each object. var k1 = Object.keys(first); var k2 = Object.keys(second); // Test the keys for equal length. if (k1.length !== k2.length) { return false; } // Sort the keys for equivalent order. k1.sort(); k2.sort(); // Compare the keys for equality. for (var i = 0, n = k1.length; i < n; ++i) { if (k1[i] !== k2[i]) { return false; } } // Compare the values for equality. for (var i = 0, n = k1.length; i < n; ++i) { if (!deepEqual(first[k1[i]], second[k1[i]])) { return false; } } // At this point, the objects are equal. return true; } Private.objectEqual = objectEqual; })(Private || (Private = {}));