Skip to content

Commit

Permalink
chore: chore
Browse files Browse the repository at this point in the history
  • Loading branch information
liihuu committed Nov 22, 2023
1 parent ac27009 commit 49879f8
Show file tree
Hide file tree
Showing 30 changed files with 171 additions and 151 deletions.
27 changes: 14 additions & 13 deletions src/Chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import VisibleRange from './common/VisibleRange'
import { createId } from './common/utils/id'
import { createDom } from './common/utils/dom'
import { getPixelRatio } from './common/utils/canvas'
import { isString, isArray, isValid, merge } from './common/utils/typeChecks'
import { isString, isArray, isValid, merge, isNumber } from './common/utils/typeChecks'
import { logWarn } from './common/utils/logger'
import { formatValue } from './common/utils/format'
import { binarySearchNearest } from './common/utils/number'
Expand All @@ -41,6 +41,8 @@ import CandlePane from './pane/CandlePane'
import IndicatorPane from './pane/IndicatorPane'
import XAxisPane from './pane/XAxisPane'
import DrawPane from './pane/DrawPane'
import SeparatorPane from './pane/SeparatorPane'

import { PaneOptions, PanePosition, PANE_DEFAULT_HEIGHT, PaneIdConstants } from './pane/types'

import Axis from './component/Axis'
Expand All @@ -54,7 +56,6 @@ import { getStyles as getExtensionStyles } from './extension/styles/index'
import Event from './Event'

import { CustomApi, LayoutChildType, Options } from './Options'
import SeparatorPane from './pane/SeparatorPane'

export enum DomPosition {
Root = 'root',
Expand Down Expand Up @@ -389,7 +390,7 @@ export default class ChartImp implements Chart {
let shouldMeasureHeight = false
if (pane !== null) {
let shouldAdjust = forceShouldAdjust
if (options.id !== PaneIdConstants.CANDLE && options.height !== undefined && options.height > 0) {
if (options.id !== PaneIdConstants.CANDLE && isNumber(options.height) && options.height > 0) {
const minHeight = Math.max(options.minHeight ?? pane.getOptions().minHeight, 0)
const height = Math.max(minHeight, options.height)
pane.setBounding({ height })
Expand Down Expand Up @@ -485,7 +486,7 @@ export default class ChartImp implements Chart {
})
indicatorData[id] = paneIndicatorData
})
if (crosshair.paneId !== undefined) {
if (isString(crosshair.paneId)) {
actionStore.execute(ActionType.OnCrosshairChange, {
...crosshair,
indicatorData
Expand All @@ -495,7 +496,7 @@ export default class ChartImp implements Chart {
}

getDom (paneId?: string, position?: DomPosition): Nullable<HTMLElement> {
if (isValid(paneId)) {
if (isString(paneId)) {
const pane = this.getDrawPaneById(paneId)
if (pane !== null) {
const pos = position ?? DomPosition.Root
Expand Down Expand Up @@ -806,7 +807,7 @@ export default class ChartImp implements Chart {

removeOverlay (remove?: string | OverlayRemove): void {
let overlayRemove
if (remove !== undefined) {
if (isValid(remove)) {
if (isString(remove)) {
overlayRemove = { id: remove }
} else {
Expand Down Expand Up @@ -837,7 +838,7 @@ export default class ChartImp implements Chart {
}

scrollByDistance (distance: number, animationDuration?: number): void {
const duration = animationDuration === undefined || animationDuration < 0 ? 0 : animationDuration
const duration = isNumber(animationDuration) && animationDuration > 0 ? animationDuration : 0
const timeScaleStore = this._chartStore.getTimeScaleStore()
if (duration > 0) {
timeScaleStore.startScroll()
Expand Down Expand Up @@ -880,7 +881,7 @@ export default class ChartImp implements Chart {
}

zoomAtCoordinate (scale: number, coordinate?: Coordinate, animationDuration?: number): void {
const duration = animationDuration === undefined || animationDuration < 0 ? 0 : animationDuration
const duration = isNumber(animationDuration) && animationDuration > 0 ? animationDuration : 0
const timeScaleStore = this._chartStore.getTimeScaleStore()
if (duration > 0) {
const { bar: barSpace } = timeScaleStore.getBarSpace()
Expand Down Expand Up @@ -926,13 +927,13 @@ export default class ChartImp implements Chart {
coordinates = ps.map(point => {
const coordinate: Partial<Coordinate> = {}
let dataIndex = point.dataIndex
if (point.timestamp !== undefined) {
if (isNumber(point.timestamp)) {
dataIndex = timeScaleStore.timestampToDataIndex(point.timestamp)
}
if (dataIndex !== undefined) {
if (isNumber(dataIndex)) {
coordinate.x = xAxis?.convertToPixel(dataIndex)
}
if (point.value !== undefined) {
if (isNumber(point.value)) {
const y = yAxis?.convertToPixel(point.value)
coordinate.y = absolute ? bounding.top + y : y
}
Expand All @@ -956,12 +957,12 @@ export default class ChartImp implements Chart {
const yAxis = pane.getAxisComponent()
points = cs.map(coordinate => {
const point: Partial<Point> = {}
if (coordinate.x !== undefined) {
if (isNumber(coordinate.x)) {
const dataIndex = xAxis?.convertFromPixel(coordinate.x) ?? -1
point.dataIndex = dataIndex
point.timestamp = timeScaleStore.dataIndexToTimestamp(dataIndex) ?? undefined
}
if (coordinate.y !== undefined) {
if (isNumber(coordinate.y)) {
const y = absolute ? coordinate.y - bounding.top : coordinate.y
point.value = yAxis.convertFromPixel(y)
}
Expand Down
4 changes: 3 additions & 1 deletion src/common/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
*/

import { isFunction } from './utils/typeChecks'

export type ActionCallback = (data?: any) => void

export enum ActionType {
Expand All @@ -35,7 +37,7 @@ export default class Delegate {
}

unsubscribe (callback?: ActionCallback): void {
if (callback !== undefined) {
if (isFunction(callback)) {
const index = this._callbacks.indexOf(callback) ?? -1
if (index > -1) {
this._callbacks.splice(index, 1)
Expand Down
4 changes: 2 additions & 2 deletions src/common/Bounding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* limitations under the License.
*/

import { merge } from './utils/typeChecks'
import { isValid, merge } from './utils/typeChecks'

export default interface Bounding {
width: number
Expand All @@ -32,7 +32,7 @@ export function getDefaultBounding (bounding?: Partial<Bounding>): Bounding {
top: 0,
bottom: 0
}
if (bounding !== undefined) {
if (isValid(bounding)) {
merge(defaultBounding, bounding)
}
return defaultBounding
Expand Down
4 changes: 3 additions & 1 deletion src/common/Eventful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
*/

import { isValid } from './utils/typeChecks'

import { EventName, MouseTouchEvent, MouseTouchEventCallback } from './SyntheticEvent'

export interface EventDispatcher {
Expand All @@ -30,7 +32,7 @@ export default abstract class Eventful implements EventDispatcher {

onEvent (name: EventName, event: MouseTouchEvent, other?: number): boolean {
const callback = this._callbacks.get(name)
if (callback !== undefined && this.checkEventOn(event)) {
if (isValid(callback) && this.checkEventOn(event)) {
return callback(event, other)
}
return false
Expand Down
21 changes: 11 additions & 10 deletions src/common/SyntheticEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Coordinate from './Coordinate'
import Nullable from './Nullable'

import { isFF, isIOS } from './utils/platform'
import { isValid } from './utils/typeChecks'

export type MouseTouchEventCallback = (event: MouseTouchEvent, other?: number) => boolean

Expand Down Expand Up @@ -286,7 +287,7 @@ export default class SyntheticEvent {

private _mouseWheelHandler (wheelEvent: WheelEvent): void {
if (Math.abs(wheelEvent.deltaX) > Math.abs(wheelEvent.deltaY)) {
if (this._handler.mouseWheelHortEvent === undefined) {
if (!isValid(this._handler.mouseWheelHortEvent)) {
return
}
this._preventDefault(wheelEvent)
Expand All @@ -295,7 +296,7 @@ export default class SyntheticEvent {
}
this._handler.mouseWheelHortEvent(this._makeCompatEvent(wheelEvent), -wheelEvent.deltaX)
} else {
if (this._handler.mouseWheelVertEvent === undefined) {
if (!isValid(this._handler.mouseWheelVertEvent)) {
return
}
let deltaY = -(wheelEvent.deltaY / 100)
Expand Down Expand Up @@ -493,7 +494,7 @@ export default class SyntheticEvent {

// do not fire mouse events if tap handler was executed
// prevent click event on new dom element (who appeared after tap)
if (this._handler.tapEvent !== undefined) {
if (isValid(this._handler.tapEvent)) {
this._preventDefault(touchEndEvent)
}
}
Expand Down Expand Up @@ -733,9 +734,9 @@ export default class SyntheticEvent {
}

private _initPinch (): void {
if (this._handler.pinchStartEvent === undefined &&
this._handler.pinchEvent === undefined &&
this._handler.pinchEndEvent === undefined
if (!isValid(this._handler.pinchStartEvent) &&
!isValid(this._handler.pinchEvent) &&
!isValid(this._handler.pinchEndEvent)
) {
return
}
Expand All @@ -752,7 +753,7 @@ export default class SyntheticEvent {
if (event.touches.length !== 2 || this._startPinchMiddleCoordinate === null) {
return
}
if (this._handler.pinchEvent !== undefined) {
if (isValid(this._handler.pinchEvent)) {
const currentDistance = this._getTouchDistance(event.touches[0], event.touches[1])
const scale = currentDistance / this._startPinchDistance
this._handler.pinchEvent({ ...this._startPinchMiddleCoordinate, pageX: 0, pageY: 0 }, scale)
Expand Down Expand Up @@ -788,7 +789,7 @@ export default class SyntheticEvent {

this._startPinchDistance = this._getTouchDistance(touches[0], touches[1])

if (this._handler.pinchStartEvent !== undefined) {
if (isValid(this._handler.pinchStartEvent)) {
this._handler.pinchStartEvent({ x: 0, y: 0, pageX: 0, pageY: 0 })
}

Expand All @@ -802,7 +803,7 @@ export default class SyntheticEvent {

this._startPinchMiddleCoordinate = null

if (this._handler.pinchEndEvent !== undefined) {
if (isValid(this._handler.pinchEndEvent)) {
this._handler.pinchEndEvent({ x: 0, y: 0, pageX: 0, pageY: 0 })
}
}
Expand Down Expand Up @@ -843,7 +844,7 @@ export default class SyntheticEvent {

private _firesTouchEvents (e: MouseEvent): boolean {
// @ts-expect-error
if (e.sourceCapabilities?.firesTouchEvents !== undefined) {
if (isValid(e.sourceCapabilities?.firesTouchEvents)) {
// @ts-expect-error
return e.sourceCapabilities.firesTouchEvents
}
Expand Down
4 changes: 3 additions & 1 deletion src/common/utils/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* limitations under the License.
*/

import { isValid } from './typeChecks'

let measureCtx: CanvasRenderingContext2D

/**
Expand All @@ -33,7 +35,7 @@ export function createFont (size?: number, weight?: string | number, family?: st
* @returns {number}
*/
export function calcTextWidth (text: string, size?: number, weight?: string | number, family?: string): number {
if (measureCtx === undefined) {
if (!isValid(measureCtx)) {
const canvas = document.createElement('canvas')
const pixelRatio = getPixelRatio(canvas)
measureCtx = canvas.getContext('2d') as CanvasRenderingContext2D
Expand Down
27 changes: 13 additions & 14 deletions src/component/Overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import BarSpace from '../common/BarSpace'
import Precision from '../common/Precision'
import { OverlayStyle } from '../common/Styles'
import { MouseTouchEvent } from '../common/SyntheticEvent'

import { clone, isNumber, merge } from '../common/utils/typeChecks'
import { clone, isNumber, isValid, merge } from '../common/utils/typeChecks'

import TimeScaleStore from '../store/TimeScaleStore'

Expand Down Expand Up @@ -376,15 +375,15 @@ export default abstract class OverlayImp implements Overlay {
}

setId (id: string): boolean {
if (this.id === undefined) {
if (!isValid(this.id)) {
this.id = id
return true
}
return false
}

setGroupId (groupId: string): boolean {
if (this.groupId === undefined) {
if (!isValid(this.groupId)) {
this.groupId = groupId
return true
}
Expand Down Expand Up @@ -623,13 +622,13 @@ export default abstract class OverlayImp implements Overlay {
eventMoveForDrawing (point: Partial<Point>): void {
const pointIndex = this.currentStep - 1
const newPoint: Partial<Point> = {}
if (point.timestamp !== undefined) {
if (isNumber(point.timestamp)) {
newPoint.timestamp = point.timestamp
}
if (point.dataIndex !== undefined) {
if (isNumber(point.dataIndex)) {
newPoint.dataIndex = point.dataIndex
}
if (point.value !== undefined) {
if (isNumber(point.value)) {
newPoint.value = point.value
}
this.points[pointIndex] = newPoint
Expand All @@ -643,11 +642,11 @@ export default abstract class OverlayImp implements Overlay {
}

eventPressedPointMove (point: Partial<Point>, pointIndex: number): void {
if (point.dataIndex !== undefined) {
if (isNumber(point.dataIndex)) {
this.points[pointIndex].dataIndex = point.dataIndex
this.points[pointIndex].timestamp = point.timestamp
}
if (point.value !== undefined) {
if (isNumber(point.value)) {
this.points[pointIndex].value = point.value
}
this.performEventPressedMove?.({
Expand All @@ -667,23 +666,23 @@ export default abstract class OverlayImp implements Overlay {
eventPressedOtherMove (point: Partial<Point>, timeScaleStore: TimeScaleStore): void {
if (this._prevPressedPoint !== null) {
let difDataIndex: number
if (point.dataIndex !== undefined && this._prevPressedPoint.dataIndex !== undefined) {
if (isNumber(point.dataIndex) && isNumber(this._prevPressedPoint.dataIndex)) {
difDataIndex = point.dataIndex - this._prevPressedPoint.dataIndex
}
let difValue: number
if (point.value !== undefined && this._prevPressedPoint.value !== undefined) {
if (isNumber(point.value) && isNumber(this._prevPressedPoint.value)) {
difValue = point.value - this._prevPressedPoint.value
}
this.points = this._prevPressedPoints.map(p => {
if (p.dataIndex === undefined && p.timestamp !== undefined) {
if (isNumber(p.dataIndex) && isNumber(p.timestamp)) {
p.dataIndex = timeScaleStore.timestampToDataIndex(p.timestamp)
}
const newPoint = { ...p }
if (difDataIndex !== undefined && p.dataIndex !== undefined) {
if (isNumber(difDataIndex) && isNumber(p.dataIndex)) {
newPoint.dataIndex = p.dataIndex + difDataIndex
newPoint.timestamp = timeScaleStore.dataIndexToTimestamp(newPoint.dataIndex) ?? undefined
}
if (difValue !== undefined && p.value !== undefined) {
if (isNumber(difValue) && isNumber(p.value)) {
newPoint.value = p.value + difValue
}
return newPoint
Expand Down
7 changes: 4 additions & 3 deletions src/component/XAxis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
*/

import Nullable from '../common/Nullable'
import { calcTextWidth } from '../common/utils/canvas'
import { isValid } from '../common/utils/typeChecks'

import { FormatDate, FormatDateType } from '../Options'

import AxisImp, { Axis, AxisExtremum, AxisTick } from './Axis'

import { calcTextWidth } from '../common/utils/canvas'

export type XAxis = Axis

export default class XAxisImp extends AxisImp {
Expand Down Expand Up @@ -75,7 +76,7 @@ export default class XAxisImp extends AxisImp {
} else {
const firstTimestamp = optimalTicks[0].value as number
const secondTimestamp = optimalTicks[1].value as number
if (optimalTicks[2] !== undefined) {
if (isValid(optimalTicks[2])) {
const thirdText = optimalTicks[2].text
if (/^[0-9]{2}-[0-9]{2}$/.test(thirdText)) {
optimalTicks[0].text = formatDate(dateTimeFormat, firstTimestamp, 'MM-DD', FormatDateType.XAxis)
Expand Down
Loading

0 comments on commit 49879f8

Please sign in to comment.