source: node_modules/@types/d3-array/index.d.ts

Last change on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago

Added visualizations

  • Property mode set to 100644
File size: 47.7 KB
Line 
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 * Computes the Cartesian product of any number of iterables.
652 *
653 * When called **without** a reducer, the result is an array of tuples,
654 * where each tuple contains one element from each input iterable.
655 *
656 * @typeParam T - A tuple type describing the element type of each iterable argument.
657 * For example, passing `[number[], string[]]` infers `T` as `[number, string]`.
658 *
659 * @param iterables - Two or more iterables to combine into a Cartesian product.
660 * @returns An array of tuples containing one value from each iterable.
661 *
662 * @example
663 * ```ts
664 * const nums = [1, 2];
665 * const chars = ['a', 'b'];
666 * const out = cross(nums, chars);
667 * // ^? type: [number, string][]
668 * // Example value: [[1,'a'], [1,'b'], [2,'a'], [2,'b']]
669 * ```
670 */
671export function cross<T extends unknown[]>(
672 ...iterables: { [K in keyof T]: Iterable<T[K]> }
673): T[];
674
675/**
676 * Computes the Cartesian product of any number of iterables and then applies
677 * a reducer to each tuple, returning the reduced values.
678 *
679 * The final argument **must** be the reducer function; all prior arguments are iterables.
680 *
681 * @typeParam T - A tuple type describing the element type of each iterable argument.
682 * @typeParam U - The result type produced by the reducer.
683 *
684 * @param args - The iterables to combine, followed by a reducer function.
685 * @param args.reducer - A function invoked with one element from each iterable
686 * (spread as individual parameters) that returns a reduced value.
687 * @returns An array of reduced values returned by the reducer.
688 *
689 * @example
690 * ```ts
691 * const nums = [1, 2];
692 * const chars = ['a', 'b'];
693 * const out = cross(nums, chars, (n, c) => `${n}${c}`);
694 * // ^? type: string[]
695 * // Example value: ['1a', '1b', '2a', '2b']
696 * ```
697 */
698export function cross<T extends unknown[], U>(
699 ...args: [...iterables: { [K in keyof T]: Iterable<T[K]> }, reducer: (...values: T) => U]
700): U[];
701
702/**
703 * Merges the specified arrays into a single array.
704 */
705export function merge<T>(iterables: Iterable<Iterable<T>>): T[];
706
707/**
708 * For each adjacent pair of elements in the specified array, returns a new array of tuples of elements i and i - 1.
709 * Returns the empty array if the input array has fewer than two elements.
710 *
711 * @param iterable Array of input elements
712 */
713export function pairs<T>(iterable: Iterable<T>): Array<[T, T]>;
714/**
715 * 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.
716 * Returns the resulting array of pair-wise reduced elements.
717 * Returns the empty array if the input array has fewer than two elements.
718 *
719 * @param iterable Array of input elements
720 * @param reducer A reducer function taking as input to adjacent elements of the input array and returning a reduced value.
721 */
722export function pairs<T, U>(iterable: Iterable<T>, reducer: (a: T, b: T) => U): U[];
723
724/**
725 * Returns a permutation of the specified source object (or array) using the specified iterable of keys.
726 * The returned array contains the corresponding property of the source object for each key in keys, in order.
727 * For example, `permute(["a", "b", "c"], [1, 2, 0]) // ["b", "c", "a"]`
728 *
729 * It is acceptable to have more keys than source elements, and for keys to be duplicated or omitted.
730 */
731export function permute<T>(source: { [key: number]: T }, keys: Iterable<number>): T[];
732/**
733 * Extract the values from an object into an array with a stable order. For example:
734 * `var object = {yield: 27, year: 1931, site: "University Farm"};`
735 * `d3.permute(object, ["site", "yield"]); // ["University Farm", 27]`
736 */
737export function permute<T, K extends keyof T>(source: T, keys: Iterable<K>): Array<T[K]>;
738
739/**
740 * Randomizes the order of the specified array using the Fisher–Yates shuffle.
741 */
742export function shuffle<T>(array: T[], lo?: number, hi?: number): T[];
743export function shuffle(array: Int8Array, lo?: number, hi?: number): Int8Array;
744export function shuffle(array: Uint8Array, lo?: number, hi?: number): Uint8Array;
745export function shuffle(array: Uint8ClampedArray, lo?: number, hi?: number): Uint8ClampedArray;
746export function shuffle(array: Int16Array, lo?: number, hi?: number): Int16Array;
747export function shuffle(array: Uint16Array, lo?: number, hi?: number): Uint16Array;
748export function shuffle(array: Int32Array, lo?: number, hi?: number): Int32Array;
749export function shuffle(array: Uint32Array, lo?: number, hi?: number): Uint32Array;
750export function shuffle(array: Float32Array, lo?: number, hi?: number): Float32Array;
751export function shuffle(array: Float64Array, lo?: number, hi?: number): Float64Array;
752
753/**
754 * Returns a shuffle function given the specified random source.
755 */
756export function shuffler(random: () => number): typeof shuffle;
757
758/**
759 * Generate an array of approximately count + 1 uniformly-spaced, nicely-rounded values between start and stop (inclusive).
760 * Each value is a power of ten multiplied by 1, 2 or 5. See also d3.tickIncrement, d3.tickStep and linear.ticks.
761 *
762 * Ticks are inclusive in the sense that they may include the specified start and stop values if (and only if) they are exact,
763 * nicely-rounded values consistent with the inferred step. More formally, each returned tick t satisfies start ≤ t and t ≤ stop.
764 *
765 * @param start Start value for ticks
766 * @param stop Stop value for ticks
767 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
768 */
769export function ticks(start: number, stop: number, count: number): number[];
770
771/**
772 * Returns the difference between adjacent tick values if the same arguments were passed to d3.ticks:
773 * a nicely-rounded value that is a power of ten multiplied by 1, 2 or 5.
774 *
775 * Like d3.tickStep, except requires that start is always less than or equal to stop, and if the tick step for the given start,
776 * stop and count would be less than one, returns the negative inverse tick step instead.
777 *
778 * This method is always guaranteed to return an integer, and is used by d3.ticks to avoid guarantee that the returned tick values
779 * are represented as precisely as possible in IEEE 754 floating point.
780 *
781 * @param start Start value for ticks
782 * @param stop Stop value for ticks
783 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
784 */
785export function tickIncrement(start: number, stop: number, count: number): number;
786
787/**
788 * Returns the difference between adjacent tick values if the same arguments were passed to d3.ticks:
789 * a nicely-rounded value that is a power of ten multiplied by 1, 2 or 5.
790 *
791 * Note that due to the limited precision of IEEE 754 floating point, the returned value may not be exact decimals;
792 * use d3-format to format numbers for human consumption.
793 *
794 * @param start Start value for ticks
795 * @param stop Stop value for ticks
796 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
797 */
798export function tickStep(start: number, stop: number, count: number): number;
799
800/**
801 * 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.
802 * Like d3.tickIncrement, this requires that start is less than or equal to stop.
803 *
804 * @param start Start value for ticks
805 * @param stop Stop value for ticks
806 * @param count count + 1 is the approximate number of ticks to be returned by d3.ticks.
807 */
808export function nice(start: number, stop: number, count: number): [number, number];
809
810/**
811 * Generates a 0-based numeric sequence. The output range does not include 'stop'.
812 */
813export function range(stop: number): number[];
814/**
815 * Generates a numeric sequence starting from the given start and stop values. 'step' defaults to 1. The output range does not include 'stop'.
816 */
817// tslint:disable-next-line:unified-signatures
818export function range(start: number, stop: number, step?: number): number[];
819
820/**
821 * Transpose a matrix provided in Array of Arrays format.
822 */
823export function transpose<T>(matrix: ArrayLike<ArrayLike<T>>): T[][];
824
825/**
826 * Returns an array of arrays, where the ith array contains the ith element from each of the argument arrays.
827 * The returned array is truncated in length to the shortest array in arrays. If arrays contains only a single array, the returned array
828 * contains one-element arrays. With no arguments, the returned array is empty.
829 */
830export function zip<T>(...arrays: Array<ArrayLike<T>>): T[][];
831
832// --------------------------------------------------------------------------------------
833// Blur
834// --------------------------------------------------------------------------------------
835
836/**
837 * Blurs an array of data in-place by applying three iterations of a moving average transform (box filter)
838 * for a fast approximation of a Gaussian kernel of the given radius, a non-negative number.
839 * Returns the given data.
840 */
841export function blur(data: ArrayLike<number>, radius: number): ArrayLike<number>;
842
843/**
844 * Blurs a matrix of the given width and height in-place by applying a horizontal blur of radius rx
845 * and a vertical blur of radius ry (which defaults to rx).
846 * The matrix values data are stored in a flat (one-dimensional) array.
847 * If height is not specified, it is inferred from the given width and data.length.
848 * Returns the blurred matrix {data, width, height}.
849 */
850export function blur2(data: Matrix, rx: number, ry?: number): Matrix;
851
852/**
853 * Blurs the given ImageData in-place, blurring each of the RGBA layers independently by applying an horizontal blur of radius rx
854 * and a vertical blur of radius ry (which defaults to rx).
855 * Returns the blurred ImageData.
856 */
857export function blurImage(imageData: ImageData, rx: number, ry?: number): ImageData;
858
859// --------------------------------------------------------------------------------------
860// Iterables
861// --------------------------------------------------------------------------------------
862
863/**
864 * Returns true if the given test function returns true for every value in the given iterable.
865 * This method returns as soon as test returns a non-truthy value or all values are iterated over.
866 * Equivalent to array.every.
867 */
868export function every<T>(
869 iterable: Iterable<T>,
870 test: (value: T, index: number, iterable: Iterable<T>) => unknown,
871): boolean;
872
873/**
874 * Returns true if the given test function returns true for any value in the given iterable.
875 * This method returns as soon as test returns a truthy value or all values are iterated over.
876 * Equivalent to array.some.
877 */
878export function some<T>(
879 iterable: Iterable<T>,
880 test: (value: T, index: number, iterable: Iterable<T>) => unknown,
881): boolean;
882
883/**
884 * Returns a new array containing the values from iterable, in order, for which the given test function returns true.
885 * Equivalent to array.filter.
886 */
887export function filter<T>(
888 iterable: Iterable<T>,
889 test: (value: T, index: number, iterable: Iterable<T>) => unknown,
890): T[];
891
892/**
893 * Returns a new array containing the mapped values from iterable, in order, as defined by given mapper function.
894 * Equivalent to array.map and Array.from.
895 */
896export function map<T, U>(iterable: Iterable<T>, mapper: (value: T, index: number, iterable: Iterable<T>) => U): U[];
897
898/**
899 * 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.
900 * Equivalent to array.reduce.
901 */
902export function reduce<T>(
903 iterable: Iterable<T>,
904 reducer: (previousValue: T, currentValue: T, currentIndex: number, iterable: Iterable<T>) => T,
905 initialValue?: T,
906): T;
907/**
908 * 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.
909 * Equivalent to array.reduce.
910 */
911export function reduce<T, U>(
912 iterable: Iterable<T>,
913 reducer: (previousValue: U, currentValue: T, currentIndex: number, iterable: Iterable<T>) => U,
914 initialValue: U,
915): U;
916
917/**
918 * Returns an array containing the values in the given iterable in reverse order.
919 * Equivalent to array.reverse, except that it does not mutate the given iterable.
920 */
921export function reverse<T>(iterable: Iterable<T>): T[];
922
923/**
924 * Returns an array containing the values in the given iterable in the sorted order defined by the given comparator function.
925 * If comparator is not specified, it defaults to d3.ascending.
926 * Equivalent to array.sort, except that it does not mutate the given iterable, and the comparator defaults to natural order instead of lexicographic order.
927 */
928export function sort<T>(iterable: Iterable<T>, comparator?: (a: T, b: T) => number): T[];
929/**
930 * Returns an array containing the values in the given iterable in the sorted order defined by the given accessor function.
931 * This is equivalent to a comparator using natural order.
932 * The accessor is only invoked once per element, and thus may be nondeterministic.
933 * Multiple accessors may be specified to break ties.
934 */
935export function sort<T>(iterable: Iterable<T>, ...accessors: Array<(a: T) => unknown>): T[];
936
937// --------------------------------------------------------------------------------------
938// Sets
939// --------------------------------------------------------------------------------------
940
941/**
942 * Returns a new InternSet containing every value in iterable that is not in any of the others iterables.
943 */
944export function difference<T>(iterable: Iterable<T>, ...others: Array<Iterable<T>>): InternSet<T>;
945
946/**
947 * Returns a new InternSet containing every (distinct) value that appears in any of the given iterables.
948 * The order of values in the returned set is based on their first occurrence in the given iterables.
949 */
950export function union<T>(...iterables: Array<Iterable<T>>): InternSet<T>;
951
952/**
953 * Returns a new InternSet containing every (distinct) value that appears in all of the given iterables.
954 * The order of values in the returned set is based on their first occurrence in the given iterables.
955 */
956export function intersection<T>(...iterables: Array<Iterable<T>>): InternSet<T>;
957
958/**
959 * Returns true if a is a superset of b: if every value in the given iterable b is also in the given iterable a.
960 */
961export function superset<T>(a: Iterable<T>, b: Iterable<T>): boolean;
962
963/**
964 * Returns true if a is a subset of b: if every value in the given iterable a is also in the given iterable b.
965 */
966export function subset<T>(a: Iterable<T>, b: Iterable<T>): boolean;
967
968/**
969 * Returns true if a and b are disjoint: if a and b contain no shared value.
970 */
971export function disjoint<T>(a: Iterable<T>, b: Iterable<T>): boolean;
972
973// --------------------------------------------------------------------------------------
974// Bins
975// --------------------------------------------------------------------------------------
976
977export interface Bin<Datum, Value extends number | Date | undefined> extends Array<Datum> {
978 x0: Value | undefined;
979 x1: Value | undefined;
980}
981
982/**
983 * Type definition for threshold generator which returns the count of recommended thresholds
984 */
985export type ThresholdCountGenerator<Value extends number | undefined = number | undefined> = (
986 values: ArrayLike<Value>,
987 min: number,
988 max: number,
989) => number;
990
991/**
992 * Type definition for threshold generator which returns an array of recommended numbers thresholds
993 */
994export type ThresholdNumberArrayGenerator<Value extends number | undefined> = (
995 values: ArrayLike<Value>,
996 min: number,
997 max: number,
998) => Value[];
999
1000/**
1001 * Type definition for threshold generator which returns an array of recommended dates thresholds
1002 */
1003export type ThresholdDateArrayGenerator<Value extends Date | undefined> = (
1004 values: ArrayLike<Value>,
1005 min: Date,
1006 max: Date,
1007) => Value[];
1008
1009export interface HistogramCommon<Datum, Value extends number | Date | undefined> {
1010 (data: ArrayLike<Datum>): Array<Bin<Datum, Value>>;
1011
1012 value(): (d: Datum, i: number, data: ArrayLike<Datum>) => Value;
1013 value(valueAccessor: (d: Datum, i: number, data: ArrayLike<Datum>) => Value): this;
1014}
1015
1016export interface HistogramGeneratorDate<Datum, Value extends Date | undefined> extends HistogramCommon<Datum, Date> {
1017 domain(): (values: ArrayLike<Value>) => [Date, Date];
1018 domain(domain: [Date, Date] | ((values: ArrayLike<Value>) => [Date, Date])): this;
1019
1020 thresholds(): ThresholdDateArrayGenerator<Value>;
1021 /**
1022 * Set the array of values to be used as thresholds in determining the bins.
1023 *
1024 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1025 * and the last bin.x1 is always equal to the maximum domain value.
1026 *
1027 * @param thresholds Either an array of threshold values used for binning. The elements must
1028 * be of the same type as the materialized values of the histogram.
1029 * Or a function which accepts as arguments the array of materialized values, and
1030 * optionally the domain minimum and maximum. The function calculates and returns the array of values to be used as
1031 * thresholds in determining the bins.
1032 */
1033 thresholds(thresholds: ArrayLike<Value> | ThresholdDateArrayGenerator<Value>): this;
1034}
1035
1036export interface HistogramGeneratorNumber<Datum, Value extends number | undefined>
1037 extends HistogramCommon<Datum, Value>
1038{
1039 domain(): (values: Iterable<Value>) => [number, number] | [undefined, undefined];
1040 domain(domain: [number, number] | ((values: Iterable<Value>) => [number, number] | [undefined, undefined])): this;
1041
1042 thresholds(): ThresholdCountGenerator<Value> | ThresholdNumberArrayGenerator<Value>;
1043 /**
1044 * Divide the domain uniformly into approximately count bins. IMPORTANT: This threshold
1045 * setting approach only works, when the materialized values are numbers!
1046 *
1047 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1048 * and the last bin.x1 is always equal to the maximum domain value.
1049 *
1050 * @param count Either the desired number of uniform bins or a function which accepts as arguments the array of
1051 * materialized values, and optionally the domain minimum and maximum. The function calculates and returns the
1052 * suggested number of bins.
1053 */
1054 thresholds(count: number | ThresholdCountGenerator<Value>): this;
1055 /**
1056 * Set the array of values to be used as thresholds in determining the bins.
1057 *
1058 * Any threshold values outside the domain are ignored. The first bin.x0 is always equal to the minimum domain value,
1059 * and the last bin.x1 is always equal to the maximum domain value.
1060 *
1061 * @param thresholds Either an array of threshold values used for binning. The elements must
1062 * be of the same type as the materialized values of the histogram.
1063 * Or a function which accepts as arguments the array of materialized values, and
1064 * optionally the domain minimum and maximum. The function calculates and returns the array of values to be used as
1065 * thresholds in determining the bins.
1066 */
1067 // tslint:disable-next-line:unified-signatures
1068 thresholds(thresholds: ArrayLike<Value> | ThresholdNumberArrayGenerator<Value>): this;
1069}
1070
1071/**
1072 * @deprecated Use bin instead.
1073 */
1074export function histogram(): HistogramGeneratorNumber<number, number>;
1075
1076/**
1077 * @deprecated Use bin instead.
1078 */
1079// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
1080export function histogram<Datum, Value extends number | undefined>(): HistogramGeneratorNumber<Datum, Value>;
1081
1082/**
1083 * @deprecated Use bin instead.
1084 */
1085// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
1086export function histogram<Datum, Value extends Date | undefined>(): HistogramGeneratorDate<Datum, Value>;
1087
1088export function bin(): HistogramGeneratorNumber<number, number>;
1089// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
1090export function bin<Datum, Value extends number | undefined>(): HistogramGeneratorNumber<Datum, Value>;
1091// eslint-disable-next-line @definitelytyped/no-unnecessary-generics
1092export function bin<Datum, Value extends Date | undefined>(): HistogramGeneratorDate<Datum, Value>;
1093
1094// --------------------------------------------------------------------------------------
1095// Histogram Thresholds
1096// --------------------------------------------------------------------------------------
1097
1098export function thresholdFreedmanDiaconis(values: ArrayLike<number | undefined>, min: number, max: number): number; // of type ThresholdCountGenerator
1099
1100export function thresholdScott(values: ArrayLike<number | undefined>, min: number, max: number): number; // of type ThresholdCountGenerator
1101
1102export function thresholdSturges(values: ArrayLike<number | undefined>): number; // of type ThresholdCountGenerator
1103
1104// --------------------------------------------------------------------------------------
1105// Interning
1106// --------------------------------------------------------------------------------------
1107
1108/**
1109 * 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.
1110 */
1111export class InternMap<K = any, V = any> extends Map<K, V> {
1112}
1113
1114/**
1115 * 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.
1116 */
1117export class InternSet<T = any> extends Set<T> {
1118}
Note: See TracBrowser for help on using the repository browser.