Skip to content

Commit bfce8e4

Browse files
committed
feat: add convertEdit
1 parent f06bc85 commit bfce8e4

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed

convertEdit.spec.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* eslint-disable @typescript-eslint/no-unused-expressions */
2+
import { expect } from '@open-wc/testing';
3+
4+
import { Insert, Remove, Update } from './editv1.js';
5+
6+
import { convertEdit } from './convertEdit.js';
7+
import { SetAttributes } from './editv2.js';
8+
9+
const doc = new DOMParser().parseFromString(
10+
'<SCL><Substation name="AA1"></Substation></SCL>',
11+
'application/xml',
12+
);
13+
14+
const subSt = doc.querySelector('Substation')!;
15+
16+
const removeV1: Remove = { node: subSt };
17+
18+
const insertV1: Insert = {
19+
parent: subSt,
20+
node: doc.createAttribute('VoltageLevel'),
21+
reference: null,
22+
};
23+
24+
const update: Update = {
25+
element: subSt,
26+
attributes: {
27+
name: 'A2',
28+
desc: null,
29+
['__proto__']: 'a string',
30+
'myns:attr': {
31+
value: 'value1',
32+
namespaceURI: 'http://example.org/myns',
33+
},
34+
'myns:attr2': {
35+
value: 'value1',
36+
namespaceURI: 'http://example.org/myns',
37+
},
38+
attr: {
39+
value: 'value2',
40+
namespaceURI: 'http://example.org/myns2',
41+
},
42+
attr2: {
43+
value: 'value2',
44+
namespaceURI: 'http://example.org/myns2',
45+
},
46+
attr3: {
47+
value: 'value3',
48+
namespaceURI: null,
49+
},
50+
},
51+
};
52+
53+
const setAttributes: SetAttributes = {
54+
element: subSt,
55+
attributes: {
56+
name: 'A2',
57+
desc: null,
58+
},
59+
attributesNS: {
60+
'http://example.org/myns': {
61+
'myns:attr': 'value1',
62+
'myns:attr2': 'value1',
63+
},
64+
'http://example.org/myns2': {
65+
attr: 'value2',
66+
attr2: 'value2',
67+
},
68+
},
69+
};
70+
71+
const invalidEdit = { someWrongKey: 'someValue' } as unknown as Update;
72+
73+
describe('convertEditToEditV2', () => {
74+
it('passes through a Remove edit', () =>
75+
expect(convertEdit(removeV1)).to.deep.equal(removeV1));
76+
77+
it('passes through a Insert edit', () =>
78+
expect(convertEdit(insertV1)).to.deep.equal(insertV1));
79+
80+
it('converts Update to SetAttributes', () =>
81+
expect(convertEdit(update)).to.deep.equal(setAttributes));
82+
83+
it('converts complex edits', () => {
84+
const editsV1 = [removeV1, insertV1, update];
85+
86+
const editsV2 = convertEdit(editsV1);
87+
88+
const [removeV2, insertV2, updateV2] = Array.from(
89+
editsV2 as Array<Remove | Insert | SetAttributes>,
90+
);
91+
92+
expect(removeV1).to.deep.equal(removeV2);
93+
expect(insertV1).to.deep.equal(insertV2);
94+
expect(updateV2).to.deep.equal(setAttributes);
95+
});
96+
97+
it('return empty array for invalid edit', () => {
98+
expect(convertEdit(invalidEdit)).to.be.an('array').that.is.empty;
99+
});
100+
});

convertEdit.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import {
2+
Edit,
3+
isComplex,
4+
isInsert,
5+
isNamespaced,
6+
isUpdate,
7+
isRemove,
8+
Update,
9+
} from './editv1.js';
10+
11+
import { EditV2 } from './editv2.js';
12+
13+
function convertUpdate(edit: Update): EditV2 {
14+
const attributes: Partial<Record<string, string | null>> = {};
15+
const attributesNS: Partial<
16+
Record<string, Partial<Record<string, string | null>>>
17+
> = {};
18+
19+
Object.entries(edit.attributes).forEach(([key, value]) => {
20+
if (isNamespaced(value!)) {
21+
const ns = value.namespaceURI;
22+
if (!ns) {
23+
return;
24+
}
25+
26+
if (!attributesNS[ns]) {
27+
attributesNS[ns] = {};
28+
}
29+
attributesNS[ns][key] = value.value;
30+
} else {
31+
attributes[key] = value;
32+
}
33+
});
34+
35+
return { element: edit.element, attributes, attributesNS };
36+
}
37+
38+
export function convertEdit(edit: Edit): EditV2 {
39+
if (isRemove(edit)) {
40+
return edit as EditV2;
41+
}
42+
if (isInsert(edit)) {
43+
return edit as EditV2;
44+
}
45+
if (isUpdate(edit)) {
46+
return convertUpdate(edit);
47+
}
48+
if (isComplex(edit)) {
49+
return edit.map(convertEdit);
50+
}
51+
52+
return [];
53+
}

0 commit comments

Comments
 (0)