UNPKG

js-utl

Version:

A collection of JS utility functions to be used across several applications or libraries.

93 lines (83 loc) 2.52 kB
/** * Copyright (c) 2013-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. * * @typechecks * */ /** * @type {Function} */ const hasOwnProperty = Object.prototype.hasOwnProperty; /** * Function implementing "Object.is" behaviour. * * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is * * @param {*} x The first value to compare. * @param {*} y The second value to compare. * @return {boolean} A boolean indicating whether or not the two arguments are the same value. */ export function is(x, y) { // SameValue algorithm if (x === y) { // Steps 1-5, 7-10 // Steps 6.b-6.e: +0 != -0 return x !== 0 || 1 / x === 1 / y; } else { // Step 6.a: NaN == NaN return x !== x && y !== y; } } /** * Checks whether a prop of an object equals in the other object (shallow comparison). * * @param {Object} objA The first object. * @param {Object} objB The second object. * @param {string} prop The name of the property. * @return {boolean} True if the value of "prop" in "objA" is shallowly equal to the value of "prop" in "objB". */ export function objectPropEqual(objA, objB, prop) { return hasOwnProperty.call(objB, prop) && is(objA[prop], objB[prop]); } /** * Performs equality by iterating through keys on an object and returning "false" * when any key has values which are not strictly equal between the arguments. * Returns "true" when the values of all keys are strictly equal. * * @see https://stackoverflow.com/questions/22266826/how-can-i-do-a-shallow-comparison-of-the-properties-of-two-objects-with-javascri#answer-37636728 * * @param {*} objA First object. * @param {*} objB Second object. * @return {boolean} */ export function shallowEqual(objA, objB) { if (is(objA, objB)) { return true; } if ( typeof objA !== "object" || objA === null || typeof objB !== "object" || objB === null ) { return false; } const keysA = Object.keys(objA); const keysB = Object.keys(objB); if (keysA.length !== keysB.length) { return false; } // Test for A's keys different from B. for (let i = 0; i < keysA.length; i++) { const prop = keysA[i]; if (!objectPropEqual(objA, objB, prop)) { return false; } } return true; }