|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2018 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# bifurcateOwn |
| 22 | + |
| 23 | +> Split an object's **own** property values into two groups according to a predicate function. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var bifurcateOwn = require( '@stdlib/object/bifurcate-own' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### bifurcateOwn( obj, \[options,] predicate ) |
| 44 | + |
| 45 | +Splits an object's **own** property values into two groups according to a `predicate` function, which specifies which group a value in the input `object` belongs to. If a `predicate` function returns a truthy value, a value belongs to the first group; otherwise, a value belongs to the second group. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function predicate( v ) { |
| 49 | + return v[ 0 ] === 'b'; |
| 50 | +} |
| 51 | +var obj = { |
| 52 | + 'a': 'beep', |
| 53 | + 'b': 'boop', |
| 54 | + 'c': 'foo', |
| 55 | + 'd': 'bar' |
| 56 | +}; |
| 57 | + |
| 58 | +var out = bifurcateOwn( obj, predicate ); |
| 59 | +// e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ] |
| 60 | +``` |
| 61 | + |
| 62 | +A `predicate` function is provided two arguments: |
| 63 | + |
| 64 | +- **value**: object value. |
| 65 | +- **key**: object index. |
| 66 | + |
| 67 | +```javascript |
| 68 | +function predicate( v, k ) { |
| 69 | + console.log( '%s: %s', k, v ); |
| 70 | + return v[ 0 ] === 'b'; |
| 71 | +} |
| 72 | +var obj = { |
| 73 | + 'a': 'beep', |
| 74 | + 'b': 'boop', |
| 75 | + 'c': 'foo', |
| 76 | + 'd': 'bar' |
| 77 | +}; |
| 78 | + |
| 79 | +var out = bifurcateOwn( obj, predicate ); |
| 80 | +// e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ] |
| 81 | +``` |
| 82 | + |
| 83 | +The function accepts the following `options`: |
| 84 | + |
| 85 | +- **returns**: specifies the output format. If the option equals `'values'`, the function outputs values. If the option equals `'keys'`, the function outputs keys. If the option equals `'*'`, the function outputs both keys and values. Default: `'values'`. |
| 86 | +- **thisArg**: execution context. |
| 87 | + |
| 88 | +By default, the function returns object values. To return object keys, set the `returns` option to `'keys'`. |
| 89 | + |
| 90 | +```javascript |
| 91 | +function predicate( v ) { |
| 92 | + return v[ 0 ] === 'b'; |
| 93 | +} |
| 94 | +var obj = { |
| 95 | + 'a': 'beep', |
| 96 | + 'b': 'boop', |
| 97 | + 'c': 'foo', |
| 98 | + 'd': 'bar' |
| 99 | +}; |
| 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 | + |
| 108 | +To return key-value pairs, set the `returns` option to `'*'`. |
| 109 | + |
| 110 | +```javascript |
| 111 | +function predicate( v ) { |
| 112 | + return v[ 0 ] === 'b'; |
| 113 | +} |
| 114 | +var obj = { |
| 115 | + 'a': 'beep', |
| 116 | + 'b': 'boop', |
| 117 | + 'c': 'foo', |
| 118 | + 'd': 'bar' |
| 119 | +}; |
| 120 | + |
| 121 | +var opts = { |
| 122 | + 'returns': '*' |
| 123 | +}; |
| 124 | +var out = bifurcateOwn( obj, opts, predicate ); |
| 125 | +// e.g., returns [ [ [ 'a', 'beep' ], [ 'b', 'boop' ], [ 'd', 'bar' ] ], [ [ 'c', 'foo' ] ] ] |
| 126 | +``` |
| 127 | + |
| 128 | +To set the `predicate` execution context, provide a `thisArg`. |
| 129 | + |
| 130 | +```javascript |
| 131 | +function predicate( v ) { |
| 132 | + this.count += 1; |
| 133 | + return v[ 0 ] === 'b'; |
| 134 | +} |
| 135 | +var context = { |
| 136 | + 'count': 0 |
| 137 | +}; |
| 138 | +var opts = { |
| 139 | + 'thisArg': context |
| 140 | +}; |
| 141 | +var obj = { |
| 142 | + 'a': 'beep', |
| 143 | + 'b': 'boop', |
| 144 | + 'c': 'foo', |
| 145 | + 'd': 'bar' |
| 146 | +}; |
| 147 | +var out = bifurcateOwn( obj, opts, predicate ); |
| 148 | +// e.g., returns [ [ 'beep', 'boop', 'bar' ], [ 'foo' ] ] |
| 149 | + |
| 150 | +console.log( context.count ); |
| 151 | +// => 4 |
| 152 | +``` |
| 153 | + |
| 154 | +</section> |
| 155 | + |
| 156 | +<!-- /.usage --> |
| 157 | + |
| 158 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 159 | + |
| 160 | +<section class="notes"> |
| 161 | + |
| 162 | +## Notes |
| 163 | + |
| 164 | +- Iteration order is **not** guaranteed, as `object` key enumeration is not specified according to the [ECMAScript specification][ecma-262-for-in]. In practice, however, most engines use insertion order to sort an `object`'s keys, thus allowing for deterministic iteration. |
| 165 | +- Because iteration order is **not** guaranteed, result order is **not** guaranteed. |
| 166 | +- The function determines the list of own enumerable properties **before** invoking the provided function. Hence, any modifications made to the input `object` **after** calling this function (such as adding and removing properties) will **not** affect the list of visited properties. |
| 167 | + |
| 168 | +</section> |
| 169 | + |
| 170 | +<!-- /.notes --> |
| 171 | + |
| 172 | +<!-- Package usage examples. --> |
| 173 | + |
| 174 | +<section class="examples"> |
| 175 | + |
| 176 | +## Examples |
| 177 | + |
| 178 | +<!-- eslint no-undef: "error" --> |
| 179 | + |
| 180 | +```javascript |
| 181 | +var randu = require( '@stdlib/random/base/randu' ); |
| 182 | +var fromCodePoint = require( '@stdlib/string/from-code-point' ); |
| 183 | +var bifurcateOwn = require( '@stdlib/object/bifurcate-own' ); |
| 184 | + |
| 185 | +var key; |
| 186 | +var obj; |
| 187 | +var out; |
| 188 | +var i; |
| 189 | + |
| 190 | +// Generate a random object... |
| 191 | +obj = {}; |
| 192 | +for ( i = 0; i < 100; i++ ) { |
| 193 | + key = fromCodePoint( 97+i ); |
| 194 | + obj[ key ] = randu(); |
| 195 | +} |
| 196 | + |
| 197 | +function predicate( v ) { |
| 198 | + return ( v < 0.5 ); |
| 199 | +} |
| 200 | + |
| 201 | +// Compute the groups: |
| 202 | +out = bifurcateOwn( obj, predicate ); |
| 203 | +console.log( out ); |
| 204 | +``` |
| 205 | + |
| 206 | +</section> |
| 207 | + |
| 208 | +<!-- /.examples --> |
| 209 | + |
| 210 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 211 | + |
| 212 | +<section class="references"> |
| 213 | + |
| 214 | +</section> |
| 215 | + |
| 216 | +<!-- /.references --> |
| 217 | + |
| 218 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 219 | + |
| 220 | +<section class="related"> |
| 221 | + |
| 222 | +* * * |
| 223 | + |
| 224 | +## See Also |
| 225 | + |
| 226 | +- <span class="package-name">[`@stdlib/utils/bifurcate`][@stdlib/utils/bifurcate]</span><span class="delimiter">: </span><span class="description">split values into two groups.</span> |
| 227 | +- <span class="package-name">[`@stdlib/utils/bifurcate-by`][@stdlib/utils/bifurcate-by]</span><span class="delimiter">: </span><span class="description">split values into two groups according to a predicate function.</span> |
| 228 | +- <span class="package-name">[`@stdlib/object/bifurcate-in`][@stdlib/object/bifurcate-in]</span><span class="delimiter">: </span><span class="description">split an object's own and inherited property values into two groups according to a predicate function.</span> |
| 229 | +- <span class="package-name">[`@stdlib/utils/group-own`][@stdlib/utils/group-own]</span><span class="delimiter">: </span><span class="description">group an object's own property values according to an indicator function.</span> |
| 230 | + |
| 231 | +</section> |
| 232 | + |
| 233 | +<!-- /.related --> |
| 234 | + |
| 235 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 236 | + |
| 237 | +<section class="links"> |
| 238 | + |
| 239 | +[ecma-262-for-in]: https://262.ecma-international.org/5.1/#sec-12.6.4 |
| 240 | + |
| 241 | +<!-- <related-links> --> |
| 242 | + |
| 243 | +[@stdlib/utils/bifurcate]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/bifurcate |
| 244 | + |
| 245 | +[@stdlib/utils/bifurcate-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/bifurcate-by |
| 246 | + |
| 247 | +[@stdlib/object/bifurcate-in]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/object/bifurcate-in |
| 248 | + |
| 249 | +[@stdlib/utils/group-own]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/group-own |
| 250 | + |
| 251 | +<!-- </related-links> --> |
| 252 | + |
| 253 | +</section> |
| 254 | + |
| 255 | +<!-- /.links --> |
0 commit comments