Skip to content

Commit

Permalink
Replace forEach with for, remove as, remove push.apply, add test, and…
Browse files Browse the repository at this point in the history
… initialize fill array (#4216)

* Replace forEach with for, remove as, add tests and initialize fill array

* Get rid of push.apply

* Unused import

* Replace more forEach with for..of.
  • Loading branch information
domoritz authored and arvind committed Oct 13, 2018
1 parent ea88efb commit 366f8c9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/compile/selection/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const interval: SelectionCompiler = {
const scaleType = model.getScaleComponent(channel).get('type');
const toNum = hasContinuousDomain(scaleType) ? '+' : '';

signals.push.apply(signals, cs);
signals.push(...cs);
dataSignals.push(dname);

scaleTriggers.push({
Expand Down
6 changes: 3 additions & 3 deletions src/compile/selection/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function assembleUnitSelectionSignals(model: UnitModel, signals: any[]) {
const name = selCmpt.name;
let modifyExpr = selCompiler.modifyExpr(model, selCmpt);

signals.push.apply(signals, selCompiler.signals(model, selCmpt));
signals.push(...selCompiler.signals(model, selCmpt));

forEachTransform(selCmpt, txCompiler => {
if (txCompiler.signals) {
Expand Down Expand Up @@ -228,11 +228,11 @@ export function assembleUnitSelectionMarks(model: UnitModel, marks: any[]): any[
}

export function assembleLayerSelectionMarks(model: LayerModel, marks: any[]): any[] {
model.children.forEach(child => {
for (const child of model.children) {
if (isUnitModel(child)) {
marks = assembleUnitSelectionMarks(child, marks);
}
});
}

return marks;
}
Expand Down
4 changes: 2 additions & 2 deletions src/compile/selection/transforms/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const inputBindings: TransformCompiler = {
const bind = selCmpt.bind;
const datum = nearest.has(selCmpt) ? '(item().isVoronoi ? datum.datum : datum)' : 'datum';

proj.forEach(p => {
for (const p of proj) {
const sgname = varName(`${name}_${p.field}`);
const hasSignal = signals.filter(s => s.name === sgname);
if (!hasSignal.length) {
Expand All @@ -31,7 +31,7 @@ const inputBindings: TransformCompiler = {
bind: bind[p.field] || bind[p.channel] || bind
});
}
});
}

return signals;
},
Expand Down
8 changes: 4 additions & 4 deletions src/compile/selection/transforms/project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ScaleChannel, SingleDefChannel} from '../../../channel';
import {ScaleChannel} from '../../../channel';
import * as log from '../../../log';
import {hasContinuousDomain, isBinScale} from '../../../scale';
import {SelectionDef} from '../../../selection';
Expand All @@ -23,10 +23,10 @@ const project: TransformCompiler = {

// TODO: find a possible channel mapping for these fields.
if (selDef.fields) {
p.push.apply(p, selDef.fields.map(field => ({field, type: 'E'})));
p.push(...selDef.fields.map<ProjectSelectionComponent>(field => ({field, type: 'E'})));
}

(selDef.encodings || []).forEach((channel: SingleDefChannel) => {
for (const channel of selDef.encodings || []) {
const fieldDef = model.fieldDef(channel);
if (fieldDef) {
let field = fieldDef.field;
Expand Down Expand Up @@ -67,7 +67,7 @@ const project: TransformCompiler = {
} else {
log.warn(log.message.cannotProjectOnChannelWithoutField(channel));
}
});
}

if (keys(timeUnits).length) {
selCmpt.timeUnit = new TimeUnitNode(null, timeUnits);
Expand Down
19 changes: 12 additions & 7 deletions src/compile/selection/transforms/scales.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {stringValue} from 'vega-util';
import {Channel, ScaleChannel, X, Y} from '../../../channel';
import {Channel, isScaleChannel, X, Y} from '../../../channel';
import * as log from '../../../log';
import {hasContinuousDomain, isBinScale} from '../../../scale';
import {accessPathWithDatum, varName} from '../../../util';
Expand All @@ -16,14 +16,19 @@ const scaleBindings: TransformCompiler = {
const name = varName(selCmpt.name);
const bound: Channel[] = (selCmpt.scales = []);

selCmpt.project.forEach(p => {
const channel = p.channel as ScaleChannel;
for (const p of selCmpt.project) {
const channel = p.channel;

if (!isScaleChannel(channel)) {
continue;
}

const scale = model.getScaleComponent(channel);
const scaleType = scale ? scale.get('type') : undefined;

if (!scale || !hasContinuousDomain(scaleType) || isBinScale(scaleType)) {
log.warn(log.message.SCALE_BINDINGS_CONTINUOUS);
return;
continue;
}

scale.set('domainRaw', {signal: accessPathWithDatum(p.field, name)}, true);
Expand All @@ -34,7 +39,7 @@ const scaleBindings: TransformCompiler = {
const scale2 = model.getScaleComponent(channel === X ? Y : X);
scale2.set('domainRaw', {signal: accessPathWithDatum(p.field, name)}, true);
}
});
}
},

topLevelSignals: (model, selCmpt, signals) => {
Expand Down Expand Up @@ -77,13 +82,13 @@ const scaleBindings: TransformCompiler = {
signals: (model, selCmpt, signals) => {
// Nested signals need only push to top-level signals with multiview displays.
if (model.parent) {
selCmpt.scales.forEach(channel => {
for (const channel of selCmpt.scales) {
const signal = signals.filter(s => s.name === channelSignalName(selCmpt, channel, 'data'))[0];

signal.push = 'outer';
delete signal.value;
delete signal.update;
});
}
}

return signals;
Expand Down
4 changes: 2 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ export function flatten(arrays: any[]) {
}

export function fill<T>(val: T, len: number) {
const arr: T[] = [];
const arr = new Array<T>(len);
for (let i = 0; i < len; ++i) {
arr.push(val);
arr[i] = val;
}
return arr;
}
Expand Down
10 changes: 9 additions & 1 deletion test/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assert} from 'chai';
import {entries, fieldIntersection, flatAccessWithDatum, prefixGenerator, unique, uniqueId} from '../src/util';
import {entries, fieldIntersection, fill, flatAccessWithDatum, prefixGenerator, unique, uniqueId} from '../src/util';

import {
accessPathDepth,
Expand Down Expand Up @@ -202,4 +202,12 @@ describe('util', () => {
expect(uniqueId() === uniqueId()).toBeFalsy();
});
});

describe('fill', () => {
it('should return array of right length and filled with the right values', () => {
const arr = fill(42, 5);
expect(arr).toHaveLength(5);
expect(arr).toEqual([42, 42, 42, 42, 42]);
});
});
});

0 comments on commit 366f8c9

Please sign in to comment.