Skip to content

Commit

Permalink
feat: new json parser (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
imranbarbhuiya committed May 16, 2022
1 parent 47c3e60 commit ea0cfe1
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
3 changes: 1 addition & 2 deletions packages/tagscript/src/lib/Interpreter/Interpreter.ts
Expand Up @@ -84,8 +84,7 @@ export class Interpreter {
}

protected getAcceptors(ctx: Context) {
const acceptors = asyncFilter(this.parsers, (p) => p.willAccept(ctx));
return acceptors;
return asyncFilter(this.parsers, (p) => p.willAccept(ctx));
}

private getContext(node: Node, final: string, response: Response, originalMessage: string, tagLimit: number, parenType = ParenType.Both) {
Expand Down
27 changes: 27 additions & 0 deletions packages/tagscript/src/lib/Parsers/JSONVar.ts
@@ -0,0 +1,27 @@
import { SafeObjectTransformer } from '../Transformer';
import type { IParser } from '../interfaces';
import type { Context } from '../Interpreter';
import { BaseParser } from './Base';

/**
* JSON is useful when using fetch. You can get all the properties of a JSON object using parameters.
* @usage
* ```yaml
* {json(name):value}
* ```
* @example
* ```yaml
* {json(data):{"name": "John Doe", "age": 30}}
* Your age is `{data.age}`.
* ```
*/
export class JSONVarParser extends BaseParser implements IParser {
public constructor() {
super(['json'], true, true);
}

public parse(ctx: Context) {
ctx.response.variables[ctx.tag.parameter!] = new SafeObjectTransformer(ctx.tag.payload!);
return '';
}
}
1 change: 1 addition & 0 deletions packages/tagscript/src/lib/Parsers/index.ts
Expand Up @@ -5,6 +5,7 @@ export * from './Define';
export * from './FiftyFifty';
export * from './Format';
export * from './Includes';
export * from './JSONVar';
export * from './LooseVars';
export * from './Random';
export * from './Range';
Expand Down
7 changes: 4 additions & 3 deletions packages/tagscript/src/lib/Transformer/Object.ts
Expand Up @@ -6,7 +6,8 @@ import type { Lexer } from '../Interpreter';
*/
export class SafeObjectTransformer implements ITransformer {
private obj: Record<string, unknown>;
public constructor(obj: Record<string, unknown>) {

public constructor(obj: Record<string, unknown> | string) {
this.obj = this.makeObject(obj);
}

Expand All @@ -18,8 +19,8 @@ export class SafeObjectTransformer implements ITransformer {
return attribute ? `${attribute}` : null;
}

private makeObject(obj: Record<string, unknown>) {
const safeObject = JSON.parse(JSON.stringify(obj)) as Record<string, unknown>;
private makeObject(obj: Record<string, unknown> | string) {
const safeObject = JSON.parse(typeof obj === 'string' ? obj : JSON.stringify(obj)) as Record<string, unknown>;
Object.defineProperty(safeObject, 'toString', {
value: obj.toString.bind(obj)
});
Expand Down
23 changes: 23 additions & 0 deletions packages/tagscript/tests/Parsers/JSONVar.test.ts
@@ -0,0 +1,23 @@
import { Interpreter, JSONVarParser, Response, SafeObjectTransformer, StrictVarsParser } from '../../src';

describe('JSONVar', () => {
test('GIVEN a JSON in json var THEN store it as an object and show results using the parameter', async () => {
const ts = new Interpreter(new JSONVarParser(), new StrictVarsParser());

const text = '{json(data):{"name": "John Doe", "age": 30}}';

expect(await ts.run(text)).toStrictEqual(
new Response({
data: new SafeObjectTransformer('{"name": "John Doe", "age": 30}')
}).setValues('', text)
);

const text1 = `${text}{data.name}`;

expect(await ts.run(text1)).toStrictEqual(
new Response({
data: new SafeObjectTransformer('{"name": "John Doe", "age": 30}')
}).setValues('John Doe', text1)
);
});
});

0 comments on commit ea0cfe1

Please sign in to comment.