Skip to content

Commit

Permalink
fix: manage object data
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi Fatkhurozi committed Jun 10, 2023
1 parent 9d597fc commit 9f7a70a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
46 changes: 43 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ExpressionParser {
}

private tokenize(expression: string): string[] {
const regex = /([-+*/(),<>!=%^\[\]])|\b(?:\d+(\.\d+)?)|(?:"[^"]*")|(?:'[^']*')|(?:\w+)/g;
const regex = /([-+*/():,<>!=%^\[\]\{\}])|\b(?:\d+(\.\d+)?)|(?:"[^"]*")|(?:'[^']*')|(?:\w+)/g;
return expression.match(regex) || [];
}

Expand Down Expand Up @@ -99,6 +99,43 @@ class ExpressionParser {
return array;
}

private parseObject(state: ParserState): object {
const obj: { [key: string]: any } = {};
while (true) {
const key = state.currentToken;
if (typeof key !== 'string') {
throw new Error('Invalid object literal');
}
state.nextToken();
if (state.currentToken !== ':') {
throw new Error('Invalid object literal');
}

state.nextToken();

const value = this.parseExpression(state);
obj[key] = value;

if (state.currentToken as any === '}') {
break;
}

if (state.currentToken as any !== ',') {
throw new Error('Invalid object literal');
}

state.nextToken();
}

if (state.currentToken as any !== '}') {
throw new Error('Invalid object literal');
}

state.nextToken();

return obj;
}

private parseFunction(state: ParserState): any {
const token = state.currentToken;
const func = state.functions[token];
Expand Down Expand Up @@ -129,13 +166,13 @@ class ExpressionParser {
}

private parseFactor(state: ParserState): any {
let value: number | string | boolean | any[] = 0;
let value: number | string | boolean | any[] | object = 0;
const token = state.currentToken;

if (token === undefined) {
throw new Error('Invalid expression');
}

console.log(token)
if (token === '(') {
state.nextToken();
value = this.parseExpression(state);
Expand Down Expand Up @@ -167,6 +204,9 @@ class ExpressionParser {
const factor = this.parseFactor(state);

value = operator(0, factor);
} else if (token === '{') {
state.nextToken()
value = this.parseObject(state);
} else {
throw new Error('Invalid expression');
}
Expand Down
22 changes: 11 additions & 11 deletions src/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ describe('Example', () => {
expect(parser.evaluate("[2 + 5, 5]")).toEqual([7, 5])
expect(parser.evaluate("[5, x]", { x: 2 })).toEqual([5, 2])
})
// it('object', () => {
// const parser = new ExpressionParser();
// expect(parser.evaluate("{ name: 'ADI', age: 20 }")).toEqual({
// name: "ADI",
// age: 20
// })
// expect(parser.evaluate("{ name: 'ADI', age: 5 + 2 }")).toEqual({
// name: "ADI",
// age: 7
// })
// })
it('object', () => {
const parser = new ExpressionParser();
expect(parser.evaluate("{ name: 'ADI', age: 20 }")).toEqual({
name: "ADI",
age: 20
})
expect(parser.evaluate("{ name: 'ADI', age: 5 + 2 }")).toEqual({
name: "ADI",
age: 7
})
})
});

0 comments on commit 9f7a70a

Please sign in to comment.