-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbase64.test.ts
More file actions
31 lines (26 loc) · 1.1 KB
/
base64.test.ts
File metadata and controls
31 lines (26 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { decode as base64decode } from 'uint8-base64';
import { expect, test } from 'vitest';
import { parse } from '../parse.ts';
// library to convert base64 <--> arrayBuffer: https://github.com/niklasvh/base64-arraybuffer/blob/master/src/index.ts
const encoder = new TextEncoder();
const decoder = new TextDecoder();
test('base64 parsing', () => {
const xmlData = encoder.encode(`
<binaryDataArray encodedLength="3">
<cvParam cvRef="MS" accession="MS:1000523" name="64-bit float" value=""/>
<cvParam cvRef="MS" accession="MS:1000574" name="test" value=""/>
<binary>AAAAAAAA8D8AAAAAAAAAQAAAAAAAAAhA</binary>
</binaryDataArray>`);
const result = parse(xmlData, {
attributeNameProcessor: (name) => name,
tagValueProcessor: (value, node) => {
if (node.tagName !== 'binary') return decoder.decode(value);
const decoded = base64decode(value);
// isLittleEndian and the data were encoded in littleEndian
return new Float64Array(decoded.buffer);
},
}) as Record<string, any>;
expect(result.binaryDataArray.binary).toStrictEqual(
Float64Array.from([1, 2, 3]),
);
});