Skip to content

Commit

Permalink
Add prettifier; formatting apocalypse inbound
Browse files Browse the repository at this point in the history
No functional changes, but now enforces a code formatter finally.
  • Loading branch information
Michael Brenan committed Aug 22, 2021
1 parent 42ae55e commit 39b1916
Show file tree
Hide file tree
Showing 41 changed files with 2,626 additions and 1,771 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ on:
branches: [ master ]

jobs:
format:
runs-on: ubuntu-latest
name: Check code formatting
steps:
- uses: actions/checkout@v2
- name: Setup node
uses: actions/setup-node@v2
with:
node-version: 16
- run: npm run check-format
build:
runs-on: ubuntu-latest
name: Build project
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
*.iml
.idea

# VSCode
.vscode

# npm
node_modules

Expand Down
8 changes: 8 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tabWidth": 4,
"semi": true,
"embeddedLanguageFormatting": "off",
"parser": "typescript",
"printWidth": 120,
"arrowParens": "avoid"
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"dev": "rollup --config rollup.config.js -w",
"lib": "rollup --config rollup.config.js --environment BUILD:lib",
"build": "rollup --config rollup.config.js --environment BUILD:production",
"format": "npx prettier --write src",
"check-format": "npx prettier --check src",
"test": "jest"
},
"keywords": [],
Expand All @@ -28,6 +30,7 @@
"@zerollup/ts-transform-paths": "^1.7.18",
"jest": "^26.6.3",
"obsidian": "^0.12.0",
"prettier": "2.3.2",
"rollup": "^2.32.1",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-typescript2": "^0.30.0",
Expand Down
98 changes: 75 additions & 23 deletions src/api/data-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ export interface DataArray<T> {
* Return a sorted array sorted by the given key; an optional comparator can be provided, which will
* be used to compare the keys in leiu of the default dataview comparator.
*/
sort<U>(key: ArrayFunc<T, U>, direction?: 'asc' | 'desc', comparator?: ArrayComparator<U>): DataArray<T>;
sort<U>(key: ArrayFunc<T, U>, direction?: "asc" | "desc", comparator?: ArrayComparator<U>): DataArray<T>;

/**
* Return an array where elements are grouped by the given key; the resulting array will have objects of the form
* { key: <key value>, rows: DataArray }.
*/
groupBy<U>(key: ArrayFunc<T, U>, comparator?: ArrayComparator<U>): DataArray<{ key: U, rows: DataArray<T> }>;
groupBy<U>(key: ArrayFunc<T, U>, comparator?: ArrayComparator<U>): DataArray<{ key: U; rows: DataArray<T> }>;

/**
* Return distinct entries. If a key is provided, then rows with distinct keys are returned.
Expand Down Expand Up @@ -107,9 +107,34 @@ export interface DataArray<T> {
/** Implementation of DataArray, minus the dynamic variable access, which is implemented via proxy. */
class DataArrayImpl<T> implements DataArray<T> {
private static ARRAY_FUNCTIONS: Set<string> = new Set([
"where", "filter", "map", "flatMap", "slice", "concat", "indexOf", "find", "findIndex", "includes",
"join", "sort", "groupBy", "distinct", "every", "some", "none", "first", "last", "to",
"lwrap", "expand", "forEach", "length", "values", "array", "defaultComparator", "toString"
"where",
"filter",
"map",
"flatMap",
"slice",
"concat",
"indexOf",
"find",
"findIndex",
"includes",
"join",
"sort",
"groupBy",
"distinct",
"every",
"some",
"none",
"first",
"last",
"to",
"lwrap",
"expand",
"forEach",
"length",
"values",
"array",
"defaultComparator",
"toString",
]);

private static ARRAY_PROXY: ProxyHandler<DataArrayImpl<any>> = {
Expand All @@ -120,17 +145,25 @@ class DataArrayImpl<T> implements DataArray<T> {
else if (DataArrayImpl.ARRAY_FUNCTIONS.has(prop.toString())) return target[prop.toString()];

return target.to(prop);
}
},
};

public static wrap<T>(arr: T[], settings: QuerySettings, defaultComparator: ArrayComparator<any> = Values.compareValue): DataArray<T> {
public static wrap<T>(
arr: T[],
settings: QuerySettings,
defaultComparator: ArrayComparator<any> = Values.compareValue
): DataArray<T> {
return new Proxy(new DataArrayImpl(arr, settings, defaultComparator), DataArrayImpl.ARRAY_PROXY);
}

public length: number;
[key: string]: any;

private constructor(public values: any[], public settings: QuerySettings, public defaultComparator: ArrayComparator<any> = Values.compareValue) {
private constructor(
public values: any[],
public settings: QuerySettings,
public defaultComparator: ArrayComparator<any> = Values.compareValue
) {
this.length = values.length;
}

Expand Down Expand Up @@ -204,33 +237,40 @@ class DataArrayImpl<T> implements DataArray<T> {
}

public join(sep?: string): string {
return this.map(s => Values.toString(s, this.settings)).array().join(sep ?? ", ");
return this.map(s => Values.toString(s, this.settings))
.array()
.join(sep ?? ", ");
}

public sort<U>(key: ArrayFunc<T, U>, direction?: 'asc' | 'desc', comparator?: ArrayComparator<U>): DataArray<T> {
public sort<U>(key: ArrayFunc<T, U>, direction?: "asc" | "desc", comparator?: ArrayComparator<U>): DataArray<T> {
if (this.values.length == 0) return this;
let realComparator = comparator ?? this.defaultComparator;

// Associate each entry with it's index for the key function, and then do a normal sort.
let copy = ([] as any[]).concat(this.array()).map((elem, index) => { return { index: index, value: elem } });
let copy = ([] as any[]).concat(this.array()).map((elem, index) => {
return { index: index, value: elem };
});
copy.sort((a, b) => {
let aKey = key(a.value, a.index, this.values);
let bKey = key(b.value, b.index, this.values);
return direction === 'desc' ? -realComparator(aKey, bKey) : realComparator(aKey, bKey);
return direction === "desc" ? -realComparator(aKey, bKey) : realComparator(aKey, bKey);
});

return this.lwrap(copy.map(e => e.value));
}

public groupBy<U>(key: ArrayFunc<T, U>, comparator?: ArrayComparator<U>): DataArray<{ key: U, rows: DataArray<T> }> {
public groupBy<U>(
key: ArrayFunc<T, U>,
comparator?: ArrayComparator<U>
): DataArray<{ key: U; rows: DataArray<T> }> {
if (this.values.length == 0) return this.lwrap([]);

// JavaScript sucks and we can't make hash maps over arbitrary types (only strings/ints), so
// we do a poor man algorithm where we SORT, followed by grouping.
let intermediate = this.sort(key, "asc", comparator);
comparator = comparator ?? this.defaultComparator;

let result: { key: U, rows: DataArray<T> }[] = [];
let result: { key: U; rows: DataArray<T> }[] = [];
let currentRow = [intermediate[0]];
let current = key(intermediate[0], 0, intermediate.values);
for (let index = 1; index < intermediate.length; index++) {
Expand All @@ -253,9 +293,9 @@ class DataArrayImpl<T> implements DataArray<T> {
let realKey = key ?? (x => x as any as U);

// For similar reasons to groupBy, do a sort and take the first element of each block.
let intermediate = this
.map((x, index) => { return { key: realKey(x, index, this.values), value: x } })
.sort(x => x.key, "asc", comparator);
let intermediate = this.map((x, index) => {
return { key: realKey(x, index, this.values), value: x };
}).sort(x => x.key, "asc", comparator);
comparator = comparator ?? this.defaultComparator;

let result: T[] = [intermediate[0].value];
Expand All @@ -268,14 +308,24 @@ class DataArrayImpl<T> implements DataArray<T> {
return this.lwrap(result);
}

public every(f: ArrayFunc<T, boolean>): boolean { return this.values.every(f); }
public every(f: ArrayFunc<T, boolean>): boolean {
return this.values.every(f);
}

public some(f: ArrayFunc<T, boolean>): boolean { return this.values.some(f); }
public some(f: ArrayFunc<T, boolean>): boolean {
return this.values.some(f);
}

public none(f: ArrayFunc<T, boolean>): boolean { return this.values.every((v, i, a) => !f(v, i, a)); }
public none(f: ArrayFunc<T, boolean>): boolean {
return this.values.every((v, i, a) => !f(v, i, a));
}

public first(): T { return this.values.length > 0 ? this.values[0] : undefined; }
public last(): T { return this.values.length > 0 ? this.values[this.values.length - 1] : undefined; }
public first(): T {
return this.values.length > 0 ? this.values[0] : undefined;
}
public last(): T {
return this.values.length > 0 ? this.values[this.values.length - 1] : undefined;
}

public to(key: string): DataArray<any> {
let result: any[] = [];
Expand Down Expand Up @@ -315,7 +365,9 @@ class DataArrayImpl<T> implements DataArray<T> {
}
}

public array(): T[] { return ([] as any[]).concat(this.values); }
public array(): T[] {
return ([] as any[]).concat(this.values);
}

public [Symbol.iterator](): Iterator<T> {
return this.values[Symbol.iterator]();
Expand Down
Loading

0 comments on commit 39b1916

Please sign in to comment.