diff --git a/d3-array/d3-array-tests.ts b/d3-array/d3-array-tests.ts index 74b749bea3..225cec33ef 100644 --- a/d3-array/d3-array-tests.ts +++ b/d3-array/d3-array-tests.ts @@ -44,13 +44,13 @@ let num: number; let date: Date; let numOrUndefined: number | undefined; -let strOrUndefined: string | undefined; -let numericOrUndefined: NumCoercible | undefined; -let dateOrUndefined: Date | undefined; +let strOrUndefined: string |  undefined; +let numericOrUndefined: NumCoercible |  undefined; +let dateOrUndefined: Date |  undefined; let numOrUndefinedExtent: [number, number] | [undefined, undefined]; let strOrUndefinedExtent: [string, string] | [undefined, undefined]; let numericOrUndefinedExtent: [NumCoercible, NumCoercible] | [undefined, undefined]; -let dateMixedOrUndefined: [Date , Date] | [undefined, undefined]; +let dateMixedOrUndefined: [Date, Date] | [undefined, undefined]; let mixedOrUndefinedExtent: [d3Array.Primitive | NumCoercible, d3Array.Primitive | NumCoercible] | [undefined, undefined]; let dateOrUndefinedExtent: [Date, Date] | [undefined, undefined]; @@ -330,12 +330,23 @@ mergedArray = d3Array.merge(testArrays); // inferred type mergedArray = d3Array.merge(testArrays); // explicit type // mergedArray = d3Array.merge([[10, 40, 30], [15, 30]]); // fails, type mismatch +// cross() --------------------------------------------------------------------- + +let crossed: Array<[string, number]>; + +crossed = d3Array.cross(['x', 'y'], [1, 2]); + +let strArray: string[] = d3Array.cross([2, 3], [5, 6], (a, b) => (a + b) + 'px') + + // pairs() --------------------------------------------------------------------- let pairs: Array<[MixedObject, MixedObject]>; pairs = d3Array.pairs(mergedArray); +numbersArray = d3Array.pairs(mergedArray, (a, b) => b.num - a.num); + // permute() ------------------------------------------------------------------- // getting a permutation of array elements diff --git a/d3-array/index.d.ts b/d3-array/index.d.ts index 336eb2bc83..d938a8c032 100644 --- a/d3-array/index.d.ts +++ b/d3-array/index.d.ts @@ -1,9 +1,9 @@ -// Type definitions for D3JS d3-array module 1.0 +// Type definitions for D3JS d3-array module 1.1 // Project: https://github.com/d3/d3-array // Definitions by: Alex Ford , Boris Yankov , Tom Wanzek // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// Last module patch version validated against: 1.0.2 +// Last module patch version validated against: 1.1.0 // -------------------------------------------------------------------------- // Shared Types and Interfaces @@ -181,6 +181,26 @@ export function descending(a: Primitive | undefined, b: Primitive | undefined): // Transforming Arrays // -------------------------------------------------------------------------------------- +/** + * Returns the Cartesian product of the two arrays a and b. + * For each element i in the specified array a and each element j in the specified array b, in order, + * it creates a two-element array for each pair. + * + * @param a First input array. + * @param b Second input array. + */ +export function cross(a: S[], b: T[]): Array<[S, T]>; + +/** + * Returns the Cartesian product of the two arrays a and b. + * For each element i in the specified array a and each element j in the specified array b, in order, + * invokes the specified reducer function passing the element i and element j. + * + * @param a First input array. + * @param b Second input array. + * @param reducer A reducer function taking as input an element from "a" and "b" and returning a reduced value. + */ +export function cross(a: S[], b: T[], reducer: (a: S, b: T) => U): U[]; /** * Merges the specified arrays into a single array. @@ -190,8 +210,19 @@ export function merge(arrays: T[][]): T[]; /** * For each adjacent pair of elements in the specified array, returns a new array of tuples of elements i and i - 1. * Returns the empty array if the input array has fewer than two elements. + * + * @param array Array of input elements */ export function pairs(array: T[]): Array<[T, T]>; +/** + * 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. + * Returns the resulting array of pair-wise reduced elements. + * Returns the empty array if the input array has fewer than two elements. + * + * @param array Array of input elements + * @param reducer A reducer function taking as input to adjecent elements of the input array and returning a reduced value. + */ +export function pairs(array: T[], reducer: (a: T, b: T) => U): U[]; /** * Given the specified array, return an array corresponding to the list of indices in 'keys'.