UNPKG

@itwin/core-bentley

Version:

Bentley JavaScript core components

126 lines 3.93 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Utils */ /** * An [[OrderedComparator]] for numbers that treats two numbers as equal if the absolute value of their difference is less than a specified tolerance. * @public */ export function compareWithTolerance(a, b, tolerance = 0.1) { if (a < b - tolerance) return -1; else if (a > b + tolerance) return 1; else return 0; } /** @public */ export function compareNumbers(a, b) { return a - b; } /** @public */ export function compareBooleans(a, b) { return a !== b ? (a < b ? -1 : 1) : 0; } /** @public */ export function compareStrings(a, b) { return a === b ? 0 : (a < b ? -1 : 1); } /** @public */ export function comparePossiblyUndefined(compareDefined, lhs, rhs) { if (undefined === lhs) return undefined === rhs ? 0 : -1; else if (undefined === rhs) return 1; else return compareDefined(lhs, rhs); } /** @public */ export function compareStringsOrUndefined(lhs, rhs) { return comparePossiblyUndefined(compareStrings, lhs, rhs); } /** @public */ export function compareNumbersOrUndefined(lhs, rhs) { return comparePossiblyUndefined(compareNumbers, lhs, rhs); } /** @public */ export function compareBooleansOrUndefined(lhs, rhs) { return comparePossiblyUndefined(compareBooleans, lhs, rhs); } /** Compare two possibly-undefined values for equality. If both are undefined, the comparison is performed by the supplied `areEqual` function. * @public */ export function areEqualPossiblyUndefined(t, u, areEqual) { if (undefined === t) return undefined === u; else if (undefined === u) return false; else return areEqual(t, u); } /** * Compare two simples types (number, string, boolean) * This essentially wraps the existing type-specific comparison functions * @beta */ export function compareSimpleTypes(lhs, rhs) { let cmp = 0; // Make sure the types are the same cmp = compareStrings(typeof lhs, typeof rhs); if (cmp !== 0) { return cmp; } // Compare actual values switch (typeof lhs) { case "number": return compareNumbers(lhs, rhs); case "string": return compareStrings(lhs, rhs); case "boolean": return compareBooleans(lhs, rhs); } return cmp; } /** * Compare two arrays of simple types (number, string, boolean) * @beta */ export function compareSimpleArrays(lhs, rhs) { if (undefined === lhs) return undefined === rhs ? 0 : -1; else if (undefined === rhs) return 1; else if (lhs.length === 0 && rhs.length === 0) { return 0; } else if (lhs.length !== rhs.length) { return lhs.length - rhs.length; } let cmp = 0; for (let i = 0; i < lhs.length; i++) { cmp = compareSimpleTypes(lhs[i], rhs[i]); if (cmp !== 0) { break; } } return cmp; } /** Compare two arrays of the same type `T` using the specified `compare` function to compare each pair of array elements. * @returns 0 if the arrays have the same length and `compare` returns 0 for each pair of elements, or a non-zero value if the arrays differ in length or contents. * @public */ export function compareArrays(lhs, rhs, compare) { let diff = compareNumbers(lhs.length, rhs.length); if (!diff) { for (let i = 0; i < lhs.length; i++) { diff = compare(lhs[i], rhs[i]); if (diff) { break; } } } return diff; } //# sourceMappingURL=Compare.js.map