Skip to content

Commit

Permalink
Merge pull request #18 from demike/fix/expanded-nodeid-tostring
Browse files Browse the repository at this point in the history
fix: expandedNodeId.toString and coerceExpandedNodeId
  • Loading branch information
demike authored Feb 24, 2025
2 parents 60cf4f5 + fe579bb commit 3cb0997
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
9 changes: 9 additions & 0 deletions src/nodeid/expanded_nodeid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 3 additions & 3 deletions src/nodeid/expanded_nodeid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
19 changes: 9 additions & 10 deletions src/nodeid/nodeid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,44 +90,43 @@ export class NodeId {
*
*/
toString(namespaceArray?: string[]): string {
let str;
const namespaceStr: string = namespaceArray
? this.namespace === 0
? ''
: `nsu=${
namespaceArray[this.namespace] || `<unknown namespace with index ${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(
this.identifierType === NodeIdType.ByteString,
'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=<null>';
valueStr = 'b=<null>';
}
break;
}

return str;
return namespaceStr + valueStr;
}

/**
Expand Down

0 comments on commit 3cb0997

Please sign in to comment.