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); + }) + }) +});