diff --git a/README.md b/README.md index 54d910c..9227e4e 100644 --- a/README.md +++ b/README.md @@ -78,15 +78,12 @@ console.log(result); // Output: 25 ## Examples ```typescript -import { DateTime } from 'luxon'; +import moment from 'moment' import { createParser, FunctionMap } from '../' describe('example', () => { it('basic operator', () => { const parser = createParser({ - luxon: { - toISO: {} - } }) expect(parser.evaluate('(2 + 3) * 4 - 4')).toBe(16) expect(parser.evaluate('-4 + 5')).toBe(1) @@ -202,15 +199,19 @@ describe('example', () => { it('date', () => { const parser = createParser(); - // expect( parser.evaluate(`date()`)).toBe(DateTime.now().toISO()) - // expect( parser.evaluate(`date("2020-01-01")`)).toBe('2020-01-01T00:00:00.000+07:00') + // expect(parser.evaluate(`date()`)).toBe(moment().toISOString()) + expect(parser.evaluate(`date("2020-01-01")`)).toBe('2019-12-31T17:00:00.000Z') expect(parser.evaluate(`date_day(date("2020-01-01"))`)).toBe(1) expect(parser.evaluate(`date_month(date("2020-01-01"))`)).toBe(1) expect(parser.evaluate(`date_year(date("2020-01-01"))`)).toBe(2020) - expect(parser.evaluate(`date_format("2020-01-01", "dd-MM-yyyy")`)).toBe("01-01-2020") - expect(parser.evaluate(`date_in_format("2020-01-01", "yyyy-MM-dd")`)).toBe('2020-01-01T00:00:00.000+07:00') - expect(parser.evaluate(`date_in_millis(${DateTime.fromISO("2020-01-01").toMillis()})`)).toBe('2020-01-01T00:00:00.000+07:00') + expect(parser.evaluate(`date_format("DD-MM-YYYY", "2020-01-01")`)).toBe("01-01-2020") + expect(parser.evaluate(`date_in_format("YYYY-MM-DD", "2020-01-01")`)).toBe('2019-12-31T17:00:00.000Z') + expect(parser.evaluate(`date_in_millis(${moment("2020/01/01", 'YYYY/MM/DD').valueOf()})`)).toBe('2019-12-31T17:00:00.000Z') expect(parser.evaluate(`date_millis("2020-01-01")`)).toBe(1577811600000) + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_plus(1, "day", "2020-01-05"))`)).toBe("06-01-2020") + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_plus(-1, "day", "2020-01-05"))`)).toBe("04-01-2020") + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_minus(1, "day", "2020-01-05"))`)).toBe("04-01-2020") + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_minus(-1, "day", "2020-01-05"))`)).toBe("06-01-2020") }) }); ``` \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 7f70b42..516db09 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "benny": "^3.7.1", "expressionparser": "^1.1.5", - "luxon": "^3.3.0" + "moment": "^2.29.4" }, "devDependencies": { "@babel/cli": "^7.21.5", @@ -20,7 +20,6 @@ "@babel/runtime": "^7.22.3", "@types/benchmark": "^2.1.2", "@types/jest": "^29.5.2", - "@types/luxon": "^3.3.0", "jest": "^29.5.0", "semantic-release": "^21.0.3", "ts-jest": "^29.1.0", @@ -3125,12 +3124,6 @@ "pretty-format": "^29.0.0" } }, - "node_modules/@types/luxon": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/@types/luxon/-/luxon-3.3.0.tgz", - "integrity": "sha512-uKRI5QORDnrGFYgcdAVnHvEIvEZ8noTpP/Bg+HeUzZghwinDlIS87DEenV5r1YoOF9G4x600YsUXLWZ19rmTmg==", - "dev": true - }, "node_modules/@types/minimist": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.2.tgz", @@ -6220,14 +6213,6 @@ "yallist": "^3.0.2" } }, - "node_modules/luxon": { - "version": "3.3.0", - "resolved": "https://registry.npmjs.org/luxon/-/luxon-3.3.0.tgz", - "integrity": "sha512-An0UCfG/rSiqtAIiBPO0Y9/zAnHUZxAMiCpTd5h2smgsj7GGmcenvrvww2cqNA8/4A5ZrD1gJpHN2mIHZQF+Mg==", - "engines": { - "node": ">=12" - } - }, "node_modules/make-dir": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz", @@ -6577,6 +6562,14 @@ "node": ">=0.10.0" } }, + "node_modules/moment": { + "version": "2.29.4", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", + "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", + "engines": { + "node": "*" + } + }, "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", diff --git a/package.json b/package.json index 0f3d92c..3a271f4 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,6 @@ "@babel/runtime": "^7.22.3", "@types/benchmark": "^2.1.2", "@types/jest": "^29.5.2", - "@types/luxon": "^3.3.0", "jest": "^29.5.0", "semantic-release": "^21.0.3", "ts-jest": "^29.1.0", @@ -77,6 +76,6 @@ "dependencies": { "benny": "^3.7.1", "expressionparser": "^1.1.5", - "luxon": "^3.3.0" + "moment": "^2.29.4" } } diff --git a/scripts/copy-files.js b/scripts/copy-files.js index 3b12682..8136fda 100644 --- a/scripts/copy-files.js +++ b/scripts/copy-files.js @@ -65,7 +65,7 @@ async function createModulePackages({ from, to }) { // add package here const packageIncludes = [ - 'luxon' + 'moment' ]; async function includeFileInBuild(file) { diff --git a/src/ExpressionParser.ts b/src/ExpressionParser.ts index adacec0..12df570 100644 --- a/src/ExpressionParser.ts +++ b/src/ExpressionParser.ts @@ -1,5 +1,3 @@ -import { ToISOTimeOptions } from "luxon"; - export interface ParserState { tokens: string[]; currentTokenIndex: number; @@ -15,9 +13,6 @@ export type OperatorMap = { [key: string]: (a: any, b: any) => any }; export type ExpressionParserConstructor = { variables?: VariableMap, regex?: RegExp, - luxon?: { - toISO?: ToISOTimeOptions - } } @@ -296,7 +291,6 @@ export class ExpressionParser { variables?: VariableMap, ): any { const tempVariables = { ...this.variables, ...(variables || {}) }; - const state: ParserState = { tokens: this.tokenize(expression), currentTokenIndex: 0, diff --git a/src/createParser.ts b/src/createParser.ts index a4985e6..588126c 100644 --- a/src/createParser.ts +++ b/src/createParser.ts @@ -1,8 +1,9 @@ import ExpressionParser, { ExpressionParserConstructor, FunctionMap, OperatorMap } from "./ExpressionParser" -import { DateTime } from 'luxon' +import moment from 'moment' + +type Unit = "year" | "years" | "y" | "month" | "months" | "M" | "week" | "weeks" | "w" | "day" | "days" | "d" | "hour" | "hours" | "h" | "minute" | "minutes" | "m" | "second" | "seconds" | "s" | "millisecond" | "milliseconds" | "ms" export function createParser(props: ExpressionParserConstructor = {}) { - const luxonConfigToISO = props.luxon?.toISO const parser = new ExpressionParser({ ...props, variables: { @@ -53,17 +54,18 @@ export function createParser(props: ExpressionParserConstructor = {}) { lower: (_, value: string) => value.toLowerCase(), regex: (_, value: any, regex: string, flags?: string) => new RegExp(regex, flags).test(value), // DATE ================================================================================== - date: (_, value: string,) => DateTime.fromISO(value).toISO(luxonConfigToISO) || DateTime.now().toISO(luxonConfigToISO), - date_day: (_, value: string,) => DateTime.fromISO(value).day || DateTime.now().day, - date_month: (_, value: string,) => DateTime.fromISO(value).month || DateTime.now().month, - date_year: (_, value: string,) => DateTime.fromISO(value).year || DateTime.now().year, - date_hour: (_, value: string,) => DateTime.fromISO(value).hour || DateTime.now().hour, - date_minute: (_, value: string,) => DateTime.fromISO(value).minute, - date_format: (_, value: string, format: string) => DateTime.fromISO(value).toFormat(format).toString(), - date_in_format: (_, value: string, format: string) => DateTime.fromFormat(value, format).toISO(luxonConfigToISO), - date_in_millis: (_, value: number) => DateTime.fromMillis(value).toISO(), - date_millis: (_, value: string) => DateTime.fromISO(value).toMillis(), - date_plus: (_, value: string) => DateTime.fromISO(value), + date: (_, date: string) => date ? moment(date).toISOString() : moment().toISOString(), + date_day: (_, date: string) => date ? moment(date).date() : moment().date(), + date_month: (_, date: string) => date ? moment(date).month() + 1 : moment().month() + 1, + date_year: (_, date: string) => date ? moment(date).year() : moment().year(), + date_hour: (_, date: string) => date ? moment(date).hour() : moment().hour(), + date_minute: (_, date: string) => date ? moment(date).minute() : moment().minute(), + date_format: (_, format: string, date: string) => date ? moment(date).format(format).toString() : moment().format(format).toString(), + date_in_format: (_, format: string, date: string) => date ? moment(date, format).toISOString() : moment().toISOString(), + date_in_millis: (_, date: number) => moment(date).toISOString(), + date_millis: (_, date: string) => date ? moment(date).valueOf() : moment().valueOf(), + date_plus: (_, amount: number, unit: Unit, date: string) => date ? moment(date).add(amount, unit) : moment().add(amount, unit), + date_minus: (_, amount: number, unit: Unit, date: string) => date ? moment(date).subtract(amount, unit) : moment().subtract(amount, unit), // CONDITION ================================================================================== if: (_, condition: boolean, truthy: any, falsy) => { diff --git a/src/test/index.test.ts b/src/test/index.test.ts index 5ccb255..2ca766e 100644 --- a/src/test/index.test.ts +++ b/src/test/index.test.ts @@ -1,12 +1,9 @@ -import { DateTime } from 'luxon'; +import moment from 'moment' import { createParser, FunctionMap } from '../' describe('example', () => { it('basic operator', () => { const parser = createParser({ - luxon: { - toISO: {} - } }) expect(parser.evaluate('(2 + 3) * 4 - 4')).toBe(16) expect(parser.evaluate('-4 + 5')).toBe(1) @@ -122,14 +119,18 @@ describe('example', () => { it('date', () => { const parser = createParser(); - // expect( parser.evaluate(`date()`)).toBe(DateTime.now().toISO()) - // expect( parser.evaluate(`date("2020-01-01")`)).toBe('2020-01-01T00:00:00.000+07:00') - // expect(parser.evaluate(`date_day(date("2020-01-01"))`)).toBe(1) - // expect(parser.evaluate(`date_month(date("2020-01-01"))`)).toBe(1) - // expect(parser.evaluate(`date_year(date("2020-01-01"))`)).toBe(2020) - // expect(parser.evaluate(`date_format("2020-01-01", "dd-MM-yyyy")`)).toBe("01-01-2020") - // expect(parser.evaluate(`date_in_format("2020-01-01", "yyyy-MM-dd")`)).toBe('2020-01-01T00:00:00.000+07:00') - // expect(parser.evaluate(`date_in_millis(${DateTime.fromISO("2020-01-01").toMillis()})`)).toBe('2020-01-01T00:00:00.000+07:00') - // expect(parser.evaluate(`date_millis("2020-01-01")`)).toBe(1577811600000) + // expect(parser.evaluate(`date()`)).toBe(moment().toISOString()) + expect(parser.evaluate(`date("2020-01-01")`)).toBe('2019-12-31T17:00:00.000Z') + expect(parser.evaluate(`date_day(date("2020-01-01"))`)).toBe(1) + expect(parser.evaluate(`date_month(date("2020-01-01"))`)).toBe(1) + expect(parser.evaluate(`date_year(date("2020-01-01"))`)).toBe(2020) + expect(parser.evaluate(`date_format("DD-MM-YYYY", "2020-01-01")`)).toBe("01-01-2020") + expect(parser.evaluate(`date_in_format("YYYY-MM-DD", "2020-01-01")`)).toBe('2019-12-31T17:00:00.000Z') + expect(parser.evaluate(`date_in_millis(${moment("2020/01/01", 'YYYY/MM/DD').valueOf()})`)).toBe('2019-12-31T17:00:00.000Z') + expect(parser.evaluate(`date_millis("2020-01-01")`)).toBe(1577811600000) + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_plus(1, "day", "2020-01-05"))`)).toBe("06-01-2020") + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_plus(-1, "day", "2020-01-05"))`)).toBe("04-01-2020") + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_minus(1, "day", "2020-01-05"))`)).toBe("04-01-2020") + expect(parser.evaluate(`date_format("DD-MM-YYYY", date_minus(-1, "day", "2020-01-05"))`)).toBe("06-01-2020") }) -}); \ No newline at end of file +});