Skip to content

Commit 21cb23f

Browse files
authored
Merge pull request keichi#119 from melphis/master
Typescript support fixes.
2 parents c7e4532 + 66b13e2 commit 21cb23f

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

lib/binary_parser.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ const aliasRegistry: { [key: string]: Parser } = {};
66
const FUNCTION_PREFIX = '___parser_';
77

88
interface ParserOptions {
9-
length?: number;
10-
type?: Types | Parser;
11-
assert?: (item: any) => void | string | number;
12-
formatter?: (item: any) => void;
13-
encoding?: 'utf8';
14-
lengthInBytes?: null;
9+
length?: number | string | ((item: any) => number);
10+
assert?: number | string | ((item: number | string) => boolean);
11+
lengthInBytes?: number | string | ((item: any) => number);
12+
type?: string | Parser;
13+
formatter?: (item: any) => string | number;
14+
encoding?: string;
1515
readUntil?: 'eof';
16-
greedy?: null;
17-
choices?: { [key: string]: string };
18-
defaultChoice?: null;
16+
greedy?: boolean;
17+
choices?: { [key: number]: string | Parser };
18+
defaultChoice?: string | Parser;
1919
zeroTerminated?: boolean;
2020
clone?: null;
2121
stripNull?: null;
2222
key?: null;
23-
tag?: null;
24-
offset?: null;
23+
tag?: string;
24+
offset?: number | string | ((item: any) => number);
2525
}
2626

2727
type Types = PrimitiveTypes | ComplexTypes;
@@ -476,21 +476,25 @@ export class Parser {
476476
throw new Error('Choices option of array is not defined.');
477477
}
478478

479-
Object.keys(options.choices).forEach(key => {
480-
if (isNaN(parseInt(key, 10))) {
479+
Object.keys(options.choices).forEach((keyString: string) => {
480+
const key = parseInt(keyString, 10);
481+
const value = options.choices[key];
482+
483+
if (isNaN(key)) {
481484
throw new Error('Key of choices must be a number.');
482485
}
483-
if (!options.choices[key]) {
484-
throw new Error(`Choice Case ${key} of ${varName} is not valid.`);
486+
487+
if (!value) {
488+
throw new Error(`Choice Case ${keyString} of ${varName} is not valid.`);
485489
}
486490

487491
if (
488-
typeof options.choices[key] === 'string' &&
489-
!aliasRegistry[options.choices[key]] &&
490-
Object.keys(PRIMITIVE_SIZES).indexOf(options.choices[key]) < 0
492+
typeof value === 'string' &&
493+
!aliasRegistry[value] &&
494+
Object.keys(PRIMITIVE_SIZES).indexOf(value) < 0
491495
) {
492496
throw new Error(
493-
`Specified primitive type "${options.choices[key]}" is not supported.`
497+
`Specified primitive type "${value}" is not supported.`
494498
);
495499
}
496500
});
@@ -679,9 +683,9 @@ export class Parser {
679683
}
680684
size = this.options.length * elementSize;
681685

682-
// if this a seek
686+
// if this a skip
683687
} else if (this.type === 'seek') {
684-
size = this.options.length;
688+
size = this.options.length as number;
685689

686690
// if this is a nested parser
687691
} else if (this.type === 'nest') {
@@ -835,7 +839,7 @@ export class Parser {
835839
(this.next && ['bit', 'nest'].indexOf(this.next.type) < 0)
836840
) {
837841
let sum = 0;
838-
ctx.bitFields.forEach(parser => (sum += parser.options.length));
842+
ctx.bitFields.forEach(parser => (sum += parser.options.length as number));
839843

840844
const val = ctx.generateTmpVariable();
841845

@@ -864,14 +868,14 @@ export class Parser {
864868

865869
let bitOffset = 0;
866870
const isBigEndian = this.endian === 'be';
871+
867872
ctx.bitFields.forEach(parser => {
868-
const offset = isBigEndian
869-
? sum - bitOffset - parser.options.length
870-
: bitOffset;
871-
const mask = (1 << parser.options.length) - 1;
873+
const length = parser.options.length as number;
874+
const offset = isBigEndian ? sum - bitOffset - length : bitOffset;
875+
const mask = (1 << length) - 1;
872876

873877
ctx.pushCode(`${parser.varName} = ${val} >> ${offset} & ${mask};`);
874-
bitOffset += parser.options.length;
878+
bitOffset += length;
875879
});
876880

877881
ctx.bitFields = [];
@@ -1054,7 +1058,7 @@ export class Parser {
10541058
}
10551059
ctx.pushCode(`switch(${tag}) {`);
10561060
Object.keys(this.options.choices).forEach(tag => {
1057-
const type = this.options.choices[tag];
1061+
const type = this.options.choices[parseInt(tag, 10)];
10581062

10591063
ctx.pushCode(`case ${tag}:`);
10601064
this.generateChoiceCase(ctx, this.varName, type);

0 commit comments

Comments
 (0)