Skip to content

Commit 45a57d6

Browse files
committed
feat!: Simplifcations of the code
closes: #21 test: replace jest by vitest chore: update all the dependencies and fix eslint / prettier
1 parent b2a88f8 commit 45a57d6

35 files changed

+626
-412
lines changed

.eslintrc.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

eslint.config.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import cheminfo from 'eslint-config-cheminfo-typescript';
2+
import globals from 'globals';
3+
4+
export default [
5+
...cheminfo,
6+
{
7+
languageOptions: {
8+
globals: {
9+
...globals.node,
10+
},
11+
},
12+
rules: {},
13+
},
14+
];

jest.config.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"prettier": "prettier --check src",
2121
"prettier-write": "prettier --write src",
2222
"test": "npm run test-only && npm run eslint && npm run prettier && npm run check-types",
23-
"test-only": "jest --coverage",
23+
"test-only": "vitest run --coverage",
2424
"tsc": "npm run clean && npm run tsc-cjs && npm run tsc-esm",
2525
"tsc-cjs": "tsc --project tsconfig.cjs.json",
2626
"tsc-esm": "tsc --project tsconfig.esm.json"
@@ -38,19 +38,19 @@
3838
"homepage": "https://github.com/cheminfo/arraybuffer-xml-parser#readme",
3939
"devDependencies": {
4040
"@types/he": "^1.2.3",
41-
"@types/jest": "^29.5.12",
41+
"@vitest/coverage-v8": "2.1.7",
4242
"cheminfo-build": "^1.2.0",
43-
"eslint": "^8.57.0",
44-
"eslint-config-cheminfo-typescript": "^13.0.0",
43+
"eslint": "^9.16.0",
44+
"eslint-config-cheminfo-typescript": "^17.0.0",
45+
"globals": "^15.13.0",
4546
"he": "^1.2.0",
4647
"iobuffer": "^5.3.2",
47-
"jest": "^29.7.0",
4848
"pako": "^2.1.0",
49-
"prettier": "^3.3.2",
50-
"rimraf": "^5.0.7",
51-
"ts-jest": "^29.1.5",
52-
"typescript": "^5.5.3",
53-
"uint8-base64": "^0.1.1"
49+
"prettier": "^3.4.1",
50+
"rimraf": "^6.0.1",
51+
"typescript": "^5.7.2",
52+
"uint8-base64": "^0.1.1",
53+
"vitest": "^2.1.7"
5454
},
5555
"dependencies": {
5656
"dynamic-typing": "^1.0.1"

src/XMLNode.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,48 @@
1+
import type { TagValueProcessor } from './traversable/defaultOptions';
2+
13
export type XMLNodeValue = string | Uint8Array | number | boolean;
4+
export type XMLAttributeValue = string | number | boolean;
25

36
export class XMLNode {
47
public tagName: string;
58
public parent?: XMLNode;
69
public children: Record<string, XMLNode[]>;
7-
public attributes?: Record<string, XMLNode | boolean>;
8-
public value?: XMLNodeValue;
10+
public attributes?: Record<string, XMLAttributeValue>;
11+
public bytes: Uint8Array;
912
public startIndex: number;
13+
private tagValueProcessor: TagValueProcessor;
14+
private cachedValue?: XMLNodeValue;
1015
public constructor(
1116
tagName: string,
12-
parent?: XMLNode,
13-
value?: Uint8Array | string | undefined | number,
17+
parent: XMLNode | undefined,
18+
bytes: Uint8Array,
19+
tagValueProcessor: TagValueProcessor,
1420
) {
1521
this.tagName = tagName;
1622
this.parent = parent;
1723
this.children = Object.create(null); //child tags
1824
this.attributes = Object.create(null); //attributes map
19-
this.value = value; //text only
25+
this.bytes = bytes; //text only
26+
this.tagValueProcessor = tagValueProcessor;
2027
this.startIndex = -1;
2128
}
29+
public append(toAppend: Uint8Array): void {
30+
if (this.bytes.length === 0) {
31+
this.bytes = toAppend;
32+
return;
33+
}
34+
const arrayConcat = new Uint8Array(this.bytes.length + toAppend.length);
35+
arrayConcat.set(this.bytes);
36+
arrayConcat.set(toAppend, this.bytes.length);
37+
this.bytes = arrayConcat;
38+
}
39+
public get value(): any {
40+
if (this.cachedValue === undefined) {
41+
const value = this.tagValueProcessor(this.bytes, this);
42+
this.cachedValue = value;
43+
}
44+
return this.cachedValue;
45+
}
2246
public addChild(child: XMLNode) {
2347
if (Array.isArray(this.children[child.tagName])) {
2448
//already presents

src/__tests__/arrayModeSpec.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, it, expect } from 'vitest';
2+
13
import { parse } from '../parse';
24

35
const encoder = new TextEncoder();

src/__tests__/arrayWithExtendedPrototypePropsSpec.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { describe, it, expect } from 'vitest';
2+
13
import { parse } from '../parse.ts';
24

35
const encoder = new TextEncoder();

src/__tests__/attributesSpec.test.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, it, expect } from 'vitest';
12
// import he from 'he';
23

34
import { parse } from '../parse';
@@ -18,9 +19,8 @@ describe('XMLParser', () => {
1819
};
1920

2021
const result = parse(xmlData, {
21-
attributeNamePrefix: '',
22+
attributeNameProcessor: (name) => name,
2223
ignoreAttributes: false,
23-
dynamicTypingAttributeValue: true,
2424
});
2525

2626
expect(result).toStrictEqual(expected);
@@ -42,9 +42,8 @@ describe('XMLParser', () => {
4242
};
4343

4444
const result = parse(xmlData, {
45-
attributeNamePrefix: '',
45+
attributeNameProcessor: (name) => name,
4646
ignoreAttributes: false,
47-
dynamicTypingAttributeValue: true,
4847
});
4948

5049
expect(result).toStrictEqual(expected);
@@ -66,9 +65,8 @@ describe('XMLParser', () => {
6665
};
6766

6867
const result = parse(xmlData, {
69-
attributeNamePrefix: '',
68+
attributeNameProcessor: (name) => name,
7069
ignoreAttributes: false,
71-
dynamicTypingAttributeValue: true,
7270
});
7371

7472
expect(result).toStrictEqual(expected);
@@ -91,9 +89,8 @@ describe('XMLParser', () => {
9189
};
9290

9391
const result = parse(xmlData, {
94-
attributeNamePrefix: '',
92+
attributeNameProcessor: (name) => name,
9593
ignoreAttributes: false,
96-
dynamicTypingAttributeValue: true,
9794
allowBooleanAttributes: true,
9895
});
9996

@@ -118,7 +115,7 @@ describe('XMLParser', () => {
118115
};
119116

120117
const result = parse(xmlData, {
121-
attributeNamePrefix: '',
118+
attributeNameProcessor: (name) => name,
122119
ignoreAttributes: false,
123120
});
124121

@@ -143,7 +140,7 @@ describe('XMLParser', () => {
143140
};
144141

145142
const result = parse(xmlData, {
146-
attributeNamePrefix: '',
143+
attributeNameProcessor: (name) => name,
147144
ignoreAttributes: false,
148145
ignoreNameSpace: true,
149146
});

src/__tests__/base64.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { decode as base64decode } from 'uint8-base64';
2+
import { test, expect } from 'vitest';
23

34
import { parse } from '../parse.ts';
45
// library to convert base64 <--> arrayBuffer: https://github.com/niklasvh/base64-arraybuffer/blob/master/src/index.ts
@@ -15,9 +16,8 @@ test('base64 parsing', () => {
1516
</binaryDataArray>`);
1617

1718
let result = parse(xmlData, {
18-
attributeNamePrefix: '',
19+
attributeNameProcessor: (name) => name,
1920
tagValueProcessor: (value, node) => {
20-
// console.log(node.parent.child.cvParam);
2121
if (node.tagName !== 'binary') return decoder.decode(value);
2222
const decoded = base64decode(value);
2323
// isLittleEndian and the data were encoded in littleEndian

src/__tests__/cdataSpec.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { readFileSync } from 'fs';
2-
import { join } from 'path';
1+
import { readFileSync } from 'node:fs';
2+
import { join } from 'node:path';
3+
4+
import { describe, it, expect } from 'vitest';
35

46
import { parse } from '../parse';
57

@@ -233,7 +235,6 @@ patronymic</person></root>`);
233235
const result = parse(xmlData, {
234236
ignoreAttributes: false,
235237
cdataTagName: '__cdata',
236-
cdataPositddionChar: '',
237238
});
238239

239240
expect(result).toStrictEqual(expected);

0 commit comments

Comments
 (0)