|
|
|
|
@ -14,14 +14,36 @@ See the License for the specific language governing permissions and |
|
|
|
|
limitations under the License. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
import { sortBy } from "lodash"; |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
ALPHABET, |
|
|
|
|
averageBetweenStrings, |
|
|
|
|
baseToString, |
|
|
|
|
midPointsBetweenStrings, |
|
|
|
|
reorderLexicographically, |
|
|
|
|
stringToBase, |
|
|
|
|
} from "../../src/utils/stringOrderField"; |
|
|
|
|
|
|
|
|
|
const moveLexicographicallyTest = ( |
|
|
|
|
orders: Array<string | undefined>, |
|
|
|
|
fromIndex: number, |
|
|
|
|
toIndex: number, |
|
|
|
|
expectedIndices: number[], |
|
|
|
|
): void => { |
|
|
|
|
const ops = reorderLexicographically(orders, fromIndex, toIndex); |
|
|
|
|
expect(ops.map(o => o.index).sort()).toStrictEqual(expectedIndices.sort()); |
|
|
|
|
|
|
|
|
|
const zipped: Array<[number, string | undefined]> = orders.map((o, i) => [i, o]); |
|
|
|
|
ops.forEach(({ index, order }) => { |
|
|
|
|
zipped[index][1] = order; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const newOrders = sortBy(zipped, i => i[1]); |
|
|
|
|
console.log("@@ moveLexicographicallyTest", {orders, zipped, newOrders, fromIndex, toIndex, ops}); |
|
|
|
|
expect(newOrders[toIndex][0]).toBe(fromIndex); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
describe("stringOrderField", () => { |
|
|
|
|
it("stringToBase", () => { |
|
|
|
|
expect(stringToBase(" ")).toBe(0); |
|
|
|
|
@ -35,6 +57,9 @@ describe("stringOrderField", () => { |
|
|
|
|
expect(stringToBase("c", "abcdefghijklmnopqrstuvwxyz")).toEqual(2); |
|
|
|
|
expect(stringToBase("ab")).toEqual(6241); |
|
|
|
|
expect(stringToBase("cb", "abcdefghijklmnopqrstuvwxyz")).toEqual(53); |
|
|
|
|
expect(stringToBase("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")).toEqual(4.5115969857961825e+78); |
|
|
|
|
expect(stringToBase("~".repeat(50))).toEqual(7.694497527671333e+98); |
|
|
|
|
// expect(typeof stringToBase("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")).toEqual("bigint");
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("baseToString", () => { |
|
|
|
|
@ -57,11 +82,18 @@ describe("stringOrderField", () => { |
|
|
|
|
{ a: "a", b: "z", output: "m", alphabet: "abcdefghijklmnopqrstuvwxyz" }, |
|
|
|
|
{ a: "AA", b: "zz", output: "^." }, |
|
|
|
|
{ a: "A", b: "z", output: "]" }, |
|
|
|
|
{ |
|
|
|
|
a: "A".repeat(50), |
|
|
|
|
b: "Z".repeat(50), |
|
|
|
|
output: "M}M}M}N ba`54Qpt\\\\Z+kNA#O(9}z>@2jJm]%Y^$m<8lRzz/2[Y", |
|
|
|
|
}, |
|
|
|
|
].forEach((c) => { |
|
|
|
|
// assert that the output string falls lexicographically between `a` and `b`
|
|
|
|
|
expect([c.a, c.b, c.output].sort()[1]).toBe(c.output); |
|
|
|
|
expect(averageBetweenStrings(c.a, c.b, c.alphabet)).toBe(c.output); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
expect(averageBetweenStrings("Q#!x+k", "V6yr>L")).toBe("S\\Mu5,"); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("midPointsBetweenStrings", () => { |
|
|
|
|
@ -69,5 +101,49 @@ describe("stringOrderField", () => { |
|
|
|
|
expect(midPointsBetweenStrings("a", "e", 0)).toStrictEqual([]); |
|
|
|
|
expect(midPointsBetweenStrings("a", "e", 4)).toStrictEqual([]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("moveLexicographically left", () => { |
|
|
|
|
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 2, 1, [2]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("moveLexicographically right", () => { |
|
|
|
|
moveLexicographicallyTest(["a", "c", "e", "g", "i"], 1, 2, [1]); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("moveLexicographically all undefined", () => { |
|
|
|
|
moveLexicographicallyTest( |
|
|
|
|
[undefined, undefined, undefined, undefined, undefined, undefined], |
|
|
|
|
4, |
|
|
|
|
1, |
|
|
|
|
[0, 4], |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("moveLexicographically all undefined to end", () => { |
|
|
|
|
moveLexicographicallyTest( |
|
|
|
|
[undefined, undefined, undefined, undefined, undefined, undefined], |
|
|
|
|
1, |
|
|
|
|
4, |
|
|
|
|
[0, 1, 2, 3, 4], |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("moveLexicographically some undefined move left", () => { |
|
|
|
|
moveLexicographicallyTest( |
|
|
|
|
["a", "c", "e", undefined, undefined, undefined], |
|
|
|
|
5, |
|
|
|
|
2, |
|
|
|
|
[5], |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
it("moveLexicographically some undefined move left close", () => { |
|
|
|
|
moveLexicographicallyTest( |
|
|
|
|
["a", "a", "e", undefined, undefined, undefined], |
|
|
|
|
5, |
|
|
|
|
1, |
|
|
|
|
[1, 5], |
|
|
|
|
); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|