UNPKG

49.1 kBTypeScriptView Raw
1// Type definitions for D3JS d3-array module 2.12
2// Project: https://github.com/d3/d3-array, https://d3js.org/d3-array
3// Definitions by: Alex Ford <https://github.com/gustavderdrache>
4// Boris Yankov <https://github.com/borisyankov>
5// Tom Wanzek <https://github.com/tomwanzek>
6// denisname <https://github.com/denisname>
7// Hugues Stefanski <https://github.com/ledragon>
8// Nathan Bierema <https://github.com/Methuselah96>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10
11// Last module patch version validated against: 2.12.1
12
13// --------------------------------------------------------------------------
14// Shared Types and Interfaces
15// --------------------------------------------------------------------------
16
17/**
18 * Administrivia: JavaScript primitive types and Date
19 */
20export type Primitive = number | string | boolean | Date;
21
22/**
23 * Administrivia: anything with a valueOf(): number method is comparable, so we allow it in numeric operations
24 */
25export interface Numeric {
26 valueOf(): number;
27}
28
29// --------------------------------------------------------------------------------------
30// Statistics
31// --------------------------------------------------------------------------------------
32
33/**
34 * Return the minimum value in the array using natural order.
35 */
36export function min(iterable: Iterable<string>): string | undefined;
37
38/**
39 * Return the minimum value in the array using natural order.
40 */
41export function min<T extends Numeric>(iterable: Iterable<T>): T | undefined;
42/**
43 * Return the minimum value in the array using natural order.
44 */
45export function min<T>(
46 iterable: Iterable<T>,
47 accessor: (datum: T, index: number, array: Iterable<T>) => string | undefined | null
48): string | undefined;
49/**
50 * Return the minimum value in the array using natural order.
51 */
52export function min<T, U extends Numeric>(
53 iterable: Iterable<T>,
54 accessor: (datum: T, index: number, array: Iterable<T>) => U | undefined | null
55): U | undefined;
56
57/**
58 * Return the index of the minimum value in the array using natural order.
59 */
60export function minIndex<T>(iterable: Iterable<T>): number;
61/**
62 * Return the index of the minimum value in the array using natural order and a projection function to map values.
63 */
64export function minIndex<TDatum, U>(
65 iterable: Iterable<TDatum>,
66 accessor: (datum: TDatum, index: number, array: Iterable<TDatum>) => U | undefined | null
67): number;
68/**
69 * Return the index of the minimum value in the array using natural order.
70 */
71export function minIndex<T>(iterable: Iterable<T>): number;
72
73/**
74 * Return the maximum value in the array of strings using natural order.
75 */
76export function max(iterable: Iterable<string>): string | undefined;
77/**
78 * Return the maximum value in the array of numbers using natural order.
79 */
80export function max<T extends Numeric>(iterable: Iterable<T>): T | undefined;
81/**
82 * Return the maximum value in the array using natural order and a projection function to map values to strings.
83 */
84export function max<T>(
85 iterable: Iterable<T>,
86 accessor: (datum: T, index: number, array: Iterable<T>) => string | undefined | null
87): string | undefined;
88/**
89 * Return the maximum value in the array using natural order and a projection function to map values to easily-sorted values.
90 */
91export function max<T, U extends Numeric>(
92 iterable: Iterable<T>,
93 accessor: (datum: T, index: number, array: Iterable<T>) => U | undefined | null
94): U | undefined;
95
96/**
97 * Return the index of the maximum value in the array using natural order.
98 */
99export function maxIndex<T>(iterable: Iterable<T>): number;
100/**
101 * Return the index of the maximum value in the array using natural order and a projection function to map values.
102 */
103export function maxIndex<TDatum, U>(
104 iterable: Iterable<TDatum>,
105 accessor: (datum: TDatum, index: number, array: Iterable<TDatum>) => U | undefined | null
106): number;
107
108/**
109 * Return the min and max simultaneously.
110 */
111export function extent(iterable: Iterable<string>): [string, string] | [undefined, undefined];
112/**
113 * Return the min and max simultaneously.
114 */
115export function extent<T extends Numeric>(iterable: Iterable<T>): [T, T] | [undefined, undefined];
116/**
117 * Return the min and max simultaneously.
118 */
119export function extent<T>(
120 iterable: Iterable<T>,
121 accessor: (datum: T, index: number, array: Iterable<T>) => string | undefined | null
122): [string, string] | [undefined, undefined];
123/**
124 * Return the min and max simultaneously.
125 */
126export function extent<T, U extends Numeric>(
127 iterable: Iterable<T>,
128 accessor: (datum: T, index: number, array: Iterable<T>) => U | undefined | null
129): [U, U] | [undefined, undefined];
130
131/**
132 * Compute the sum of an array of numbers.
133 */
134export function sum<T extends Numeric>(iterable: Iterable<T | undefined | null>): number;
135/**
136 * Compute the sum of an array, using the given accessor to convert values to numbers.
137 */
138export function sum<T>(
139 iterable: Iterable<T>,
140 accessor: (datum: T, index: number, array: Iterable<T>) => number | undefined | null
141): number;
142
143/**
144 * Return the mean of an array of numbers
145 */
146export function mean<T extends Numeric>(iterable: Iterable<T | undefined | null>): number | undefined;
147/**
148 * Return the mean of an array of numbers
149 */
150export function mean<T>(
151 iterable: Iterable<T>,
152 accessor: (datum: T, index: number, array: Iterable<T>) => number | undefined | null
153): number | undefined;
154
155/**
156 * Return the median of an array of numbers
157 */
158export function median<T extends Numeric>(iterable: Iterable<T | undefined | null>): number | undefined;
159/**
160 * Return the median of an array of numbers
161 */
162export function median<T>(
163 iterable: Iterable<T>,
164 accessor: (element: T, i: number, array: Iterable<T>) => number | undefined | null
165): number | undefined;
166
167/**
168 * Returns the cumulative sum of the given iterable of numbers, as a Float64Array of the same length.
169 * If the iterable contains no numbers, returns zeros.
170 * An optional accessor function may be specified, which is equivalent to calling Array.from before computing the cumulative sum.
171 * This method ignores undefined and NaN values; this is useful for ignoring missing data.
172 */
173export function cumsum(iterable: Iterable<Numeric | undefined | null>): Float64Array;
174/**
175 * Returns the cumulative sum of the given iterable of numbers, as a Float64Array of the same length.
176 * If the iterable contains no numbers, returns zeros.
177 * An optional accessor function may be specified, which is equivalent to calling Array.from before computing the cumulative sum.
178 * This method ignores undefined and NaN values; this is useful for ignoring missing data.
179 */
180export function cumsum<T>(
181 iterable: Iterable<T>,
182 accessor: (element: T, i: number, array: Iterable<T>) => number | undefined | null
183): Float64Array;
184
185/**
186 * Returns the p-quantile of the given iterable of numbers, where p is a number in the range [0, 1].
187 *
188 * An optional accessor function may be specified, which is equivalent to calling array.map(accessor) before computing the quantile.
189 */
190export function quantile<T extends Numeric>(iterable: Iterable<T | undefined | null>, p: number): number | undefined;
191/**
192 * Returns the p-quantile of the given iterable of numbers, where p is a number in the range [0, 1].
193 *
194 * An optional accessor function may be specified, which is equivalent to calling array.map(accessor) before computing the quantile.
195 */
196export function quantile<T>(
197 iterable: Iterable<T>,
198 p: number,
199 accessor: (element: T, i: number, array: Iterable<T>) => number | undefined | null
200): number | undefined;
201
202/**
203 * Similar to quantile, but expects the input to be a sorted array of values.
204 * In contrast with quantile, the accessor is only called on the elements needed to compute the quantile.
205 */
206export function quantileSorted<T extends Numeric>(
207 array: Array<T | undefined | null>,
208 p: number
209): number | undefined;
210/**
211 * Similar to quantile, but expects the input to be a sorted array of values.
212 * In contrast with quantile, the accessor is only called on the elements needed to compute the quantile.
213 */
214export function quantileSorted<T>(
215 array: T[],
216 p: number,
217 accessor: (element: T, i: number, array: T[]) => number | undefined | null
218): number | undefined;
219
220/**
221 * Returns an unbiased estimator of the population variance of the given iterable of numbers using Welford’s algorithm.
222 * If the iterable has fewer than two numbers, returns undefined.
223 * An optional accessor function may be specified, which is equivalent to calling Array.from before computing the variance.
224 * This method ignores undefined and NaN values; this is useful for ignoring missing data.
225 */
226export function variance<T extends Numeric>(iterable: Iterable<T | undefined | null>): number | undefined;
227/**
228 * Returns an unbiased estimator of the population variance of the given iterable of numbers using Welford’s algorithm.
229 * If the iterable has fewer than two numbers, returns undefined.
230 * An optional accessor function may be specified, which is equivalent to calling Array.from before computing the variance.
231 * This method ignores undefined and NaN values; this is useful for ignoring missing data.
232 */
233export function variance<T>(
234 iterable: Iterable<T>,
235 accessor: (datum: T, index: number, array: Iterable<T>) => number | undefined | null
236): number | undefined;
237
238/**
239 * Compute the standard deviation, defined as the square root of the bias-corrected variance, of the given array of numbers.
240 */
241export function deviation<T extends Numeric>(iterable: Iterable<T | undefined | null>): number | undefined;
242/**
243 * Compute the standard deviation, defined as the square root of the bias-corrected variance, of the given array,
244 * using the given accessor to convert values to numbers.
245 */
246export function deviation<T>(
247 iterable: Iterable<T>,
248 accessor: (datum: T, index: number, array: Iterable<T>) => number | undefined | null
249): number | undefined;
250
251/**
252 * Returns a full precision summation of the given values.
253 * Although slower, d3.fsum can replace d3.sum wherever greater precision is needed. Uses d3.Adder.
254 */
255export function fsum<T extends Numeric>(values: Iterable<T | undefined | null>): number;
256/**
257 * Returns a full precision summation of the given values.
258 * Although slower, d3.fsum can replace d3.sum wherever greater precision is needed. Uses d3.Adder.
259 */
260export function fsum<T>(
261 values: Iterable<T>,
262 accessor: (datum: T, index: number, array: Iterable<T>) => number | undefined | null
263): number;
264
265/**
266 * Returns a full precision cumulative sum of the given values.
267 * Although slower, d3.fcumsum can replace d3.cumsum when greater precision is needed. Uses d3.Adder.
268 */
269export function fcumsum(values: Iterable<Numeric | undefined | null>): Float64Array;
270/**
271 * Returns a full precision cumulative sum of the given values.
272 * Although slower, d3.fcumsum can replace d3.cumsum when greater precision is needed. Uses d3.Adder.
273 */
274export function fcumsum<T>(
275 values: Iterable<T>,
276 accessor: (datum: T, index: number, array: Iterable<T>) => number | undefined | null
277): Float64Array;
278
279export class Adder {
280 /**
281 * Creates a full precision adder for IEEE 754 floating point numbers, setting its initial value to 0.
282 */
283 constructor();
284
285 /**
286 * Adds the specified number to the adder’s current value and returns the adder.
287 */
288 add(number: number): Adder;
289
290 /**
291 * Returns the IEEE 754 double precision representation of the adder’s current value.
292 * Most useful as the short-hand notation +adder.
293 */
294 valueOf(): number;
295}
296
297// --------------------------------------------------------------------------------------
298// Search
299// --------------------------------------------------------------------------------------
300
301/**
302 * Returns the least element of the specified iterable.
303 */
304export function least<T>(iterable: Iterable<T>): T | undefined;
305/**
306 * Returns the least element of the specified iterable according to the specified comparator.
307 */
308export function least<T>(iterable: Iterable<T>, comparator: (a: T, b: T) => number): T | undefined;
309/**
310 * Returns the least element of the specified iterable according to the specified accessor.
311 */
312export function least<T, U>(iterable: Iterable<T>, accessor: (a: T) => U): T | undefined;
313
314/**
315 * Returns the index of the least element of the specified iterable according to the specified comparator.
316 */
317export function leastIndex<T>(iterable: Iterable<T>): number | undefined;
318/**
319 * Returns the index of the least element of the specified iterable according to the specified comparator.
320 */
321export function leastIndex<T>(iterable: Iterable<T>, comparator: (a: T, b: T) => number): number | undefined;
322/**
323 * Returns the index of the least element of the specified iterable according to the specified accessor.
324 */
325export function leastIndex<T, U>(iterable: Iterable<T>, accessor: (a: T) => U): number | undefined;
326
327/**
328 * Returns the greatest element of the specified iterable according to the specified comparator or accessor.
329 * If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns undefined.
330 * If comparator is not specified, it defaults to ascending.
331 */
332export function greatest<T>(iterable: Iterable<T>): T | undefined;
333/**
334 * Returns the greatest element of the specified iterable according to the specified comparator or accessor.
335 * If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns undefined.
336 * If comparator is not specified, it defaults to ascending.
337 */
338export function greatest<T>(iterable: Iterable<T>, comparator: (a: T, b: T) => number): T | undefined;
339/**
340 * Returns the greatest element of the specified iterable according to the specified comparator or accessor.
341 * If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns undefined.
342 * If comparator is not specified, it defaults to ascending.
343 */
344export function greatest<T, U>(iterable: Iterable<T>, accessor: (a: T) => U): T | undefined;
345
346/**
347 * Returns the index of the greatest element of the specified iterable according to the specified comparator or accessor.
348 * If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns -1.
349 * If comparator is not specified, it defaults to ascending.
350 */
351export function greatestIndex<T>(iterable: Iterable<T>): number | undefined;
352/**
353 * Returns the index of the greatest element of the specified iterable according to the specified comparator or accessor.
354 * If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns -1.
355 * If comparator is not specified, it defaults to ascending.
356 */
357export function greatestIndex<T>(iterable: Iterable<T>, comparator: (a: T, b: T) => number): number | undefined;
358/**
359 * Returns the index of the greatest element of the specified iterable according to the specified comparator or accessor.
360 * If the given iterable contains no comparable elements (i.e., the comparator returns NaN when comparing each element to itself), returns -1.
361 * If comparator is not specified, it defaults to ascending.
362 */
363export function greatestIndex<T, U>(iterable: Iterable<T>, accessor: (a: T) => U): number | undefined;
364
365export function bisectLeft(array: ArrayLike<number>, x: number, lo?: number, hi?: number): number;
366export function bisectLeft(array: ArrayLike<string>, x: string, lo?: number, hi?: number): number;
367export function bisectLeft(array: ArrayLike<Date>, x: Date, lo?: number, hi?: number): number;
368
369export function bisectRight(array: ArrayLike<number>, x: number, lo?: number, hi?: number): number;
370export function bisectRight(array: ArrayLike<string>, x: string, lo?: number, hi?: number): number;
371export function bisectRight(array: ArrayLike<Date>, x: Date, lo?: number, hi?: number): number;
372
373export function bisectCenter(array: ArrayLike<number>, x: number, lo?: number, hi?: number): number;
374export function bisectCenter(array: ArrayLike<string>, x: string, lo?: number, hi?: number): number;
375export function bisectCenter(array: ArrayLike<Date>, x: Date, lo?: number, hi?: number): number;
376
377export const bisect: typeof bisectRight;
378
379export interface Bisector<T, U> {
380 left(array: ArrayLike<T>, x: U, lo?: number, hi?: number): number;
381 right(array: ArrayLike<T>, x: U, lo?: number, hi?: number): number;
382 center(array: ArrayLike<T>, x: U, lo?: number, hi?: number): number;
383}
384
385export function bisector<T, U>(comparator: (a: T, b: U) => number): Bisector<T, U>;
386export function bisector<T, U>(accessor: (x: T) => U): Bisector<T, U>;
387
388/**
389 * Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
390 *
391 * @param array The array to partially sort (in place).
392 * @param k The middle index for partial sorting.
393 */
394export function quickselect<T>(array: ArrayLike<T>, k: number): T[];
395/**
396 * Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
397 *
398 * @param array The array to partially sort (in place).
399 * @param k The middle index for partial sorting.
400 * @param left The left index of the range to sort.
401 */
402export function quickselect<T>(array: ArrayLike<T>, k: number, left: number): T[];
403/**
404 * Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
405 *
406 * @param array The array to partially sort (in place).
407 * @param k The middle index for partial sorting.
408 * @param left The left index of the range to sort.
409 * @param right The right index.
410 */
411export function quickselect<T>(array: ArrayLike<T>, k: number, left: number, right: number): T[];
412/**
413 * Rearranges items so that all items in the [left, k] are the smallest. The k-th element will have the (k - left + 1)-th smallest value in [left, right].
414 *
415 * @param array The array to partially sort (in place).
416 * @param k The middle index for partial sorting.
417 * @param left The left index of the range to sort.
418 * @param right The right index.
419 * @param compare The compare function.
420 */
421export function quickselect<T>(array: ArrayLike<T>, k: number, left: number, right: number, compare: (a: Primitive | undefined, b: Primitive | undefined) => number): T[];
422
423// NB. this is limited to primitive values due to D3's use of the <, >, and >= operators. Results get weird for object instances.
424/**
425 * Compares two primitive values for sorting (in ascending order).
426 */
427export function ascending(a: Primitive | undefined, b: Primitive | undefined): number;
428
429// NB. this is limited to primitive values due to D3's use of the <, >, and >= operators. Results get weird for object instances.
430/**
431 * Compares two primitive values for sorting (in descending order).
432 */
433export function descending(a: Primitive | undefined, b: Primitive | undefined): number;
434
435// --------------------------------------------------------------------------------------
436// Transformations
437// --------------------------------------------------------------------------------------
438
439/**
440 * Groups the specified array of values into an InternMap from key to array of value.
441 *
442 * @param iterable The array to group.
443 * @param key The key function.
444 */
445export function group<TObject, TKey>(iterable: Iterable<TObject>, key: (value: TObject) => TKey): InternMap<TKey, TObject[]>;
446/**
447 * Groups the specified array of values into an InternMap from key to array of value.
448 *
449 * @param iterable The array to group.
450 * @param key1 The first key function.
451 * @param key2 The second key function.
452 */
453export function group<TObject, TKey1, TKey2>(
454 iterable: Iterable<TObject>,
455 key1: (value: TObject) => TKey1,
456 key2: (value: TObject) => TKey2
457): InternMap<TKey1, Map<TKey2, TObject[]>>;
458/**
459 * Groups the specified array of values into an InternMap from key to array of value.
460 *
461 * @param iterable The array to group.
462 * @param key1 The first key function.
463 * @param key2 The second key function.
464 * @param key3 The third key function.
465 */
466export function group<TObject, TKey1, TKey2, TKey3>(
467 iterable: Iterable<TObject>,
468 key1: (value: TObject) => TKey1,
469 key2: (value: TObject) => TKey2,
470 key3: (value: TObject) => TKey3
471): InternMap<TKey1, Map<TKey2, Map<TKey3, TObject[]>>>;
472
473/**
474 * Equivalent to group, but returns nested arrays instead of nested maps.
475 *
476 * @param iterable The array to group.
477 * @param key The key function.
478 */
479export function groups<TObject, TKey>(
480 iterable: Iterable<TObject>,
481 key: (value: TObject) => TKey
482): Array<[TKey, TObject[]]>;
483/**
484 * Equivalent to group, but returns nested arrays instead of nested maps.
485 *
486 * @param iterable The array to group.
487 * @param key1 The first key function.
488 * @param key2 The second key function.
489 */
490export function groups<TObject, TKey1, TKey2>(
491 iterable: Iterable<TObject>,
492 key1: (value: TObject) => TKey1,
493 key2: (value: TObject) => TKey2
494): Array<[TKey1, Array<[TKey2, TObject[]]>]>;
495/**
496 * Equivalent to group, but returns nested arrays instead of nested maps.
497 *
498 * @param iterable The array to group.
499 * @param key1 The first key function.
500 * @param key2 The second key function.
501 * @param key3 The third key function.
502 */
503export function groups<TObject, TKey1, TKey2, TKey3>(
504 iterable: Iterable<TObject>,
505 key1: (value: TObject) => TKey1,
506 key2: (value: TObject) => TKey2,
507 key3: (value: TObject) => TKey3
508): Array<[TKey1, Array<[TKey2, Array<[TKey3, TObject[]]>]>]>;
509
510/**
511 * Equivalent to group but returns a unique value per compound key instead of an array, throwing if the key is not unique.
512 *
513 * @param iterable The array to group.
514 * @param key The key function.
515 */
516export function index<TObject, TKey>(iterable: Iterable<TObject>, key: (value: TObject) => TKey): InternMap<TKey, TObject>;
517/**
518 * Equivalent to group but returns a unique value per compound key instead of an array, throwing if the key is not unique.
519 *
520 * @param iterable The array to group.
521 * @param key1 The first key function.
522 * @param key2 The second key function.
523 */
524export function index<TObject, TKey1, TKey2>(
525 iterable: Iterable<TObject>,
526 key1: (value: TObject) => TKey1,
527 key2: (value: TObject) => TKey2
528): InternMap<TKey1, InternMap<TKey2, TObject>>;
529/**
530 * Equivalent to group but returns a unique value per compound key instead of an array, throwing if the key is not unique.
531 *
532 * @param iterable The array to group.
533 * @param key1 The first key function.
534 * @param key2 The second key function.
535 * @param key3 The third key function.
536 */
537export function index<TObject, TKey1, TKey2, TKey3>(
538 iterable: Iterable<TObject>,
539 key1: (value: TObject) => TKey1,
540 key2: (value: TObject) => TKey2,
541 key3: (value: TObject) => TKey3
542): InternMap<TKey1, InternMap<TKey2, InternMap<TKey3, TObject>>>;
543
544/**
545 * Equivalent to index, but returns nested arrays instead of nested maps.
546 *
547 * @param iterable The array to group.
548 * @param key The key function.
549 */
550export function indexes<TObject, TKey>(
551 iterable: Iterable<TObject>,
552 key: (value: TObject) => TKey
553): Array<[TKey, TObject]>;
554/**
555 * Equivalent to index, but returns nested arrays instead of nested maps.
556 *
557 * @param iterable The array to group.
558 * @param key1 The first key function.
559 * @param key2 The second key function.
560 */
561export function indexes<TObject, TKey1, TKey2>(
562 iterable: Iterable<TObject>,
563 key1: (value: TObject) => TKey1,
564 key2: (value: TObject) => TKey2
565): Array<[TKey1, Array<[TKey2, TObject]>]>;
566/**
567 * Equivalent to index, but returns nested arrays instead of nested maps.
568 *
569 * @param iterable The array to group.
570 * @param key1 The first key function.
571 * @param key2 The second key function.
572 * @param key3 The third key function.
573 */
574export function indexes<TObject, TKey1, TKey2, TKey3>(
575 iterable: Iterable<TObject>,
576 key1: (value: TObject) => TKey1,
577 key2: (value: TObject) => TKey2,
578 key3: (value: TObject) => TKey3
579): Array<[TKey1, Array<[TKey2, Array<[TKey3, TObject]>]>]>;
580
581/**
582 * Groups and reduces the specified array of values into an InternMap from key to value.
583 *
584 * @param iterable The array to group.
585 * @param reduce The reduce function.
586 * @param key The key function.
587 */
588export function rollup<TObject, TReduce, TKey>(
589 iterable: Iterable<TObject>,
590 reduce: (value: TObject[]) => TReduce,
591 key: (value: TObject) => TKey
592): InternMap<TKey, TReduce>;
593/**
594 * Groups and reduces the specified array of values into an InternMap from key to value.
595 *
596 * @param iterable The array to group.
597 * @param reduce The reduce function.
598 * @param key1 The first key function.
599 * @param key2 The second key function.
600 */
601export function rollup<TObject, TReduce, TKey1, TKey2>(
602 iterable: Iterable<TObject>,
603 reduce: (value: TObject[]) => TReduce,
604 key1: (value: TObject) => TKey1,
605 key2: (value: TObject) => TKey2
606): InternMap<TKey1, InternMap<TKey2, TReduce>>;
607/**
608 * Groups and reduces the specified array of values into an InternMap from key to value.
609 *
610 * @param iterable The array to group.
611 * @param reduce The reduce function.
612 * @param key1 The first key function.
613 * @param key2 The second key function.
614 * @param key3 The third key function.
615 */
616export function rollup<TObject, TReduce, TKey1, TKey2, TKey3>(
617 iterable: Iterable<TObject>,
618 reduce: (value: TObject[]) => TReduce,
619 key1: (value: TObject) => TKey1,
620 key2: (value: TObject) => TKey2,
621 key3: (value: TObject) => TKey3
622): InternMap<TKey1, InternMap<TKey2, InternMap<TKey3, TReduce>>>;
623
624/**
625 * Equivalent to rollup, but returns nested arrays instead of nested maps.
626 *
627 * @param iterable The array to group.
628 * @param reduce The reduce function.
629 * @param key The key function.
630 */
631export function rollups<TObject, TReduce, TKey>(
632 iterable: Iterable<TObject>,
633 reduce: (value: TObject[]) => TReduce,
634 key: (value: TObject) => TKey
635): Array<[TKey, TReduce]>;
636/**
637 * Equivalent to rollup, but returns nested arrays instead of nested maps.
638 *
639 * @param iterable The array to group.
640 * @param reduce The reduce function.
641 * @param key1 The first key function.
642 * @param key2 The second key function.
643 */
644export function rollups<TObject, TReduce, TKey1, TKey2>(
645 iterable: Iterable<TObject>,
646 reduce: (value: TObject[]) => TReduce,
647 key1: (value: TObject) => TKey1,
648 key2: (value: TObject) => TKey2
649): Array<[TKey1, Array<[TKey2, TReduce]>]>;
650/**
651 * Equivalent to rollup, but returns nested arrays instead of nested maps.
652 *
653 * @param iterable The array to group.
654 * @param reduce The reduce function.
655 * @param key1 The first key function.
656 * @param key2 The second key function.
657 * @param key3 The third key function.
658 */
659export function rollups<TObject, TReduce, TKey1, TKey2, TKey3>(
660 iterable: Iterable<TObject>,
661 reduce: (value: TObject[]) => TReduce,
662 key1: (value: TObject) => TKey1,
663 key2: (value: TObject) => TKey2,
664 key3: (value: TObject) => TKey3
665): Array<[TKey1, Array<[TKey2, Array<[TKey3, TReduce]>]>]>;
666
667/**
668 * Groups the specified iterable of elements according to the specified key function, sorts the groups according to the specified comparator, and then returns an array of keys in sorted order.
669 * The comparator will be asked to compare two groups a and b and should return a negative value if a should be before b, a positive value if a should be after b, or zero for a partial ordering.
670 */
671export function groupSort<TObject, TKey>(
672 iterable: Iterable<TObject>,
673 comparator: (a: TObject[], b: TObject[]) => number,
674 key: (value: TObject) => TKey
675): TKey[];
676/**
677 * Groups the specified iterable of elements according to the specified key function, sorts the groups according to the specified accessor, and then returns an array of keys in sorted order.
678 */
679export function groupSort<TObject, TReduce, TKey>(
680 iterable: Iterable<TObject>,
681 accessor: (value: TObject[]) => TReduce,
682 key: (value: TObject) => TKey
683): TKey[];
684
685/**
686 * Returns the number of valid number values (i.e., not null, NaN, or undefined) in the specified iterable; accepts an accessor.
687 *
688 * @param iterable Input array.
689 */
690export function count<TObject>(iterable: Iterable<TObject>): number;
691/**
692 * Returns the number of valid number values (i.e., not null, NaN, or undefined) in the specified iterable; accepts an accessor.
693 *
694 * @param iterable Input array.
695 * @param accessor Accessor method.
696 */
697export function count<TObject>(
698 iterable: Iterable<TObject>,
699 accessor: (a: TObject, b: TObject) => number | null | undefined
700): number;
701
702/**
703 * Returns the Cartesian product of the two arrays a and b.
704 * For each element i in the specified array a and each element j in the specified array b, in order,
705 * it creates a two-element array for each pair.
706 *
707 * @param a First input array.
708 * @param b Second input array.
709 */
710export function cross<S, T>(a: Iterable<S>, b: Iterable<T>): Array<[S, T]>;
711
712/**
713 * Returns the Cartesian product of the two arrays a and b.
714 * For each element i in the specified array a and each element j in the specified array b, in order,
715 * invokes the specified reducer function passing the element i and element j.
716 *
717 * @param a First input array.
718 * @param b Second input array.
719 * @param reducer A reducer function taking as input an element from "a" and "b" and returning a reduced value.
720 */
721export function cross<S, T, U>(a: Iterable<S>, b: Iterable<T>, reducer: (a: S, b: T) => U): U[];
722
723/**
724 * Merges the specified arrays into a single array.
725 */
726export function merge<T>(iterables: Iterable<Iterable<T>>): T[];
727
728/**
729 * For each adjacent pair of elements in the specified array, returns a new array of tuples of elements i and i - 1.
730 * Returns the empty array if the input array has fewer than two elements.
731 *
732 * @param iterable Array of input elements
733 */
734export function pairs<T>(iterable: Iterable<T>): Array<[T, T]>;
735/**
736 * For each adjacent pair of elements in the specified array, in order, invokes the specified reducer function passing the element i and element i - 1.
737 * Returns the resulting array of pair-wise reduced elements.
738 * Returns the empty array if the input array has fewer than two elements.
739 *
740 * @param iterable Array of input elements
741 * @param reducer A reducer function taking as input to adjacent elements of the input array and returning a reduced value.
742 */
743export function pairs<T, U>(iterable: Iterable<T>, reducer: (a: T, b: T) => U): U[];
744
745/**
746 * Returns a permutation of the specified source object (or array) using the specified iterable of keys.
747 * The returned array contains the corresponding property of the source object for each key in keys, in order.
748 * For example, `permute(["a", "b", "c"], [1, 2, 0]) // ["b", "c", "a"]`
749 *
750 * It is acceptable to have more keys than source elements, and for keys to be duplicated or omitted.
751 */
752export function permute<T>(source: { [key: number]: T; }, keys: Iterable<number>): T[];
753/**
754 * Extract the values from an object into an array with a stable order. For example:
755 * `var object = {yield: 27, year: 1931, site: "University Farm"};`
756 * `d3.permute(object, ["site", "yield"]); // ["University Farm", 27]`
757 */
758export function permute<T, K extends keyof T>(source: T, keys: Iterable<K>): Array<T[K]>;
759
760/**
761 * Randomizes the order of the specified array using the Fisher–Yates shuffle.
762 */
763export function shuffle<T>(array: T[], lo?: number, hi?: number): T[];
764export function shuffle(array: Int8Array, lo?: number, hi?: number): Int8Array;
765export function shuffle(array: Uint8Array, lo?: number, hi?: number): Uint8Array;
766export function shuffle(array: Uint8ClampedArray, lo?: number, hi?: number): Uint8ClampedArray;
767export function shuffle(array: Int16Array, lo?: number, hi?: number): Int16Array;
768export function shuffle(array: Uint16Array, lo?: number, hi?: number): Uint16Array;
769export function shuffle(array: Int32Array, lo?: number, hi?: number): Int32Array;
770export function shuffle(array: Uint32Array, lo?: number, hi?: number): Uint32Array;
771export function shuffle(array: Float32Array, lo?: number, hi?: number): Float32Array;
772export function shuffle(array: Float64Array, lo?: number, hi?: number): Float64Array;
773
774/**
775 * Returns a shuffle function given the specified random source.
776 */
777export function shuffler(random: () => number): typeof shuffle;
778
779/**
780 * Generate an array of approximately count + 1 uniformly-spaced, nicely-rounded values between start and stop (inclusive).
781 * Each value is a power of ten multiplied by 1, 2 or 5. See also d3.tickIncrement, d3.tickStep and linear.ticks.
782 *
783 * Ticks are inclusive in the sense that they may include the specified start and stop values if (and only if) they are exact,
784 * nicely-rounded values consistent with the inferred step. More formally, each returned tick t satisfies start ≤ t and t ≤ stop.
785 *
786 * @param start Start value for ticks
787 * @param stop Stop value for ticks
788 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
789 */
790export function ticks(start: number, stop: number, count: number): number[];
791
792/**
793 * Returns the difference between adjacent tick values if the same arguments were passed to d3.ticks:
794 * a nicely-rounded value that is a power of ten multiplied by 1, 2 or 5.
795 *
796 * Like d3.tickStep, except requires that start is always less than or equal to stop, and if the tick step for the given start,
797 * stop and count would be less than one, returns the negative inverse tick step instead.
798 *
799 * This method is always guaranteed to return an integer, and is used by d3.ticks to avoid guarantee that the returned tick values
800 * are represented as precisely as possible in IEEE 754 floating point.
801 *
802 * @param start Start value for ticks
803 * @param stop Stop value for ticks
804 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
805 */
806export function tickIncrement(start: number, stop: number, count: number): number;
807
808/**
809 * Returns the difference between adjacent tick values if the same arguments were passed to d3.ticks:
810 * a nicely-rounded value that is a power of ten multiplied by 1, 2 or 5.
811 *
812 * Note that due to the limited precision of IEEE 754 floating point, the returned value may not be exact decimals;
813 * use d3-format to format numbers for human consumption.
814 *
815 * @param start Start value for ticks
816 * @param stop Stop value for ticks
817 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
818 */
819export function tickStep(start: number, stop: number, count: number): number;
820
821/**
822 * Returns a new interval [niceStart, niceStop] covering the given interval [start, stop] and where niceStart and niceStop are guaranteed to align with the corresponding tick step.
823 * Like d3.tickIncrement, this requires that start is less than or equal to stop.
824 *
825 * @param start Start value for ticks
826 * @param stop Stop value for ticks
827 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
828 */
829export function nice(start: number, stop: number, count: number): [number, number];
830
831/**
832 * Generates a 0-based numeric sequence. The output range does not include 'stop'.
833 */
834export function range(stop: number): number[];
835/**
836 * Generates a numeric sequence starting from the given start and stop values. 'step' defaults to 1. The output range does not include 'stop'.
837 */
838export function range(start: number, stop: number, step?: number): number[];
839
840/**
841 * Transpose a matrix provided in Array of Arrays format.
842 */
843export function transpose<T>(matrix: ArrayLike<ArrayLike<T>>): T[][];
844
845/**
846 * Returns an array of arrays, where the ith array contains the ith element from each of the argument arrays.
847 * The returned array is truncated in length to the shortest array in arrays. If arrays contains only a single array, the returned array
848 * contains one-element arrays. With no arguments, the returned array is empty.
849 */
850export function zip<T>(...arrays: Array<ArrayLike<T>>): T[][];
851
852// --------------------------------------------------------------------------------------
853// Iterables
854// --------------------------------------------------------------------------------------
855
856/**
857 * Returns true if the given test function returns true for every value in the given iterable.
858 * This method returns as soon as test returns a non-truthy value or all values are iterated over.
859 * Equivalent to array.every.
860 */
861export function every<T>(
862 iterable: Iterable<T>,
863 test: (value: T, index: number, iterable: Iterable<T>) => unknown
864): boolean;
865
866/**
867 * Returns true if the given test function returns true for any value in the given iterable.
868 * This method returns as soon as test returns a truthy value or all values are iterated over.
869 * Equivalent to array.some.
870 */
871export function some<T>(
872 iterable: Iterable<T>,
873 test: (value: T, index: number, iterable: Iterable<T>) => unknown
874): boolean;
875
876/**
877 * Returns a new array containing the values from iterable, in order, for which the given test function returns true.
878 * Equivalent to array.filter.
879 */
880export function filter<T>(
881 iterable: Iterable<T>,
882 test: (value: T, index: number, iterable: Iterable<T>) => unknown
883): T[];
884
885/**
886 * Returns a new array containing the mapped values from iterable, in order, as defined by given mapper function.
887 * Equivalent to array.map and Array.from.
888 */
889export function map<T, U>(iterable: Iterable<T>, mapper: (value: T, index: number, iterable: Iterable<T>) => U): U[];
890
891/**
892 * Returns the reduced value defined by given reducer function, which is repeatedly invoked for each value in iterable, being passed the current reduced value and the next value.
893 * Equivalent to array.reduce.
894 */
895export function reduce<T>(
896 iterable: Iterable<T>,
897 reducer: (previousValue: T, currentValue: T, currentIndex: number, iterable: Iterable<T>) => T,
898 initialValue?: T
899): T;
900/**
901 * Returns the reduced value defined by given reducer function, which is repeatedly invoked for each value in iterable, being passed the current reduced value and the next value.
902 * Equivalent to array.reduce.
903 */
904export function reduce<T, U>(
905 iterable: Iterable<T>,
906 reducer: (previousValue: U, currentValue: T, currentIndex: number, iterable: Iterable<T>) => U,
907 initialValue: U
908): U;
909
910/**
911 * Returns an array containing the values in the given iterable in reverse order.
912 * Equivalent to array.reverse, except that it does not mutate the given iterable.
913 */
914export function reverse<T>(iterable: Iterable<T>): T[];
915
916/**
917 * Returns an array containing the values in the given iterable in the sorted order defined by the given comparator function.
918 * If comparator is not specified, it defaults to d3.ascending.
919 * Equivalent to array.sort, except that it does not mutate the given iterable, and the comparator defaults to natural order instead of lexicographic order.
920 */
921export function sort<T>(iterable: Iterable<T>, comparator?: (a: T, b: T) => number): T[];
922/**
923 * Returns an array containing the values in the given iterable in the sorted order defined by the given accessor function.
924 * This is equivalent to a comparator using natural order.
925 * The accessor is only invoked once per element, and thus may be nondeterministic.
926 * Multiple accessors may be specified to break ties.
927 */
928export function sort<T>(iterable: Iterable<T>, ...accessors: Array<(a: T) => unknown>): T[];
929
930// --------------------------------------------------------------------------------------
931// Sets
932// --------------------------------------------------------------------------------------
933
934/**
935 * Returns a new Set containing every value in iterable that is not in any of the others iterables.
936 */
937export function difference<T>(iterable: Iterable<T>, ...others: Array<Iterable<T>>): Set<T>;
938
939/**
940 * Returns a new Set containing every (distinct) value that appears in any of the given iterables.
941 * The order of values in the returned Set is based on their first occurrence in the given iterables.
942 */
943export function union<T>(...iterables: Array<Iterable<T>>): Set<T>;
944
945/**
946 * Returns a new Set containing every (distinct) value that appears in all of the given iterables.
947 * The order of values in the returned Set is based on their first occurrence in the given iterables.
948 */
949export function intersection<T>(...iterables: Array<Iterable<T>>): Set<T>;
950
951/**
952 * Returns true if a is a superset of b: if every value in the given iterable b is also in the given iterable a.
953 */
954export function superset<T>(a: Iterable<T>, b: Iterable<T>): boolean;
955
956/**
957 * Returns true if a is a subset of b: if every value in the given iterable a is also in the given iterable b.
958 */
959export function subset<T>(a: Iterable<T>, b: Iterable<T>): boolean;
960
961/**
962 * Returns true if a and b are disjoint: if a and b contain no shared value.
963 */
964export function disjoint<T>(a: Iterable<T>, b: Iterable<T>): boolean;
965
966// --------------------------------------------------------------------------------------
967// Bins
968// --------------------------------------------------------------------------------------
969
970export interface Bin<Datum, Value extends number | Date | undefined> extends Array<Datum> {
971 x0: Value | undefined;
972 x1: Value | undefined;
973}
974
975/**
976 * Type definition for threshold generator which returns the count of recommended thresholds
977 */
978export type ThresholdCountGenerator<Value extends number | undefined = number | undefined> =
979 (values: ArrayLike<Value>, min: number, max: number) => number;
980
981/**
982 * Type definition for threshold generator which returns an array of recommended numbers thresholds
983 */
984export type ThresholdNumberArrayGenerator<Value extends number | undefined> =
985 (values: ArrayLike<Value>, min: number, max: number) => Value[];
986
987/**
988 * Type definition for threshold generator which returns an array of recommended dates thresholds
989 */
990export type ThresholdDateArrayGenerator<Value extends Date | undefined> =
991 (values: ArrayLike<Value>, min: Date, max: Date) => Value[];
992
993export interface HistogramCommon<Datum, Value extends number | Date | undefined> {
994 (data: ArrayLike<Datum>): Array<Bin<Datum, Value>>;
995
996 value(): (d: Datum, i: number, data: ArrayLike<Datum>) => Value;
997 value(valueAccessor: (d: Datum, i: number, data: ArrayLike<Datum>) => Value): this;
998}
999
1000export interface HistogramGeneratorDate<Datum, Value extends Date | undefined> extends HistogramCommon<Datum, Date> {
1001 domain(): (values: ArrayLike<Value>) => [Date, Date];
1002 domain(domain: [Date, Date]): this;
1003 domain(domainAccessor: (values: ArrayLike<Value>) => [Date, Date]): this;
1004
1005 thresholds(): ThresholdDateArrayGenerator<Value>;
1006 /**
1007 * Set the array of values to be used as thresholds in determining the bins.
1008 *
1009 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1010 * and the last bin.x1 is always equal to the maximum domain value.
1011 *
1012 * @param thresholds Array of threshold values used for binning. The elements must
1013 * be of the same type as the materialized values of the histogram.
1014 */
1015 thresholds(thresholds: ArrayLike<Value>): this;
1016 /**
1017 * Set a threshold accessor function, which returns the array of values to be used as
1018 * thresholds in determining the bins.
1019 *
1020 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1021 * and the last bin.x1 is always equal to the maximum domain value.
1022 *
1023 * @param thresholds A function which accepts as arguments the array of materialized values, and
1024 * optionally the domain minimum and maximum. The function calculates and returns the array of values to be used as
1025 * thresholds in determining the bins.
1026 */
1027 thresholds(thresholds: ThresholdDateArrayGenerator<Value>): this;
1028}
1029
1030export interface HistogramGeneratorNumber<Datum, Value extends number | undefined> extends HistogramCommon<Datum, Value> {
1031 domain(): (values: Iterable<Value>) => [number, number] | [undefined, undefined];
1032 domain(domain: [number, number]): this;
1033 domain(domainAccessor: (values: Iterable<Value>) => [number, number] | [undefined, undefined]): this;
1034
1035 thresholds(): ThresholdCountGenerator<Value> | ThresholdNumberArrayGenerator<Value>;
1036 /**
1037 * Divide the domain uniformly into approximately count bins. IMPORTANT: This threshold
1038 * setting approach only works, when the materialized values are numbers!
1039 *
1040 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1041 * and the last bin.x1 is always equal to the maximum domain value.
1042 *
1043 * @param count The desired number of uniform bins.
1044 */
1045 thresholds(count: number): this;
1046 /**
1047 * Set a threshold accessor function, which returns the desired number of bins.
1048 * Divides the domain uniformly into approximately count bins. IMPORTANT: This threshold
1049 * setting approach only works, when the materialized values are numbers!
1050 *
1051 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1052 * and the last bin.x1 is always equal to the maximum domain value.
1053 *
1054 * @param count A function which accepts as arguments the array of materialized values, and
1055 * optionally the domain minimum and maximum. The function calculates and returns the suggested
1056 * number of bins.
1057 */
1058 thresholds(count: ThresholdCountGenerator<Value>): this;
1059 /**
1060 * Set the array of values to be used as thresholds in determining the bins.
1061 *
1062 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1063 * and the last bin.x1 is always equal to the maximum domain value.
1064 *
1065 * @param thresholds Array of threshold values used for binning. The elements must
1066 * be of the same type as the materialized values of the histogram.
1067 */
1068 thresholds(thresholds: ArrayLike<Value>): this;
1069 /**
1070 * Set a threshold accessor function, which returns the array of values to be used as
1071 * thresholds in determining the bins.
1072 *
1073 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1074 * and the last bin.x1 is always equal to the maximum domain value.
1075 *
1076 * @param thresholds A function which accepts as arguments the array of materialized values, and
1077 * optionally the domain minimum and maximum. The function calculates and returns the array of values to be used as
1078 * thresholds in determining the bins.
1079 */
1080 thresholds(thresholds: ThresholdNumberArrayGenerator<Value>): this;
1081}
1082
1083/**
1084 * @deprecated. Use bin instead.
1085 */
1086export function histogram(): HistogramGeneratorNumber<number, number>;
1087
1088/**
1089 * @deprecated Use bin instead.
1090 */
1091export function histogram<Datum, Value extends number | undefined>(): HistogramGeneratorNumber<Datum, Value>;
1092
1093/**
1094 * @deprecated Use bin instead.
1095 */
1096export function histogram<Datum, Value extends Date | undefined>(): HistogramGeneratorDate<Datum, Value>;
1097
1098export function bin(): HistogramGeneratorNumber<number, number>;
1099export function bin<Datum, Value extends number | undefined>(): HistogramGeneratorNumber<Datum, Value>;
1100export function bin<Datum, Value extends Date | undefined>(): HistogramGeneratorDate<Datum, Value>;
1101
1102// --------------------------------------------------------------------------------------
1103// Histogram Thresholds
1104// --------------------------------------------------------------------------------------
1105
1106export function thresholdFreedmanDiaconis(values: ArrayLike<number | undefined>, min: number, max: number): number; // of type ThresholdCountGenerator
1107
1108export function thresholdScott(values: ArrayLike<number | undefined>, min: number, max: number): number; // of type ThresholdCountGenerator
1109
1110export function thresholdSturges(values: ArrayLike<number | undefined>): number; // of type ThresholdCountGenerator
1111
1112// --------------------------------------------------------------------------------------
1113// Interning
1114// --------------------------------------------------------------------------------------
1115
1116/**
1117 * The InternMap class extends the native JavaScript Map class, allowing Dates and other non-primitive keys by bypassing the SameValueZero algorithm when determining key equality.
1118 */
1119export class InternMap<K = any, V = any> extends Map<K, V> {
1120}
1121
1122/**
1123 * The InternSet class extends the native JavaScript Set class, allowing Dates and other non-primitive keys by bypassing the SameValueZero algorithm when determining key equality.
1124 */
1125export class InternSet<T = any> extends Set<T> {
1126}