1 | /*! *****************************************************************************
|
2 | Copyright (c) Microsoft Corporation. All rights reserved.
|
3 | Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
4 | this file except in compliance with the License. You may obtain a copy of the
|
5 | License at http://www.apache.org/licenses/LICENSE-2.0
|
6 |
|
7 | THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
8 | KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
|
9 | WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
|
10 | MERCHANTABLITY OR NON-INFRINGEMENT.
|
11 |
|
12 | See the Apache Version 2.0 License for specific language governing permissions
|
13 | and limitations under the License.
|
14 | ***************************************************************************** */
|
15 |
|
16 |
|
17 |
|
18 | /// <reference no-default-lib="true"/>
|
19 |
|
20 |
|
21 | type FlatArray<Arr, Depth extends number> = {
|
22 | "done": Arr,
|
23 | "recur": Arr extends ReadonlyArray<infer InnerArr>
|
24 | ? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]>
|
25 | : Arr
|
26 | }[Depth extends -1 ? "done" : "recur"];
|
27 |
|
28 | interface ReadonlyArray<T> {
|
29 |
|
30 | /**
|
31 | * Calls a defined callback function on each element of an array. Then, flattens the result into
|
32 | * a new array.
|
33 | * This is identical to a map followed by flat with depth 1.
|
34 | *
|
35 | * @param callback A function that accepts up to three arguments. The flatMap method calls the
|
36 | * callback function one time for each element in the array.
|
37 | * @param thisArg An object to which the this keyword can refer in the callback function. If
|
38 | * thisArg is omitted, undefined is used as the this value.
|
39 | */
|
40 | flatMap<U, This = undefined> (
|
41 | callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
|
42 | thisArg?: This
|
43 | ): U[]
|
44 |
|
45 |
|
46 | /**
|
47 | * Returns a new array with all sub-array elements concatenated into it recursively up to the
|
48 | * specified depth.
|
49 | *
|
50 | * @param depth The maximum recursion depth
|
51 | */
|
52 | flat<A, D extends number = 1>(
|
53 | this: A,
|
54 | depth?: D
|
55 | ): FlatArray<A, D>[]
|
56 | }
|
57 |
|
58 | interface Array<T> {
|
59 |
|
60 | /**
|
61 | * Calls a defined callback function on each element of an array. Then, flattens the result into
|
62 | * a new array.
|
63 | * This is identical to a map followed by flat with depth 1.
|
64 | *
|
65 | * @param callback A function that accepts up to three arguments. The flatMap method calls the
|
66 | * callback function one time for each element in the array.
|
67 | * @param thisArg An object to which the this keyword can refer in the callback function. If
|
68 | * thisArg is omitted, undefined is used as the this value.
|
69 | */
|
70 | flatMap<U, This = undefined> (
|
71 | callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
|
72 | thisArg?: This
|
73 | ): U[]
|
74 |
|
75 | /**
|
76 | * Returns a new array with all sub-array elements concatenated into it recursively up to the
|
77 | * specified depth.
|
78 | *
|
79 | * @param depth The maximum recursion depth
|
80 | */
|
81 | flat<A, D extends number = 1>(
|
82 | this: A,
|
83 | depth?: D
|
84 | ): FlatArray<A, D>[]
|
85 | }
|