main.js (1914B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MAIN // 22 23 /** 24 * Tests if two arguments are the same value. 25 * 26 * ## Notes 27 * 28 * - The function implements the [SameValue Algorithm][ecma-262-same-value-algorithm], as specified in ECMAScript 5. 29 * - In contrast to the strict equality operator `===`, `-0` and `+0` are distinguishable and `NaNs` are the same. 30 * 31 * [ecma-262-same-value-algorithm]: http://ecma-international.org/ecma-262/5.1/#sec-9.12 32 * 33 * @param {*} a - first input value 34 * @param {*} b - second input value 35 * @returns {boolean} boolean indicating whether two arguments are the same value 36 * 37 * @example 38 * var bool = isSameValue( true, true ); 39 * // returns true 40 * 41 * @example 42 * var bool = isSameValue( 3.14, 3.14 ); 43 * // returns true 44 * 45 * @example 46 * var bool = isSameValue( {}, {} ); 47 * // returns false 48 * 49 * @example 50 * var bool = isSameValue( -0.0, -0.0 ); 51 * // returns true 52 * 53 * @example 54 * var bool = isSameValue( -0.0, 0.0 ); 55 * // returns false 56 * 57 * @example 58 * var bool = isSameValue( NaN, NaN ); 59 * // returns true 60 * 61 * @example 62 * var bool = isSameValue( [], [] ); 63 * // returns false 64 */ 65 function isSameValue( a, b ) { 66 if ( a === b ) { 67 if ( a === 0.0 ) { 68 return 1.0 / a === 1.0 / b; // handles +-0 69 } 70 return true; 71 } 72 return ( a !== a && b !== b ); // handles NaNs 73 } 74 75 76 // EXPORTS // 77 78 module.exports = isSameValue;