Skip to content

Commit

Permalink
Working date parsing; fix version issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brenan committed Feb 9, 2021
1 parent ad515a5 commit aa2516b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "dataview",
"name": "Dataview",
"version": "0.1.2",
"version": "0.1.4",
"minAppVersion": "0.10.7",
"description": "Complex data views for the data-obsessed.",
"author": "Michael Brenan <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-dataview",
"version": "0.1.3",
"version": "0.1.4",
"description": "Advanced data views for Obsidian.md.",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class DataviewPlugin extends Plugin {
this.index = null;
this.tasks = null;

console.log("Dataview Plugin - Version 0.1.3 Loaded");
console.log("Dataview Plugin - Version 0.1.4 Loaded");

if (!this.workspace.layoutReady) {
this.workspace.on("layout-ready", async () => this.prepareIndexes());
Expand Down
34 changes: 28 additions & 6 deletions src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ interface QueryLanguageTypes {
bool: boolean;
tag: string;
identifier: string;
rootDate: DateTime;
date: DateTime;
binaryPlusMinus: BinaryOp;
binaryCompareOp: BinaryOp;
Expand Down Expand Up @@ -240,6 +241,22 @@ export function createBinaryParser<T, U>(child: Parsimmon.Parser<T>, sep: Parsim
});
}

export function chainOpt<T>(base: Parsimmon.Parser<T>, ...funcs: ((r: T) => Parsimmon.Parser<T>)[]): Parsimmon.Parser<T> {
return Parsimmon((input, i) => {
let result = (base as any)._(input, i);
if (!result.status) return result;

for (let func of funcs) {
let next = (func(result.value as T) as any)._(input, result.index);
if (!next.status) return result;

result = next;
}

return result;
});
}

/** A parsimmon-powered parser-combinator implementation of the query language. */
export const QUERY_LANGUAGE = Parsimmon.createLanguage<QueryLanguageTypes>({
// Simple atom parsing, like words, identifiers, numbers.
Expand All @@ -264,12 +281,17 @@ export const QUERY_LANGUAGE = Parsimmon.createLanguage<QueryLanguageTypes>({
}),
// TODO: Add time-zone support.
// TODO: Will probably want a custom combinator for optional parsing.
date: q => Parsimmon.seqMap(q.number, Parsimmon.string("-"), q.number, (year, _, month) => DateTime.fromObject({ year, month }))
.chain(ym => Parsimmon.seqMap(Parsimmon.string("-"), q.number, (_, day) => ym.set({ day })))
.chain(ymd => Parsimmon.seqMap(Parsimmon.string("T"), q.number, (_, hour) => ymd.set({ hour })))
.chain(ymdh => Parsimmon.seqMap(Parsimmon.string(":"), q.number, (_, minute) => ymdh.set({ minute })))
.chain(ymdhm => Parsimmon.seqMap(Parsimmon.string(":"), q.number, (_, second) => ymdhm.set({ second })))
.chain(ymdhms => Parsimmon.seqMap(Parsimmon.string("."), q.number, (_, millisecond) => ymdhms.set({ millisecond }))),
rootDate: q => Parsimmon.seqMap(Parsimmon.regexp(/\d{4}/), Parsimmon.string("-"), Parsimmon.regexp(/\d{2}/), (year, _, month) => {
return DateTime.fromObject({ year: Number.parseInt(year), month: Number.parseInt(month) })
}),
date: q => chainOpt<DateTime>(q.rootDate,
(ym: DateTime) => Parsimmon.seqMap(Parsimmon.string("-"), q.number, (_, day) => ym.set({ day })),
(ymd: DateTime) => Parsimmon.seqMap(Parsimmon.string("T"), q.number, (_, hour) => ymd.set({ hour })),
(ymdh: DateTime) => Parsimmon.seqMap(Parsimmon.string(":"), q.number, (_, minute) => ymdh.set({ minute })),
(ymdhm: DateTime) => Parsimmon.seqMap(Parsimmon.string(":"), q.number, (_, second) => ymdhm.set({ second })),
(ymdhms: DateTime) => Parsimmon.seqMap(Parsimmon.string("."), q.number, (_, millisecond) => ymdhms.set({ millisecond }))
),


// Source parsing.
tagSource: q => q.tag.map(tag => Sources.tag(tag)),
Expand Down
12 changes: 10 additions & 2 deletions src/test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ test("Task query with no fields", () => {
});

// Pending date support.
/*
test("Parse Year-Month date", () => {
let date = QUERY_LANGUAGE.date.tryParse("2020-04");
expect(date.year).toBe(2020);
Expand All @@ -133,4 +132,13 @@ test("Parse Year-Month-Day date", () => {
expect(date.month).toBe(8);
expect(date.day).toBe(15);
});
*/

test("Parse Year-Month-DayTHour:Minute:Second", () => {
let date = QUERY_LANGUAGE.date.tryParse("1984-08-15T12:42:59");
expect(date.year).toBe(1984);
expect(date.month).toBe(8);
expect(date.day).toBe(15);
expect(date.hour).toBe(12);
expect(date.minute).toBe(42);
expect(date.second).toBe(59);
});
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"0.1.0": "0.10.7",
"0.1.1": "0.10.7",
"0.1.2": "0.10.7",
"0.1.3": "0.10.7"
"0.1.3": "0.10.7",
"0.1.4": "0.10.7"
}

0 comments on commit aa2516b

Please sign in to comment.