From ffd2a62c3f7df94365980302fef3bc3376b48182 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 3 Aug 2020 13:00:48 +0530 Subject: modularize repo, use pnpm, improve typechecking --- packages/idb-bridge/src/util/cmp.ts | 146 ++++++++++++++++++------------------ 1 file changed, 73 insertions(+), 73 deletions(-) (limited to 'packages/idb-bridge/src/util/cmp.ts') diff --git a/packages/idb-bridge/src/util/cmp.ts b/packages/idb-bridge/src/util/cmp.ts index 9d0dc99a2..ddd43f2a6 100644 --- a/packages/idb-bridge/src/util/cmp.ts +++ b/packages/idb-bridge/src/util/cmp.ts @@ -18,91 +18,91 @@ import { DataError } from "./errors"; import valueToKey from "./valueToKey"; const getType = (x: any) => { - if (typeof x === "number") { - return "Number"; - } - if (x instanceof Date) { - return "Date"; - } - if (Array.isArray(x)) { - return "Array"; - } - if (typeof x === "string") { - return "String"; - } - if (x instanceof ArrayBuffer) { - return "Binary"; - } - - throw new DataError(); + if (typeof x === "number") { + return "Number"; + } + if (x instanceof Date) { + return "Date"; + } + if (Array.isArray(x)) { + return "Array"; + } + if (typeof x === "string") { + return "String"; + } + if (x instanceof ArrayBuffer) { + return "Binary"; + } + + throw new DataError(); }; // https://w3c.github.io/IndexedDB/#compare-two-keys const compareKeys = (first: any, second: any): -1 | 0 | 1 => { - if (second === undefined) { - throw new TypeError(); - } + if (second === undefined) { + throw new TypeError(); + } - first = valueToKey(first); - second = valueToKey(second); - - const t1 = getType(first); - const t2 = getType(second); - - if (t1 !== t2) { - if (t1 === "Array") { - return 1; - } - if ( - t1 === "Binary" && - (t2 === "String" || t2 === "Date" || t2 === "Number") - ) { - return 1; - } - if (t1 === "String" && (t2 === "Date" || t2 === "Number")) { - return 1; - } - if (t1 === "Date" && t2 === "Number") { - return 1; - } - return -1; - } + first = valueToKey(first); + second = valueToKey(second); - if (t1 === "Binary") { - first = new Uint8Array(first); - second = new Uint8Array(second); + const t1 = getType(first); + const t2 = getType(second); + + if (t1 !== t2) { + if (t1 === "Array") { + return 1; + } + if ( + t1 === "Binary" && + (t2 === "String" || t2 === "Date" || t2 === "Number") + ) { + return 1; + } + if (t1 === "String" && (t2 === "Date" || t2 === "Number")) { + return 1; + } + if (t1 === "Date" && t2 === "Number") { + return 1; + } + return -1; + } + + if (t1 === "Binary") { + first = new Uint8Array(first); + second = new Uint8Array(second); + } + + if (t1 === "Array" || t1 === "Binary") { + const length = Math.min(first.length, second.length); + for (let i = 0; i < length; i++) { + const result = compareKeys(first[i], second[i]); + + if (result !== 0) { + return result; + } } - if (t1 === "Array" || t1 === "Binary") { - const length = Math.min(first.length, second.length); - for (let i = 0; i < length; i++) { - const result = compareKeys(first[i], second[i]); - - if (result !== 0) { - return result; - } - } - - if (first.length > second.length) { - return 1; - } - if (first.length < second.length) { - return -1; - } - return 0; + if (first.length > second.length) { + return 1; + } + if (first.length < second.length) { + return -1; } + return 0; + } - if (t1 === "Date") { - if (first.getTime() === second.getTime()) { - return 0; - } - } else { - if (first === second) { - return 0; - } + if (t1 === "Date") { + if (first.getTime() === second.getTime()) { + return 0; + } + } else { + if (first === second) { + return 0; } + } - return first > second ? 1 : -1; + return first > second ? 1 : -1; }; export default compareKeys; -- cgit v1.2.3