diff --git a/package.json b/package.json index 620658a..0c4b32f 100644 --- a/package.json +++ b/package.json @@ -41,18 +41,18 @@ "@types/jest": "^29.5.12", "cheminfo-build": "^1.2.0", "eslint": "^8.57.0", - "eslint-config-cheminfo-typescript": "^12.2.0", + "eslint-config-cheminfo-typescript": "^12.4.0", "he": "^1.2.0", "iobuffer": "^5.3.2", "jest": "^29.7.0", "pako": "^2.1.0", "prettier": "^3.2.5", - "rimraf": "^5.0.5", + "rimraf": "^5.0.7", "ts-jest": "^29.1.2", - "typescript": "^5.3.3", + "typescript": "^5.4.5", "uint8-base64": "^0.1.1" }, "dependencies": { - "dynamic-typing": "^1.0.0" + "dynamic-typing": "^1.0.1" } } diff --git a/src/.npmignore b/src/.npmignore index d1c932d..e75adf4 100644 --- a/src/.npmignore +++ b/src/.npmignore @@ -1,3 +1,2 @@ __tests__ -.npmignore -.DS_Store \ No newline at end of file +.npmignore \ No newline at end of file diff --git a/src/XMLNode.ts b/src/XMLNode.ts index 1f5308c..1ddd5b9 100644 --- a/src/XMLNode.ts +++ b/src/XMLNode.ts @@ -1,19 +1,21 @@ +export type XMLNodeValue = string | Uint8Array | number | boolean; + export class XMLNode { public tagName: string; public parent?: XMLNode; public children: Record; public attributes?: Record; - public value?: string | Uint8Array; + public value?: XMLNodeValue; public startIndex: number; public constructor( tagName: string, parent?: XMLNode, - value?: Uint8Array | string, + value?: Uint8Array | string | undefined | number, ) { this.tagName = tagName; this.parent = parent; - this.children = Object.create({}); //child tags - this.attributes = Object.create({}); //attributes map + this.children = Object.create(null); //child tags + this.attributes = Object.create(null); //attributes map this.value = value; //text only this.startIndex = -1; } diff --git a/src/traversable/utils/concat.ts b/src/traversable/utils/concat.ts index 4b85f27..60d1d29 100644 --- a/src/traversable/utils/concat.ts +++ b/src/traversable/utils/concat.ts @@ -1,7 +1,6 @@ -export function concat( - a?: string | ArrayLike | undefined, - b?: string | ArrayLike, -) { +import { XMLNodeValue } from '../../XMLNode'; + +export function concat(a?: XMLNodeValue, b?: XMLNodeValue) { if (a === undefined) { a = typeof b === 'string' ? '' : new Uint8Array(0); } diff --git a/src/traversableToJSON.ts b/src/traversableToJSON.ts index 20e8ac9..df61629 100644 --- a/src/traversableToJSON.ts +++ b/src/traversableToJSON.ts @@ -1,11 +1,9 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { XMLNode } from './XMLNode'; +import { parseString } from 'dynamic-typing'; + +import { XMLNode, XMLNodeValue } from './XMLNode'; import { ParseOptions } from './traversable/defaultOptions'; import { isTagNameInArrayMode, merge, isEmptyObject } from './util'; -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { parseString } = require('dynamic-typing'); - /** * * @param {*} node @@ -17,7 +15,7 @@ export function traversableToJSON( node: XMLNode, options: ParseOptions, parentTagName?: string, -): string | Uint8Array | Record { +): XMLNodeValue | Record { const { dynamicTypingNodeValue, tagValueProcessor, @@ -43,7 +41,12 @@ export function traversableToJSON( } // otherwise create a textnode if node has some text - if (node.value !== undefined && node.value.length !== 0) { + if ( + node.value !== undefined && + (typeof node.value === 'number' || + typeof node.value === 'boolean' || + node.value.length !== 0) + ) { const asArray = isTagNameInArrayMode( node.tagName, arrayMode, diff --git a/tsconfig.json b/tsconfig.json index a3c234e..18aa530 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,7 @@ "esModuleInterop": true, "moduleResolution": "node", "outDir": "lib", + "skipLibCheck": true, "sourceMap": true, "strict": true, "target": "es2022"