Skip to content

Commit ada41ad

Browse files
authored
Merge pull request #20 from rezo-labs/feature/add_new_ops
Add new operators for Date
2 parents eaf44f0 + 14ef1df commit ada41ad

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ Operator | Description
7373
--- | ---
7474
`DATE_ISO(a)` | transform date or date-like object to ISO string
7575
`DATE_UTC(a)` | transform date or date-like object to UTC string
76+
`YEAR(a)` | get year of a date object, similar to `getFullYear`
77+
`MONTH(a)` | get month of a date object, similar to `getMonth`
78+
`GET_DATE(a)` | get date of a date object, similar to `getDate`
79+
`DAY(a)` | get day of a date object, similar to `getDay`
80+
`HOURS(a)` | get hours of a date object, similar to `getHours`
81+
`MINUTES(a)` | get minutes of a date object, similar to `getMinutes`
82+
`SECONDS(a)` | get seconds of a date object, similar to `getSeconds`
83+
`TIME(a)` | get time of a date object, similar to `getTime`
7684

7785
### Arithmetic
7886

src/operations.test.ts

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { describe, expect, test } from '@jest/globals';
1+
import { describe, expect, test, jest } from '@jest/globals';
22
import { parseExpression, parseOp, toSlug } from './operations';
33

4+
jest
5+
.useFakeTimers()
6+
.setSystemTime(new Date('2023-01-01'));
7+
48
describe('Test parseExpression', () => {
9+
test('Dynamic variables', () => {
10+
expect(parseExpression('$NOW', {})).toStrictEqual(new Date());
11+
});
12+
513
test('INT op', () => {
614
expect(parseExpression('INT(a)', { a: '1' })).toBe(1);
715
});
@@ -28,12 +36,48 @@ describe('Test parseExpression', () => {
2836
expect(parseExpression('CURRENCY(a)', { a: 1000 })).toBe('1,000');
2937
});
3038

31-
test('DATE_ISO op', () => {
32-
expect(parseExpression('DATE_ISO(a)', { a: '2022-01-01' })).toBe('2022-01-01T00:00:00.000Z');
33-
});
39+
describe('Date ops', () => {
40+
test('DATE_ISO op', () => {
41+
expect(parseExpression('DATE_ISO(a)', { a: '2022-01-01' })).toBe('2022-01-01T00:00:00.000Z');
42+
expect(parseExpression('DATE_ISO($NOW)', {})).toBe('2023-01-01T00:00:00.000Z');
43+
});
44+
45+
test('DATE_UTC op', () => {
46+
expect(parseExpression('DATE_UTC(a)', { a: '2022-01-01' })).toBe('Sat, 01 Jan 2022 00:00:00 GMT');
47+
expect(parseExpression('DATE_UTC($NOW)', {})).toBe('Sun, 01 Jan 2023 00:00:00 GMT');
48+
});
49+
50+
test('YEAR op', () => {
51+
expect(parseExpression('YEAR($NOW)', {})).toBe(new Date().getFullYear());
52+
});
53+
54+
test('MONTH op', () => {
55+
expect(parseExpression('MONTH($NOW)', {})).toBe(new Date().getMonth());
56+
});
3457

35-
test('DATE_UTC op', () => {
36-
expect(parseExpression('DATE_UTC(a)', { a: '2022-01-01' })).toBe('Sat, 01 Jan 2022 00:00:00 GMT');
58+
test('GET_DATE op', () => {
59+
expect(parseExpression('GET_DATE($NOW)', {})).toBe(new Date().getDate());
60+
});
61+
62+
test('DAY op', () => {
63+
expect(parseExpression('DAY($NOW)', {})).toBe(new Date().getDay());
64+
});
65+
66+
test('HOURS op', () => {
67+
expect(parseExpression('HOURS($NOW)', {})).toBe(new Date().getHours());
68+
});
69+
70+
test('MINUTES op', () => {
71+
expect(parseExpression('MINUTES($NOW)', {})).toBe(new Date().getMinutes());
72+
});
73+
74+
test('SECONDS op', () => {
75+
expect(parseExpression('SECONDS($NOW)', {})).toBe(new Date().getSeconds());
76+
});
77+
78+
test('TIME op', () => {
79+
expect(parseExpression('TIME($NOW)', {})).toBe(new Date().getTime());
80+
});
3781
});
3882

3983
test('ABS op', () => {

src/operations.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
99
return value;
1010
}
1111

12+
// Dynamic variables
13+
if (exp === '$NOW') {
14+
return new Date();
15+
}
16+
1217
const opMatch = parseOp(exp);
1318
if (opMatch) {
1419
const { op, a, b } = opMatch;
@@ -43,6 +48,22 @@ export function parseExpression(exp: string, values: Record<string, any>): any {
4348
if (op === 'DATE_UTC') {
4449
return new Date(valueA).toUTCString();
4550
}
51+
if (['YEAR', 'MONTH', 'GET_DATE', 'DAY', 'HOURS', 'MINUTES', 'SECONDS', 'TIME'].includes(op)) {
52+
if (valueA instanceof Date) {
53+
const op2func = {
54+
YEAR: 'getFullYear',
55+
MONTH: 'getMonth',
56+
GET_DATE: 'getDate',
57+
DAY: 'getDay',
58+
HOURS: 'getHours',
59+
MINUTES: 'getMinutes',
60+
SECONDS: 'getSeconds',
61+
TIME: 'getTime',
62+
};
63+
return valueA[op2func[op]]();
64+
}
65+
return 0;
66+
}
4667
// arithmetic
4768
if (op === 'ABS') {
4869
return Math.abs(valueA);

0 commit comments

Comments
 (0)