UNPKG

@itwin/core-bentley

Version:

Bentley JavaScript core components

140 lines 4.55 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * 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 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.compareWithTolerance = compareWithTolerance; exports.compareNumbers = compareNumbers; exports.compareBooleans = compareBooleans; exports.compareStrings = compareStrings; exports.comparePossiblyUndefined = comparePossiblyUndefined; exports.compareStringsOrUndefined = compareStringsOrUndefined; exports.compareNumbersOrUndefined = compareNumbersOrUndefined; exports.compareBooleansOrUndefined = compareBooleansOrUndefined; exports.areEqualPossiblyUndefined = areEqualPossiblyUndefined; exports.compareSimpleTypes = compareSimpleTypes; exports.compareSimpleArrays = compareSimpleArrays; exports.compareArrays = compareArrays; /** * An [[OrderedComparator]] for numbers that treats two numbers as equal if the absolute value of their difference is less than a specified tolerance. * @public */ function compareWithTolerance(a, b, tolerance = 0.1) { if (a < b - tolerance) return -1; else if (a > b + tolerance) return 1; else return 0; } /** @public */ function compareNumbers(a, b) { return a - b; } /** @public */ function compareBooleans(a, b) { return a !== b ? (a < b ? -1 : 1) : 0; } /** @public */ function compareStrings(a, b) { return a === b ? 0 : (a < b ? -1 : 1); } /** @public */ 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 */ function compareStringsOrUndefined(lhs, rhs) { return comparePossiblyUndefined(compareStrings, lhs, rhs); } /** @public */ function compareNumbersOrUndefined(lhs, rhs) { return comparePossiblyUndefined(compareNumbers, lhs, rhs); } /** @public */ 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 */ 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 */ 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 */ 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 */ 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