Skip to content

Commit e6892a9

Browse files
authored
Convert helpers.extra to TS (#10728)
Co-authored-by: Chart.js <>
1 parent 1d6b8f3 commit e6892a9

File tree

5 files changed

+21
-60
lines changed

5 files changed

+21
-60
lines changed

src/helpers/helpers.extras.js renamed to src/helpers/helpers.extras.ts

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import {type ChartMeta, type PointElement} from '../../types';
2+
13
import {_limitValue} from './helpers.math';
24
import {_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 {

src/helpers/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export * from './helpers.color';
88
export * from './helpers.collection';
99
export * from './helpers.core';
1010
export * from './helpers.easing';
11+
export * from './helpers.extras';
1112
export * from './helpers.interpolation';
1213
export * from './helpers.intl';
1314
export * from './helpers.math';

src/platform/platform.dom.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,7 @@ function createProxyAndListen(chart, type, listener) {
242242
if (chart.ctx !== null) {
243243
listener(fromNativeEvent(event, chart));
244244
}
245-
}, chart, (args) => {
246-
const event = args[0];
247-
return [event, event.offsetX, event.offsetY];
248-
});
245+
}, chart);
249246

250247
addListener(canvas, type, proxy);
251248

types/helpers/helpers.extras.d.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

types/helpers/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export * from './helpers.canvas';
22
export * from './helpers.curve';
33
export * from './helpers.dom';
4-
export * from './helpers.extras';
54
export * from './helpers.options';
65
export * from './helpers.canvas';
76
export * from './helpers.segment';

0 commit comments

Comments
 (0)