Skip to content

Commit

Permalink
style: using utility methods for type checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Jan 4, 2022
1 parent 951b044 commit 53babd2
Show file tree
Hide file tree
Showing 53 changed files with 153 additions and 194 deletions.
7 changes: 4 additions & 3 deletions src/animation/basicTrasition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { AnimationEasing } from 'zrender/src/animation/easing';
import Element, { ElementAnimateConfig } from 'zrender/src/Element';
import Model from '../model/Model';
import {
isFunction,
isObject,
retrieve2
} from 'zrender/src/core/util';
Expand Down Expand Up @@ -101,13 +102,13 @@ export function getAnimationConfig(
animationPayload.easing != null && (easing = animationPayload.easing);
animationPayload.delay != null && (delay = animationPayload.delay);
}
if (typeof delay === 'function') {
if (isFunction(delay)) {
delay = delay(
dataIndex as number,
extraDelayParams
);
}
if (typeof duration === 'function') {
if (isFunction(duration)) {
duration = duration(dataIndex as number);
}
const config = {
Expand Down Expand Up @@ -136,7 +137,7 @@ function animateOrSetProps<Props>(
) {
let isFrom = false;
let removeOpt: AnimationOption;
if (typeof dataIndex === 'function') {
if (isFunction(dataIndex)) {
during = cb;
cb = dataIndex;
dataIndex = null;
Expand Down
5 changes: 3 additions & 2 deletions src/chart/funnel/funnelLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import FunnelSeriesModel, { FunnelSeriesOption, FunnelDataItemOption } from './F
import ExtensionAPI from '../../core/ExtensionAPI';
import SeriesData from '../../data/SeriesData';
import GlobalModel from '../../model/Global';
import { isFunction } from 'zrender/src/core/util';

function getViewRect(seriesModel: FunnelSeriesModel, api: ExtensionAPI) {
return layout.getLayoutRect(
Expand All @@ -45,8 +46,8 @@ function getSortedIndices(data: SeriesData, sort: FunnelSeriesOption['sort']) {
}

// Add custom sortable function & none sortable opetion by "options.sort"
if (typeof sort === 'function') {
indices.sort(sort);
if (isFunction(sort)) {
indices.sort(sort as any);
}
else if (sort !== 'none') {
indices.sort(function (a, b) {
Expand Down
6 changes: 3 additions & 3 deletions src/chart/gauge/GaugeView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import SeriesData from '../../data/SeriesData';
import Sausage from '../../util/shape/sausage';
import {createSymbol} from '../../util/symbol';
import ZRImage from 'zrender/src/graphic/Image';
import {extend} from 'zrender/src/core/util';
import {extend, isFunction, isString} from 'zrender/src/core/util';
import {setCommonECData} from '../../util/innerStore';

type ECSymbol = ReturnType<typeof createSymbol>;
Expand Down Expand Up @@ -61,10 +61,10 @@ function parsePosition(seriesModel: GaugeSeriesModel, api: ExtensionAPI): PosInf
function formatLabel(value: number, labelFormatter: string | ((value: number) => string)): string {
let label = value == null ? '' : (value + '');
if (labelFormatter) {
if (typeof labelFormatter === 'string') {
if (isString(labelFormatter)) {
label = labelFormatter.replace('{value}', label);
}
else if (typeof labelFormatter === 'function') {
else if (isFunction(labelFormatter)) {
label = labelFormatter(value);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/chart/graph/categoryFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import GlobalModel from '../../model/Global';
import GraphSeriesModel, { GraphNodeItemOption } from './GraphSeries';
import type LegendModel from '../../component/legend/LegendModel';
import { isNumber } from 'zrender/src/core/util';

export default function categoryFilter(ecModel: GlobalModel) {
const legendModels = ecModel.findComponents({
Expand All @@ -39,7 +40,7 @@ export default function categoryFilter(ecModel: GlobalModel) {
const model = data.getItemModel<GraphNodeItemOption>(idx);
let category = model.getShallow('category');
if (category != null) {
if (typeof category === 'number') {
if (isNumber(category)) {
category = categoryNames[category];
}
// If in any legend component the status is not selected.
Expand Down
4 changes: 2 additions & 2 deletions src/chart/graph/categoryVisual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import GlobalModel from '../../model/Global';
import GraphSeriesModel, { GraphNodeItemOption } from './GraphSeries';
import { Dictionary, ColorString } from '../../util/types';
import { extend } from 'zrender/src/core/util';
import { extend, isString } from 'zrender/src/core/util';

export default function categoryVisual(ecModel: GlobalModel) {

Expand Down Expand Up @@ -60,7 +60,7 @@ export default function categoryVisual(ecModel: GlobalModel) {
const model = data.getItemModel<GraphNodeItemOption>(idx);
let categoryIdx = model.getShallow('category');
if (categoryIdx != null) {
if (typeof categoryIdx === 'string') {
if (isString(categoryIdx)) {
categoryIdx = categoryNameIdxMap['ec-' + categoryIdx];
}

Expand Down
2 changes: 1 addition & 1 deletion src/chart/helper/EffectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class EffectLine extends graphic.Group {

if (period > 0) {
let delayNum: number;
if (typeof delayExpr === 'function') {
if (zrUtil.isFunction(delayExpr)) {
delayNum = delayExpr(idx);
}
else {
Expand Down
3 changes: 2 additions & 1 deletion src/chart/helper/createClipPathFromCoordSys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { SeriesOption } from '../../util/types';
import type Cartesian2D from '../../coord/cartesian/Cartesian2D';
import type Polar from '../../coord/polar/Polar';
import { CoordinateSystem } from '../../coord/CoordinateSystem';
import { isFunction } from 'zrender/src/core/util';

type SeriesModelWithLineWidth = SeriesModel<SeriesOption & {
lineStyle?: { width?: number }
Expand Down Expand Up @@ -80,7 +81,7 @@ function createGridClipPath(
clipPath.shape.height = 0;
}

const duringCb = typeof during === 'function'
const duringCb = isFunction(during)
? (percent: number) => {
during(percent, clipPath);
}
Expand Down
2 changes: 1 addition & 1 deletion src/chart/helper/multipleGraphEdgeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const createCurveness = function (seriesModel, appendLength) {
let curvenessList = [];

// handler the function set
if (typeof autoCurvenessParmas === 'number') {
if (zrUtil.isNumber(autoCurvenessParmas)) {
length = autoCurvenessParmas;
}
else if (zrUtil.isArray(autoCurvenessParmas)) {
Expand Down
2 changes: 1 addition & 1 deletion src/chart/helper/treeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function retrieveTargetInfo(
const root = seriesModel.getData().tree.root;
let targetNode = payload.targetNode;

if (typeof targetNode === 'string') {
if (zrUtil.isString(targetNode)) {
targetNode = root.getNodeById(targetNode);
}

Expand Down
8 changes: 4 additions & 4 deletions src/chart/line/LineView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function getBoundingDiff(points1: ArrayLike<number>, points2: ArrayLike<number>)
}

function getSmooth(smooth: number | boolean) {
return typeof smooth === 'number' ? smooth : (smooth ? 0.5 : 0);
return zrUtil.isNumber(smooth) ? smooth : (smooth ? 0.5 : 0);
}

function getStackedOnPoints(
Expand Down Expand Up @@ -1064,11 +1064,11 @@ class LineView extends ChartView {

const seriesModel = data.hostModel;
let seriesDuration = seriesModel.get('animationDuration');
if (typeof seriesDuration === 'function') {
if (zrUtil.isFunction(seriesDuration)) {
seriesDuration = seriesDuration(null);
}
const seriesDalay = seriesModel.get('animationDelay') || 0;
const seriesDalayValue = typeof seriesDalay === 'function'
const seriesDalayValue = zrUtil.isFunction(seriesDalay)
? seriesDalay(null)
: seriesDalay;

Expand Down Expand Up @@ -1113,7 +1113,7 @@ class LineView extends ChartView {
ratio = 1 - ratio;
}

const delay = typeof seriesDalay === 'function' ? seriesDalay(idx)
const delay = zrUtil.isFunction(seriesDalay) ? seriesDalay(idx)
: (seriesDuration * ratio) + seriesDalayValue;

const symbolPath = el.getSymbolPath();
Expand Down
4 changes: 2 additions & 2 deletions src/chart/lines/LinesSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import SeriesModel from '../../model/Series';
import SeriesData from '../../data/SeriesData';
import { concatArray, mergeAll, map } from 'zrender/src/core/util';
import { concatArray, mergeAll, map, isNumber } from 'zrender/src/core/util';
import CoordinateSystem from '../../core/CoordinateSystem';
import {
SeriesOption,
Expand Down Expand Up @@ -259,7 +259,7 @@ class LinesSeriesModel extends SeriesModel<LinesSeriesOption> {
}
// Stored as a typed array. In format
// Points Count(2) | x | y | x | y | Points Count(3) | x | y | x | y | x | y |
if (typeof data[0] === 'number') {
if (isNumber(data[0])) {
const len = data.length;
// Store offset and len of each segment
const coordsOffsetAndLenStorage = new Uint32Arr(len) as Uint32Array;
Expand Down
4 changes: 2 additions & 2 deletions src/chart/pie/labelLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { HorizontalAlign, ZRTextAlign } from '../../util/types';
import { Sector, Polyline, Point } from '../../util/graphic';
import ZRText from 'zrender/src/graphic/Text';
import BoundingRect, {RectLike} from 'zrender/src/core/BoundingRect';
import { each } from 'zrender/src/core/util';
import { each, isNumber } from 'zrender/src/core/util';
import { limitTurnAngle, limitSurfaceAngle } from '../../label/labelGuideHelper';
import { shiftLayoutOnY } from '../../label/labelLayoutHelper';

Expand Down Expand Up @@ -352,7 +352,7 @@ export default function pieLabelLayout(

let labelRotate;
const rotate = labelModel.get('rotate');
if (typeof rotate === 'number') {
if (isNumber(rotate)) {
labelRotate = rotate * (Math.PI / 180);
}
else if (labelPosition === 'center') {
Expand Down
19 changes: 11 additions & 8 deletions src/chart/sankey/SankeyView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import SeriesData from '../../data/SeriesData';
import { RectLike } from 'zrender/src/core/BoundingRect';
import { setLabelStyle, getLabelStatesModels } from '../../label/labelStyle';
import { getECData } from '../../util/innerStore';
import { isString } from 'zrender/src/core/util';

class SankeyPathShape {
x1 = 0;
Expand Down Expand Up @@ -206,14 +207,16 @@ class SankeyView extends ChartView {
case 'gradient':
const sourceColor = edge.node1.getVisual('color');
const targetColor = edge.node2.getVisual('color');
if (typeof sourceColor === 'string' && typeof targetColor === 'string') {
curve.style.fill = new graphic.LinearGradient(0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
color: sourceColor,
offset: 0
}, {
color: targetColor,
offset: 1
}]);
if (isString(sourceColor) && isString(targetColor)) {
curve.style.fill = new graphic.LinearGradient(
0, 0, +(orient === 'horizontal'), +(orient === 'vertical'), [{
color: sourceColor,
offset: 0
}, {
color: targetColor,
offset: 1
}]
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/chart/sunburst/SunburstPiece.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class SunburstPiece extends graphic.Sector {
rotate += Math.PI;
}
}
else if (typeof rotateType === 'number') {
else if (zrUtil.isNumber(rotateType)) {
rotate = rotateType * Math.PI / 180;
}

Expand Down
2 changes: 1 addition & 1 deletion src/chart/sunburst/sunburstLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ function initChildren(node: TreeNode, sortOrder?: SunburstSeriesOption['sort'])
* See SunburstSeries.js for details.
*/
function sort(children: TreeNode[], sortOrder: SunburstSeriesOption['sort']) {
if (typeof sortOrder === 'function') {
if (zrUtil.isFunction(sortOrder)) {
const sortTargets = zrUtil.map(children, (child, idx) => {
const value = child.getValue() as number;
return {
Expand Down
12 changes: 3 additions & 9 deletions src/chart/sunburst/sunburstVisual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@
*/

import { lift } from 'zrender/src/tool/color';
import { extend, map } from 'zrender/src/core/util';
import { extend, isString } from 'zrender/src/core/util';
import GlobalModel from '../../model/Global';
import SunburstSeriesModel, { SunburstSeriesNodeItemOption } from './SunburstSeries';
import { Dictionary, ColorString, ZRColor } from '../../util/types';
import { setItemVisualFromData } from '../../visual/helper';
import { Dictionary, ColorString } from '../../util/types';
import { TreeNode } from '../../data/Tree';

type ParentColorMap = {
node: TreeNode,
parentColor: ZRColor
};

export default function sunburstVisual(ecModel: GlobalModel) {

const paletteScope: Dictionary<ColorString> = {};
Expand All @@ -42,7 +36,7 @@ export default function sunburstVisual(ecModel: GlobalModel) {
current = current.parentNode;
}
let color = seriesModel.getColorFromPalette((current.name || current.dataIndex + ''), paletteScope);
if (node.depth > 1 && typeof color === 'string') {
if (node.depth > 1 && isString(color)) {
// Lighter on the deeper level.
color = lift(color, (node.depth - 1) / (treeHeight - 1) * 0.5);
}
Expand Down
12 changes: 5 additions & 7 deletions src/component/axis/AxisBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import {retrieve, defaults, extend, each, isObject, map} from 'zrender/src/core/util';
import {retrieve, defaults, extend, each, isObject, map, isString, isNumber, isFunction} from 'zrender/src/core/util';
import * as graphic from '../../util/graphic';
import {getECData} from '../../util/innerStore';
import {createTextStyle} from '../../label/labelStyle';
Expand Down Expand Up @@ -287,15 +287,13 @@ const builders: Record<'axisLine' | 'axisTickLabel' | 'axisName', AxisElementsBu
if (arrows != null) {
let arrowSize = axisModel.get(['axisLine', 'symbolSize']);

if (typeof arrows === 'string') {
if (isString(arrows)) {
// Use the same arrow for start and end point
arrows = [arrows, arrows];
}
if (typeof arrowSize === 'string'
|| typeof arrowSize === 'number'
) {
if (isString(arrowSize) || isNumber(arrowSize)) {
// Use the same size for width and height
arrowSize = [arrowSize, arrowSize];
arrowSize = [arrowSize as number, arrowSize as number];
}

const arrowOffset = normalizeSymbolOffset(axisModel.get(['axisLine', 'symbolOffset']) || 0, arrowSize);
Expand Down Expand Up @@ -789,7 +787,7 @@ function buildAxisLabel(
verticalAlign: itemLabelModel.getShallow('verticalAlign', true)
|| itemLabelModel.getShallow('baseline', true)
|| labelLayout.textVerticalAlign,
fill: typeof textColor === 'function'
fill: isFunction(textColor)
? textColor(
// (1) In category axis with data zoom, tick is not the original
// index of axis.data. So tick should not be exposed to user
Expand Down
6 changes: 3 additions & 3 deletions src/component/calendar/CalendarView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { isString, extend, map } from 'zrender/src/core/util';
import { isString, extend, map, isFunction } from 'zrender/src/core/util';
import * as graphic from '../../util/graphic';
import {createTextStyle} from '../../label/labelStyle';
import { formatTplSimple } from '../../util/format';
Expand Down Expand Up @@ -229,11 +229,11 @@ class CalendarView extends ComponentView {
params: T
) {

if (typeof formatter === 'string' && formatter) {
if (isString(formatter) && formatter) {
return formatTplSimple(formatter, params);
}

if (typeof formatter === 'function') {
if (isFunction(formatter)) {
return formatter(params);
}

Expand Down
4 changes: 2 additions & 2 deletions src/component/legend/LegendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ class LegendModel<Ops extends LegendOption = LegendOption> extends ComponentMode

const legendData = zrUtil.map(rawData, function (dataItem) {
// Can be string or number
if (typeof dataItem === 'string' || typeof dataItem === 'number') {
if (zrUtil.isString(dataItem) || zrUtil.isNumber(dataItem)) {
dataItem = {
name: dataItem
name: dataItem as string
};
}
return new Model(dataItem, this, this.ecModel);
Expand Down
6 changes: 3 additions & 3 deletions src/component/legend/LegendView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ class LegendView extends ComponentView {

const textStyleModel = legendItemModel.getModel('textStyle');

if (typeof seriesModel.getLegendIcon === 'function'
if (zrUtil.isFunction(seriesModel.getLegendIcon)
&& (!legendIconType || legendIconType === 'inherit')
) {
// Series has specific way to define legend icon
Expand Down Expand Up @@ -404,10 +404,10 @@ class LegendView extends ComponentView {

const formatter = legendModel.get('formatter');
let content = name;
if (typeof formatter === 'string' && formatter) {
if (zrUtil.isString(formatter) && formatter) {
content = formatter.replace('{name}', name != null ? name : '');
}
else if (typeof formatter === 'function') {
else if (zrUtil.isFunction(formatter)) {
content = formatter(name);
}

Expand Down
Loading

0 comments on commit 53babd2

Please sign in to comment.