From fe579bb93ccbe73a237d976ef4d32e31a7a3c360 Mon Sep 17 00:00:00 2001 From: Derfler Michael Date: Mon, 24 Feb 2025 14:21:00 +0100 Subject: [PATCH] fix: expandedNodeId.toString and coerceExpandedNodeId --- src/nodeid/expanded_nodeid.spec.ts | 9 +++++++++ src/nodeid/expanded_nodeid.ts | 6 +++--- src/nodeid/nodeid.ts | 19 +++++++++---------- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/nodeid/expanded_nodeid.spec.ts b/src/nodeid/expanded_nodeid.spec.ts index c7fe0662..749c54af 100644 --- a/src/nodeid/expanded_nodeid.spec.ts +++ b/src/nodeid/expanded_nodeid.spec.ts @@ -76,6 +76,15 @@ describe('testing ExpandedNodeId', function () { const exNodeId = coerceExpandedNodeId('svr=2;nsu=testuri;i=10'); expect(exNodeId.toString()).toBe('svr=2;nsu=testuri;ns=0;i=10'); }); + it('coerceExpandedNodeId should coerce a string with svr=2 and nsu="testuri" and ns=3', function () { + const exNodeId = coerceExpandedNodeId('svr=2;nsu=testuri;ns=3;i=10'); + expect(exNodeId.namespace).toBe(3); + expect(exNodeId.namespaceUri).toBe('testuri'); + expect(exNodeId.serverIndex).toBe(2); + expect(exNodeId.identifierType).toBe(NodeIdType.Numeric); + expect(exNodeId.value).toBe(10); + expect(exNodeId.toString()).toBe('svr=2;nsu=testuri;ns=3;i=10'); + }); [ 'ns=urn:engel:foo;s=myid', diff --git a/src/nodeid/expanded_nodeid.ts b/src/nodeid/expanded_nodeid.ts index e730734a..91f37af3 100644 --- a/src/nodeid/expanded_nodeid.ts +++ b/src/nodeid/expanded_nodeid.ts @@ -118,9 +118,9 @@ export function coerceExpandedNodeId(value: any): ExpandedNodeId { } const n = coerceNodeId(value); - if (namespaceUri) { - n.namespace = 0; - } + // if (namespaceUri) { + // n.namespace = 0; + // } return new ExpandedNodeId(n.identifierType, n.value, n.namespace, namespaceUri, serverIndex); } diff --git a/src/nodeid/nodeid.ts b/src/nodeid/nodeid.ts index ec40e474..ab82a084 100644 --- a/src/nodeid/nodeid.ts +++ b/src/nodeid/nodeid.ts @@ -90,7 +90,6 @@ export class NodeId { * */ toString(namespaceArray?: string[]): string { - let str; const namespaceStr: string = namespaceArray ? this.namespace === 0 ? '' @@ -98,15 +97,17 @@ export class NodeId { namespaceArray[this.namespace] || `` };` : `ns=${this.namespace};`; + + let valueStr; switch (this.identifierType) { case NodeIdType.Numeric: - str = namespaceStr + 'i=' + this.value; + valueStr = 'i=' + this.value; break; case NodeIdType.String: - str = namespaceStr + 's=' + this.value; + valueStr = 's=' + this.value; break; case NodeIdType.Guid: - str = namespaceStr + 'g=' + normalizeGuid(this.value as string); + valueStr = 'g=' + normalizeGuid(this.value as string); break; default: assert( @@ -114,20 +115,18 @@ export class NodeId { 'invalid identifierType in NodeId : ' + this.identifierType ); if (this.value) { - str = - 'ns=' + - this.namespace + - ';b=' + + valueStr = + 'b=' + Array.prototype.map .call(new Uint8Array(this.value as Uint8Array), (x: any) => x.toString(16).slice(-2)) .join(''); } else { - str = 'ns=' + this.namespace + ';b='; + valueStr = 'b='; } break; } - return str; + return namespaceStr + valueStr; } /**