|
| 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 | +type Returns = 'values' | 'indices' | '*'; |
| 22 | + |
| 23 | +/** |
| 24 | +* Interface defining function options. |
| 25 | +*/ |
| 26 | +interface Options { |
| 27 | + /** |
| 28 | + * Execution context. |
| 29 | + */ |
| 30 | + thisArg?: any; |
| 31 | + |
| 32 | + /** |
| 33 | + * If `values`, values are returned; if `indices`, indices are returned; if `*`, both indices and values are returned. |
| 34 | + */ |
| 35 | + returns?: Returns; |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | +* Returns a boolean indicating which group an object's own and inherited property values belongs to. |
| 40 | +* |
| 41 | +* @returns boolean indicating whether a property value should be placed in the first or second group |
| 42 | +*/ |
| 43 | +type Nullary = () => boolean; |
| 44 | + |
| 45 | +/** |
| 46 | +* Returns a boolean indicating which group an object's own and inherited property values belongs to. |
| 47 | +* |
| 48 | +* @param value - object value |
| 49 | +* @returns boolean indicating whether a property value should be placed in the first or second group |
| 50 | +*/ |
| 51 | +type Unary = ( value: any ) => boolean; |
| 52 | + |
| 53 | +/** |
| 54 | +* Returns a boolean indicating which group an object's own and inherited property values belongs to. |
| 55 | +* |
| 56 | +* @param value - object value |
| 57 | +* @param key - object key |
| 58 | +* @returns boolean indicating whether a property value should be placed in the first or second group |
| 59 | +*/ |
| 60 | +type Binary = ( value: any, key: string | symbol ) => boolean; |
| 61 | + |
| 62 | +/** |
| 63 | +* Returns a boolean indicating which group an object's own and inherited property values belongs to. |
| 64 | +* |
| 65 | +* @param value - object value |
| 66 | +* @param key - object key |
| 67 | +* @returns boolean indicating whether a property value should be placed in the first or second group |
| 68 | +*/ |
| 69 | +type Predicate = Nullary | Unary | Binary; |
| 70 | + |
| 71 | +/** |
| 72 | +* Splits an object's own property values into two groups according to a predicate function. |
| 73 | +* |
| 74 | +* @param obj - input object |
| 75 | +* @param predicate - predicate function indicating which group an element in the input object belongs to |
| 76 | +* @returns group results |
| 77 | +* |
| 78 | +* @example |
| 79 | +* function predicate( v ) { |
| 80 | +* return v[ 0 ] === 'b'; |
| 81 | +* } |
| 82 | +* var obj = { |
| 83 | +* 'a': 'beep', |
| 84 | +* 'b': 'boop', |
| 85 | +* 'c': 'foo', |
| 86 | +* 'd': 'bar' |
| 87 | +* }; |
| 88 | +* var out = bifurcateOwn( obj, predicate ); |
| 89 | +* // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ] |
| 90 | +* |
| 91 | +* @example |
| 92 | +* function predicate( v ) { |
| 93 | +* return v[ 0 ] === 'b'; |
| 94 | +* } |
| 95 | +* var obj = { |
| 96 | +* 'a': 'beep', |
| 97 | +* 'b': 'boop', |
| 98 | +* 'c': 'foo', |
| 99 | +* 'd': 'bar' |
| 100 | +* }; |
| 101 | +* var opts = { |
| 102 | +* 'returns': 'keys' |
| 103 | +* }; |
| 104 | +* var out = bifurcateOwn( obj, opts, predicate ); |
| 105 | +* // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ] |
| 106 | +* |
| 107 | +* @example |
| 108 | +* function predicate( v ) { |
| 109 | +* return v[ 0 ] === 'b'; |
| 110 | +* } |
| 111 | +* var obj = { |
| 112 | +* 'a': 'beep', |
| 113 | +* 'b': 'boop', |
| 114 | +* 'c': 'foo', |
| 115 | +* 'd': 'bar' |
| 116 | +* }; |
| 117 | +* var opts = { |
| 118 | +* 'returns': '*' |
| 119 | +* }; |
| 120 | +* var out = bifurcateOwn( obj, opts, predicate ); |
| 121 | +* // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ] |
| 122 | +*/ |
| 123 | +declare function bifurcateOwn( obj: any, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length |
| 124 | + |
| 125 | +/** |
| 126 | +* Splits an object's own property values into two groups according to a predicate function. |
| 127 | +* |
| 128 | +* @param obj - input object |
| 129 | +* @param options - function options |
| 130 | +* @param options.thisArg - execution context |
| 131 | +* @param options.returns - if `values`, values are returned; if `keys`, keys are returned; if `*`, both keys and values are returned |
| 132 | +* @param predicate - predicate function indicating which group an element in the input object belongs to |
| 133 | +* @returns group results |
| 134 | +* |
| 135 | +* @example |
| 136 | +* function predicate( v ) { |
| 137 | +* return v[ 0 ] === 'b'; |
| 138 | +* } |
| 139 | +* var obj = { |
| 140 | +* 'a': 'beep', |
| 141 | +* 'b': 'boop', |
| 142 | +* 'c': 'foo', |
| 143 | +* 'd': 'bar' |
| 144 | +* }; |
| 145 | +* var out = bifurcateOwn( obj, predicate ); |
| 146 | +* // e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ] |
| 147 | +* |
| 148 | +* @example |
| 149 | +* function predicate( v ) { |
| 150 | +* return v[ 0 ] === 'b'; |
| 151 | +* } |
| 152 | +* var obj = { |
| 153 | +* 'a': 'beep', |
| 154 | +* 'b': 'boop', |
| 155 | +* 'c': 'foo', |
| 156 | +* 'd': 'bar' |
| 157 | +* }; |
| 158 | +* var opts = { |
| 159 | +* 'returns': 'keys' |
| 160 | +* }; |
| 161 | +* var out = bifurcateOwn( obj, opts, predicate ); |
| 162 | +* // e.g., returns [ [ 'a', 'b', 'd' ], [ 'c' ] ] |
| 163 | +* |
| 164 | +* @example |
| 165 | +* function predicate( v ) { |
| 166 | +* return v[ 0 ] === 'b'; |
| 167 | +* } |
| 168 | +* var obj = { |
| 169 | +* 'a': 'beep', |
| 170 | +* 'b': 'boop', |
| 171 | +* 'c': 'foo', |
| 172 | +* 'd': 'bar' |
| 173 | +* }; |
| 174 | +* var opts = { |
| 175 | +* 'returns': '*' |
| 176 | +* }; |
| 177 | +* var out = bifurcateOwn( obj, opts, predicate ); |
| 178 | +* // e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ] |
| 179 | +*/ |
| 180 | +declare function bifurcateOwn( obj: any, options: Options, predicate: Predicate ): Array<Array<any>>; // tslint-disable-line max-line-length |
| 181 | + |
| 182 | + |
| 183 | +// EXPORTS // |
| 184 | + |
| 185 | +export = bifurcateOwn; |
0 commit comments