mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
Merge pull request #15 from brendan-ch/experimental/tuples
experimental/tuples
This commit is contained in:
19
src/types/TupleKey.ts
Normal file
19
src/types/TupleKey.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
31
test/types/TupleKeyTests.test.ts
Normal file
31
test/types/TupleKeyTests.test.ts
Normal 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");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user