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

Commit 13aa312

Browse files
committed
luxon fns
1 parent 66c508d commit 13aa312

File tree

6 files changed

+69
-20
lines changed

6 files changed

+69
-20
lines changed

src/automations/Footer.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ export default class Footer {
150150
* @returns
151151
*/
152152
public earliestDate(): string {
153-
const earliest = this.colValues
154-
.filter((value) => DateTime.isDateTime(value))
155-
.reduce((acc: DateTime, value: DateTime) => acc < value ? acc : value, DateTime.max);
156-
return DateTime.isDateTime(earliest) ?
153+
const earliest = DbAutomationService.coreFns.luxon.earliest(this.colValues);
154+
return earliest.isValid ?
157155
`Earliest: ${earliest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
158156
null;
159157
}
@@ -163,10 +161,8 @@ export default class Footer {
163161
* @returns
164162
*/
165163
public latestDate(): string {
166-
const latest = this.colValues
167-
.filter((value) => DateTime.isDateTime(value))
168-
.reduce((acc: DateTime, value: DateTime) => acc > value ? acc : value, DateTime.min);
169-
return DateTime.isDateTime(latest) ?
164+
const latest = DbAutomationService.coreFns.luxon.latest(this.colValues);
165+
return latest.isValid ?
170166
`Latest: ${latest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
171167
null;
172168
}
@@ -176,15 +172,8 @@ export default class Footer {
176172
* @returns
177173
*/
178174
public rangeDate(): string {
179-
const earliest = this.colValues
180-
.filter((value) => DateTime.isDateTime(value))
181-
.reduce((acc: DateTime, value: DateTime) => acc < value ? acc : value, DateTime.max);
182-
const latest = this.colValues
183-
.filter((value) => DateTime.isDateTime(value))
184-
.reduce((acc: DateTime, value: DateTime) => acc > value ? acc : value, DateTime.min);
185-
return DateTime.isDateTime(earliest) && DateTime.isDateTime(latest) ?
186-
`Range: ${earliest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)} - ${latest.toFormat(DEFAULT_SETTINGS.local_settings.datetime_format)}` :
187-
null;
175+
const range = DbAutomationService.coreFns.luxon.range(this.colValues);
176+
return `Range: ${range} days`
188177
}
189178

190179
}

src/automations/ModulesGenerator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { IGenerateObject } from "automations/core/IGenerateObject";
22
import { DatabaseFnType } from "cdm/ModulesFnModel";
33
import { DbModule } from "./core/DbModule";
4+
import { LuxonFn } from "./core/modules/LuxonFn";
45
import { NumbersFn } from "./core/modules/NumbersFn";
56
export class ModulesGenerator implements IGenerateObject {
67
private modules_array: Array<DbModule> = [];
78

89
constructor() {
910
this.modules_array.push(new NumbersFn());
11+
this.modules_array.push(new LuxonFn());
1012
}
1113

1214
async init(): Promise<void> {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { DbModule } from "automations/core/DbModule";
2+
import { Literal } from "obsidian-dataview";
3+
import { DateTime, DurationLikeObject } from "luxon";
4+
5+
export class LuxonFn extends DbModule {
6+
public name: string = "luxon";
7+
8+
async create_static_functions(): Promise<void> {
9+
this.static_functions.set("earliest", this.earliest.bind(this));
10+
this.static_functions.set("latest", this.latest.bind(this));
11+
this.static_functions.set("range", this.rangeDate.bind(this));
12+
}
13+
14+
async create_dynamic_functions(): Promise<void> { }
15+
16+
private parseRaw(rawValues: Literal[]): DateTime[] {
17+
return rawValues
18+
.filter((value) => DateTime.isDateTime(value))
19+
.map((value) => value as DateTime);
20+
}
21+
22+
/**
23+
* Obtains the earliest date of the column (only for dates)
24+
* @param rawValues
25+
* @returns
26+
*/
27+
private earliest(rawValues: Literal[]): DateTime {
28+
return DateTime.min(...this
29+
.parseRaw(rawValues))
30+
31+
}
32+
33+
/**
34+
* Obtains the latest date of the column (only for dates)
35+
* @returns
36+
*/
37+
private latest(rawValues: Literal[]): DateTime {
38+
return DateTime.max(...this
39+
.parseRaw(rawValues))
40+
}
41+
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 {
47+
const earliest = this.earliest(rawValues);
48+
const latest = this.latest(rawValues);
49+
return latest.diff(earliest).as(dl);
50+
}
51+
}

src/cdm/ModulesFnModel.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
import { Literal } from "obsidian-dataview"
2+
import { DateTime, DurationLikeObject } from "luxon";
23

3-
export type NumbersFnType = {
4+
type NumbersFnType = {
45
sum: (values: Literal[]) => number;
56
min: (values: Literal[]) => number;
67
max: (values: Literal[]) => number;
78
[key: string]: unknown
89
}
910

11+
type LuxonFnType = {
12+
earliest: (values: Literal[]) => DateTime;
13+
latest: (values: Literal[]) => DateTime;
14+
range: (values: Literal[], dl?: keyof DurationLikeObject) => number;
15+
[key: string]: unknown
16+
}
17+
1018
export type DatabaseFnType = {
1119
numbers: NumbersFnType;
20+
luxon: LuxonFnType;
1221
[key: string]: unknown
1322
}

src/parsers/handlers/marshall/MarshallColumnsHandler.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { YamlHandlerResponse } from 'cdm/MashallModel';
33
import { RowSelectOption } from 'cdm/ComponentsModel';
44
import { InputType, DEFAULT_COLUMN_CONFIG } from 'helpers/Constants';
55
import { AbstractYamlHandler } from 'parsers/handlers/marshall/AbstractYamlPropertyHandler';
6-
import { RowType } from 'cdm/RowTypeModel';
76
import { Literal } from 'obsidian-dataview';
87

98
export class MarshallColumnsHandler extends AbstractYamlHandler {

src/services/DataviewService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ class DataviewProxy {
3030
return isPluginEnabled(app);
3131
}
3232

33-
3433
isStasks(value: Literal): value is STask {
3534
return (value as STask).task;
3635
}

0 commit comments

Comments
 (0)