Skip to content

Commit

Permalink
Merge pull request #80 from molgenis/feat/sortByPath
Browse files Browse the repository at this point in the history
Sort on path instead of metadata
  • Loading branch information
dennishendriksen authored Jun 2, 2022
2 parents 5bfe5d2 + a1c16f5 commit 035f28a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 244 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@molgenis/vip-report-api",
"version": "3.3.1",
"version": "3.4.0",
"description": "TypeScript Report API for Variant Call Format (VCF) Report Templates",
"scripts": {
"build": "tsc --build",
Expand Down
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/Api.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Metadata as RecordMetadata, Record } from "@molgenis/vip-report-vcf/src/Vcf";
import { FieldMetadata } from "@molgenis/vip-report-vcf/src/MetadataParser";

export interface Api {
getRecordsMeta(): Promise<RecordMetadata>;
Expand Down Expand Up @@ -39,7 +38,7 @@ export interface Params {
}

export interface SortOrder {
property: string | FieldMetadata;
property: string | string[];
compare?: "asc" | "desc" | CompareFn;
}

Expand Down
38 changes: 1 addition & 37 deletions src/ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
SortOrder,
} from "./Api";
import { Metadata as RecordMetadata, Record } from "@molgenis/vip-report-vcf/src/Vcf";
import { FieldMetadata, NestedFieldMetadata } from "@molgenis/vip-report-vcf/src/MetadataParser";
import { compareAsc, compareDesc } from "./compare";

export interface ReportData {
Expand Down Expand Up @@ -231,43 +230,8 @@ function getCompareFn(sortOrder: SortOrder): CompareFn {
return compareFn;
}

class UnknownFieldError extends Error {
constructor(fieldId: string, path: Path) {
super(`unknown field '${fieldId}' in path '[${path.join(",")}]'`);
this.name = "UnknownFieldError";
}
}

type Path = (string | number)[];

function getPath(property: string | FieldMetadata): Path {
if (typeof property === "string") {
return [property];
}

const path = [];
let field: FieldMetadata = property;
do {
const parent = field.parent;
if (parent && parent.number.count !== 1) {
const index = (parent.nested as NestedFieldMetadata).items.findIndex((item) => field.id === item.id);
if (index === -1) {
throw new UnknownFieldError(field.id, path);
}
path.push(index);
} else {
path.push(field.id);
}

if (parent) field = parent;
else break;
} while (true);
path.push("n");
path.reverse();

return path;
}

function getValue(item: Item<Resource>, path: Path): CompareValue {
let value: unknown = item.data;
for (const token of path) {
Expand All @@ -289,7 +253,7 @@ function sort<T extends Resource>(resources: Item<T>[], sortOrders: SortOrder[])
resources.sort((a, b) => {
let val = 0;
for (const sortOrder of sortOrders) {
const path = getPath(sortOrder.property);
const path = typeof sortOrder.property === "string" ? [sortOrder.property] : sortOrder.property;
const valueA = getValue(a, path);
const valueB = getValue(b, path);

Expand Down
Loading

0 comments on commit 035f28a

Please sign in to comment.