Skip to content
Open
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
6,849 changes: 6,849 additions & 0 deletions rita-core/package-lock.json

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions rita-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,23 @@
"dependencies": {
"ajv": "^8.17.1",
"ajv-formats": "^2.1.1",
"luxon": "^3.6.1",
"dayjs": "^1.11.20",
"tslib": "^2.8.1"
},
"devDependencies": {
"@adobe/jsonschema2md": "^8.0.3",
"@types/deep-equal": "^1.0.4",
"@types/luxon": "^3.6.2",
"@types/madge": "^5.0.3",
"jest-extended": "^6.0.0",
"madge": "^8.0.0",
"mkdirp": "^3.0.1",
"rimraf": "^6.0.1",
"typedoc": "0.28.7",
"typescript": "5.8.3",
"vite": "~7.2.4",
"vitest": "^2.1.9",
"unplugin-dts": "~1.0.0-beta.6",
"vite-plugin-externalize-deps": "~0.10.0"
"vite": "~7.2.4",
"vite-plugin-externalize-deps": "~0.10.0",
"vitest": "^2.1.9"
},
"engines": {
"node": ">=10"
Expand Down
7 changes: 5 additions & 2 deletions rita-core/src/Assertions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Duration } from 'luxon';
import dayjs, { type Duration } from 'dayjs';
import duration from 'dayjs/plugin/duration';
import { Formula } from './logicElements';

dayjs.extend(duration);
import {
HasNoLengthError,
NotBooleanError,
Expand Down Expand Up @@ -49,7 +52,7 @@ export function assertDuration(
value: unknown,
context: Formula
): asserts value is Duration {
if (!Duration.isDuration(value)) {
if (!dayjs.isDuration(value)) {
throw new NotDurationError(context, value);
}
}
Expand Down
4 changes: 2 additions & 2 deletions rita-core/src/logicElements/Atom.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Formula, FormulaResults } from './Formula';
import { DateTime } from 'luxon';
import dayjs from 'dayjs';
import { UndefinedPathError, UsageError } from '../Errors';

/**
Expand All @@ -8,7 +8,7 @@ import { UndefinedPathError, UsageError } from '../Errors';
* @param context the context of the formula
*/
export function parseDate(val: string, context?: Formula): Date {
const testDate = DateTime.fromISO(val).toJSDate();
const testDate = dayjs(val).toDate();
if (!isNaN(testDate.getTime())) {
return testDate;
} else {
Expand Down
4 changes: 2 additions & 2 deletions rita-core/src/logicElements/Calculation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Atom } from './Atom';
import { Formula } from './Formula';
import { DateTime } from 'luxon';
import dayjs from 'dayjs';
import { UsageError } from '../Errors';
import { DateCalculation } from './DateCalculation';

Expand All @@ -27,7 +27,7 @@ export function mapArgumentsToJSONReady(
if (item instanceof Formula) {
return item.toJsonReady();
} else if (item instanceof Date) {
return DateTime.fromJSDate(item).toISO();
return dayjs(item).toISOString();
} else {
return item;
}
Expand Down
35 changes: 16 additions & 19 deletions rita-core/src/logicElements/DateCalculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { Atom } from './Atom';
import { Calculation, mapArgumentsToJSONReady } from './Calculation';
import { RulesetError } from '../Errors';
import { assertNumberOrDate } from '../Assertions';
import { DateTime, Duration } from 'luxon';
import dayjs, { type Duration } from 'dayjs';
import duration from 'dayjs/plugin/duration';

dayjs.extend(duration);

export enum dateOperations {
add = 'add',
Expand Down Expand Up @@ -91,36 +94,33 @@ export class DateCalculation extends Formula {
): Date | Duration {
//If arguments are two dates, calculate the result with the milliseconds of the dates
if (d1 instanceof Date && d2 instanceof Date) {
return Duration.fromMillis(func(d1.getTime(), d2.getTime()), {
conversionAccuracy: 'longterm',
});
return dayjs.duration(func(d1.getTime(), d2.getTime()));
}

//If neither arguments are dates, combine the durations by applying the function to their millisecond values
if (!(d1 instanceof Date) && !(d2 instanceof Date)) {
return Duration.fromMillis(func(d1.toMillis(), d2.toMillis()), {
conversionAccuracy: 'longterm',
});
return dayjs.duration(
func(d1.asMilliseconds(), d2.asMilliseconds())
);
}

//If neither of the above returned, we now know one must be a date and one a duration, so let's find out which is which
let date: Date;
let duration: Duration;
let dur: Duration;
if (d1 instanceof Date && !(d2 instanceof Date)) {
date = d1;
duration = <Duration>d2;
dur = d2;
} else {
date = <Date>d2;
duration = <Duration>d1;
dur = <Duration>d1;
}

//Add or subtract the duration to/from the date
const lDate: DateTime = DateTime.fromJSDate(date);
switch (operation) {
case dateOperations.add:
return lDate.plus(duration).toJSDate();
return dayjs(date).add(dur).toDate();
case dateOperations.subtract:
return lDate.minus(duration).toJSDate();
return dayjs(date).subtract(dur).toDate();
default:
throw new RulesetError(
'Invalid Operation for Dates',
Expand Down Expand Up @@ -159,12 +159,9 @@ export class DateCalculation extends Formula {
const tmp = results.map((item) => {
assertNumberOrDate(item, this);
if (typeof item === 'number') {
return Duration.fromObject(
{
[this.dateCalculationUnit]: item,
},
{ conversionAccuracy: 'longterm' }
);
return dayjs.duration({
[this.dateCalculationUnit]: item,
});
} else {
return item;
}
Expand Down
4 changes: 2 additions & 2 deletions rita-core/src/test/assets/macros.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DateTime } from 'luxon';
import dayjs from 'dayjs';

export default {
$schema: '../../src/schema/schema.json',
Expand All @@ -17,7 +17,7 @@ export default {
type: 'now',
},
},
DateTime.fromISO('2022-04-22T13:14:12.122+02:00').toISO(),
dayjs('2022-04-22T13:14:12.122+02:00').toISOString(),
],
},
},
Expand Down
4 changes: 2 additions & 2 deletions rita-core/src/test/implementationTests/calculation.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { evaluateAll, Parser } from '../../index';
import { exampleData } from '../assets/exampleData';
import { DateTime } from 'luxon';
import dayjs from 'dayjs';
import modulo from '../assets/modulo.json';

const p = new Parser();
Expand Down Expand Up @@ -85,7 +85,7 @@ describe('Numbers', () => {
});

function formatDate(d: Date): string {
return DateTime.fromJSDate(d).toFormat('yyyy-MM-dd');
return dayjs(d).format('YYYY-MM-DD');
}

describe('Dates', () => {
Expand Down
4 changes: 2 additions & 2 deletions rita-core/src/test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import rule_qfa from './assets/quantifiers_fa.json';
import rule_qex from './assets/quantifiers_ex.json';
import rule_defaultValue from './assets/defaultVal.json';
import macros from './assets/macros';
import { DateTime } from 'luxon';
import dayjs from 'dayjs';

//Prevent timezone error when converting from json and back
const exampleMath = JSON.parse(JSON.stringify(exampleMathDefault));
exampleMath.rules[0].rule.arguments[0].arguments[0].arguments[0] =
DateTime.fromJSDate(new Date()).toISO();
dayjs(new Date()).toISOString();

const schemas = [
{
Expand Down
Loading