2021-06-10 10:40:10 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2021-06-10 10:40:10 +00:00
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-06-10 10:40:10 +00:00
|
|
|
*/
|
|
|
|
|
2021-06-11 09:33:00 +00:00
|
|
|
import { sortBy } from "lodash";
|
2021-06-16 11:12:00 +00:00
|
|
|
import { averageBetweenStrings, DEFAULT_ALPHABET } from "matrix-js-sdk/src/utils";
|
2021-06-11 09:33:00 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import { midPointsBetweenStrings, reorderLexicographically } from "../../../src/utils/stringOrderField";
|
2021-06-10 10:40:10 +00:00
|
|
|
|
2021-06-11 09:33:00 +00:00
|
|
|
const moveLexicographicallyTest = (
|
|
|
|
orders: Array<string | undefined>,
|
|
|
|
fromIndex: number,
|
|
|
|
toIndex: number,
|
2021-06-11 15:28:07 +00:00
|
|
|
expectedChanges: number,
|
|
|
|
maxLength?: number,
|
2021-06-11 09:33:00 +00:00
|
|
|
): void => {
|
2021-06-11 15:28:07 +00:00
|
|
|
const ops = reorderLexicographically(orders, fromIndex, toIndex, maxLength);
|
2021-06-11 09:33:00 +00:00
|
|
|
|
|
|
|
const zipped: Array<[number, string | undefined]> = orders.map((o, i) => [i, o]);
|
|
|
|
ops.forEach(({ index, order }) => {
|
|
|
|
zipped[index][1] = order;
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const newOrders = sortBy(zipped, (i) => i[1]);
|
2021-06-11 09:33:00 +00:00
|
|
|
expect(newOrders[toIndex][0]).toBe(fromIndex);
|
2021-06-11 15:28:07 +00:00
|
|
|
expect(ops).toHaveLength(expectedChanges);
|
2021-06-11 09:33:00 +00:00
|
|
|
};
|
|
|
|
|
2021-06-10 10:40:10 +00:00
|
|
|
describe("stringOrderField", () => {
|
2021-06-16 08:30:47 +00:00
|
|
|
describe("midPointsBetweenStrings", () => {
|
|
|
|
it("should work", () => {
|
|
|
|
expect(averageBetweenStrings("!!", "##")).toBe('""');
|
|
|
|
const midpoints = ["a", ...midPointsBetweenStrings("a", "e", 3, 1), "e"].sort();
|
|
|
|
expect(midpoints[0]).toBe("a");
|
|
|
|
expect(midpoints[4]).toBe("e");
|
|
|
|
expect(midPointsBetweenStrings(" ", "!'Tu:}", 1, 50)).toStrictEqual([" S:J\\~"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should return empty array when the request is not possible", () => {
|
|
|
|
expect(midPointsBetweenStrings("a", "e", 0, 1)).toStrictEqual([]);
|
|
|
|
expect(midPointsBetweenStrings("a", "e", 4, 1)).toStrictEqual([]);
|
|
|
|
});
|
2021-06-10 10:40:10 +00:00
|
|
|
});
|
|
|
|
|
2021-06-16 08:30:47 +00:00
|
|
|
describe("reorderLexicographically", () => {
|
|
|
|
it("should work when moving left", () => {
|
|
|
|
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 2, 1, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work when moving right", () => {
|
|
|
|
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 1, 2, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work when all orders are undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined, undefined, undefined], 4, 1, 2);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work when moving to end and all orders are undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined, undefined, undefined], 1, 4, 5);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work when moving left and some orders are undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["a", "c", "e", undefined, undefined, undefined], 5, 2, 1);
|
2021-06-16 08:30:47 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["a", "a", "e", undefined, undefined, undefined], 5, 1, 2);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving to the start when all is undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined], 2, 0, 1);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving to the end when all is undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined], 1, 3, 4);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving left when all is undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined, undefined, undefined], 4, 1, 2);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving right when all is undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest([undefined, undefined, undefined, undefined], 1, 2, 3);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving more right when all is undefined", () => {
|
|
|
|
moveLexicographicallyTest(
|
|
|
|
[undefined, undefined, undefined, undefined, undefined, /**/ undefined, undefined],
|
|
|
|
1,
|
|
|
|
4,
|
|
|
|
5,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving left when right is undefined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["20", undefined, undefined, undefined, undefined, undefined], 4, 2, 2);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving right when right is undefined", () => {
|
|
|
|
moveLexicographicallyTest(
|
|
|
|
["50", undefined, undefined, undefined, undefined, /**/ undefined, undefined],
|
|
|
|
1,
|
|
|
|
4,
|
|
|
|
4,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving left when right is defined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["10", "20", "30", "40", undefined, undefined], 3, 1, 1);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving right when right is defined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["10", "20", "30", "40", "50", undefined], 1, 3, 1);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving left when all is defined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["11", "13", "15", "17", "19"], 2, 1, 1);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving right when all is defined", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["11", "13", "15", "17", "19"], 1, 2, 1);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving left into no left space", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["11", "12", "13", "14", "19"], 3, 1, 2, 2);
|
2021-06-16 08:30:47 +00:00
|
|
|
|
|
|
|
moveLexicographicallyTest(
|
|
|
|
[
|
|
|
|
DEFAULT_ALPHABET.charAt(0),
|
|
|
|
// Target
|
|
|
|
DEFAULT_ALPHABET.charAt(1),
|
|
|
|
DEFAULT_ALPHABET.charAt(2),
|
|
|
|
DEFAULT_ALPHABET.charAt(3),
|
|
|
|
DEFAULT_ALPHABET.charAt(4),
|
|
|
|
DEFAULT_ALPHABET.charAt(5),
|
|
|
|
],
|
|
|
|
5,
|
|
|
|
1,
|
|
|
|
5,
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving right into no right space", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["15", "16", "17", "18", "19"], 1, 3, 3, 2);
|
2021-06-16 08:30:47 +00:00
|
|
|
|
|
|
|
moveLexicographicallyTest(
|
|
|
|
[
|
|
|
|
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,
|
|
|
|
3,
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving right into no left space", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["11", "12", "13", "14", "15", "16", undefined], 1, 3, 3);
|
2021-06-16 08:30:47 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["0", "1", "2", "3", "4", "5"], 1, 3, 3, 1);
|
2021-06-16 08:30:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should work moving left into no right space", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
moveLexicographicallyTest(["15", "16", "17", "18", "19"], 4, 3, 4, 2);
|
2021-06-16 08:30:47 +00:00
|
|
|
|
|
|
|
moveLexicographicallyTest(
|
|
|
|
[
|
|
|
|
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,
|
|
|
|
4,
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
});
|
2021-06-14 13:37:05 +00:00
|
|
|
});
|
2021-06-10 10:40:10 +00:00
|
|
|
});
|