Skip to content

Commit

Permalink
refactor: use optional chaining (#9494)
Browse files Browse the repository at this point in the history
Simple simplification.

---------

Co-authored-by: GitHub Actions Bot <[email protected]>
  • Loading branch information
domoritz and GitHub Actions Bot authored Dec 12, 2024
1 parent cbad083 commit 056989b
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/compile/data/facet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class FacetNode extends DataFlowNode {
const hasSharedAxis: {row?: true; column?: true} = {};
for (const headerChannel of HEADER_CHANNELS) {
for (const headerType of HEADER_TYPES) {
const headers = (layoutHeaders[headerChannel] && layoutHeaders[headerChannel][headerType]) ?? [];
const headers = layoutHeaders[headerChannel]?.[headerType] ?? [];
for (const header of headers) {
if (header.axes?.length > 0) {
hasSharedAxis[headerChannel] = true;
Expand Down
2 changes: 1 addition & 1 deletion src/compile/selection/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const inputBindings: SelectionCompiler<'point'> = {
const name = selCmpt.name;
const proj = selCmpt.project;
const bind = selCmpt.bind;
const init = selCmpt.init && selCmpt.init[0]; // Can only exist on single selections (one initial value).
const init = selCmpt.init?.[0]; // Can only exist on single selections (one initial value).
const datum = nearest.defined(selCmpt) ? '(item().isVoronoi ? datum.datum : datum)' : 'datum';

proj.items.forEach((p, i) => {
Expand Down
10 changes: 5 additions & 5 deletions src/compile/selection/interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const interval: SelectionCompiler<'interval'> = {
const init = selCmpt.init ? selCmpt.init[0] : null;

signals.push(
...channels.reduce((arr, proj) => arr.concat(channelSignals(model, selCmpt, proj, init && init[proj.index])), [])
...channels.reduce((arr, proj) => arr.concat(channelSignals(model, selCmpt, proj, init?.[proj.index])), [])
);

if (!model.hasProjection) {
Expand Down Expand Up @@ -119,10 +119,10 @@ const interval: SelectionCompiler<'interval'> = {
const projection = stringValue(model.projectionName());
const centerSg = model.projectionName() + CENTER;
const {x, y} = selCmpt.project.hasChannel;
const xvname = x && x.signals.visual;
const yvname = y && y.signals.visual;
const xinit = x ? init && init[x.index] : `${centerSg}[0]`;
const yinit = y ? init && init[y.index] : `${centerSg}[1]`;
const xvname = x?.signals.visual;
const yvname = y?.signals.visual;
const xinit = x ? init?.[x.index] : `${centerSg}[0]`;
const yinit = y ? init?.[y.index] : `${centerSg}[1]`;
const sizeSg = (layout: keyof LayoutSizeIndex) => model.getSizeSignalRef(layout).signal;
const bbox =
`[` +
Expand Down
4 changes: 2 additions & 2 deletions src/compile/selection/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ function onDelta(
const signal = signals.find(s => s.name === proj.signals[boundScales ? 'data' : 'visual']);
const sizeSg = model.getSizeSignalRef(size).signal;
const scaleCmpt = model.getScaleComponent(channel);
const scaleType = scaleCmpt && scaleCmpt.get('type');
const reversed = scaleCmpt && scaleCmpt.get('reverse'); // scale parsing sets this flag for fieldDef.sort
const scaleType = scaleCmpt?.get('type');
const reversed = scaleCmpt?.get('reverse'); // scale parsing sets this flag for fieldDef.sort
const sign = !boundScales ? '' : channel === X ? (reversed ? '' : '-') : reversed ? '-' : '';
const extent = `${anchor}.extent_${channel}`;
const offset = `${sign}${delta}.${channel} / ${boundScales ? `${sizeSg}` : `span(${extent})`}`;
Expand Down
2 changes: 1 addition & 1 deletion src/compile/selection/zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function onDelta(
const signal = signals.find(s => s.name === proj.signals[boundScales ? 'data' : 'visual']);
const sizeSg = model.getSizeSignalRef(size).signal;
const scaleCmpt = model.getScaleComponent(channel);
const scaleType = scaleCmpt && scaleCmpt.get('type');
const scaleType = scaleCmpt?.get('type');
const base = boundScales ? domain(model, channel) : signal.name;
const delta = name + DELTA;
const anchor = `${name}${ANCHOR}.${channel}`;
Expand Down
4 changes: 2 additions & 2 deletions src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export function channelHasField<F extends Field>(
encoding: EncodingWithFacet<F>,
channel: keyof EncodingWithFacet<F>
): boolean {
const channelDef = encoding && encoding[channel];
const channelDef = encoding?.[channel];
if (channelDef) {
if (isArray(channelDef)) {
return some(channelDef, fieldDef => !!fieldDef.field);
Expand All @@ -352,7 +352,7 @@ export function channelHasFieldOrDatum<F extends Field>(
encoding: EncodingWithFacet<F>,
channel: keyof EncodingWithFacet<F>
): boolean {
const channelDef = encoding && encoding[channel];
const channelDef = encoding?.[channel];
if (channelDef) {
if (isArray(channelDef)) {
return some(channelDef, fieldDef => !!fieldDef.field);
Expand Down
2 changes: 1 addition & 1 deletion src/timeunit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function isBinnedTimeUnit(
}

export function isBinnedTimeUnitString(timeUnit: TimeUnit | BinnedTimeUnit | undefined): timeUnit is BinnedTimeUnit {
return timeUnit && timeUnit.startsWith('binned');
return timeUnit?.startsWith('binned');
}

export const UTC_MULTI_TIMEUNIT_INDEX = {
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ export function stringify(data: any) {
const seen: any[] = [];

return (function _stringify(node: any) {
if (node && node.toJSON && typeof node.toJSON === 'function') {
if (node?.toJSON && typeof node.toJSON === 'function') {
node = node.toJSON();
}

Expand Down

0 comments on commit 056989b

Please sign in to comment.