From 2879b9086c1ace24fc3307e6af5b404fff0059c2 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 14 Jun 2021 21:32:11 +0100 Subject: [PATCH] Use alphabet from js-sdk --- src/utils/stringOrderField.ts | 13 ++------ test/utils/stringOrderField-test.ts | 50 +++++++++++++---------------- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/utils/stringOrderField.ts b/src/utils/stringOrderField.ts index e09f7fbea4..4336583b9d 100644 --- a/src/utils/stringOrderField.ts +++ b/src/utils/stringOrderField.ts @@ -14,23 +14,16 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { alphabetPad, baseToString, stringToBase } from "matrix-js-sdk/src/utils"; +import { alphabetPad, baseToString, stringToBase, DEFAULT_ALPHABET } from "matrix-js-sdk/src/utils"; import { reorder } from "./arrays"; -export const ALPHABET_START = 0x20; -export const ALPHABET_END = 0x7E; -export const ALPHABET = new Array(1 + ALPHABET_END - ALPHABET_START) - .fill(undefined) - .map((_, i) => String.fromCharCode(ALPHABET_START + i)) - .join(""); - export const midPointsBetweenStrings = ( a: string, b: string, count: number, maxLen: number, - alphabet = ALPHABET, + alphabet = DEFAULT_ALPHABET, ): string[] => { const padN = Math.min(Math.max(a.length, b.length), maxLen); const padA = alphabetPad(a, padN, alphabet); @@ -144,7 +137,7 @@ export const reorderLexicographically = ( const prevOrder = newOrder[leftBoundIdx - 1]?.order ?? ""; const nextOrder = newOrder[rightBoundIdx + 1]?.order - ?? String.fromCharCode(ALPHABET_END).repeat(prevOrder.length || 1); + ?? DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1).repeat(prevOrder.length || 1); const changes = midPointsBetweenStrings(prevOrder, nextOrder, 1 + rightBoundIdx - leftBoundIdx, maxLen); diff --git a/test/utils/stringOrderField-test.ts b/test/utils/stringOrderField-test.ts index d5671ebe76..a8bc00eeb9 100644 --- a/test/utils/stringOrderField-test.ts +++ b/test/utils/stringOrderField-test.ts @@ -15,13 +15,9 @@ limitations under the License. */ import { sortBy } from "lodash"; -import { stringToBase, baseToString, averageBetweenStrings } from "matrix-js-sdk/src/utils"; +import { stringToBase, baseToString, averageBetweenStrings, DEFAULT_ALPHABET } from "matrix-js-sdk/src/utils"; -import { - ALPHABET, - midPointsBetweenStrings, - reorderLexicographically, -} from "../../src/utils/stringOrderField"; +import { midPointsBetweenStrings, reorderLexicographically } from "../../src/utils/stringOrderField"; const moveLexicographicallyTest = ( orders: Array, @@ -66,7 +62,7 @@ describe("stringOrderField", () => { }); it("baseToString", () => { - expect(baseToString(BigInt(10))).toBe(ALPHABET[9]); + expect(baseToString(BigInt(10))).toBe(DEFAULT_ALPHABET[9]); expect(baseToString(BigInt(10), "abcdefghijklmnopqrstuvwxyz")).toEqual("j"); expect(baseToString(BigInt(6241))).toEqual("`a"); expect(baseToString(BigInt(53), "abcdefghijklmnopqrstuvwxyz")).toEqual("ba"); @@ -246,13 +242,13 @@ describe("stringOrderField", () => { moveLexicographicallyTest( [ - ALPHABET.charAt(0), + DEFAULT_ALPHABET.charAt(0), // Target - ALPHABET.charAt(1), - ALPHABET.charAt(2), - ALPHABET.charAt(3), - ALPHABET.charAt(4), - ALPHABET.charAt(5), + DEFAULT_ALPHABET.charAt(1), + DEFAULT_ALPHABET.charAt(2), + DEFAULT_ALPHABET.charAt(3), + DEFAULT_ALPHABET.charAt(4), + DEFAULT_ALPHABET.charAt(5), ], 5, 1, @@ -272,11 +268,11 @@ describe("stringOrderField", () => { moveLexicographicallyTest( [ - ALPHABET.charAt(ALPHABET.length - 5), - ALPHABET.charAt(ALPHABET.length - 4), - ALPHABET.charAt(ALPHABET.length - 3), - ALPHABET.charAt(ALPHABET.length - 2), - ALPHABET.charAt(ALPHABET.length - 1), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 5), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 4), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 3), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 2), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1), ], 1, 3, @@ -313,11 +309,11 @@ describe("stringOrderField", () => { moveLexicographicallyTest( [ - ALPHABET.charAt(ALPHABET.length - 5), - ALPHABET.charAt(ALPHABET.length - 4), - ALPHABET.charAt(ALPHABET.length - 3), - ALPHABET.charAt(ALPHABET.length - 2), - ALPHABET.charAt(ALPHABET.length - 1), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 5), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 4), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 3), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 2), + DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1), ], 4, 3, @@ -339,10 +335,10 @@ describe("stringOrderField", () => { const fiftyFirstChar = " ".repeat(51); expect(next(maxSpaceValue)).toBe(fiftyFirstChar); expect(prev(fiftyFirstChar)).toBe(maxSpaceValue); - expect(Number(stringToBase(ALPHABET[0]))).toEqual(1); - expect(Number(stringToBase(ALPHABET[1]))).toEqual(2); - expect(ALPHABET[ALPHABET.length - 1]).toBe("~"); - expect(ALPHABET[0]).toBe(" "); + expect(Number(stringToBase(DEFAULT_ALPHABET[0]))).toEqual(1); + expect(Number(stringToBase(DEFAULT_ALPHABET[1]))).toEqual(2); + expect(DEFAULT_ALPHABET[DEFAULT_ALPHABET.length - 1]).toBe("~"); + expect(DEFAULT_ALPHABET[0]).toBe(" "); }); });