Skip to content

Commit 82d33ad

Browse files
committed
chore: refactor functions
1 parent e25175d commit 82d33ad

File tree

6 files changed

+55
-68
lines changed

6 files changed

+55
-68
lines changed

src/parse.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,7 @@ import { getTraversable } from './traversable/getTraversable';
33
import { traversableToJSON } from './traversableToJSON';
44

55
/**
6-
* Parse an ArrayBuffer or Uint8Array representing an XML
7-
* @param {string|ArrayBuffer|Uint8Array} xmlData
8-
* @param {object} [options={}]
9-
* @param {string} [options.attributeNamePrefix='$']
10-
* @param {boolean} [options.attributesNodeName='']
11-
* @param {string} [options.textNodeName='#text']
12-
* @param {boolean} [options.trimValues=true] should we remove ascii < 32
13-
* @param {boolean} [options.ignoreAttributes=false] skip attributes
14-
* @param {boolean} [options.ignoreNameSpace=false]
15-
* @param {boolean} [options.dynamicTypingAttributeValue=true] Parse attribute values that looks like number or boolean
16-
* @param {boolean} [options.allowBooleanAttributes=false]
17-
* @param {boolean} [options.dynamicTypingNodeValue=true] Parse tag values that looks like number or boolean
18-
* @param {boolean} [options.arrayMode=false]
19-
* @param {boolean} [options.cdataTagName=false]
20-
* @param {function} [options.tagValueProcessor=(v, node) => decoder.decode(v)] Tag values can be modified during parsing. By default we decode the tag value (a Uint8Array) using TextDecoder
21-
* @param {function} [options.attributeValueProcessor=(v) => v] Attribute values can be modified during parsing
22-
* @param {function} [options.tagNameProcessor=(v) => v] Callback allowing to rename tag names
23-
* @param {function} [options.attributeNameProcessor=(v) => v] Callback allowing to rename attribute name
24-
* @param {boolean} [options.stopNodes=[]] prevent further parsing
25-
*
26-
* @returns {string}
6+
* Parse an ArrayBuffer or Uint8Array representing an XML and return an object
277
*/
288
export function parse(
299
xmlData: string | Uint8Array | ArrayBufferLike,

src/traversable/closingIndexForOpeningTag.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { decoder } from './getTraversable';
1+
import { decoder } from './utils/utf8Decoder';
22

33
export function closingIndexForOpeningTag(
44
data: Uint8Array,

src/traversable/getTraversable.ts

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ import { closingIndexForOpeningTag } from './closingIndexForOpeningTag';
66
import { ParseOptions } from './defaultOptions';
77
import { findClosingIndex } from './findClosingIndex';
88
import { parseAttributesString } from './parseAttributesString';
9-
10-
const utf8Decoder = new TextDecoder();
11-
12-
export const decoder = {
13-
decode: (array: BufferSource | Uint8Array) => {
14-
return utf8Decoder.decode(array);
15-
},
16-
};
9+
import { concat } from './utils/concat';
10+
import { removeNameSpaceIfNeeded } from './utils/removeNameSpaceIfNeeded';
11+
import { decoder } from './utils/utf8Decoder';
1712

1813
export function getTraversable(xmlData: Uint8Array, options: ParseOptions) {
1914
const traversable = new XMLNode('!xml');
@@ -227,41 +222,3 @@ export function getTraversable(xmlData: Uint8Array, options: ParseOptions) {
227222
}
228223
return traversable;
229224
}
230-
231-
function concat(
232-
a?: string | ArrayLike<number> | undefined,
233-
b?: string | ArrayLike<number>,
234-
) {
235-
if (a === undefined) {
236-
a = typeof b === 'string' ? '' : new Uint8Array(0);
237-
}
238-
if (b === undefined) {
239-
b = typeof a === 'string' ? '' : new Uint8Array(0);
240-
}
241-
if (typeof a === 'string' && typeof b === 'string') {
242-
return a + b;
243-
} else if (
244-
typeof a !== 'string' &&
245-
typeof b !== 'string' &&
246-
ArrayBuffer.isView(a) &&
247-
ArrayBuffer.isView(b)
248-
) {
249-
const arrayConcat = new Uint8Array(a.length + b.length);
250-
arrayConcat.set(a);
251-
arrayConcat.set(b, a.length);
252-
return arrayConcat;
253-
} else {
254-
throw new Error(
255-
`Unsuported value type for concatenation: ${typeof a} ${typeof b}`,
256-
);
257-
}
258-
}
259-
260-
function removeNameSpaceIfNeeded(tagName: string, options: ParseOptions) {
261-
if (!options.ignoreNameSpace) return tagName;
262-
const colonIndex = tagName.indexOf(':');
263-
if (colonIndex !== -1) {
264-
tagName = tagName.substr(colonIndex + 1);
265-
}
266-
return tagName;
267-
}

src/traversable/utils/concat.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export function concat(
2+
a?: string | ArrayLike<number> | undefined,
3+
b?: string | ArrayLike<number>,
4+
) {
5+
if (a === undefined) {
6+
a = typeof b === 'string' ? '' : new Uint8Array(0);
7+
}
8+
if (b === undefined) {
9+
b = typeof a === 'string' ? '' : new Uint8Array(0);
10+
}
11+
if (typeof a === 'string' && typeof b === 'string') {
12+
return a + b;
13+
} else if (
14+
typeof a !== 'string' &&
15+
typeof b !== 'string' &&
16+
ArrayBuffer.isView(a) &&
17+
ArrayBuffer.isView(b)
18+
) {
19+
const arrayConcat = new Uint8Array(a.length + b.length);
20+
arrayConcat.set(a);
21+
arrayConcat.set(b, a.length);
22+
return arrayConcat;
23+
} else {
24+
throw new Error(
25+
`Unsuported value type for concatenation: ${typeof a} ${typeof b}`,
26+
);
27+
}
28+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ParseOptions } from '../defaultOptions';
2+
3+
export function removeNameSpaceIfNeeded(
4+
tagName: string,
5+
options: ParseOptions,
6+
) {
7+
if (!options.ignoreNameSpace) {
8+
return tagName;
9+
}
10+
const colonIndex = tagName.indexOf(':');
11+
if (colonIndex !== -1) {
12+
tagName = tagName.substr(colonIndex + 1);
13+
}
14+
return tagName;
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const utf8Decoder = new TextDecoder();
2+
3+
export const decoder = {
4+
decode: (array: BufferSource | Uint8Array) => {
5+
return utf8Decoder.decode(array);
6+
},
7+
};

0 commit comments

Comments
 (0)