Skip to content

Commit

Permalink
refactor: use Number.Inf... when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Feb 27, 2021
1 parent 4fc3d58 commit 68ea6b9
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/builder/column/NumberColumnBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ export default class NumberColumnBuilder extends ColumnBuilder<INumberColumnDesc
const minValue = min(data, (d) => {
const v = resolveValue(d, col);
const vs: number[] = asArray(v, 'min');
return vs.length === 0 ? Infinity : min(vs);
return vs.length === 0 ? Number.POSITIVE_INFINITY : min(vs);
});
const maxValue = max(data, (d) => {
const v = resolveValue(d, col);
const vs: number[] = asArray(v, 'max');
return vs.length === 0 ? -Infinity : max(vs);
return vs.length === 0 ? Number.NEGATIVE_INFINITY : max(vs);
});
return [minValue, maxValue];
}
Expand Down
6 changes: 3 additions & 3 deletions src/model/DateColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ export default class DateColumn extends ValueColumn<Date> implements IDateColumn
}

setFilter(value: IDateFilter | null) {
value = value || { min: -Infinity, max: +Infinity, filterMissing: false };
value = value || { min: Number.NEGATIVE_INFINITY, max: Number.POSITIVE_INFINITY, filterMissing: false };
if (isEqualDateFilter(value, this.currentFilter)) {
return;
}
const bak = this.getFilter();
this.currentFilter.min = isUnknown(value.min) ? -Infinity : value.min;
this.currentFilter.max = isUnknown(value.max) ? Infinity : value.max;
this.currentFilter.min = isUnknown(value.min) ? Number.NEGATIVE_INFINITY : value.min;
this.currentFilter.max = isUnknown(value.max) ? Number.POSITIVE_INFINITY : value.max;
this.currentFilter.filterMissing = value.filterMissing;
this.fire([DateColumn.EVENT_FILTER_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY], bak, this.getFilter());
}
Expand Down
6 changes: 3 additions & 3 deletions src/model/HierarchyColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default class HierarchyColumn extends ValueColumn<string> implements ICat
readonly hierarchy: Readonly<ICategoryInternalNode>;

private currentNode: Readonly<ICategoryInternalNode>;
private currentMaxDepth = Infinity;
private currentMaxDepth = Number.POSITIVE_INFINITY;
private currentLeaves: Readonly<ICategoryInternalNode>[] = [];
private readonly currentLeavesNameCache = new Map<string, Readonly<ICategoryInternalNode>>();
private readonly currentLeavesPathCache = new Map<string, Readonly<ICategoryInternalNode>>();
Expand Down Expand Up @@ -236,7 +236,7 @@ export default class HierarchyColumn extends ValueColumn<string> implements ICat
}

setCutOff(value: ICutOffNode) {
const maxDepth = value.maxDepth == null ? Infinity : value.maxDepth;
const maxDepth = value.maxDepth == null ? Number.POSITIVE_INFINITY : value.maxDepth;
if (this.currentNode === value.node && this.currentMaxDepth === maxDepth) {
return;
}
Expand Down Expand Up @@ -348,7 +348,7 @@ export default class HierarchyColumn extends ValueColumn<string> implements ICat
}
}

function computeLeaves(node: ICategoryInternalNode, maxDepth = Infinity) {
function computeLeaves(node: ICategoryInternalNode, maxDepth = Number.POSITIVE_INFINITY) {
const leaves: ICategoryInternalNode[] = [];
//depth first
const visit = (node: ICategoryInternalNode, depth: number) => {
Expand Down
6 changes: 3 additions & 3 deletions src/model/NumberColumn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,13 @@ export default class NumberColumn extends ValueColumn<number> implements INumber
}

setFilter(value: INumberFilter | null) {
value = value || { min: -Infinity, max: +Infinity, filterMissing: false };
value = value || { min: Number.NEGATIVE_INFINITY, max: Number.POSITIVE_INFINITY, filterMissing: false };
if (isEqualNumberFilter(value, this.currentFilter, this.filterAccuracy)) {
return;
}
const bak = this.getFilter();
this.currentFilter.min = isUnknown(value.min) ? -Infinity : value.min;
this.currentFilter.max = isUnknown(value.max) ? Infinity : value.max;
this.currentFilter.min = isUnknown(value.min) ? Number.NEGATIVE_INFINITY : value.min;
this.currentFilter.max = isUnknown(value.max) ? Number.POSITIVE_INFINITY : value.max;
this.currentFilter.filterMissing = value.filterMissing;
this.fire(
[NumberColumn.EVENT_FILTER_CHANGED, Column.EVENT_DIRTY_VALUES, Column.EVENT_DIRTY],
Expand Down
6 changes: 3 additions & 3 deletions src/model/internalNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function numberCompare(a: number | null, b: number | null, aMissing = fal

/** @internal */
export function noNumberFilter() {
return { min: -Infinity, max: Infinity, filterMissing: false };
return { min: Number.NEGATIVE_INFINITY, max: Number.POSITIVE_INFINITY, filterMissing: false };
}

/** @internal */
Expand All @@ -83,8 +83,8 @@ export function isDummyNumberFilter(filter: INumberFilter) {
/** @internal */
export function restoreNumberFilter(v: INumberFilter): INumberFilter {
return {
min: v.min != null && isFinite(v.min) ? v.min : -Infinity,
max: v.max != null && isFinite(v.max) ? v.max : +Infinity,
min: v.min != null && isFinite(v.min) ? v.min : Number.NEGATIVE_INFINITY,
max: v.max != null && isFinite(v.max) ? v.max : Number.POSITIVE_INFINITY,
filterMissing: v.filterMissing,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/provider/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function createSetter(
missingCount: number
): (index: number, v: ICompareValue) => void {
switch (type) {
case ECompareValueType.BINARY: // just 0 or 1 -> convert to 0=-Infinity 1 2 255=+Infinity
case ECompareValueType.BINARY: // just 0 or 1 -> convert to 0=Number.NEGATIVE_INFINITY 1 2 255=Number.POSITIVE_INFINITY
return (index, v) => (lookup[index] = v == null || Number.isNaN(v as number) ? missingBinary : (v as number) + 1);
case ECompareValueType.COUNT: // uint32
return (index, v) => (lookup[index] = v == null || Number.isNaN(v as number) ? missingCount : (v as number) + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/provider/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export function rangeSelection(
const order = ranking.getOrder();
const lookup = new Map(Array.from(order).map((d, i) => [d, i]));
const distances = selection.map((d) => {
const index = lookup.has(d) ? lookup.get(d)! : Infinity;
const index = lookup.has(d) ? lookup.get(d)! : Number.POSITIVE_INFINITY;
return { s: d, index, distance: Math.abs(relIndex - index) };
});
const nearest = distances.sort((a, b) => a.distance - b.distance)[0]!;
Expand Down
2 changes: 1 addition & 1 deletion src/ui/dialogs/CutOffHierarchyDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default class CutOffHierarchyDialog extends ADialog {
const newNodeIndex = this.innerNodePaths.indexOf(newNode);
const node = this.innerNodes[newNodeIndex];
const maxDepthText = this.findInput('input[type="number"]').value;
const maxDepth = maxDepthText === '' ? Infinity : parseInt(maxDepthText, 10);
const maxDepth = maxDepthText === '' ? Number.POSITIVE_INFINITY : parseInt(maxDepthText, 10);
this.column.setCutOff({ node, maxDepth });
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/ui/panel/Hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export default class Hierarchy {
last.querySelector<HTMLElement>('i[title=Sort]')!.onclick = (evt) => {
evt.preventDefault();
evt.stopPropagation();
click(s, Infinity);
click(s, Number.POSITIVE_INFINITY);
};
};

Expand Down Expand Up @@ -232,7 +232,7 @@ export default class Hierarchy {
last.querySelector<HTMLElement>('i[title="Sort Group"]')!.onclick = (evt) => {
evt.preventDefault();
evt.stopPropagation();
click(s, Infinity);
click(s, Number.POSITIVE_INFINITY);
};
};

Expand Down

0 comments on commit 68ea6b9

Please sign in to comment.