UNPKG

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