Skip to content

Commit

Permalink
refactor: simplify updateScale
Browse files Browse the repository at this point in the history
  • Loading branch information
kristw committed Jul 24, 2020
1 parent 9e24d65 commit 70a9a66
Showing 1 changed file with 4 additions and 95 deletions.
99 changes: 4 additions & 95 deletions packages/vx-scale/src/updateScale.ts
@@ -1,20 +1,9 @@
import { PickScaleConfigWithoutType, ScaleConfigWithoutType } from './types/ScaleConfig';
import { DefaultThresholdInput, D3Scale, PickD3Scale } from './types/Scale';
import { StringLike, DefaultOutput } from './types/Base';
import { updateThresholdScale } from './scales/threshold';
import { updateQuantileScale } from './scales/quantile';
import { updateBandScale } from './scales/band';
import { updatePointScale } from './scales/point';
import { updateOrdinalScale } from './scales/ordinal';
import { updateLogScale } from './scales/log';
import { updateSymlogScale } from './scales/symlog';
import { updatePowScale } from './scales/power';
import { updateQuantizeScale } from './scales/quantize';
import { updateTimeScale } from './scales/time';
import { updateLinearScale } from './scales/linear';
import { updateSqrtScale } from './scales/squareRoot';
import { updateUtcScale } from './scales/utc';
import inferScaleType from './utils/inferScaleType';
import scaleOperator, { ALL_OPERATORS } from './operators/scaleOperator';

const applyAllOperators = scaleOperator(...ALL_OPERATORS);

// Overload function signature for more strict typing, e.g.,
// If the scale is a ScaleLinear, the config is a linear config.
Expand Down Expand Up @@ -157,87 +146,7 @@ function updateScale<
scale: D3Scale<Output, DiscreteInput, ThresholdInput>,
config?: ScaleConfigWithoutType<Output, DiscreteInput, ThresholdInput>,
) {
const scaleOut = scale.copy() as D3Scale<Output, DiscreteInput, ThresholdInput>;

// If a config is not specified, just return a copy
if (typeof config === 'undefined') {
return scaleOut;
}

const type = inferScaleType(scale);

// Function overloading above should ensure the scale and config
// are compatible matches.
// Just cast the scale and config to the correct types.

switch (type) {
default:
case 'linear':
return updateLinearScale(
scaleOut as PickD3Scale<'linear', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'linear', Output, DiscreteInput, ThresholdInput>,
);
case 'log':
return updateLogScale(
scaleOut as PickD3Scale<'log', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'log', Output, DiscreteInput, ThresholdInput>,
);
case 'pow':
return updatePowScale(
scaleOut as PickD3Scale<'pow', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'pow', Output, DiscreteInput, ThresholdInput>,
);
case 'sqrt':
return updateSqrtScale(
scaleOut as PickD3Scale<'sqrt', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'sqrt', Output, DiscreteInput, ThresholdInput>,
);
case 'symlog':
return updateSymlogScale(
scaleOut as PickD3Scale<'symlog', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'symlog', Output, DiscreteInput, ThresholdInput>,
);
case 'time':
return updateTimeScale(
scaleOut as PickD3Scale<'time', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'time', Output, DiscreteInput, ThresholdInput>,
);
case 'utc':
return updateUtcScale(
scaleOut as PickD3Scale<'utc', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'utc', Output, DiscreteInput, ThresholdInput>,
);
case 'quantile':
return updateQuantileScale(
scaleOut as PickD3Scale<'quantile', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'quantile', Output, DiscreteInput, ThresholdInput>,
);
case 'quantize':
return updateQuantizeScale(
scaleOut as PickD3Scale<'quantize', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'quantize', Output, DiscreteInput, ThresholdInput>,
);
case 'threshold':
return updateThresholdScale(
scaleOut as PickD3Scale<'threshold', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'threshold', Output, DiscreteInput, ThresholdInput>,
);
case 'ordinal':
return updateOrdinalScale(
scaleOut as PickD3Scale<'ordinal', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'ordinal', Output, DiscreteInput, ThresholdInput>,
);
case 'point':
return updatePointScale(
scaleOut as PickD3Scale<'point', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'point', Output, DiscreteInput, ThresholdInput>,
);
case 'band':
return updateBandScale(
scaleOut as PickD3Scale<'band', Output, DiscreteInput, ThresholdInput>,
config as PickScaleConfigWithoutType<'band', Output, DiscreteInput, ThresholdInput>,
);
}
return applyAllOperators(scale.copy(), config);
}

export default updateScale;

0 comments on commit 70a9a66

Please sign in to comment.