Skip to content

Commit

Permalink
support negating of derive columns by using -...
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Nov 17, 2018
1 parent 38653c4 commit cbea9f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/builder/adapter/lineup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,14 @@ export class Adapter {
this.prevColumns = ctx;
const columns = ctx.columns.map((d) => Object.assign({}, d)); // work on copy
if (ctx.deriveColumns) {
columns.push(...deriveColumnDescriptions(data, {columns: ctx.deriveColumnNames}));
const labels = new Set(columns.map((d) => d.label));
const derived = deriveColumnDescriptions(data, {columns: ctx.deriveColumnNames});
for (const derive of derived) {
if (labels.has(derive.label)) { // skip same name
continue;
}
columns.push(derive);
}
}
if (ctx.deriveColors) {
deriveColors(columns);
Expand Down
13 changes: 12 additions & 1 deletion src/provider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ function deriveType(label: string, value: any, column: number | string, data: an
return base;
}

function selectColumns(existing: string[], columns: string[]) {
const allNots = columns.every((d) => d.startsWith('-'));
if (!allNots) {
return columns;
}
// negate case, exclude columns that are given using -notation
const exclude = new Set(columns);
return existing.filter((d) => !exclude.has(`-${d}`));
}

export function deriveColumnDescriptions(data: any[], options: Partial<IDeriveOptions> = {}) {
const config = Object.assign({
categoricalThreshold: 0.7,
Expand All @@ -105,7 +115,8 @@ export function deriveColumnDescriptions(data: any[], options: Partial<IDeriveOp
return first.map((v, i) => deriveType(`Col${i}`, v, i, data, config));
}
//objects
const columns = config.columns.length > 0 ? config.columns : Object.keys(first);
const existing = Object.keys(first);
const columns = config.columns.length > 0 ? selectColumns(existing, config.columns) : existing;
return columns.map((key) => deriveType(key, first[key], key, data, config));
}

Expand Down

0 comments on commit cbea9f0

Please sign in to comment.