@@ -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 ;
84116export 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 ;
125158export 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 ] ) ;
0 commit comments