Skip to content

Commit

Permalink
fix: onToken to not stop tokenizer sending data to tokenParser
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Jul 26, 2023
1 parent 5b0e267 commit 3d1bcaa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/plainjs/dist/deno/jsonparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export default class JSONParser {
}

public set onToken(cb: (parsedTokenInfo: ParsedTokenInfo) => void) {
this.tokenizer.onToken = cb;
this.tokenizer.onToken = (parsedToken) => {
cb(parsedToken);
this.tokenParser.write(parsedToken);
};
}

public set onValue(cb: (parsedElementInfo: ParsedElementInfo) => void) {
Expand Down
5 changes: 4 additions & 1 deletion packages/plainjs/src/jsonparser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export default class JSONParser {
}

public set onToken(cb: (parsedTokenInfo: ParsedTokenInfo) => void) {
this.tokenizer.onToken = cb;
this.tokenizer.onToken = (parsedToken) => {
cb(parsedToken);
this.tokenParser.write(parsedToken);
};
}

public set onValue(cb: (parsedElementInfo: ParsedElementInfo) => void) {
Expand Down
13 changes: 13 additions & 0 deletions packages/plainjs/test/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ describe("callback", () => {
}
});

test("should emit onValue callback when the onToken callback is also set", () => {
const p = new JSONParser();

const onValueCb = jest.fn(() => {
/* Do nothing */
});
p.onValue = onValueCb;

p.write('"test"');
expect(onValueCb.mock.calls).toHaveLength(1);
expect((onValueCb.mock.calls[0] as any)[0].value).toBe("test");
});

test("should handle invalid input using the onError callback if set", () => {
const p = new JSONParser();
p.onValue = () => {
Expand Down

0 comments on commit 3d1bcaa

Please sign in to comment.