Skip to content

Commit f5365f5

Browse files
feat: allow to set user-defined id
1 parent db24969 commit f5365f5

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

tDataTypeTemplates/insertSelecetdLNodeType.spec.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
mhaiSelection,
1010
mmxuSelection,
1111
ptocSelection,
12-
lln0Selection
12+
lln0Selection,
13+
ptrcSelection
1314
} from "./insertSelectedLNodeType.testdata.js";
1415

1516
import {
@@ -22,6 +23,31 @@ import {
2223
import { insertSelectedLNodeType } from "./insertSelectedLNodeType.js";
2324
import { CdcChildren, DaDescription, LNodeDescription, nsdToJson } from "./nsdToJson.js";
2425

26+
27+
function cyrb64(str: string): string {
28+
/* eslint-disable no-bitwise */
29+
let h1 = 0xdeadbeef;
30+
let h2 = 0x41c6ce57;
31+
/* eslint-disable-next-line no-plusplus */
32+
for (let i = 0, ch; i < str.length; i++) {
33+
ch = str.charCodeAt(i);
34+
h1 = Math.imul(h1 ^ ch, 2654435761);
35+
h2 = Math.imul(h2 ^ ch, 1597334677);
36+
}
37+
h1 =
38+
Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^
39+
Math.imul(h2 ^ (h2 >>> 13), 3266489909);
40+
h2 =
41+
Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^
42+
Math.imul(h1 ^ (h1 >>> 13), 3266489909);
43+
return (
44+
(h2 >>> 0).toString(16).padStart(8, "0") +
45+
(h1 >>> 0).toString(16).padStart(8, "0")
46+
);
47+
/* eslint-enable no-bitwise */
48+
}
49+
50+
2551
const incompleteMmxu = findElement(missingMmxuTypes) as XMLDocument;
2652
const imcompleteLtrk = findElement(incompleteLtrkTypes) as XMLDocument;
2753
const incompleteAtcc = findElement(incompleteAtccTypes) as XMLDocument;
@@ -239,5 +265,15 @@ describe("insertLNodeTypeSelection", () => {
239265

240266
expect(edits.length).to.equal(5);
241267
expect((edits[3].node as Element).querySelector('DA[name="dataNs"] > Val')?.textContent).to.equal("TestNameSpace-1-d-1234567890");
268+
});
269+
270+
it('set user defined LNodeType.id', () => {
271+
const id = cyrb64("TestFile");
272+
const edits = insertSelectedLNodeType(missingDataTypes, ptrcSelection, { class: "PTRC", id });
273+
274+
expect(edits.length).to.equal(4);
275+
expect((edits[1].node as Element).getAttribute("id")).to.equal("96ba6481aba181d8");
276+
expect((edits[2].node as Element).getAttribute("id")).to.equal("Beh$oscd$_c6ed035c8137b35a");
277+
expect((edits[3].node as Element).getAttribute("id")).to.equal("stVal$oscd$_48ba16345b8e7f5b");
242278
})
243279
});

tDataTypeTemplates/insertSelectedLNodeType.testdata.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,17 @@ export const lln0Selection = {
533533
"dataNs": {}
534534
},
535535
}
536+
537+
export const ptrcSelection = {
538+
"Beh": {
539+
"q": {},
540+
"stVal": {
541+
"blocked": {},
542+
"off": {},
543+
"on": {},
544+
"test": {},
545+
"test/blocked": {}
546+
},
547+
"t": {}
548+
},
549+
}

tDataTypeTemplates/insertSelectedLNodeType.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ function data(lnData: any, path: string[]): any {
170170
export function insertSelectedLNodeType(
171171
doc: XMLDocument,
172172
selection: TreeSelection,
173-
logicalnode: { class: string, desc?: string, data?: LNodeDescription },
173+
logicalnode: { class: string, desc?: string, id?: string, data?: LNodeDescription },
174174
): Insert[] {
175175
const types = new Set<string>();
176176
const elements: Templates = {
@@ -193,9 +193,9 @@ export function insertSelectedLNodeType(
193193
return !alreadyCreate && !alreadyExist;
194194
}
195195

196-
function identify(element: Element, name: string): string {
196+
function identify(element: Element, name: string, userId?: string): string {
197197
const hash = hashElement(element);
198-
const id = `${name}$oscd$_${hash}`;
198+
const id = userId ?? `${name}$oscd$_${hash}`;
199199

200200
element.setAttribute("id", id);
201201
if (isUnknownId(id)) {
@@ -406,7 +406,8 @@ export function insertSelectedLNodeType(
406406
lnType.append(doElement);
407407
});
408408

409-
identify(lnType, lnClass);
409+
// write LNodeType.id user defined id or content hash
410+
identify(lnType, lnClass, logicalnode.id);
410411

411412
const dataTypeTemplates: Element =
412413
(doc.querySelector(":root > DataTypeTemplates") as Element) ||

0 commit comments

Comments
 (0)