|
1 | 1 | import { DbModule } from "lib/core/DbModule"; |
2 | 2 | import { Literal } from "obsidian-dataview"; |
3 | 3 | import { DateTime, DurationLikeObject } from "luxon"; |
| 4 | +import { DEFAULT_SETTINGS } from "helpers/Constants"; |
| 5 | +import { LuxonInterface } from "cdm/ModulesFnModel"; |
4 | 6 |
|
5 | | -export class LuxonFn extends DbModule { |
| 7 | + |
| 8 | +export class LuxonFn extends DbModule implements LuxonInterface { |
6 | 9 | public name: string = "luxon"; |
7 | 10 |
|
8 | 11 | async create_static_functions(): Promise<void> { |
9 | 12 | this.static_functions.set("earliest", this.earliest.bind(this)); |
10 | 13 | 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)); |
12 | 17 | } |
13 | 18 |
|
14 | 19 | async create_dynamic_functions(): Promise<void> { } |
15 | 20 |
|
16 | | - private parseRaw(rawValues: Literal[]): DateTime[] { |
| 21 | + parseRaw(rawValues: Literal[]): DateTime[] { |
17 | 22 | return rawValues |
18 | 23 | .filter((value) => DateTime.isDateTime(value)) |
19 | 24 | .map((value) => value as DateTime); |
20 | 25 | } |
21 | 26 |
|
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 { |
28 | 28 | return DateTime.min(...this |
29 | 29 | .parseRaw(rawValues)) |
30 | 30 |
|
31 | 31 | } |
32 | 32 |
|
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 { |
38 | 34 | return DateTime.max(...this |
39 | 35 | .parseRaw(rawValues)) |
40 | 36 | } |
41 | 37 |
|
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 { |
47 | 39 | const earliest = this.earliest(rawValues); |
48 | 40 | const latest = this.latest(rawValues); |
49 | 41 | return latest.diff(earliest).as(dl); |
50 | 42 | } |
| 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 | + } |
51 | 59 | } |
0 commit comments