UNPKG

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