Skip to content

Commit

Permalink
fix: refactoring code for more clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi Fatkhurozi committed Jun 12, 2023
1 parent a087c73 commit fcc22b4
Show file tree
Hide file tree
Showing 5 changed files with 535 additions and 557 deletions.
87 changes: 40 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,25 @@ npm install @adifkz/exp-p
To use ExpressionParser in your JavaScript code, you need to import the ExpressionParser class and create an instance of it:

```typescript
import ExpressionParser from '@adifkz/exp-p';
import { createParser } from '@adifkz/exp-p';

const parser = new ExpressionParser();
const parser = createParser();
```

## Evaluating Expressions
Once you have created an instance of ExpressionParser, you can use the evaluate method to evaluate expressions. The evaluate method takes two parameters: the expression to evaluate and an optional context object that contains variables and functions used in the expression.

```typescript
const parser = createParser();

const result = parser.evaluate('2 + 3 * 4'); // 14
```

## Variable Mapping
You can define variables and use them in your expressions by providing a context object to the evaluate method.

```typescript
const parser = new ExpressionParser();
const parser = createParser();

// Define variables
const variables = {
Expand All @@ -52,32 +54,32 @@ const variables = {
};

// Evaluate expression with variables
const result = parser.evaluate("x + y", { variables });
const result = parser.evaluate("x + y", variables);
console.log(result); // Output: 15
```

## Function Extension
The Expression Parser allows you to extend its functionality by adding custom functions. Here's an example of defining a custom function:

```typescript
const parser = new ExpressionParser();
const parser = createParser();

// Define custom function
const functions = {
square: (_, value: number) => value * value,
};

parser.setFunctions(functions)
// Evaluate expression with custom function
const result = parser.evaluate("square(5)", { functions });
const result = parser.evaluate("square(5)");
console.log(result); // Output: 25
```


## Examples

```typescript
it('basic operator', () => {
const parser = new ExpressionParser()
const parser = createParser()
expect(parser.evaluate('(2 + 3) * 4 - 4')).toBe(16)
expect(parser.evaluate('-4 + 5')).toBe(1)
expect(parser.evaluate('4 <= (5 + 2)')).toBe(true)
Expand All @@ -92,47 +94,46 @@ it('function', () => {
// Usage example
const variables = { x: 5 };
const functions: FunctionMap = {
ADD: (_, a: number, b: number) => a + b,
LENGTH: (_, str: string) => str.length,
LENGTH_ALL: (_, str1: string, str2: string, str3: string) => [str1.length, str2.length, str3.length],
add: (_, a: number, b: number) => a + b,
length: (_, str: string) => str.length,
length_all: (_, str1: string, str2: string, str3: string) => [str1.length, str2.length, str3.length],
};
const parser = new ExpressionParser({ variables, functions });
expect(parser.evaluate('ADD(1 + 1, 5) + x')).toBe(12)
expect(parser.evaluate('LENGTH("ADI") + 5', { functions, variables },)).toBe(8)
expect(parser.evaluate('LENGTH_ALL("ADI", "FA", "TK")', { variables, functions })).toEqual([3, 2, 2])
const parser = createParser({ variables });
parser.setFunctions(functions)
expect(parser.evaluate('add(1 + 1, 5) + x')).toBe(12)
expect(parser.evaluate('length("ADI") + 5', variables)).toBe(8)
expect(parser.evaluate('length_all("ADI", "FA", "TK")', variables)).toEqual([3, 2, 2])
});
it('string', () => {
const parser = new ExpressionParser();
const parser = createParser();
expect(parser.evaluate('"ADI"')).toBe("ADI")
expect(parser.evaluate('\'ADI\'')).toBe("ADI")
})
it('boolean', () => {
const parser = new ExpressionParser();
expect(parser.evaluate('true AND false')).toBe(false)
expect(parser.evaluate('true OR false')).toBe(true)
const parser = createParser();
expect(parser.evaluate('true and false')).toBe(false)
expect(parser.evaluate('true or false')).toBe(true)
expect(parser.evaluate('!true')).toBe(false)
expect(parser.evaluate('!!true')).toBe(true)
})
it('array', () => {
const parser = new ExpressionParser();
const parser = createParser();
expect(parser.evaluate("[1, 2, 3, 4]")).toEqual([1, 2, 3, 4])
expect(parser.evaluate("[\"2\", 5]")).toEqual(["2", 5])
expect(parser.evaluate("[2 + 5, 5]")).toEqual([7, 5])
expect(parser.evaluate("[5, x]", { variables: { x: 2 } })).toEqual([5, 2])
expect(parser.evaluate("[5, x]", { x: 2 })).toEqual([5, 2])
})
it('array method', () => {
const parser = new ExpressionParser();
const parser = createParser();
const products = [
{ name: 'Product 1', price: 150, quantity: 2 },
{ name: 'Product 2', price: 80, quantity: 0 },
{ name: 'Product 3', price: 200, quantity: 5 },
{ name: 'Product 4', price: 120, quantity: 1 }
];
expect(
parser.evaluate('FILTER(products, "__ITEM__.price > 100 AND __ITEM__.quantity > 0")', {
variables: {
products
}
parser.evaluate('filter(products, "_item_.price > 100 and _item_.quantity > 0")', {
products
})
).toEqual([
{ name: 'Product 1', price: 150, quantity: 2 },
Expand All @@ -141,10 +142,8 @@ it('array method', () => {
])

expect(
parser.evaluate('MAP(products, "__ITEM__.name")', {
variables: {
products
}
parser.evaluate('map(products, "_item_.name")', {
products
})
).toEqual([
'Product 1',
Expand All @@ -154,10 +153,8 @@ it('array method', () => {
])

expect(
parser.evaluate('FIND(products, "__ITEM__.price > 0")', {
variables: {
products
}
parser.evaluate('find(products, "_item_.price > 0")', {
products
})
).toEqual({
"name": "Product 1",
Expand All @@ -166,23 +163,19 @@ it('array method', () => {
})

expect(
parser.evaluate('SOME(products, "__ITEM__.price == 200")', {
variables: {
products
}
parser.evaluate('some(products, "_item_.price == 200")', {
products
})
).toBe(true)

expect(
parser.evaluate('REDUCE(products, "__CURR__ + __ITEM__.price", 0)', {
variables: {
products
}
parser.evaluate('reduce(products, "_curr_ + _item_.price", 0)', {
products
})
).toBe(550)
})
it('object', () => {
const parser = new ExpressionParser();
const parser = createParser();
expect(parser.evaluate("{ name: 'ADI', age: 20 }")).toEqual({
name: "ADI",
age: 20
Expand All @@ -191,9 +184,9 @@ it('object', () => {
name: "ADI",
age: 7
})
expect(parser.evaluate("object.name", { variables: { object: { name: 'ADI' } } })).toEqual('ADI')
expect(parser.evaluate("object.name", { variables: { object: { name: 'ADI' } } })).toEqual('ADI')
expect(parser.evaluate("object.0.name", { variables: { object: [{ name: 'ADI' }] } })).toEqual('ADI')
expect(parser.evaluate("object.0.object.0.name", { variables: { object: [{ name: 'ADI', object: [{ name: "ADI" }] }] } })).toEqual('ADI')
expect(parser.evaluate("object.name", { object: { name: 'ADI' } })).toEqual('ADI')
expect(parser.evaluate("object.name", { object: { name: 'ADI' } })).toEqual('ADI')
expect(parser.evaluate("object.0.name", { object: [{ name: 'ADI' }] })).toEqual('ADI')
expect(parser.evaluate("object.0.object.0.name", { object: [{ name: 'ADI', object: [{ name: "ADI" }] }] })).toEqual('ADI')
})
```
Loading

0 comments on commit fcc22b4

Please sign in to comment.