add TupleKey method to convert string key back to tuple

This commit is contained in:
2025-02-12 19:39:37 -08:00
parent bb77aca4ed
commit c1ec50db7b
2 changed files with 20 additions and 1 deletions

View File

@@ -16,4 +16,9 @@ export class TupleKey<T extends any[]> {
valueOf(): string { valueOf(): string {
return this.strKey; return this.strKey;
} }
static fromExistingStringKey(strKey: string) {
const tuple = strKey.split(separator);
return new TupleKey(...tuple);
}
} }

View File

@@ -28,4 +28,18 @@ describe("TupleKey", () => {
expect(sampleObject[tupleKey1.toString()]).toEqual("value1"); expect(sampleObject[tupleKey1.toString()]).toEqual("value1");
expect(sampleObject[(new TupleKey("1", "2")).toString()]).toEqual("value1"); expect(sampleObject[(new TupleKey("1", "2")).toString()]).toEqual("value1");
}); });
describe("fromExistingStringKey", () => {
it("creates a new TupleKey from an existing string key", () => {
const strKey = "hello|there";
const tupleKey = TupleKey.fromExistingStringKey(strKey);
expect(tupleKey.toString()).toEqual(strKey);
});
it("creates an empty tuple if there is no string", () => {
const strKey = "";
const tupleKey = TupleKey.fromExistingStringKey(strKey);
expect(tupleKey.toString()).toEqual(strKey);
})
})
}); });