Merge pull request #15 from brendan-ch/experimental/tuples

experimental/tuples
This commit is contained in:
2025-01-29 17:10:29 -08:00
committed by GitHub
2 changed files with 50 additions and 0 deletions

19
src/types/TupleKey.ts Normal file
View File

@@ -0,0 +1,19 @@
const separator = "|";
export class TupleKey<T extends any[]> {
public readonly tuple: T;
private readonly strKey: string;
constructor(...tuple: T) {
this.tuple = tuple;
this.strKey = tuple.join(separator);
}
toString(): string {
return this.strKey;
}
valueOf(): string {
return this.strKey;
}
}

View File

@@ -0,0 +1,31 @@
import { describe, expect, it } from "@jest/globals";
import { TupleKey } from "../../src/types/TupleKey";
describe("TupleKey", () => {
it("stores a value copy of the original tuple", () => {
const tuple: [string, string] = ["150", "539"];
const tupleKey = new TupleKey(...tuple);
expect(tupleKey.tuple).toEqual(tuple);
});
it("returns a string representation of itself", () => {
const tuple: [string, string] = ["150", "539"];
const tupleKey = new TupleKey(...tuple);
expect(`${tupleKey}`).toEqual("150|539");
});
it("supports usage as key in object", () => {
const tupleKey1 = new TupleKey("1", "2");
const tupleKey2 = new TupleKey("3", "4");
const sampleObject = {
[tupleKey1.toString()]: "value1",
[tupleKey2.toString()]: "value2",
};
expect(sampleObject[tupleKey1.toString()]).toEqual("value1");
expect(sampleObject[(new TupleKey("1", "2")).toString()]).toEqual("value1");
});
});