1+ import { type ChartMeta , type PointElement } from '../../types' ;
2+
13import { _limitValue } from './helpers.math' ;
24import { _lookupByKey } from './helpers.collection' ;
35
4- export function fontString ( pixelSize , fontStyle , fontFamily ) {
6+ export function fontString ( pixelSize : number , fontStyle : string , fontFamily : string ) {
57 return fontStyle + ' ' + pixelSize + 'px ' + fontFamily ;
68}
79
@@ -20,18 +22,14 @@ export const requestAnimFrame = (function() {
2022/**
2123 * Throttles calling `fn` once per animation frame
2224 * Latest arguments are used on the actual call
23- * @param {function } fn
24- * @param {* } thisArg
25- * @param {function } [updateFn]
2625 */
27- export function throttled ( fn , thisArg , updateFn ) {
28- const updateArgs = updateFn || ( ( args ) => Array . prototype . slice . call ( args ) ) ;
26+ export function throttled < TArgs extends Array < any > > (
27+ fn : ( ...args : TArgs ) => void ,
28+ thisArg : any ,
29+ ) {
2930 let ticking = false ;
30- let args = [ ] ;
31-
32- return function ( ...rest ) {
33- args = updateArgs ( rest ) ;
3431
32+ return function ( ...args : TArgs ) {
3533 if ( ! ticking ) {
3634 ticking = true ;
3735 requestAnimFrame . call ( window , ( ) => {
@@ -44,61 +42,46 @@ export function throttled(fn, thisArg, updateFn) {
4442
4543/**
4644 * Debounces calling `fn` for `delay` ms
47- * @param {function } fn - Function to call.
48- * @param {number } delay - Delay in ms. 0 = immediate invocation.
49- * @returns {function }
5045 */
51- export function debounce ( fn , delay ) {
46+ export function debounce < TArgs extends Array < any > > ( fn : ( ... args : TArgs ) => void , delay : number ) {
5247 let timeout ;
53- return function ( ...args ) {
48+ return function ( ...args : TArgs ) {
5449 if ( delay ) {
5550 clearTimeout ( timeout ) ;
5651 timeout = setTimeout ( fn , delay , args ) ;
5752 } else {
58- fn . apply ( this , args ) ;
53+ fn . apply < any , TArgs , void > ( this , args ) ;
5954 }
6055 return delay ;
6156 } ;
6257}
6358
6459/**
6560 * Converts 'start' to 'left', 'end' to 'right' and others to 'center'
66- * @param {string } align start, end, center
6761 * @private
6862 */
69- export const _toLeftRightCenter = ( align ) => align === 'start' ? 'left' : align === 'end' ? 'right' : 'center' ;
63+ export const _toLeftRightCenter = ( align : 'start' | 'end' | 'center' ) => align === 'start' ? 'left' : align === 'end' ? 'right' : 'center' ;
7064
7165/**
7266 * Returns `start`, `end` or `(start + end) / 2` depending on `align`. Defaults to `center`
73- * @param {string } align start, end, center
74- * @param {number } start value for start
75- * @param {number } end value for end
7667 * @private
7768 */
78- export const _alignStartEnd = ( align , start , end ) => align === 'start' ? start : align === 'end' ? end : ( start + end ) / 2 ;
69+ export const _alignStartEnd = ( align : 'start' | 'end' | 'center' , start : number , end : number ) => align === 'start' ? start : align === 'end' ? end : ( start + end ) / 2 ;
7970
8071/**
8172 * Returns `left`, `right` or `(left + right) / 2` depending on `align`. Defaults to `left`
82- * @param {string } align start, end, center
83- * @param {number } left value for start
84- * @param {number } right value for end
85- * @param {boolean } rtl Is this an RTL draw
8673 * @private
8774 */
88- export const _textX = ( align , left , right , rtl ) => {
75+ export const _textX = ( align : 'left' | 'right' | 'center' , left : number , right : number , rtl : boolean ) => {
8976 const check = rtl ? 'left' : 'right' ;
9077 return align === check ? right : align === 'center' ? ( left + right ) / 2 : left ;
9178} ;
9279
9380/**
9481 * Return start and count of visible points.
95- * @param {object } meta - dataset meta.
96- * @param {array } points - array of point elements.
97- * @param {boolean } animationsDisabled - if true animation is disabled.
98- * @returns {{start: number; count: number} }
9982 * @private
10083 */
101- export function _getStartAndCountOfVisiblePoints ( meta , points , animationsDisabled ) {
84+ export function _getStartAndCountOfVisiblePoints ( meta : ChartMeta < 'line' | 'scatter' > , points : PointElement [ ] , animationsDisabled : boolean ) {
10285 const pointCount = points . length ;
10386
10487 let start = 0 ;
@@ -111,13 +94,17 @@ export function _getStartAndCountOfVisiblePoints(meta, points, animationsDisable
11194
11295 if ( minDefined ) {
11396 start = _limitValue ( Math . min (
97+ // @ts -expect-error Need to type _parsed
11498 _lookupByKey ( _parsed , iScale . axis , min ) . lo ,
99+ // @ts -expect-error Need to fix types on _lookupByKey
115100 animationsDisabled ? pointCount : _lookupByKey ( points , axis , iScale . getPixelForValue ( min ) ) . lo ) ,
116101 0 , pointCount - 1 ) ;
117102 }
118103 if ( maxDefined ) {
119104 count = _limitValue ( Math . max (
105+ // @ts -expect-error Need to type _parsed
120106 _lookupByKey ( _parsed , iScale . axis , max , true ) . hi + 1 ,
107+ // @ts -expect-error Need to fix types on _lookupByKey
121108 animationsDisabled ? 0 : _lookupByKey ( points , axis , iScale . getPixelForValue ( max ) , true ) . hi + 1 ) ,
122109 start , pointCount ) - start ;
123110 } else {
0 commit comments