Skip to content

Commit

Permalink
0.2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brenan committed Apr 5, 2021
1 parent 0e824f5 commit 9e091b5
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 148 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.2.3",
"version": "0.2.4",
"minAppVersion": "0.10.13",
"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.2.3",
"version": "0.2.4",
"description": "Advanced data views for Obsidian.md.",
"main": "main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ git push
npm run build

# And do a github release.
gh release create "${NEW_VERSION}" build/main.js styles.css manifest.json
gh release create "${NEW_VERSION}" build/main.js styles.css manifest.json
278 changes: 164 additions & 114 deletions src/eval.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MarkdownRenderChild, Plugin, Workspace, Vault, MarkdownPostProcessorContext, PluginSettingTab, App, Setting } from 'obsidian';
import { createAnchor, prettifyYamlKey, renderErrorPre, renderField, renderList, renderMinimalDate, renderTable } from 'src/render';
import { createAnchor, renderErrorPre, renderField, renderList, renderMinimalDate, renderTable } from 'src/render';
import { FullIndex, TaskCache } from 'src/index';
import * as Tasks from 'src/tasks';
import { Query } from 'src/query';
Expand Down
16 changes: 15 additions & 1 deletion src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DateTime, Duration } from 'luxon';
export type QueryType = 'list' | 'table' | 'task';

/** The literal types supported by the query engine. */
export type LiteralType = 'boolean' | 'number' | 'string' | 'date' | 'duration' | 'link' | 'array' | 'object' | 'null';
export type LiteralType = 'boolean' | 'number' | 'string' | 'date' | 'duration' | 'link' | 'array' | 'object' | 'html' | 'null';
/** Maps the string type to it's actual javascript representation. */
export type LiteralTypeRepr<T extends LiteralType> =
T extends 'boolean' ? boolean :
Expand All @@ -17,6 +17,7 @@ export type LiteralTypeRepr<T extends LiteralType> =
T extends 'link'? string :
T extends 'array' ? Array<LiteralField> :
T extends 'object' ? Map<string, LiteralField> :
T extends 'html' ? HTMLElement :
any;

/** Valid binary operators. */
Expand All @@ -33,6 +34,7 @@ export type LiteralField =
| LiteralFieldRepr<'link'>
| LiteralFieldRepr<'array'>
| LiteralFieldRepr<'object'>
| LiteralFieldRepr<'html'>
| LiteralFieldRepr<'null'>;

/** Literal representation of some field type. */
Expand Down Expand Up @@ -188,6 +190,10 @@ export namespace Fields {
return object(new Map());
}

export function html(elem: HTMLElement): LiteralFieldRepr<'html'> {
return Fields.literal('html', elem);
}

export function binaryOp(left: Field, op: BinaryOp, right: Field): Field {
return { type: 'binaryop', left, op, right } as BinaryOpField;
}
Expand Down Expand Up @@ -237,6 +243,12 @@ export namespace Fields {
return field.value.toMillis() != 0;
case "duration":
return field.value.as("seconds") != 0;
case "object":
return field.value.size > 0;
case "array":
return field.value.length > 0;
case "html":
return true;
default:
return false;
}
Expand Down Expand Up @@ -279,6 +291,8 @@ export namespace Fields {
return `array:[${field.value.map(toLiteralKey).join(", ")}]`;
case "object":
return `object:[${Object.entries(field.value).map(val => `${val[0]}:${toLiteralKey(val[1])}`).join(", ")}]`
case "html":
return "" + field.value;
}
}

Expand Down
30 changes: 4 additions & 26 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,6 @@ export function createAnchor(text: string, target: string, internal: boolean): H
return a;
}

/** Pretifies YAML keys like 'time-played' into 'Time Played' */
export function prettifyYamlKey(key: string): string {
if (key.length == 0) return key;
let result = key[0].toUpperCase();

// Hacky camel case detection. Will do unwanted things for stuff like 'LaTeX'.
// May remove in the future, dunno.
for (let index = 1; index < key.length; index++) {
let isNewWord = key[index].toUpperCase() == key[index]
&& key[index - 1].toLowerCase() == key[index - 1];
isNewWord = isNewWord || (key[index - 1] == "_");
isNewWord = isNewWord || (key[index - 1] == "-");
isNewWord = isNewWord || (key[index - 1] == ".");

if (isNewWord) {
result += " " + key[index].toUpperCase();
} else {
result += key[index];
}
}

return result.replace("-", "").replace("_", "").replace(".", "");
}

/** Create a list inside the given container, with the given data. */
export function renderList(container: HTMLElement, elements: (string | HTMLElement)[]) {
let listEl = container.createEl('ul', { cls: 'list-view-ul' });
Expand Down Expand Up @@ -172,10 +148,12 @@ export function renderField(field: LiteralField, nullField: string, expandList:
}
return "{ " + entries.join(", ") + " }";
}
case "null":
return nullField;
case "link":
return createAnchor(getFileName(field.value), field.value.replace(".md", ""), true);
case "null":
return nullField;
case "html":
return field.value;
default:
return "" + field.value;
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/eval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ test("Evaluate simple link resolution", () => {
});

// <-- Functions -->
// <-- Function vectorization -->

test("Evaluate lower(list)", () => {
expect(parseEval("lower(list(\"A\", \"B\"))")).toEqual(Fields.array([Fields.string("a"), Fields.string("b")]));
})

test("Evaluate replace(list, string, string)", () => {
expect(parseEval("replace(list(\"yes\", \"re\"), \"e\", \"a\")")).toEqual(Fields.array([
Fields.string("yas"), Fields.string("ra")
]));
})

// <-- Length -->

test("Evaluate length(array)", () => {
Expand Down Expand Up @@ -188,6 +200,26 @@ test("Evaluate default()", () => {
expect(parseEval("default(2, 1)")).toEqual(Fields.number(2));
});

// <-- any/all() -->

test("Evaluate any()", () => {
expect(parseEval("any(true, false)")).toEqual(Fields.bool(true));
expect(parseEval("any(list(true, false))")).toEqual(Fields.bool(true));
})

test("Evaluate all()", () => {
expect(parseEval("all(true, false)")).toEqual(Fields.bool(false));
expect(parseEval("all(true, list(false))")).toEqual(Fields.bool(true));
expect(parseEval("all(list(true, false))")).toEqual(Fields.bool(false));
expect(parseEval("all(list(true, list(false)))")).toEqual(Fields.bool(true));
})

test("Evaluate vectorized all()", () => {
expect(parseEval("all(regexmatch(\"a+\", list(\"a\", \"aaaa\")))")).toEqual(Fields.bool(true));
expect(parseEval("all(regexmatch(\"a+\", list(\"a\", \"aaab\")))")).toEqual(Fields.bool(false));
expect(parseEval("any(regexmatch(\"a+\", list(\"a\", \"aaab\")))")).toEqual(Fields.bool(true));
});

/** Parse a field expression and evaluate it in the simple context. */
function parseEval(text: string): LiteralField {
let field = EXPRESSION.field.tryParse(text);
Expand Down
6 changes: 4 additions & 2 deletions src/util/result.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/** Monadic utility class for handling potentially failing computations. */
export class Result<T, E> {

static success<T, E>(result: T) {
/** Create a successful result. */
public static success<T, E>(result: T) {
return new Result<T, E>(true, result, null);
}

static failure<T, E>(error: E) {
/** Create a failing result. */
public static failure<T, E>(error: E) {
return new Result<T, E>(false, null, error);
}

Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"": "0.10.13",
"0.2.1": "0.10.13",
"0.2.2": "0.10.13",
"0.2.3": "0.10.13"
"0.2.3": "0.10.13",
"0.2.4": "0.10.13"
}

0 comments on commit 9e091b5

Please sign in to comment.