Skip to content
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

Add ternary operator #25

Merged
merged 1 commit into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
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
42 changes: 26 additions & 16 deletions src/operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,31 @@ describe('Test parseExpression', () => {
expect(parseExpression('ASUM(a, b)', { a: [{b: 5}, {b: 10}, {b: 0}, {b: 15}] })).toBe(30);
expect(parseExpression('ASUM(a, MULTIPLY(b, c))', { a: [{b: 5, c: 1}, {b: 10, c: 2}, {b: 1000, c: 0}, {b: 15, c: 10}] })).toBe(175);
});

test('IF op', () => {
expect(parseExpression('IF(a, b, c)', { a: true, b: 1, c: 2})).toBe(1);
expect(parseExpression('IF(a, b, c)', { a: false, b: 1, c: 2})).toBe(2);
expect(parseExpression('IF(a, b, c)', { a: 1, b: 1, c: 2})).toBe(2);
expect(parseExpression('IF(a, b, c)', { a: '1', b: 1, c: 2})).toBe(2);
expect(parseExpression('IF(a, b, c)', { a: {}, b: 1, c: 2})).toBe(2);
expect(parseExpression('IF(a, b, c)', { a: [], b: 1, c: 2})).toBe(2);
expect(parseExpression('IF(EQUAL(a, 5), b, c)', { a: 5, b: 1, c: 2})).toBe(1);
expect(parseExpression('IF(AND(GT(a, 0), LT(a, 10)), b, c)', { a: 5, b: 1, c: 2})).toBe(1);
});
});

describe('Test parseOp', () => {
test('Simple unary op', () => {
expect(parseOp('OP_(var)')).toStrictEqual({
op: 'OP_',
a: 'var',
b: null,
args: ['var'],
});
});

test('Simple binary op', () => {
expect(parseOp('OP_(var1,var2)')).toStrictEqual({
op: 'OP_',
a: 'var1',
b: 'var2',
args: ['var1', 'var2'],
});
});

Expand All @@ -274,48 +283,49 @@ describe('Test parseOp', () => {
test('Complex op 1', () => {
expect(parseOp('OP_(OP_(var1))')).toStrictEqual({
op: 'OP_',
a: 'OP_(var1)',
b: null,
args: ['OP_(var1)'],
});
});

test('Complex op 2', () => {
expect(parseOp('OP_(OP_(var1),var2)')).toStrictEqual({
op: 'OP_',
a: 'OP_(var1)',
b: 'var2',
args: ['OP_(var1)', 'var2'],
});
});

test('Complex op 3', () => {
expect(parseOp('OP_(OP_(var1),OP_(var2))')).toStrictEqual({
op: 'OP_',
a: 'OP_(var1)',
b: 'OP_(var2)',
args: ['OP_(var1)', 'OP_(var2)'],
});
});

test('Complex op 4', () => {
expect(parseOp('OP_(OP_(OP_(var1), var2),OP_(var3))')).toStrictEqual({
op: 'OP_',
a: 'OP_(OP_(var1), var2)',
b: 'OP_(var3)',
args: ['OP_(OP_(var1), var2)', 'OP_(var3)'],
});
});

test('Complex op 5', () => {
expect(parseOp('OP_(OP_(OP_(var1), var2),OP_(var3, OP_(var4, var5)))')).toStrictEqual({
op: 'OP_',
a: 'OP_(OP_(var1), var2)',
b: 'OP_(var3, OP_(var4, var5))',
args: ['OP_(OP_(var1), var2)', 'OP_(var3, OP_(var4, var5))'],
});
});

test('Complex op 5', () => {
expect(parseOp('OP_(OP_(OP_(var1, OP_(var2, OP_(var3, var4))), var5))')).toStrictEqual({
op: 'OP_',
a: 'OP_(OP_(var1, OP_(var2, OP_(var3, var4))), var5)',
b: null,
args: ['OP_(OP_(var1, OP_(var2, OP_(var3, var4))), var5)'],
});
});

test('Ternary op', () => {
expect(parseOp('OP_(OP_(var1),var2,var3)')).toStrictEqual({
op: 'OP_',
args: ['OP_(var1)', 'var2', 'var3'],
});
});
});
Expand Down
50 changes: 30 additions & 20 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export function parseExpression(exp: string, values: Record<string, any>): any {

const opMatch = parseOp(exp);
if (opMatch) {
const { op, a, b } = opMatch;
const valueA = parseExpression(a, values);
const { op, args } = opMatch;

// unary operators
if (b === null) {
if (args.length === 1) {
const valueA = parseExpression(args[0], values);
// type conversion
if (op === 'INT') {
return parseInt(valueA);
Expand Down Expand Up @@ -113,12 +113,13 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
}
return 0;
}
} else if (op === 'ASUM') {
} else if (op === 'ASUM' && args.length === 2) {
// aggregated sum
return (values[a] as unknown[])?.reduce((acc, item) => acc + parseExpression(b, item as typeof values), 0) ?? 0;
} else {
return (values[args[0]] as unknown[])?.reduce((acc, item) => acc + parseExpression(args[1], item as typeof values), 0) ?? 0;
} else if (args.length === 2) {
// binary operators
const valueB = parseExpression(b, values);
const valueA = parseExpression(args[0], values);
const valueB = parseExpression(args[1], values);

// arithmetic
if (op === 'SUM') {
Expand Down Expand Up @@ -183,6 +184,13 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
if (op === 'OR') {
return valueA || valueB;
}
} else if (args.length === 3) {
if (op === 'IF') {
if (parseExpression(args[0], values) === true) {
return parseExpression(args[1], values);
}
return parseExpression(args[2], values);
}
}
}

Expand All @@ -196,29 +204,31 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
return '';
}

export function parseOp(exp: string) {
export function parseOp(exp: string): {
op: string;
args: string[];
} | null {
const match = exp.match(/^([A-Z_]+)\((.+)\)$/);
if (match) {
const args = [];
const op = match[1] as string;
const innerExp = match[2] as string;
let braceCount = 0;
for (let i = 0; i < innerExp.length; i += 1) {

let braceCount = 0, i = 0, j = 0;
for (; i < innerExp.length; i += 1) {
const c = innerExp[i];
if (c === '(') braceCount += 1;
if (c === ')') braceCount -= 1;
if (c === ',' && braceCount === 0) {
return {
op,
a: innerExp.slice(0, i),
b: innerExp.slice(i + 1),
};
args.push(innerExp.slice(j, i));
j = i + 1;
}
}
return {
op,
a: innerExp,
b: null,
};
if (j < i) {
args.push(innerExp.slice(j, i));
}

return { op, args };
}
return null;
}
Expand Down