From c1ec50db7b29f4c8dbe91176195df1fadf43aea8 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Wed, 12 Feb 2025 19:39:37 -0800 Subject: [PATCH] add TupleKey method to convert string key back to tuple --- src/types/TupleKey.ts | 5 +++++ test/types/TupleKeyTests.test.ts | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/types/TupleKey.ts b/src/types/TupleKey.ts index 08dcfb4..f95f4f0 100644 --- a/src/types/TupleKey.ts +++ b/src/types/TupleKey.ts @@ -16,4 +16,9 @@ export class TupleKey { valueOf(): string { return this.strKey; } + + static fromExistingStringKey(strKey: string) { + const tuple = strKey.split(separator); + return new TupleKey(...tuple); + } } diff --git a/test/types/TupleKeyTests.test.ts b/test/types/TupleKeyTests.test.ts index ac260dd..b04c3ec 100644 --- a/test/types/TupleKeyTests.test.ts +++ b/test/types/TupleKeyTests.test.ts @@ -28,4 +28,18 @@ describe("TupleKey", () => { expect(sampleObject[tupleKey1.toString()]).toEqual("value1"); expect(sampleObject[(new TupleKey("1", "2")).toString()]).toEqual("value1"); }); -}); \ No newline at end of file + + 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); + }) + }) +});