Skip to content

Typescript support fixes. #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions lib/binary_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ const aliasRegistry: { [key: string]: Parser } = {};
const FUNCTION_PREFIX = '___parser_';

interface ParserOptions {
length?: number;
type?: Types | Parser;
assert?: (item: any) => void | string | number;
formatter?: (item: any) => void;
encoding?: 'utf8';
lengthInBytes?: null;
length?: number | string | ((item: any) => number);
assert?: number | string | ((item: number | string) => boolean);
lengthInBytes?: number | string | ((item: any) => number);
type?: string | Parser;
formatter?: (item: any) => string | number;
encoding?: string;
readUntil?: 'eof';
greedy?: null;
choices?: { [key: string]: string };
defaultChoice?: null;
greedy?: boolean;
choices?: { [key: number]: string | Parser };
defaultChoice?: string | Parser;
zeroTerminated?: boolean;
clone?: null;
stripNull?: null;
key?: null;
tag?: null;
offset?: null;
tag?: string;
offset?: number | string | ((item: any) => number);
}

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

Object.keys(options.choices).forEach(key => {
if (isNaN(parseInt(key, 10))) {
Object.keys(options.choices).forEach((keyString: string) => {
const key = parseInt(keyString, 10);
const value = options.choices[key];

if (isNaN(key)) {
throw new Error('Key of choices must be a number.');
}
if (!options.choices[key]) {
throw new Error(`Choice Case ${key} of ${varName} is not valid.`);

if (!value) {
throw new Error(`Choice Case ${keyString} of ${varName} is not valid.`);
}

if (
typeof options.choices[key] === 'string' &&
!aliasRegistry[options.choices[key]] &&
Object.keys(PRIMITIVE_SIZES).indexOf(options.choices[key]) < 0
typeof value === 'string' &&
!aliasRegistry[value] &&
Object.keys(PRIMITIVE_SIZES).indexOf(value) < 0
) {
throw new Error(
`Specified primitive type "${options.choices[key]}" is not supported.`
`Specified primitive type "${value}" is not supported.`
);
}
});
Expand Down Expand Up @@ -679,9 +683,9 @@ export class Parser {
}
size = this.options.length * elementSize;

// if this a seek
// if this a skip
} else if (this.type === 'seek') {
size = this.options.length;
size = this.options.length as number;

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

const val = ctx.generateTmpVariable();

Expand Down Expand Up @@ -864,14 +868,14 @@ export class Parser {

let bitOffset = 0;
const isBigEndian = this.endian === 'be';

ctx.bitFields.forEach(parser => {
const offset = isBigEndian
? sum - bitOffset - parser.options.length
: bitOffset;
const mask = (1 << parser.options.length) - 1;
const length = parser.options.length as number;
const offset = isBigEndian ? sum - bitOffset - length : bitOffset;
const mask = (1 << length) - 1;

ctx.pushCode(`${parser.varName} = ${val} >> ${offset} & ${mask};`);
bitOffset += parser.options.length;
bitOffset += length;
});

ctx.bitFields = [];
Expand Down Expand Up @@ -1054,7 +1058,7 @@ export class Parser {
}
ctx.pushCode(`switch(${tag}) {`);
Object.keys(this.options.choices).forEach(tag => {
const type = this.options.choices[tag];
const type = this.options.choices[parseInt(tag, 10)];

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