diff --git a/src/adap-b02/names/Name.ts b/src/adap-b02/names/Name.ts index 165e202..fa123b0 100644 --- a/src/adap-b02/names/Name.ts +++ b/src/adap-b02/names/Name.ts @@ -1,21 +1,52 @@ export const DEFAULT_DELIMITER: string = '.'; export const ESCAPE_CHARACTER = '\\'; +/** + * A name is a sequence of string components separated by a delimiter character. + * Special characters within the string may need masking, if they are to appear verbatim. + * There are only two special characters, the delimiter character and the escape character. + * The escape character can't be set, the delimiter character can. + * + * Homogenous name examples + * + * "oss.cs.fau.de" is a name with four name components and the delimiter character '.'. + * "///" is a name with four empty components and the delimiter character '/'. + * "Oh\.\.\." is a name with one component, if the delimiter character is '.'. + */ export interface Name { - /** Returns human-readable representation of Name instance */ - asNameString(delimiter?: string): string; + /** + * Returns a human-readable representation of the Name instance using user-set control characters + * Control characters are not escaped (creating a human-readable string) + * Users can vary the delimiter character to be used + */ + asString(delimiter?: string): string; + + /** + * Returns a machine-readable representation of Name instance using default control characters + * Machine-readable means that from a data string, a Name can be parsed back in + * The control characters in the data string are the default characters + */ + asDataString(): string; isEmpty(): boolean; + getDelimiterCharacter(): string; + /** Returns number of components in Name instance */ getNoComponents(): number; getComponent(i: number): string; + + /** Assumes that new Name component c is properly masked */ setComponent(i: number, c: string): void; + /** Assumes that new Name component c is properly masked */ insert(i: number, c: string): void; + + /** Assumes that new Name component c is properly masked */ append(c: string): void; + remove(i: number): void; } \ No newline at end of file diff --git a/src/adap-b02/names/StringArrayName.ts b/src/adap-b02/names/StringArrayName.ts index eff6395..6ba5bc4 100644 --- a/src/adap-b02/names/StringArrayName.ts +++ b/src/adap-b02/names/StringArrayName.ts @@ -9,7 +9,11 @@ export class StringArrayName implements Name { throw new Error("needs implementation"); } - public asNameString(delimiter: string = this.delimiter): string { + public asString(delimiter: string = this.delimiter): string { + throw new Error("needs implementation"); + } + + public asDataString(): string { throw new Error("needs implementation"); } @@ -17,6 +21,10 @@ export class StringArrayName implements Name { throw new Error("needs implementation"); } + public getDelimiterCharacter(): string { + throw new Error("needs implementation"); + } + public getNoComponents(): number { throw new Error("needs implementation"); } diff --git a/src/adap-b02/names/StringName.ts b/src/adap-b02/names/StringName.ts index 6fcd9c4..e39f9bd 100644 --- a/src/adap-b02/names/StringName.ts +++ b/src/adap-b02/names/StringName.ts @@ -5,13 +5,17 @@ export class StringName implements Name { protected delimiter: string = DEFAULT_DELIMITER; protected name: string = ""; - protected length: number = 0; // Number of components in Name instance + protected length: number = 0; constructor(other: string, delimiter?: string) { throw new Error("needs implementation"); } - public asNameString(delimiter: string = this.delimiter): string { + public asString(delimiter: string = this.delimiter): string { + throw new Error("needs implementation"); + } + + public asDataString(): string { throw new Error("needs implementation"); } @@ -19,6 +23,10 @@ export class StringName implements Name { throw new Error("needs implementation"); } + public getDelimiterCharacter(): string { + throw new Error("needs implementation"); + } + public getNoComponents(): number { throw new Error("needs implementation"); } diff --git a/test/adap-b02/names/Name.test.ts b/test/adap-b02/names/Name.test.ts new file mode 100644 index 0000000..b361ec1 --- /dev/null +++ b/test/adap-b02/names/Name.test.ts @@ -0,0 +1,59 @@ +import { describe, it, expect } from "vitest"; + +import { Name } from "../../../src/adap-b02/names/Name"; +import { StringName } from "../../../src/adap-b02/names/StringName"; +import { StringArrayName } from "../../../src/adap-b02/names/StringArrayName"; + +describe("Basic StringName function tests", () => { + it("test insert", () => { + let n: Name = new StringName("oss.fau.de"); + n.insert(1, "cs"); + expect(n.asString()).toBe("oss.cs.fau.de"); + }); + it("test append", () => { + let n: Name = new StringName("oss.cs.fau"); + n.append("de"); + expect(n.asString()).toBe("oss.cs.fau.de"); + }); + it("test remove", () => { + let n: Name = new StringName("oss.cs.fau.de"); + n.remove(0); + expect(n.asString()).toBe("cs.fau.de"); + }); +}); + +describe("Basic StringArrayName function tests", () => { + it("test insert", () => { + let n: Name = new StringArrayName(["oss", "fau", "de"]); + n.insert(1, "cs"); + expect(n.asString()).toBe("oss.cs.fau.de"); + }); + it("test append", () => { + let n: Name = new StringArrayName(["oss", "cs", "fau"]); + n.append("de"); + expect(n.asString()).toBe("oss.cs.fau.de"); + }); + it("test remove", () => { + let n: Name = new StringArrayName(["oss", "cs", "fau", "de"]); + n.remove(0); + expect(n.asString()).toBe("cs.fau.de"); + }); +}); + +describe("Delimiter function tests", () => { + it("test insert", () => { + let n: Name = new StringName("oss#fau#de", '#'); + n.insert(1, "cs"); + expect(n.asString()).toBe("oss#cs#fau#de"); + }); +}); + +describe("Escape character extravaganza", () => { + it("test escape and delimiter boundary conditions", () => { + let n: Name = new StringName("oss.cs.fau.de", '#'); + expect(n.getNoComponents()).toBe(1); + expect(n.asString()).toBe("oss.cs.fau.de"); + n.append("people"); + expect(n.asString()).toBe("oss.cs.fau.de#people"); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 7625be6..868e772 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,7 @@ // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ /* Language and Environment */ - "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ + "target": "ES2023", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ // "jsx": "preserve", /* Specify what JSX code is generated. */ // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */