-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
32 lines (25 loc) · 853 Bytes
/
index.ts
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
32
import * as fs from 'fs';
import * as path from 'path';
class Fixture {
constructor(private readonly value: Buffer) {}
public toBinaryString(): string {
return this.value.toString('binary');
}
public toHex(): string {
return this.value.toString('hex');
}
public toUtf8(): string {
return this.value.toString('utf8');
}
public toJson(): any {
return JSON.parse(this.toBinaryString().trim());
}
public toArrayBuffer(): ArrayBuffer {
// `Buffer.buffer` might contain more data the the read fixture,
// so we need to use `slice` to only get the data of the fixture.
return this.value.buffer.slice(this.value.byteOffset, this.value.byteOffset + this.value.byteLength);
}
}
export default function loadfxt(filePath: string): Fixture {
return new Fixture(fs.readFileSync(path.normalize(filePath)));
}