@twin.org/context 0.9.1 → 0.9.2-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,6 +60,39 @@ export class ContextIdHelper {
60
60
  }
61
61
  }
62
62
  }
63
+ /**
64
+ * Gets the long version of a context ID, expanding from a short form if a handler is registered.
65
+ * @param contextIds The context IDs to get the long version from.
66
+ * @param key The context ID key to get the long version for.
67
+ * @returns The long version of the context ID.
68
+ * @throws Guard error if the value is invalid.
69
+ */
70
+ static long(contextIds, key) {
71
+ Guards.stringValue(ContextIdHelper.CLASS_NAME, "key", key);
72
+ if (Is.undefined(contextIds?.[key])) {
73
+ throw new GeneralError(ContextIdHelper.CLASS_NAME, "contextIdMissing", { key });
74
+ }
75
+ const handler = ContextIdHandlerFactory.getIfExists(key);
76
+ if (handler?.long) {
77
+ return handler.long(contextIds[key]);
78
+ }
79
+ return contextIds[key];
80
+ }
81
+ /**
82
+ * Gets the long versions of multiple context IDs.
83
+ * @param contextIds The context IDs to get the long versions from.
84
+ * @param keys The context ID keys to get the long versions for.
85
+ * @returns The long versions of the context IDs.
86
+ */
87
+ static longAll(contextIds, keys) {
88
+ const long = {};
89
+ if (Is.arrayValue(keys)) {
90
+ for (const key of keys) {
91
+ long[key] = ContextIdHelper.long(contextIds, key);
92
+ }
93
+ }
94
+ return long;
95
+ }
63
96
  /**
64
97
  * Gets the short versions of multiple context IDs.
65
98
  * @param contextIds The context IDs to get the short versions from.
@@ -1 +1 @@
1
- {"version":3,"file":"contextIdHelper.js","sourceRoot":"","sources":["../../../src/helpers/contextIdHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAE1D,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAGlF;;GAEG;AACH,MAAM,OAAO,eAAe;IAC3B;;OAEG;I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,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC;YAClE,OAAO,EAAE,CAAC;QACX,CAAC;QAED,OAAO,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IAC/D,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { ContextIdHandlerFactory } from \"../factories/contextIdHandlerFactory.js\";\nimport type { IContextIds } from \"../models/IContextIds.js\";\n\n/**\n * Class to help with context IDs.\n */\nexport class ContextIdHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ContextIdHelper>();\n\n\t/**\n\t * Perform a runtime guard on the provided context ID value.\n\t * @param contextIds The context IDs to guard.\n\t * @param key The context ID key to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static guard<T extends { [k: string]: string | undefined }, K extends string>(\n\t\tcontextIds: T | undefined,\n\t\tkey: K\n\t): asserts contextIds is T & { [P in K]: string } {\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(key), key);\n\n\t\tif (Is.undefined(contextIds?.[key])) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdMissing\", { key });\n\t\t}\n\n\t\tconst handler = ContextIdHandlerFactory.getIfExists(key);\n\t\tif (handler?.guard) {\n\t\t\thandler.guard(contextIds[key]);\n\t\t} else {\n\t\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, `contextIds.${key}`, contextIds[key]);\n\t\t}\n\t}\n\n\t/**\n\t * Gets the short version of a context ID.\n\t * @param contextIds The context IDs to get the short version from.\n\t * @param key The context ID key to get the short version for.\n\t * @returns The short version of the context ID.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static short(contextIds: IContextIds | undefined, key: string): string {\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(key), key);\n\t\tif (Is.undefined(contextIds?.[key])) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdMissing\", { key });\n\t\t}\n\n\t\tconst handler = ContextIdHandlerFactory.getIfExists(key);\n\t\tif (handler?.short) {\n\t\t\treturn handler.short(contextIds[key]);\n\t\t}\n\t\treturn contextIds[key];\n\t}\n\n\t/**\n\t * Perform a runtime guard on the provided context ID values.\n\t * @param contextIds The context IDs to guard.\n\t * @param keys The context ID keys to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static guardAll<T extends { [k: string]: string | undefined }, K extends string>(\n\t\tcontextIds: T | undefined,\n\t\tkeys: readonly K[] | undefined\n\t): asserts contextIds is T & { [P in K]: string } {\n\t\tif (Is.arrayValue(keys)) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tContextIdHelper.guard(contextIds, key);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Gets the short versions of multiple context IDs.\n\t * @param contextIds The context IDs to get the short versions from.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @returns The short versions of the context IDs.\n\t */\n\tpublic static shortAll(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined\n\t): IContextIds {\n\t\tconst short: IContextIds = {};\n\t\tif (Is.arrayValue(keys)) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tshort[key] = ContextIdHelper.short(contextIds, key);\n\t\t\t}\n\t\t}\n\t\treturn short;\n\t}\n\n\t/**\n\t * Gets the combined short version.\n\t * @param contextIds The context IDs to get the short versions from.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param separator The separator to use between parts.\n\t * @returns The short version combined.\n\t */\n\tpublic static shortCombined(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined,\n\t\tseparator: string = \"/\"\n\t): string | undefined {\n\t\tconst short = ContextIdHelper.shortAll(contextIds, keys);\n\t\tif (Object.keys(short).length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn Object.values(short).join(separator);\n\t}\n\n\t/**\n\t * Split a combined short version in to the separate context IDs.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param combined The combined short version to separate.\n\t * @param separator The separator used between parts.\n\t * @returns The short version combined.\n\t * @throws GeneralError if the number of parts does not match the number of keys.\n\t */\n\tpublic static shortSplit(keys: string[], combined: string, separator: string = \"/\"): IContextIds {\n\t\tGuards.arrayValue<string>(ContextIdHelper.CLASS_NAME, nameof(keys), keys);\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(combined), combined);\n\n\t\tconst parts = combined.split(separator);\n\t\tconst result: IContextIds = {};\n\n\t\tif (parts.length !== keys.length) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdSplitMismatch\", {\n\t\t\t\texpected: keys.length,\n\t\t\t\tactual: parts.length\n\t\t\t});\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tresult[keys[i]] = parts[i];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Create a combined key.\n\t * @param contextIds The context IDs to create the combined key for.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param separator The separator to use between parts.\n\t * @returns The short version combined.\n\t */\n\tpublic static combinedContextKey(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined,\n\t\tseparator: string = \"/\"\n\t): string | undefined {\n\t\tContextIdHelper.guardAll(contextIds, keys);\n\n\t\tconst short = ContextIdHelper.shortAll(contextIds, keys);\n\n\t\tif (Object.keys(short).length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn Object.values(short).join(separator);\n\t}\n\n\t/**\n\t * Pick only the desired keys from the available keys.\n\t * @param availableKeys The available keys to pick from.\n\t * @param desiredKeys The desired keys to pick.\n\t * @returns The picked keys.\n\t */\n\tpublic static pickKeysFromAvailable(availableKeys?: string[], desiredKeys?: string[]): string[] {\n\t\tif (!Is.arrayValue(availableKeys) || !Is.arrayValue(desiredKeys)) {\n\t\t\treturn [];\n\t\t}\n\n\t\treturn desiredKeys.filter(key => availableKeys.includes(key));\n\t}\n}\n"]}
1
+ {"version":3,"file":"contextIdHelper.js","sourceRoot":"","sources":["../../../src/helpers/contextIdHelper.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,uCAAuC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAC;AAE1D,OAAO,EAAE,uBAAuB,EAAE,MAAM,yCAAyC,CAAC;AAGlF;;GAEG;AACH,MAAM,OAAO,eAAe;IAC3B;;OAEG;IACI,MAAM,CAAU,UAAU,qBAAqC;IAEtE;;;;;OAKG;IACI,MAAM,CAAC,KAAK,CAClB,UAAyB,EACzB,GAAM;QAEN,MAAM,CAAC,WAAW,CAAC,eAAe,CAAC,UAAU,SAAe,GAAG,CAAC,CAAC;QAEjE,IAAI,EAAE,CAAC,SAAS,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;YACrC,MAAM,IAAI,YAAY,CAAC,eAAe,CAAC,UAAU,EAAE,kBAAkB,EAAE,EAAE,GAAG,EAAE,CAAC,CAAC;QACjF,CAAC;QAED,MAAM,OAAO,GAAG,uBAAuB,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;QACzD,IAAI,OAAO,EAAE,KAAK,EAAE,CAAC;YACpB,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;QAChC,CAAC;aAAM,CAAC;YACP,MAAM,CAAC,WAAW,CAAC,eAAe,CAAC,UAAU,EAAE,cAAc,GAAG,EAAE,EAAE,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;QACtF,CAAC;IACF,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,UAAmC,EAAE,GAAW;QACnE,MAAM,CAAC,WAAW,CAAC,eAAe,C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AAC;IAED;;;;;OAKG;IACI,MAAM,CAAC,qBAAqB,CAAC,aAAwB,EAAE,WAAsB;QACnF,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,aAAa,CAAC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,WAAW,CAAC,EAAE,CAAC;YAClE,OAAO,EAAE,CAAC;QACX,CAAC;QAED,OAAO,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,aAAa,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;IAC/D,CAAC","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport { GeneralError, Guards, Is } from \"@twin.org/core\";\nimport { nameof } from \"@twin.org/nameof\";\nimport { ContextIdHandlerFactory } from \"../factories/contextIdHandlerFactory.js\";\nimport type { IContextIds } from \"../models/IContextIds.js\";\n\n/**\n * Class to help with context IDs.\n */\nexport class ContextIdHelper {\n\t/**\n\t * Runtime name for the class.\n\t */\n\tpublic static readonly CLASS_NAME: string = nameof<ContextIdHelper>();\n\n\t/**\n\t * Perform a runtime guard on the provided context ID value.\n\t * @param contextIds The context IDs to guard.\n\t * @param key The context ID key to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static guard<T extends { [k: string]: string | undefined }, K extends string>(\n\t\tcontextIds: T | undefined,\n\t\tkey: K\n\t): asserts contextIds is T & { [P in K]: string } {\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(key), key);\n\n\t\tif (Is.undefined(contextIds?.[key])) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdMissing\", { key });\n\t\t}\n\n\t\tconst handler = ContextIdHandlerFactory.getIfExists(key);\n\t\tif (handler?.guard) {\n\t\t\thandler.guard(contextIds[key]);\n\t\t} else {\n\t\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, `contextIds.${key}`, contextIds[key]);\n\t\t}\n\t}\n\n\t/**\n\t * Gets the short version of a context ID.\n\t * @param contextIds The context IDs to get the short version from.\n\t * @param key The context ID key to get the short version for.\n\t * @returns The short version of the context ID.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static short(contextIds: IContextIds | undefined, key: string): string {\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(key), key);\n\t\tif (Is.undefined(contextIds?.[key])) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdMissing\", { key });\n\t\t}\n\n\t\tconst handler = ContextIdHandlerFactory.getIfExists(key);\n\t\tif (handler?.short) {\n\t\t\treturn handler.short(contextIds[key]);\n\t\t}\n\t\treturn contextIds[key];\n\t}\n\n\t/**\n\t * Perform a runtime guard on the provided context ID values.\n\t * @param contextIds The context IDs to guard.\n\t * @param keys The context ID keys to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static guardAll<T extends { [k: string]: string | undefined }, K extends string>(\n\t\tcontextIds: T | undefined,\n\t\tkeys: readonly K[] | undefined\n\t): asserts contextIds is T & { [P in K]: string } {\n\t\tif (Is.arrayValue(keys)) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tContextIdHelper.guard(contextIds, key);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Gets the long version of a context ID, expanding from a short form if a handler is registered.\n\t * @param contextIds The context IDs to get the long version from.\n\t * @param key The context ID key to get the long version for.\n\t * @returns The long version of the context ID.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tpublic static long(contextIds: IContextIds | undefined, key: string): string {\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(key), key);\n\t\tif (Is.undefined(contextIds?.[key])) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdMissing\", { key });\n\t\t}\n\n\t\tconst handler = ContextIdHandlerFactory.getIfExists(key);\n\t\tif (handler?.long) {\n\t\t\treturn handler.long(contextIds[key]);\n\t\t}\n\t\treturn contextIds[key];\n\t}\n\n\t/**\n\t * Gets the long versions of multiple context IDs.\n\t * @param contextIds The context IDs to get the long versions from.\n\t * @param keys The context ID keys to get the long versions for.\n\t * @returns The long versions of the context IDs.\n\t */\n\tpublic static longAll(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined\n\t): IContextIds {\n\t\tconst long: IContextIds = {};\n\t\tif (Is.arrayValue(keys)) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tlong[key] = ContextIdHelper.long(contextIds, key);\n\t\t\t}\n\t\t}\n\t\treturn long;\n\t}\n\n\t/**\n\t * Gets the short versions of multiple context IDs.\n\t * @param contextIds The context IDs to get the short versions from.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @returns The short versions of the context IDs.\n\t */\n\tpublic static shortAll(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined\n\t): IContextIds {\n\t\tconst short: IContextIds = {};\n\t\tif (Is.arrayValue(keys)) {\n\t\t\tfor (const key of keys) {\n\t\t\t\tshort[key] = ContextIdHelper.short(contextIds, key);\n\t\t\t}\n\t\t}\n\t\treturn short;\n\t}\n\n\t/**\n\t * Gets the combined short version.\n\t * @param contextIds The context IDs to get the short versions from.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param separator The separator to use between parts.\n\t * @returns The short version combined.\n\t */\n\tpublic static shortCombined(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined,\n\t\tseparator: string = \"/\"\n\t): string | undefined {\n\t\tconst short = ContextIdHelper.shortAll(contextIds, keys);\n\t\tif (Object.keys(short).length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn Object.values(short).join(separator);\n\t}\n\n\t/**\n\t * Split a combined short version in to the separate context IDs.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param combined The combined short version to separate.\n\t * @param separator The separator used between parts.\n\t * @returns The short version combined.\n\t * @throws GeneralError if the number of parts does not match the number of keys.\n\t */\n\tpublic static shortSplit(keys: string[], combined: string, separator: string = \"/\"): IContextIds {\n\t\tGuards.arrayValue<string>(ContextIdHelper.CLASS_NAME, nameof(keys), keys);\n\t\tGuards.stringValue(ContextIdHelper.CLASS_NAME, nameof(combined), combined);\n\n\t\tconst parts = combined.split(separator);\n\t\tconst result: IContextIds = {};\n\n\t\tif (parts.length !== keys.length) {\n\t\t\tthrow new GeneralError(ContextIdHelper.CLASS_NAME, \"contextIdSplitMismatch\", {\n\t\t\t\texpected: keys.length,\n\t\t\t\tactual: parts.length\n\t\t\t});\n\t\t}\n\n\t\tfor (let i = 0; i < keys.length; i++) {\n\t\t\tresult[keys[i]] = parts[i];\n\t\t}\n\n\t\treturn result;\n\t}\n\n\t/**\n\t * Create a combined key.\n\t * @param contextIds The context IDs to create the combined key for.\n\t * @param keys The context ID keys to get the short versions for.\n\t * @param separator The separator to use between parts.\n\t * @returns The short version combined.\n\t */\n\tpublic static combinedContextKey(\n\t\tcontextIds: IContextIds | undefined,\n\t\tkeys: string[] | undefined,\n\t\tseparator: string = \"/\"\n\t): string | undefined {\n\t\tContextIdHelper.guardAll(contextIds, keys);\n\n\t\tconst short = ContextIdHelper.shortAll(contextIds, keys);\n\n\t\tif (Object.keys(short).length === 0) {\n\t\t\treturn undefined;\n\t\t}\n\n\t\treturn Object.values(short).join(separator);\n\t}\n\n\t/**\n\t * Pick only the desired keys from the available keys.\n\t * @param availableKeys The available keys to pick from.\n\t * @param desiredKeys The desired keys to pick.\n\t * @returns The picked keys.\n\t */\n\tpublic static pickKeysFromAvailable(availableKeys?: string[], desiredKeys?: string[]): string[] {\n\t\tif (!Is.arrayValue(availableKeys) || !Is.arrayValue(desiredKeys)) {\n\t\t\treturn [];\n\t\t}\n\n\t\treturn desiredKeys.filter(key => availableKeys.includes(key));\n\t}\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"IContextIdHandler.js","sourceRoot":"","sources":["../../../src/models/IContextIdHandler.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IComponent } from \"@twin.org/core\";\n\n/**\n * Interface describing a context ID handler.\n */\nexport interface IContextIdHandler extends IComponent {\n\t/**\n\t * The short form version of the context ID, should be unique enough to partition data.\n\t * @param value The full context ID value.\n\t * @returns The short form version of the context ID.\n\t */\n\tshort?(value: string): string;\n\n\t/**\n\t * Performs a runtime guard on the provided context ID value.\n\t * @param value The context ID value to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tguard?(value: string): void;\n}\n"]}
1
+ {"version":3,"file":"IContextIdHandler.js","sourceRoot":"","sources":["../../../src/models/IContextIdHandler.ts"],"names":[],"mappings":"","sourcesContent":["// Copyright 2024 IOTA Stiftung.\n// SPDX-License-Identifier: Apache-2.0.\nimport type { IComponent } from \"@twin.org/core\";\n\n/**\n * Interface describing a context ID handler.\n */\nexport interface IContextIdHandler extends IComponent {\n\t/**\n\t * The short form version of the context ID, should be unique enough to partition data.\n\t * @param value The full context ID value.\n\t * @returns The short form version of the context ID.\n\t */\n\tshort?(value: string): string;\n\n\t/**\n\t * The long form version of the context ID, expanded from a short version.\n\t * @param value The short form context ID value.\n\t * @returns The long form version of the context ID.\n\t */\n\tlong?(value: string): string;\n\n\t/**\n\t * Performs a runtime guard on the provided context ID value.\n\t * @param value The context ID value to guard.\n\t * @throws Guard error if the value is invalid.\n\t */\n\tguard?(value: string): void;\n}\n"]}
@@ -37,6 +37,21 @@ export declare class ContextIdHelper {
37
37
  }, K extends string>(contextIds: T | undefined, keys: readonly K[] | undefined): asserts contextIds is T & {
38
38
  [P in K]: string;
39
39
  };
40
+ /**
41
+ * Gets the long version of a context ID, expanding from a short form if a handler is registered.
42
+ * @param contextIds The context IDs to get the long version from.
43
+ * @param key The context ID key to get the long version for.
44
+ * @returns The long version of the context ID.
45
+ * @throws Guard error if the value is invalid.
46
+ */
47
+ static long(contextIds: IContextIds | undefined, key: string): string;
48
+ /**
49
+ * Gets the long versions of multiple context IDs.
50
+ * @param contextIds The context IDs to get the long versions from.
51
+ * @param keys The context ID keys to get the long versions for.
52
+ * @returns The long versions of the context IDs.
53
+ */
54
+ static longAll(contextIds: IContextIds | undefined, keys: string[] | undefined): IContextIds;
40
55
  /**
41
56
  * Gets the short versions of multiple context IDs.
42
57
  * @param contextIds The context IDs to get the short versions from.
@@ -9,6 +9,12 @@ export interface IContextIdHandler extends IComponent {
9
9
  * @returns The short form version of the context ID.
10
10
  */
11
11
  short?(value: string): string;
12
+ /**
13
+ * The long form version of the context ID, expanded from a short version.
14
+ * @param value The short form context ID value.
15
+ * @returns The long form version of the context ID.
16
+ */
17
+ long?(value: string): string;
12
18
  /**
13
19
  * Performs a runtime guard on the provided context ID value.
14
20
  * @param value The context ID value to guard.
package/docs/changelog.md CHANGED
@@ -1,5 +1,56 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.9.2-next.2](https://github.com/iotaledger/twin-framework/compare/context-v0.9.2-next.1...context-v0.9.2-next.2) (2026-07-29)
4
+
5
+
6
+ ### Features
7
+
8
+ * context id long method ([#423](https://github.com/iotaledger/twin-framework/issues/423)) ([70a7384](https://github.com/iotaledger/twin-framework/commit/70a7384f201ea5dafdff5de34fb90308d5e80338))
9
+
10
+
11
+ ### Dependencies
12
+
13
+ * The following workspace dependencies were updated
14
+ * dependencies
15
+ * @twin.org/core bumped from 0.9.2-next.1 to 0.9.2-next.2
16
+ * @twin.org/nameof bumped from 0.9.2-next.1 to 0.9.2-next.2
17
+ * devDependencies
18
+ * @twin.org/nameof-transformer bumped from 0.9.2-next.1 to 0.9.2-next.2
19
+ * @twin.org/nameof-vitest-plugin bumped from 0.9.2-next.1 to 0.9.2-next.2
20
+ * @twin.org/validate-locales bumped from 0.9.2-next.1 to 0.9.2-next.2
21
+
22
+ ## [0.9.2-next.1](https://github.com/iotaledger/twin-framework/compare/context-v0.9.2-next.0...context-v0.9.2-next.1) (2026-07-28)
23
+
24
+
25
+ ### Features
26
+
27
+ * add context id features ([#206](https://github.com/iotaledger/twin-framework/issues/206)) ([ef0d4ee](https://github.com/iotaledger/twin-framework/commit/ef0d4ee11a4f5fc6cc6f52a4958ce905c04ee13b))
28
+ * add user organization context key ([a3da436](https://github.com/iotaledger/twin-framework/commit/a3da4360451860052a508bdc147255a0b9ca8410))
29
+ * concurrency in SharedStore ([#388](https://github.com/iotaledger/twin-framework/issues/388)) ([0610198](https://github.com/iotaledger/twin-framework/commit/0610198ba482273c3f6ba918e34f5875a3659571))
30
+ * context id handler derives from component ([c868ec2](https://github.com/iotaledger/twin-framework/commit/c868ec21d3a576d4faa222bf130270a21936e50e))
31
+ * typescript 6 update ([1d10f31](https://github.com/iotaledger/twin-framework/commit/1d10f31e6516ec622773f45e88af82fe749b384a))
32
+ * update dependencies ([4da77ab](https://github.com/iotaledger/twin-framework/commit/4da77ab30f499e52825ac5a76f51436ceb59c26e))
33
+
34
+
35
+ ### Bug Fixes
36
+
37
+ * context id backwards compatibility ([#406](https://github.com/iotaledger/twin-framework/issues/406)) ([afab5fa](https://github.com/iotaledger/twin-framework/commit/afab5fa2a876b01736e117e4992536627837afde))
38
+ * ensure __decorate is defined for decorators ([103a563](https://github.com/iotaledger/twin-framework/commit/103a563ce01ebdef6240d2e590e7b026e8692684))
39
+ * use singleton pattern for context storage ([c69f358](https://github.com/iotaledger/twin-framework/commit/c69f358e45361b45d4e46f19846cd5b8c99b0ccd))
40
+ * use singleton pattern for context storage ([5cc706a](https://github.com/iotaledger/twin-framework/commit/5cc706a2bbfc601fa3d00f3efd8b764052e9f91d))
41
+
42
+
43
+ ### Dependencies
44
+
45
+ * The following workspace dependencies were updated
46
+ * dependencies
47
+ * @twin.org/core bumped from 0.9.2-next.0 to 0.9.2-next.1
48
+ * @twin.org/nameof bumped from 0.9.2-next.0 to 0.9.2-next.1
49
+ * devDependencies
50
+ * @twin.org/nameof-transformer bumped from 0.9.2-next.0 to 0.9.2-next.1
51
+ * @twin.org/nameof-vitest-plugin bumped from 0.9.2-next.0 to 0.9.2-next.1
52
+ * @twin.org/validate-locales bumped from 0.9.2-next.0 to 0.9.2-next.1
53
+
3
54
  ## [0.9.1](https://github.com/iotaledger/twin-framework/compare/context-v0.9.1...context-v0.9.1) (2026-07-26)
4
55
 
5
56
 
@@ -134,6 +134,66 @@ Guard error if the value is invalid.
134
134
 
135
135
  ***
136
136
 
137
+ ### long() {#long}
138
+
139
+ > `static` **long**(`contextIds`, `key`): `string`
140
+
141
+ Gets the long version of a context ID, expanding from a short form if a handler is registered.
142
+
143
+ #### Parameters
144
+
145
+ ##### contextIds
146
+
147
+ [`IContextIds`](../interfaces/IContextIds.md) \| `undefined`
148
+
149
+ The context IDs to get the long version from.
150
+
151
+ ##### key
152
+
153
+ `string`
154
+
155
+ The context ID key to get the long version for.
156
+
157
+ #### Returns
158
+
159
+ `string`
160
+
161
+ The long version of the context ID.
162
+
163
+ #### Throws
164
+
165
+ Guard error if the value is invalid.
166
+
167
+ ***
168
+
169
+ ### longAll() {#longall}
170
+
171
+ > `static` **longAll**(`contextIds`, `keys`): [`IContextIds`](../interfaces/IContextIds.md)
172
+
173
+ Gets the long versions of multiple context IDs.
174
+
175
+ #### Parameters
176
+
177
+ ##### contextIds
178
+
179
+ [`IContextIds`](../interfaces/IContextIds.md) \| `undefined`
180
+
181
+ The context IDs to get the long versions from.
182
+
183
+ ##### keys
184
+
185
+ `string`[] \| `undefined`
186
+
187
+ The context ID keys to get the long versions for.
188
+
189
+ #### Returns
190
+
191
+ [`IContextIds`](../interfaces/IContextIds.md)
192
+
193
+ The long versions of the context IDs.
194
+
195
+ ***
196
+
137
197
  ### shortAll() {#shortall}
138
198
 
139
199
  > `static` **shortAll**(`contextIds`, `keys`): [`IContextIds`](../interfaces/IContextIds.md)
@@ -30,6 +30,28 @@ The short form version of the context ID.
30
30
 
31
31
  ***
32
32
 
33
+ ### long()? {#long}
34
+
35
+ > `optional` **long**(`value`): `string`
36
+
37
+ The long form version of the context ID, expanded from a short version.
38
+
39
+ #### Parameters
40
+
41
+ ##### value
42
+
43
+ `string`
44
+
45
+ The short form context ID value.
46
+
47
+ #### Returns
48
+
49
+ `string`
50
+
51
+ The long form version of the context ID.
52
+
53
+ ***
54
+
33
55
  ### guard()? {#guard}
34
56
 
35
57
  > `optional` **guard**(`value`): `void`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twin.org/context",
3
- "version": "0.9.1",
3
+ "version": "0.9.2-next.2",
4
4
  "description": "Helper methods/classes for context handling",
5
5
  "repository": {
6
6
  "type": "git",
@@ -14,8 +14,8 @@
14
14
  "node": ">=24.0.0"
15
15
  },
16
16
  "dependencies": {
17
- "@twin.org/core": "^0.9.1",
18
- "@twin.org/nameof": "^0.9.1"
17
+ "@twin.org/core": "0.9.2-next.2",
18
+ "@twin.org/nameof": "0.9.2-next.2"
19
19
  },
20
20
  "main": "./dist/es/index.js",
21
21
  "types": "./dist/types/index.d.ts",