1 | // Type definitions for D3JS d3-collection module 1.0
|
2 | // Project: https://github.com/d3/d3-collection/, https://d3js.org/d3-collection
|
3 | // Definitions by: Tom Wanzek <https://github.com/tomwanzek>, Alex Ford <https://github.com/gustavderdrache>, Boris Yankov <https://github.com/borisyankov>
|
4 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
5 | // TypeScript Version: 2.3
|
6 |
|
7 | // Last module patch version validated against: 1.0.7
|
8 |
|
9 | /**
|
10 | * Reference type things that can be coerced to string implicitly
|
11 | */
|
12 | export interface Stringifiable {
|
13 | toString(): string;
|
14 | }
|
15 |
|
16 | // ---------------------------------------------------------------------
|
17 | // Objects
|
18 | // ---------------------------------------------------------------------
|
19 |
|
20 | /**
|
21 | * Returns an array containing the property names of the specified object (an associative array).
|
22 | * The order of the returned array is undefined.
|
23 | *
|
24 | * @param obj An object.
|
25 | */
|
26 | export function keys(obj: object): string[];
|
27 |
|
28 | /**
|
29 | * Returns an array containing the property values of the specified object (an associative array).
|
30 | * The order of the returned array is undefined.
|
31 | *
|
32 | * The generic refers to the data type of the values.
|
33 | *
|
34 | * @param obj An object.
|
35 | */
|
36 | export function values<T>(obj: { [key: string]: T } | ArrayLike<T>): T[];
|
37 | /**
|
38 | * Returns an array containing the property values of the specified object (an associative array).
|
39 | * The order of the returned array is undefined.
|
40 | *
|
41 | * @param obj An object.
|
42 | */
|
43 | export function values(obj: object): any[];
|
44 |
|
45 | /**
|
46 | * Returns an array containing the property keys and values of the specified object (an associative array).
|
47 | * Each entry is an object with a key and value attribute.The order of the returned array is undefined.
|
48 | *
|
49 | * The generic refers to the data type of the values.
|
50 | *
|
51 | * @param obj An object.
|
52 | */
|
53 | export function entries<T>(obj: { [key: string]: T } | ArrayLike<T>): Array<{ key: string, value: T }>;
|
54 | /**
|
55 | * Returns an array containing the property keys and values of the specified object (an associative array).
|
56 | * Each entry is an object with a key and value attribute.The order of the returned array is undefined.
|
57 | *
|
58 | * @param obj An object.
|
59 | */
|
60 | export function entries(obj: object): Array<{ key: string, value: any }>;
|
61 |
|
62 | // ---------------------------------------------------------------------
|
63 | // map / Map
|
64 | // ---------------------------------------------------------------------
|
65 |
|
66 | /**
|
67 | * A data structure similar to ES6 Maps, but with a few differences:
|
68 | * - Keys are coerced to strings.
|
69 | * - map.each, not map.forEach. (Also, no thisArg.)
|
70 | * - map.remove, not map.delete.
|
71 | * - map.entries returns an array of {key, value} objects, not an iterator of [key, value].
|
72 | * - map.size is a method, not a property; also, there’s map.empty.
|
73 | *
|
74 | * The generic refers to the data type of the map entry values.
|
75 | */
|
76 | export interface Map<T> {
|
77 | /**
|
78 | * Returns true if and only if this map has an entry for the specified key string.
|
79 | * Note: the value may be null or undefined.
|
80 | *
|
81 | * @param key Key of map entry to access.
|
82 | */
|
83 | has(key: string): boolean;
|
84 | /**
|
85 | * Returns the value for the specified key string.
|
86 | * If the map does not have an entry for the specified key, returns undefined.
|
87 | *
|
88 | * @param key Key of map entry to access.
|
89 | */
|
90 | get(key: string): T | undefined;
|
91 | /**
|
92 | * Sets the value for the specified key string and returns the updated map.
|
93 | * If the map previously had an entry for the same key string, the old entry is replaced with the new value.
|
94 | *
|
95 | * @param key Key of map entry to access.
|
96 | * @param value Value to set for entry at key.
|
97 | */
|
98 | set(key: string, value: T): this;
|
99 | /**
|
100 | * If the map has an entry for the specified key string, removes the entry and returns true.
|
101 | * Otherwise, this method does nothing and returns false.
|
102 | *
|
103 | * @param key Map key for which to remove the entry.
|
104 | */
|
105 | remove(key: string): boolean;
|
106 | /**
|
107 | * Removes all entries from this map.
|
108 | */
|
109 | clear(): void;
|
110 | /**
|
111 | * Returns an array of string keys for every entry in this map.
|
112 | * The order of the returned keys is arbitrary.
|
113 | */
|
114 | keys(): string[];
|
115 | /**
|
116 | * Returns an array of values for every entry in this map.
|
117 | * The order of the returned values is arbitrary.
|
118 | */
|
119 | values(): T[];
|
120 | /**
|
121 | * Returns an array of key-value objects for each entry in this map. The order of the returned entries is arbitrary.
|
122 | * Each entry’s key is a string, but the value can have arbitrary type.
|
123 | */
|
124 | entries(): Array<{ key: string, value: T }>;
|
125 | /**
|
126 | * Calls the specified function for each entry in this map and returns undefined.
|
127 | * The iteration order is arbitrary.
|
128 | *
|
129 | * @param func Function to call for each entry. The function is passed the entry’s value and key as arguments,
|
130 | * followed by the map itself.
|
131 | */
|
132 | each(func: (value: T, key: string, map: Map<T>) => void): void;
|
133 | /**
|
134 | * Returns true if and only if this map has zero entries.
|
135 | */
|
136 | empty(): boolean;
|
137 | /**
|
138 | * Returns the number of entries in this map.
|
139 | */
|
140 | size(): number;
|
141 | }
|
142 |
|
143 | /**
|
144 | * Constructs a new empty map.
|
145 | *
|
146 | * The generic refers to the data type of the map entry values.
|
147 | */
|
148 | export function map<T = any>(): Map<T>;
|
149 | /**
|
150 | * Constructs a new map by copying another map.
|
151 | *
|
152 | * The generic refers to the data type of the map entry values.
|
153 | *
|
154 | * @param d3Map A D3 Map.
|
155 | */
|
156 | export function map<T>(d3Map: Map<T>): Map<T>;
|
157 | /**
|
158 | * Constructs a new map by copying all enumerable properties from the specified object into this map.
|
159 | *
|
160 | * The generic refers to the data type of the map entry values.
|
161 | *
|
162 | * @param obj Object to construct the map from.
|
163 | */
|
164 | export function map<T>(obj: { [key: string]: T }): Map<T>;
|
165 | /**
|
166 | * Constructs a new map by copying all enumerable properties from the specified object into this map.
|
167 | *
|
168 | * The generic refers to the data type of the map entry values.
|
169 | *
|
170 | * @param obj Object to construct the map from.
|
171 | */
|
172 | export function map<T>(obj: { [key: number]: T }): Map<T>;
|
173 | /**
|
174 | * Constructs a new map from the elements of an array.
|
175 | * An optional key function may be specified to compute the key for each value in the array.
|
176 | *
|
177 | * The generic refers to the data type of the map entry values.
|
178 | *
|
179 | * @param array Array to convert into a map
|
180 | * @param key An optional key function. The functions is invoked for each element in the array being passed
|
181 | * the element's value , it's zero-based index in the array, and the array itself. The function must return a unique string
|
182 | * to be used as the map entry's key.
|
183 | */
|
184 | export function map<T>(array: T[], key?: (value: T, i?: number, array?: T[]) => string): Map<T>;
|
185 | /**
|
186 | * Constructs a new map by copying all enumerable properties from the specified object into this map.
|
187 | *
|
188 | * @param obj Object to construct the map from.
|
189 | */
|
190 | export function map(obj: object): Map<any>;
|
191 |
|
192 | // ---------------------------------------------------------------------
|
193 | // set / Set
|
194 | // ---------------------------------------------------------------------
|
195 |
|
196 | /**
|
197 | * A data structure similar to ES6 Sets, but with a few differences:
|
198 | *
|
199 | * - Values are coerced to strings.
|
200 | * - set.each, not set.forEach. (Also, no thisArg.)
|
201 | * - set.remove, not set.delete.
|
202 | * - set.size is a method, not a property; also, there’s set.empty.
|
203 | */
|
204 | export interface Set {
|
205 | /**
|
206 | * Returns true if and only if this set has an entry for the specified value string.
|
207 | *
|
208 | * @param value Value whose membership in the class to test.
|
209 | */
|
210 | has(value: string | Stringifiable): boolean;
|
211 | /**
|
212 | * Adds the specified value string to this set and returns the set.
|
213 | *
|
214 | * @param value Value to add to set.
|
215 | */
|
216 | add(value: string | Stringifiable): this;
|
217 | /**
|
218 | * If the set contains the specified value string, removes it and returns true.
|
219 | * Otherwise, this method does nothing and returns false.
|
220 | *
|
221 | * @param value Value to remove from set.
|
222 | */
|
223 | remove(value: string | Stringifiable): boolean;
|
224 | /**
|
225 | * Removes all values from this set.
|
226 | */
|
227 | clear(): void;
|
228 | /**
|
229 | * Returns an array of the string values in this set. The order of the returned values is arbitrary.
|
230 | * Can be used as a convenient way of computing the unique values for a set of strings.
|
231 | */
|
232 | values(): string[];
|
233 | /**
|
234 | * Calls the specified function for each value in this set, passing the value as the first two arguments (for symmetry with map.each),
|
235 | * followed by the set itself. Returns undefined.
|
236 | * The iteration order is arbitrary.
|
237 | *
|
238 | * @param func Function to call for each set element. The first and second argument of the function are both passed
|
239 | * the 'value' of the set entry for consistency with the map.each(...) signature, as a third argument the entire set is passed in.
|
240 | */
|
241 | each(func: (value: string, valueRepeat: string, set: Set) => void): void;
|
242 | /**
|
243 | * Returns true if and only if this set has zero values.
|
244 | */
|
245 | empty(): boolean;
|
246 | /**
|
247 | * Returns the number of values in this set.
|
248 | */
|
249 | size(): number;
|
250 | }
|
251 |
|
252 | /**
|
253 | * Constructs a new empty set.
|
254 | */
|
255 | export function set(): Set;
|
256 | /**
|
257 | * Constructs a new set by copying an existing set.
|
258 | *
|
259 | * @param set A D3 set.
|
260 | */
|
261 | export function set(d3Set: Set): Set;
|
262 | /**
|
263 | * Constructs a new set by adding the given array of string values to the returned set.
|
264 | *
|
265 | * @param array An array of strings of values which can be implicitly converted to strings.
|
266 | */
|
267 | export function set(array: Array<string | Stringifiable>): Set;
|
268 | /**
|
269 | * Constructs a new set from an array, adds an array of mapped string values to the returned set.
|
270 | * The specified accessor function is invoked equivalent to calling array.map(accessor) before constructing the set.
|
271 | *
|
272 | * The generic refers to the data type of the array elements.
|
273 | *
|
274 | * @param array An Array of values to map and add as set elements.
|
275 | * @param key An accessor function used to map the original array elements to string elements to be added to the set.
|
276 | * The function is invoked for each array element, being passed the element's value, it's zero-based index in the array, and the array itself.
|
277 | */
|
278 | export function set<T>(array: T[], key: (value: T, index: number, array: T[]) => string): Set;
|
279 |
|
280 | // ---------------------------------------------------------------------
|
281 | // nest / Nest
|
282 | // ---------------------------------------------------------------------
|
283 |
|
284 | /**
|
285 | * A more formal definition of the nested array returned by Nest.entries(...). This data structure is intended as a reference only.
|
286 | *
|
287 | * As the union types cannot be ex ante simplified without knowledge
|
288 | * of the nesting level (number of key(...) operations) and whether the data were rolled-up, this data structure becomes cumbersome
|
289 | * to use in practice. This is particularly true for discrimination of array element types.
|
290 | * The use of the rollup function, or lack thereof, also determines whether NestedArray has the 'values' property
|
291 | * with an array of type Datum at leaf level, or has a rolled-up 'value' property.
|
292 | */
|
293 | export interface NestedArray<Datum, RollupType> extends Array<{ key: string, values: NestedArray<Datum, RollupType> | Datum[] | undefined, value: RollupType | undefined }> { }
|
294 |
|
295 | /**
|
296 | * A more formal definition of the nested array returned by Nest.map(...). This data structure is intended as a reference only.
|
297 | *
|
298 | * As the union types cannot be ex ante simplified without knowledge
|
299 | * of the nesting level (number of key(...) operations) and whether the data were rolled-up, this data structure becomes cumbersome
|
300 | * to use in practice.
|
301 | */
|
302 | export interface NestedMap<Datum, RollupType> extends Map<NestedMap<Datum, RollupType> | Datum[] | RollupType> { }
|
303 |
|
304 | /**
|
305 | * A more formal definition of the nested array returned by Nest.object(...). This data structure is intended as a reference only.
|
306 | *
|
307 | * As the union types cannot be ex ante simplified without knowledge
|
308 | * of the nesting level (number of key(...) operations) and whether the data were rolled-up, this data structure becomes cumbersome
|
309 | * to use in practice.
|
310 | */
|
311 | export interface NestedObject<Datum, RollupType> {
|
312 | [key: string]: NestedObject<Datum, RollupType> | Datum[] | RollupType;
|
313 | }
|
314 |
|
315 | /**
|
316 | * A nest operator for generating nested data structures from arrays.
|
317 | *
|
318 | * Nesting allows elements in an array to be grouped into a hierarchical tree structure;
|
319 | * think of it like the GROUP BY operator in SQL, except you can have multiple levels of grouping, and the resulting output is a tree rather than a flat table.
|
320 | * The levels in the tree are specified by key functions. The leaf nodes of the tree can be sorted by value, while the internal nodes can be sorted by key.
|
321 | * An optional rollup function will collapse the elements in each leaf node using a summary function.
|
322 | * The nest operator is reusable, and does not retain any references to the data that is nested.
|
323 | *
|
324 | * The first generic refers to the data type of the array elements on which the nest operator will
|
325 | * be invoked.
|
326 | *
|
327 | * The second generic refers to the data type returned by the roll-up function to be used with the
|
328 | * nest operator.
|
329 | */
|
330 | export interface Nest<Datum, RollupType> {
|
331 | /**
|
332 | * Registers a new key function and returns this nest operator.
|
333 | * The key function will be invoked for each element in the input array and must return a string identifier to assign the element to its group.
|
334 | * Most often, the function is a simple accessor. (Keys functions are not passed the input array index.)
|
335 | *
|
336 | * Each time a key is registered, it is pushed onto the end of the internal array of keys,
|
337 | * and the nest operator applies an additional level of nesting.
|
338 | *
|
339 | * @param func A key accessor function being invoked for each element.
|
340 | */
|
341 | key(func: (datum: Datum) => string): this;
|
342 | /**
|
343 | * Sorts key values for the current key using the specified comparator function, such as d3.ascending or d3.descending.
|
344 | *
|
345 | * If no comparator is specified for the current key, the order in which keys will be returned is undefined.
|
346 | *
|
347 | * Note that this only affects the result of nest.entries;
|
348 | * the order of keys returned by nest.map and nest.object is always undefined, regardless of comparator.
|
349 | *
|
350 | * @param comparator A comparator function which returns a negative value if, according to the sorting criterion,
|
351 | * a is less than b, or a positive value if a is greater than b, or 0 if the two values are the same under the sorting criterion.
|
352 | */
|
353 | sortKeys(comparator: (a: string, b: string) => number): this;
|
354 | /**
|
355 | * Sorts leaf elements using the specified comparator function, such as d3.ascending or d3.descending.
|
356 | * This is roughly equivalent to sorting the input array before applying the nest operator;
|
357 | * however it is typically more efficient as the size of each group is smaller.
|
358 | *
|
359 | * If no value comparator is specified, elements will be returned in the order they appeared in the input array.
|
360 | * This applies to nest.map, nest.entries and nest.object.
|
361 | *
|
362 | * @param comparator A comparator function which returns a negative value if, according to the sorting criterion,
|
363 | * a is less than b, or a positive value if a is greater than b, or 0 if the two values are the same under the sorting criterion.
|
364 | */
|
365 | sortValues(comparator: (a: Datum, b: Datum) => number): this;
|
366 | /**
|
367 | * Specifies a rollup function to be applied on each group of leaf elements and returns this nest operator.
|
368 | * The return value of the rollup function will replace the array of leaf values in either the associative array returned by nest.map or nest.object;
|
369 | * for nest.entries, it replaces the leaf entry.values with entry.value.
|
370 | *
|
371 | * If a leaf comparator is specified, the leaf elements are sorted prior to invoking the rollup function.
|
372 | *
|
373 | * @param func A function computing the rollup value for a group of leaf elements.
|
374 | */
|
375 | rollup(func: (values: Datum[]) => RollupType): this;
|
376 | /**
|
377 | * Applies the nest operator to the specified array, returning a nested map.
|
378 | *
|
379 | * Each entry in the returned map corresponds to a distinct key value returned by the first key function.
|
380 | * The entry value depends on the number of registered key functions: if there is an additional key, the value is another map;
|
381 | * otherwise, the value is the array of elements filtered from the input array that have the given key value.
|
382 | *
|
383 | * NOTE:
|
384 | *
|
385 | * Strictly speaking the return type of this method is:
|
386 | *
|
387 | * (1) NestedMap<Datum, RollupType>, if at least one key function was defined,
|
388 | *
|
389 | * (2) Datum[], if neither a key nor a rollup function were defined, and
|
390 | *
|
391 | * (3) RollupType, if no keys, but a rollup function were defined.
|
392 | *
|
393 | * Since (2) and (3) are edge cases with little to no practical relevance, they have been omitted in favour of ease-of-use.
|
394 | *
|
395 | * Should you determine that this simplification creates an issue in practice, please file an issue on
|
396 | * https://github.com/DefinitelyTyped/DefinitelyTyped.
|
397 | *
|
398 | * The formal, generalized return type under (1) is cumbersome to work with in practice. The recommended approach
|
399 | * is to define the type of the variable being assigned the return value using knowledge specific to the use-case at hand.
|
400 | * I.e. making use of knowing how many keys are applied, and the nature of any roll-up function will make working with
|
401 | * the variable more meaningful, despite the compromise in type-safety.
|
402 | *
|
403 | * @param array An array to create a nested data structure from.
|
404 | */
|
405 | map(array: Datum[]): Map<any>;
|
406 | /**
|
407 | * Applies the nest operator to the specified array, returning a nested object.
|
408 | * Each entry in the returned associative array corresponds to a distinct key value returned by the first key function.
|
409 | * The entry value depends on the number of registered key functions: if there is an additional key, the value is another associative array;
|
410 | * otherwise, the value is the array of elements filtered from the input array that have the given key value.
|
411 | *
|
412 | * WARNING: this method is unsafe if any of the keys conflict with built-in JavaScript properties, such as __proto__.
|
413 | * If you cannot guarantee that the keys will be safe, you should use nest.map instead.
|
414 | *
|
415 | * NOTE:
|
416 | *
|
417 | * Strictly speaking the return type of this method is:
|
418 | *
|
419 | * (1) NestedObject<Datum, RollupType>, if at least one key function was defined,
|
420 | *
|
421 | * (2) Datum[], if neither a key nor a rollup function were defined, and
|
422 | *
|
423 | * (3) RollupType, if no keys, but a rollup function were defined.
|
424 | *
|
425 | * Since (2) and (3) are edge cases with little to no practical relevance, they have been omitted in favour of ease-of-use.
|
426 | *
|
427 | * Should you determine that this simplification creates an issue in practice, please file an issue on
|
428 | * https://github.com/DefinitelyTyped/DefinitelyTyped.
|
429 | *
|
430 | * The formal, generalized return type under (1) is cumbersome to work with in practice. The recommended approach
|
431 | * is to define the type of the variable being assigned the return value using knowledge specific to the use-case at hand.
|
432 | * I.e. making use of knowing how many keys are applied, and the nature of any roll-up function will make working with
|
433 | * the variable more meaningful, despite the compromise in type-safety.
|
434 | *
|
435 | * @param array An array to create a nested data structure from.
|
436 | */
|
437 | object(array: Datum[]): { [key: string]: any };
|
438 | /**
|
439 | * Applies the nest operator to the specified array, returning an array of key-values entries.
|
440 | * Conceptually, this is similar to applying map.entries to the associative array returned by nest.map,
|
441 | * but it applies to every level of the hierarchy rather than just the first (outermost) level.
|
442 | * Each entry in the returned array corresponds to a distinct key value returned by the first key function.
|
443 | * The entry value depends on the number of registered key functions: if there is an additional key, the value is another nested array of entries;
|
444 | * otherwise, the value is the array of elements filtered from the input array that have the given key value.
|
445 | *
|
446 | * NOTE:
|
447 | *
|
448 | * Strictly speaking the return type of this method is:
|
449 | *
|
450 | * (1) NestedArray<Datum, RollupType>, if at least one key function was defined,
|
451 | *
|
452 | * (2) Datum[], if neither a key nor a rollup function were defined, and
|
453 | *
|
454 | * (3) RollupType, if no keys, but a rollup function were defined.
|
455 | *
|
456 | * Since (2) and (3) are edge cases with little to no practical relevance, they have been omitted in favour of ease-of-use.
|
457 | *
|
458 | * Should you determine that this simplification creates an issue in practice, please file an issue on
|
459 | * https://github.com/DefinitelyTyped/DefinitelyTyped.
|
460 | *
|
461 | * The formal, generalized return type under (1) is cumbersome to work with in practice. The recommended approach
|
462 | * is to define the type of the variable being assigned the return value using knowledge specific to the use-case at hand.
|
463 | * I.e. making use of knowing how many keys are applied, and the nature of any roll-up function will make working with
|
464 | * the variable more meaningful, despite the compromise in type-safety.
|
465 | *
|
466 | * @param array An array to create a nested data structure from.
|
467 | */
|
468 | entries(array: Datum[]): Array<{ key: string; values: any; value: RollupType | undefined }>;
|
469 | }
|
470 |
|
471 | /**
|
472 | * Creates a new nest operator.
|
473 | *
|
474 | * The first generic refers to the data type of the array elements on which the nest operator will
|
475 | * be invoked.
|
476 | *
|
477 | * The second generic refers to the data type returned by the roll-up function to be used with the
|
478 | * nest operator. If not explicitly set, this generic parameter defaults to undefined, implying that
|
479 | * no rollup function will be applied.
|
480 | */
|
481 | export function nest<Datum, RollupType = undefined>(): Nest<Datum, RollupType>;
|