mirror of https://github.com/vector-im/riot-web
Use alphabet from js-sdk
parent
8fd72fcf79
commit
2879b9086c
|
@ -14,23 +14,16 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
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";
|
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 = (
|
export const midPointsBetweenStrings = (
|
||||||
a: string,
|
a: string,
|
||||||
b: string,
|
b: string,
|
||||||
count: number,
|
count: number,
|
||||||
maxLen: number,
|
maxLen: number,
|
||||||
alphabet = ALPHABET,
|
alphabet = DEFAULT_ALPHABET,
|
||||||
): string[] => {
|
): string[] => {
|
||||||
const padN = Math.min(Math.max(a.length, b.length), maxLen);
|
const padN = Math.min(Math.max(a.length, b.length), maxLen);
|
||||||
const padA = alphabetPad(a, padN, alphabet);
|
const padA = alphabetPad(a, padN, alphabet);
|
||||||
|
@ -144,7 +137,7 @@ export const reorderLexicographically = (
|
||||||
|
|
||||||
const prevOrder = newOrder[leftBoundIdx - 1]?.order ?? "";
|
const prevOrder = newOrder[leftBoundIdx - 1]?.order ?? "";
|
||||||
const nextOrder = newOrder[rightBoundIdx + 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);
|
const changes = midPointsBetweenStrings(prevOrder, nextOrder, 1 + rightBoundIdx - leftBoundIdx, maxLen);
|
||||||
|
|
||||||
|
|
|
@ -15,13 +15,9 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { sortBy } from "lodash";
|
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 {
|
import { midPointsBetweenStrings, reorderLexicographically } from "../../src/utils/stringOrderField";
|
||||||
ALPHABET,
|
|
||||||
midPointsBetweenStrings,
|
|
||||||
reorderLexicographically,
|
|
||||||
} from "../../src/utils/stringOrderField";
|
|
||||||
|
|
||||||
const moveLexicographicallyTest = (
|
const moveLexicographicallyTest = (
|
||||||
orders: Array<string | undefined>,
|
orders: Array<string | undefined>,
|
||||||
|
@ -66,7 +62,7 @@ describe("stringOrderField", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it("baseToString", () => {
|
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(10), "abcdefghijklmnopqrstuvwxyz")).toEqual("j");
|
||||||
expect(baseToString(BigInt(6241))).toEqual("`a");
|
expect(baseToString(BigInt(6241))).toEqual("`a");
|
||||||
expect(baseToString(BigInt(53), "abcdefghijklmnopqrstuvwxyz")).toEqual("ba");
|
expect(baseToString(BigInt(53), "abcdefghijklmnopqrstuvwxyz")).toEqual("ba");
|
||||||
|
@ -246,13 +242,13 @@ describe("stringOrderField", () => {
|
||||||
|
|
||||||
moveLexicographicallyTest(
|
moveLexicographicallyTest(
|
||||||
[
|
[
|
||||||
ALPHABET.charAt(0),
|
DEFAULT_ALPHABET.charAt(0),
|
||||||
// Target
|
// Target
|
||||||
ALPHABET.charAt(1),
|
DEFAULT_ALPHABET.charAt(1),
|
||||||
ALPHABET.charAt(2),
|
DEFAULT_ALPHABET.charAt(2),
|
||||||
ALPHABET.charAt(3),
|
DEFAULT_ALPHABET.charAt(3),
|
||||||
ALPHABET.charAt(4),
|
DEFAULT_ALPHABET.charAt(4),
|
||||||
ALPHABET.charAt(5),
|
DEFAULT_ALPHABET.charAt(5),
|
||||||
],
|
],
|
||||||
5,
|
5,
|
||||||
1,
|
1,
|
||||||
|
@ -272,11 +268,11 @@ describe("stringOrderField", () => {
|
||||||
|
|
||||||
moveLexicographicallyTest(
|
moveLexicographicallyTest(
|
||||||
[
|
[
|
||||||
ALPHABET.charAt(ALPHABET.length - 5),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 5),
|
||||||
ALPHABET.charAt(ALPHABET.length - 4),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 4),
|
||||||
ALPHABET.charAt(ALPHABET.length - 3),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 3),
|
||||||
ALPHABET.charAt(ALPHABET.length - 2),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 2),
|
||||||
ALPHABET.charAt(ALPHABET.length - 1),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1),
|
||||||
],
|
],
|
||||||
1,
|
1,
|
||||||
3,
|
3,
|
||||||
|
@ -313,11 +309,11 @@ describe("stringOrderField", () => {
|
||||||
|
|
||||||
moveLexicographicallyTest(
|
moveLexicographicallyTest(
|
||||||
[
|
[
|
||||||
ALPHABET.charAt(ALPHABET.length - 5),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 5),
|
||||||
ALPHABET.charAt(ALPHABET.length - 4),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 4),
|
||||||
ALPHABET.charAt(ALPHABET.length - 3),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 3),
|
||||||
ALPHABET.charAt(ALPHABET.length - 2),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 2),
|
||||||
ALPHABET.charAt(ALPHABET.length - 1),
|
DEFAULT_ALPHABET.charAt(DEFAULT_ALPHABET.length - 1),
|
||||||
],
|
],
|
||||||
4,
|
4,
|
||||||
3,
|
3,
|
||||||
|
@ -339,10 +335,10 @@ describe("stringOrderField", () => {
|
||||||
const fiftyFirstChar = " ".repeat(51);
|
const fiftyFirstChar = " ".repeat(51);
|
||||||
expect(next(maxSpaceValue)).toBe(fiftyFirstChar);
|
expect(next(maxSpaceValue)).toBe(fiftyFirstChar);
|
||||||
expect(prev(fiftyFirstChar)).toBe(maxSpaceValue);
|
expect(prev(fiftyFirstChar)).toBe(maxSpaceValue);
|
||||||
expect(Number(stringToBase(ALPHABET[0]))).toEqual(1);
|
expect(Number(stringToBase(DEFAULT_ALPHABET[0]))).toEqual(1);
|
||||||
expect(Number(stringToBase(ALPHABET[1]))).toEqual(2);
|
expect(Number(stringToBase(DEFAULT_ALPHABET[1]))).toEqual(2);
|
||||||
expect(ALPHABET[ALPHABET.length - 1]).toBe("~");
|
expect(DEFAULT_ALPHABET[DEFAULT_ALPHABET.length - 1]).toBe("~");
|
||||||
expect(ALPHABET[0]).toBe(" ");
|
expect(DEFAULT_ALPHABET[0]).toBe(" ");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue