|
| 1 | +/* |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2019 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +// TypeScript Version: 2.0 |
| 20 | + |
| 21 | +/// <reference types="@stdlib/types"/> |
| 22 | + |
| 23 | +import { Collection } from '@stdlib/types/object'; |
| 24 | + |
| 25 | +type Returns = 'values' | 'indices' | '*'; |
| 26 | + |
| 27 | +/** |
| 28 | +* Interface defining function options. |
| 29 | +*/ |
| 30 | +interface Options { |
| 31 | + /** |
| 32 | + * Execution context. |
| 33 | + */ |
| 34 | + thisArg?: any; |
| 35 | + |
| 36 | + /** |
| 37 | + * If `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned. |
| 38 | + */ |
| 39 | + returns?: Returns; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | +* Returns a boolean indicating which group an element in an collection belongs to. |
| 44 | +* |
| 45 | +* @returns boolean indicating whether an element in a collection should be placed in the first or second group |
| 46 | +*/ |
| 47 | +type Nullary = () => boolean; |
| 48 | + |
| 49 | +/** |
| 50 | +* Returns a boolean indicating which group an element in an collection belongs to. |
| 51 | +* |
| 52 | +* @param value - collection value |
| 53 | +* @returns boolean indicating whether an element in a collection should be placed in the first or second group |
| 54 | +*/ |
| 55 | +type Unary = ( value: any ) => boolean; |
| 56 | + |
| 57 | +/** |
| 58 | +* Returns a boolean indicating which group an element in an collection belongs to. |
| 59 | +* |
| 60 | +* @param value - collection value |
| 61 | +* @param index - collection index |
| 62 | +* @returns boolean indicating whether an element in a collection should be placed in the first or second group |
| 63 | +*/ |
| 64 | +type Binary = ( value: any, index: number ) => boolean; |
| 65 | + |
| 66 | +/** |
| 67 | +* Returns a boolean indicating which group an element in an collection belongs to. |
| 68 | +* |
| 69 | +* @param value - collection value |
| 70 | +* @param index - collection index |
| 71 | +* @returns boolean indicating whether an element in a collection should be placed in the first or second group |
| 72 | +*/ |
| 73 | +type Predicate = Nullary | Unary | Binary; |
| 74 | + |
| 75 | +/** |
| 76 | +* Splits values into two groups according to a predicate function. |
| 77 | +* |
| 78 | +* ## Notes |
| 79 | +* |
| 80 | +* - When invoked, the predicate function is provided two arguments: |
| 81 | +* |
| 82 | +* - `value`: collection value |
| 83 | +* - `index`: collection index |
| 84 | +* |
| 85 | +* - If a predicate function returns a truthy value, a collection value is placed in the first group; otherwise, a collection value is placed in the second group. |
| 86 | +* |
| 87 | +* - If provided an empty collection, the function returns an empty array. |
| 88 | +* |
| 89 | +* |
| 90 | +* @param collection - input collection |
| 91 | +* @param predicate - predicate function indicating which group an element in the input collection belongs to |
| 92 | +* @returns group results |
| 93 | +* |
| 94 | +* @example |
| 95 | +* function predicate( v ) { |
| 96 | +* return v[ 0 ] === 'b'; |
| 97 | +* } |
| 98 | +* var arr = [ 'beep', 'boop', 'foo', 'bar' ]; |
| 99 | +* |
| 100 | +* var out = bifurcateBy( arr, predicate ); |
| 101 | +* // returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ] |
| 102 | +*/ |
| 103 | +declare function bifurcateBy( collection: Collection, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length |
| 104 | + |
| 105 | +/** |
| 106 | +* Splits values into two groups according to a predicate function. |
| 107 | +* |
| 108 | +* ## Notes |
| 109 | +* |
| 110 | +* - When invoked, the predicate function is provided two arguments: |
| 111 | +* |
| 112 | +* - `value`: collection value |
| 113 | +* - `index`: collection index |
| 114 | +* |
| 115 | +* - If a predicate function returns a truthy value, a collection value is placed in the first group; otherwise, a collection value is placed in the second group. |
| 116 | +* |
| 117 | +* - If provided an empty collection, the function returns an empty array. |
| 118 | +* |
| 119 | +* |
| 120 | +* @param collection - input collection |
| 121 | +* @param options - function options |
| 122 | +* @param options.thisArg - execution context |
| 123 | +* @param options.returns - if `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned (default: 'values') |
| 124 | +* @param predicate - predicate function indicating which group an element in the input collection belongs to |
| 125 | +* @returns group results |
| 126 | +* |
| 127 | +* |
| 128 | +* @example |
| 129 | +* function predicate( v ) { |
| 130 | +* return v[ 0 ] === 'b'; |
| 131 | +* } |
| 132 | +* var arr = [ 'beep', 'boop', 'foo', 'bar' ]; |
| 133 | +* |
| 134 | +* var opts = { |
| 135 | +* 'returns': 'indices' |
| 136 | +* }; |
| 137 | +* var out = bifurcateBy( arr, opts, predicate ); |
| 138 | +* // returns [ [ 0, 1, 3 ], [ 2 ] ] |
| 139 | +* |
| 140 | +* @example |
| 141 | +* function predicate( v ) { |
| 142 | +* return v[ 0 ] === 'b'; |
| 143 | +* } |
| 144 | +* var arr = [ 'beep', 'boop', 'foo', 'bar' ]; |
| 145 | +* |
| 146 | +* var opts = { |
| 147 | +* 'returns': '*' |
| 148 | +* }; |
| 149 | +* var out = bifurcateBy( arr, opts, predicate ); |
| 150 | +* // returns [ [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], [ [ 2, 'foo' ] ] ] |
| 151 | +*/ |
| 152 | +declare function bifurcateBy( collection: Collection, options: Options, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length |
| 153 | + |
| 154 | + |
| 155 | +// EXPORTS // |
| 156 | + |
| 157 | +export = bifurcateBy; |
0 commit comments