Skip to content
This repository was archived by the owner on Jul 28, 2025. It is now read-only.

Commit 715129a

Browse files
committed
apify luxonhelper
1 parent 5291d09 commit 715129a

File tree

11 files changed

+82
-142
lines changed

11 files changed

+82
-142
lines changed

src/cdm/ModulesFnModel.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,74 @@
11
import { Literal } from "obsidian-dataview"
22
import { DateTime, DurationLikeObject } from "luxon";
33

4-
type NumbersFnType = {
4+
export interface NumbersInterface {
55
/**
66
* Summatory of the list of values
77
* @param values
88
* @returns
99
*/
1010
sum: (values: Literal[]) => number;
11+
1112
/**
1213
* Obtains the minimum value of the list of values
1314
* @param values
1415
* @returns
1516
*/
1617
min: (values: Literal[]) => number;
18+
1719
/**
1820
* Obtains the maximum value of the list of values
1921
* @param values
2022
* @returns
2123
*/
2224
max: (values: Literal[]) => number;
23-
[key: string]: unknown
2425
}
2526

26-
type LuxonFnType = {
27+
export interface LuxonInterface {
2728
/**
2829
* Returns the earliest date in the list of values
2930
* @param values
3031
* @returns
3132
*/
3233
earliest: (values: Literal[]) => DateTime;
34+
3335
/**
3436
* Returns the latest date in the list of values
3537
* @param values
3638
* @returns
3739
*/
3840
latest: (values: Literal[]) => DateTime;
41+
3942
/**
4043
* Returns the duration between the earliest and latest dates in the list of values
4144
* @param values
4245
* @param dl - Duration like object key. "days" by default. See https://www.jsdocs.io/package/@types/luxon#DurationLikeObject
4346
* @returns
4447
*/
4548
range: (values: Literal[], dl?: keyof DurationLikeObject) => number;
46-
[key: string]: unknown
49+
50+
/**
51+
* Parse datetime to string under a given format
52+
* @param datetime
53+
* @param format - Default: yyyy-MM-dd HH:mm:ss
54+
* @returns
55+
*/
56+
dateToString: (datetime: DateTime, format?: string) => string;
57+
58+
/**
59+
* Parse string to datetime under a given format
60+
* @param datetime
61+
* @param format - Default: yyyy-MM-dd HH:mm:ss
62+
* @returns
63+
*/
64+
stringToDate(datetime: string, format?: string): DateTime;
4765
}
4866

4967
/**
5068
* Exposed functionalities under the `db` variable of Formulas
5169
*/
5270
export type DatabaseFnType = {
53-
numbers: NumbersFnType;
54-
luxon: LuxonFnType;
71+
numbers: NumbersInterface;
72+
luxon: LuxonInterface;
5573
[key: string]: unknown
5674
}

src/components/cellTypes/CalendarCell.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { TableColumn } from "cdm/FolderModel";
1313
import { ParseService } from "services/ParseService";
1414
import { DEFAULT_SETTINGS, InputType } from "helpers/Constants";
1515
import { Platform } from "obsidian";
16-
import { parseLuxonDateToString } from "helpers/LuxonHelper";
1716
import { OBSIDIAN_LOCALE } from "lang/helpers";
17+
import { Db } from "services/CoreService";
1818

1919
const CalendarCell = (calendarProps: CellComponentProps) => {
2020
const { defaultCell } = calendarProps;
@@ -111,7 +111,7 @@ const CalendarCell = (calendarProps: CellComponentProps) => {
111111
}}
112112
tabIndex={0}
113113
>
114-
{parseLuxonDateToString(
114+
{Db.coreFns.luxon.dateToString(
115115
calendarCell,
116116
configInfo.getLocalSettings().date_format
117117
)}

src/components/cellTypes/CalendarTimeCell.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { ParseService } from "services/ParseService";
1313
import { DEFAULT_SETTINGS, InputType } from "helpers/Constants";
1414
import { c } from "helpers/StylesHelper";
1515
import { Platform } from "obsidian";
16-
import { parseLuxonDatetimeToString } from "helpers/LuxonHelper";
1716
import { OBSIDIAN_LOCALE } from "lang/helpers";
17+
import { Db } from "services/CoreService";
1818

1919
const CalendarTimeCell = (calendarTimeProps: CellComponentProps) => {
2020
const { defaultCell } = calendarTimeProps;
@@ -112,7 +112,7 @@ const CalendarTimeCell = (calendarTimeProps: CellComponentProps) => {
112112
}}
113113
tabIndex={0}
114114
>
115-
{parseLuxonDatetimeToString(
115+
{Db.coreFns.luxon.dateToString(
116116
calendarCell,
117117
configInfo.getLocalSettings().datetime_format
118118
)}

src/helpers/LuxonHelper.ts

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/lib/core/DbModule.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export abstract class DbModule implements IGenerateObject {
2525
await this.create_dynamic_functions();
2626

2727
return {
28-
...this.static_object,
2928
...this.static_object,
3029
...Object.fromEntries(this.dynamic_functions),
3130
};

src/lib/core/modules/LuxonFn.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
11
import { DbModule } from "lib/core/DbModule";
22
import { Literal } from "obsidian-dataview";
33
import { DateTime, DurationLikeObject } from "luxon";
4+
import { DEFAULT_SETTINGS } from "helpers/Constants";
5+
import { LuxonInterface } from "cdm/ModulesFnModel";
46

5-
export class LuxonFn extends DbModule {
7+
8+
export class LuxonFn extends DbModule implements LuxonInterface {
69
public name: string = "luxon";
710

811
async create_static_functions(): Promise<void> {
912
this.static_functions.set("earliest", this.earliest.bind(this));
1013
this.static_functions.set("latest", this.latest.bind(this));
11-
this.static_functions.set("range", this.rangeDate.bind(this));
14+
this.static_functions.set("range", this.range.bind(this));
15+
this.static_functions.set("dateToString", this.dateToString.bind(this));
16+
this.static_functions.set("stringToDate", this.stringToDate.bind(this));
1217
}
1318

1419
async create_dynamic_functions(): Promise<void> { }
1520

16-
private parseRaw(rawValues: Literal[]): DateTime[] {
21+
parseRaw(rawValues: Literal[]): DateTime[] {
1722
return rawValues
1823
.filter((value) => DateTime.isDateTime(value))
1924
.map((value) => value as DateTime);
2025
}
2126

22-
/**
23-
* Obtains the earliest date of the column (only for dates)
24-
* @param rawValues
25-
* @returns
26-
*/
27-
private earliest(rawValues: Literal[]): DateTime {
27+
earliest(rawValues: Literal[]): DateTime {
2828
return DateTime.min(...this
2929
.parseRaw(rawValues))
3030

3131
}
3232

33-
/**
34-
* Obtains the latest date of the column (only for dates)
35-
* @returns
36-
*/
37-
private latest(rawValues: Literal[]): DateTime {
33+
latest(rawValues: Literal[]): DateTime {
3834
return DateTime.max(...this
3935
.parseRaw(rawValues))
4036
}
4137

42-
/**
43-
* Obtains a range of dates of the column (only for dates) in a given duration
44-
* @returns
45-
*/
46-
private rangeDate(rawValues: Literal[], dl: keyof DurationLikeObject = "days"): number {
38+
range(rawValues: Literal[], dl: keyof DurationLikeObject = "days"): number {
4739
const earliest = this.earliest(rawValues);
4840
const latest = this.latest(rawValues);
4941
return latest.diff(earliest).as(dl);
5042
}
43+
44+
dateToString(datetime: DateTime, format = DEFAULT_SETTINGS.local_settings.datetime_format) {
45+
const isDate = DateTime.isDateTime(datetime);
46+
return isDate
47+
? datetime.toFormat(format) : null;
48+
}
49+
50+
stringToDate(datetime: string, format = DEFAULT_SETTINGS.local_settings.datetime_format) {
51+
let result: DateTime = null;
52+
result = DateTime.fromFormat(datetime, format);
53+
if (!result.isValid) {
54+
result = null;
55+
}
56+
57+
return result;
58+
}
5159
}

src/lib/core/modules/NumbersFn.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import { NumbersInterface } from "cdm/ModulesFnModel";
12
import { DbModule } from "lib/core/DbModule";
23
import { Literal } from "obsidian-dataview";
34

45

5-
export class NumbersFn extends DbModule {
6+
export class NumbersFn extends DbModule implements NumbersInterface {
67
public name: string = "numbers";
78

89
async create_static_functions(): Promise<void> {
@@ -13,46 +14,35 @@ export class NumbersFn extends DbModule {
1314

1415
async create_dynamic_functions(): Promise<void> { }
1516

16-
private parseRaw(rawValues: Literal[]): number[] {
17-
return rawValues
18-
.filter((value) => value &&
19-
typeof value === "string" ||
20-
typeof value === "number")
21-
.map((value) => {
22-
return parseFloat(value.toString());
23-
}).filter((value) => {
24-
return !isNaN(value);
25-
});
26-
}
27-
28-
/**
29-
* Obtains the sum of all values
30-
* @returns
31-
*/
32-
private sum(rawValues: Literal[]): number {
17+
sum(rawValues: Literal[]): number {
3318
// Check if key is not truthy, return empty string
3419
return this
3520
.parseRaw(rawValues)
3621
.reduce((a, b) => a + b, 0);
3722
}
3823

39-
/**
40-
* Obtains the minimum value of the column (only for numbers)
41-
* @returns
42-
*/
43-
private min(rawValues: Literal[]): number {
24+
min(rawValues: Literal[]): number {
4425
return this
4526
.parseRaw(rawValues)
4627
.reduce((acc: number, value: number) => Math.min(acc, value), Number.MAX_SAFE_INTEGER);
4728
}
4829

49-
/**
50-
* Obtains the maximum value of the column (only for numbers)
51-
* @returns
52-
*/
53-
private max(rawValues: Literal[]): number {
30+
max(rawValues: Literal[]): number {
5431
return this
5532
.parseRaw(rawValues)
5633
.reduce((acc: number, value: number) => Math.max(acc, value), Number.MIN_SAFE_INTEGER);
5734
}
35+
36+
37+
private parseRaw(rawValues: Literal[]): number[] {
38+
return rawValues
39+
.filter((value) => value &&
40+
typeof value === "string" ||
41+
typeof value === "number")
42+
.map((value) => {
43+
return parseFloat(value.toString());
44+
}).filter((value) => {
45+
return !isNaN(value);
46+
});
47+
}
5848
}

src/services/parseServiceHelpers/CalendarParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TypeParser } from "cdm/ServicesModel";
2-
import { parseStringToLuxonDatetime } from "helpers/LuxonHelper";
32
import { WrappedLiteral } from "obsidian-dataview";
43
import { DateTime } from "luxon";
4+
import { Db } from "services/CoreService";
55

66
class CalendarParser extends TypeParser<DateTime> {
77
private dateFormat: string;
@@ -13,7 +13,7 @@ class CalendarParser extends TypeParser<DateTime> {
1313

1414
parse(wrapped: WrappedLiteral) {
1515
if (wrapped.type === 'string') {
16-
return parseStringToLuxonDatetime(wrapped.value, this.dateFormat);
16+
return Db.coreFns.luxon.stringToDate(wrapped.value, this.dateFormat);
1717
}
1818

1919
if (DateTime.isDateTime(wrapped.value)) {

src/services/parseServiceHelpers/SelectParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { TypeParser } from "cdm/ServicesModel";
22
import { satinizedColumnOption } from "helpers/FileManagement";
33
import { Literal, WrappedLiteral } from "obsidian-dataview";
44
import { DateTime } from "luxon";
5-
import { parseLuxonDatetimeToString } from "helpers/LuxonHelper";
65
import { DataviewService } from "services/DataviewService";
6+
import { Db } from "services/CoreService";
77

88
class SelectParser extends TypeParser<string> {
99
/**
@@ -16,7 +16,7 @@ class SelectParser extends TypeParser<string> {
1616
switch (wrapped.type) {
1717
case 'object':
1818
if (DateTime.isDateTime(wrapped.value)) {
19-
parsedValue = parseLuxonDatetimeToString(wrapped.value, this.config.datetime_format);
19+
parsedValue = Db.coreFns.luxon.dateToString(wrapped.value, this.config.datetime_format);
2020
} else if (DataviewService.getDataviewAPI().isDataArray(wrapped.value)) {
2121
parsedValue = this.parseArrayToText(
2222
(wrapped.value.values as Literal[])

0 commit comments

Comments
 (0)