Skip to content

Commit d1e118a

Browse files
authored
refactor: move batch of helpers to ts (#10722)
* refactor: move batch of helpers to ts * refactor: review fixes
1 parent 52cf8e8 commit d1e118a

19 files changed

+204
-240
lines changed

src/core/core.animation.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const interpolators = {
77
boolean(from, to, factor) {
88
return factor > 0.5 ? to : from;
99
},
10+
/**
11+
* @param {string} from
12+
* @param {string} to
13+
* @param {number} factor
14+
*/
1015
color(from, to, factor) {
1116
const c0 = helpersColor(from || transparent);
1217
const c1 = c0.valid && helpersColor(to || transparent);

src/core/core.datasetController.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,31 +1030,19 @@ export default class DatasetController {
10301030
this.chart._dataChanges.push([this.index, ...args]);
10311031
}
10321032

1033-
/**
1034-
* @private
1035-
*/
10361033
_onDataPush() {
10371034
const count = arguments.length;
10381035
this._sync(['_insertElements', this.getDataset().data.length - count, count]);
10391036
}
10401037

1041-
/**
1042-
* @private
1043-
*/
10441038
_onDataPop() {
10451039
this._sync(['_removeElements', this._cachedMeta.data.length - 1, 1]);
10461040
}
10471041

1048-
/**
1049-
* @private
1050-
*/
10511042
_onDataShift() {
10521043
this._sync(['_removeElements', 0, 1]);
10531044
}
10541045

1055-
/**
1056-
* @private
1057-
*/
10581046
_onDataSplice(start, count) {
10591047
if (count) {
10601048
this._sync(['_removeElements', start, count]);
@@ -1065,9 +1053,6 @@ export default class DatasetController {
10651053
}
10661054
}
10671055

1068-
/**
1069-
* @private
1070-
*/
10711056
_onDataUnshift() {
10721057
this._sync(['_insertElements', 0, arguments.length]);
10731058
}

src/elements/element.arc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {PI, _isBetween, _limitValue} from '../helpers/helpers.math';
44
import {_readValueToProps} from '../helpers/helpers.options';
55

66
/** @typedef {{ x: number, y: number, startAngle: number, endAngle: number, innerRadius: number, outerRadius: number, circumference: number }} ArcProps */
7+
/** @typedef {import('../../types/geometric').Point} Point */
78

89
function clipArc(ctx, element, endAngle) {
910
const {startAngle, pixelMargin, x, y, outerRadius, innerRadius} = element;
@@ -310,7 +311,7 @@ export default class ArcElement extends Element {
310311
* @param {boolean} [useFinalPosition]
311312
*/
312313
inRange(chartX, chartY, useFinalPosition) {
313-
const point = this.getProps(['x', 'y'], useFinalPosition);
314+
const point = /** @type {Point} */ (this.getProps(['x', 'y'], useFinalPosition));
314315
const {angle, distance} = getAngleFromPoint(point, {x: chartX, y: chartY});
315316
const {startAngle, endAngle, innerRadius, outerRadius, circumference} = /** @type {ArcProps} */ (this.getProps([
316317
'startAngle',

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

Lines changed: 56 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@ import {_capitalize} from './helpers.core';
22

33
/**
44
* Binary search
5-
* @param {array} table - the table search. must be sorted!
6-
* @param {number} value - value to find
7-
* @param {function} [cmp]
5+
* @param table - the table search. must be sorted!
6+
* @param value - value to find
7+
* @param cmp
88
* @private
99
*/
10-
export function _lookup(table, value, cmp) {
10+
export function _lookup(
11+
table: number[],
12+
value: number,
13+
cmp?: (value: number) => boolean
14+
): {lo: number, hi: number};
15+
export function _lookup<T>(
16+
table: T[],
17+
value: number,
18+
cmp: (value: number) => boolean
19+
): {lo: number, hi: number};
20+
export function _lookup(
21+
table: unknown[],
22+
value: number,
23+
cmp?: (value: number) => boolean
24+
) {
1125
cmp = cmp || ((index) => table[index] < value);
1226
let hi = table.length - 1;
1327
let lo = 0;
14-
let mid;
28+
let mid: number;
1529

1630
while (hi - lo > 1) {
1731
mid = (lo + hi) >> 1;
@@ -27,13 +41,18 @@ export function _lookup(table, value, cmp) {
2741

2842
/**
2943
* Binary search
30-
* @param {array} table - the table search. must be sorted!
31-
* @param {string} key - property name for the value in each entry
32-
* @param {number} value - value to find
33-
* @param {boolean} [last] - lookup last index
44+
* @param table - the table search. must be sorted!
45+
* @param key - property name for the value in each entry
46+
* @param value - value to find
47+
* @param last - lookup last index
3448
* @private
3549
*/
36-
export const _lookupByKey = (table, key, value, last) =>
50+
export const _lookupByKey = (
51+
table: Record<string, number>[],
52+
key: string,
53+
value: number,
54+
last?: boolean
55+
) =>
3756
_lookup(table, value, last
3857
? index => {
3958
const ti = table[index][key];
@@ -43,22 +62,26 @@ export const _lookupByKey = (table, key, value, last) =>
4362

4463
/**
4564
* Reverse binary search
46-
* @param {array} table - the table search. must be sorted!
47-
* @param {string} key - property name for the value in each entry
48-
* @param {number} value - value to find
65+
* @param table - the table search. must be sorted!
66+
* @param key - property name for the value in each entry
67+
* @param value - value to find
4968
* @private
5069
*/
51-
export const _rlookupByKey = (table, key, value) =>
70+
export const _rlookupByKey = (
71+
table: Record<string, number>[],
72+
key: string,
73+
value: number
74+
) =>
5275
_lookup(table, value, index => table[index][key] >= value);
5376

5477
/**
5578
* Return subset of `values` between `min` and `max` inclusive.
5679
* Values are assumed to be in sorted order.
57-
* @param {number[]} values - sorted array of values
58-
* @param {number} min - min value
59-
* @param {number} max - max value
80+
* @param values - sorted array of values
81+
* @param min - min value
82+
* @param max - max value
6083
*/
61-
export function _filterBetween(values, min, max) {
84+
export function _filterBetween(values: number[], min: number, max: number) {
6285
let start = 0;
6386
let end = values.length;
6487

@@ -74,13 +97,22 @@ export function _filterBetween(values, min, max) {
7497
: values;
7598
}
7699

77-
const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift'];
100+
const arrayEvents = ['push', 'pop', 'shift', 'splice', 'unshift'] as const;
101+
102+
export interface ArrayListener<T> {
103+
_onDataPush?(...item: T[]): void;
104+
_onDataPop?(): void;
105+
_onDataShift?(): void;
106+
_onDataSplice?(index: number, deleteCount: number, ...items: T[]): void;
107+
_onDataUnshift?(...item: T[]): void;
108+
}
78109

79110
/**
80111
* Hooks the array methods that add or remove values ('push', pop', 'shift', 'splice',
81112
* 'unshift') and notify the listener AFTER the array has been altered. Listeners are
82113
* called on the '_onData*' callbacks (e.g. _onDataPush, etc.) with same arguments.
83114
*/
115+
export function listenArrayEvents<T>(array: T[], listener: ArrayListener<T>): void;
84116
export function listenArrayEvents(array, listener) {
85117
if (array._chartjs) {
86118
array._chartjs.listeners.push(listener);
@@ -122,6 +154,7 @@ export function listenArrayEvents(array, listener) {
122154
* Removes the given array event listener and cleanup extra attached properties (such as
123155
* the _chartjs stub and overridden methods) if array doesn't have any more listeners.
124156
*/
157+
export function unlistenArrayEvents<T>(array: T[], listener: ArrayListener<T>): void;
125158
export function unlistenArrayEvents(array, listener) {
126159
const stub = array._chartjs;
127160
if (!stub) {
@@ -146,11 +179,11 @@ export function unlistenArrayEvents(array, listener) {
146179
}
147180

148181
/**
149-
* @param {Array} items
182+
* @param items
150183
*/
151-
export function _arrayUnique(items) {
152-
const set = new Set();
153-
let i, ilen;
184+
export function _arrayUnique<T>(items: T[]) {
185+
const set = new Set<T>();
186+
let i: number, ilen: number;
154187

155188
for (i = 0, ilen = items.length; i < ilen; ++i) {
156189
set.add(items[i]);

src/helpers/helpers.color.js

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

src/helpers/helpers.color.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import colorLib, {Color} from '@kurkle/color';
2+
3+
export function isPatternOrGradient(value: unknown): value is CanvasPattern | CanvasGradient {
4+
if (value && typeof value === 'object') {
5+
const type = value.toString();
6+
return type === '[object CanvasPattern]' || type === '[object CanvasGradient]';
7+
}
8+
9+
return false;
10+
}
11+
12+
export function color(value: CanvasGradient): CanvasGradient;
13+
export function color(value: CanvasPattern): CanvasPattern;
14+
export function color(
15+
value:
16+
| string
17+
| { r: number; g: number; b: number; a: number }
18+
| [number, number, number]
19+
| [number, number, number, number]
20+
): Color;
21+
export function color(value) {
22+
return isPatternOrGradient(value) ? value : colorLib(value);
23+
}
24+
25+
export function getHoverColor(value: CanvasGradient): CanvasGradient;
26+
export function getHoverColor(value: CanvasPattern): CanvasPattern;
27+
export function getHoverColor(value: string): string;
28+
export function getHoverColor(value) {
29+
return isPatternOrGradient(value)
30+
? value
31+
: colorLib(value).saturate(0.5).darken(0.1).hexString();
32+
}

src/helpers/helpers.interpolation.js

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type {Point} from '../../types/geometric';
2+
import type {MonotoneSplinePoint} from '../../types/helpers';
3+
4+
/**
5+
* @private
6+
*/
7+
export function _pointInLine(p1: Point, p2: Point, t: number, mode?) { // eslint-disable-line @typescript-eslint/no-unused-vars
8+
return {
9+
x: p1.x + t * (p2.x - p1.x),
10+
y: p1.y + t * (p2.y - p1.y)
11+
};
12+
}
13+
14+
/**
15+
* @private
16+
*/
17+
export function _steppedInterpolation(
18+
p1: Point,
19+
p2: Point,
20+
t: number, mode: 'middle' | 'after' | unknown
21+
) {
22+
return {
23+
x: p1.x + t * (p2.x - p1.x),
24+
y: mode === 'middle' ? t < 0.5 ? p1.y : p2.y
25+
: mode === 'after' ? t < 1 ? p1.y : p2.y
26+
: t > 0 ? p2.y : p1.y
27+
};
28+
}
29+
30+
/**
31+
* @private
32+
*/
33+
export function _bezierInterpolation(p1: MonotoneSplinePoint, p2: MonotoneSplinePoint, t: number, mode?) { // eslint-disable-line @typescript-eslint/no-unused-vars
34+
const cp1 = {x: p1.cp2x, y: p1.cp2y};
35+
const cp2 = {x: p2.cp1x, y: p2.cp1y};
36+
const a = _pointInLine(p1, cp1, t);
37+
const b = _pointInLine(cp1, cp2, t);
38+
const c = _pointInLine(cp2, p2, t);
39+
const d = _pointInLine(a, b, t);
40+
const e = _pointInLine(b, c, t);
41+
return _pointInLine(d, e, t);
42+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

2-
const intlCache = new Map();
2+
const intlCache = new Map<string, Intl.NumberFormat>();
33

4-
function getNumberFormat(locale, options) {
4+
function getNumberFormat(locale: string, options?: Intl.NumberFormatOptions) {
55
options = options || {};
66
const cacheKey = locale + JSON.stringify(options);
77
let formatter = intlCache.get(cacheKey);
@@ -12,6 +12,6 @@ function getNumberFormat(locale, options) {
1212
return formatter;
1313
}
1414

15-
export function formatNumber(num, locale, options) {
15+
export function formatNumber(num: number, locale: string, options?: Intl.NumberFormatOptions) {
1616
return getNumberFormat(locale, options).format(num);
1717
}

0 commit comments

Comments
 (0)