README.md (4350B)
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 # Object Inverse 22 23 > Invert an object, such that keys become values and values become keys, according to a transform function. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var invertBy = require( '@stdlib/utils/object-inverse-by' ); 31 ``` 32 33 #### invertBy( obj, \[options,] transform ) 34 35 Inverts an `object`, such that keys become values and values become keys, according to a `transform` function. 36 37 ```javascript 38 function transform( key, value ) { 39 return value; 40 } 41 var obj = { 42 'a': 'beep', 43 'b': 'boop' 44 }; 45 var out = invertBy( obj, transform ); 46 // returns { 'beep': 'a', 'boop': 'b' } 47 ``` 48 49 The function accepts the following `options`: 50 51 - **duplicates**: `boolean` indicating whether to store keys mapped to duplicate values in `arrays`. Default: `true`. 52 53 By default, keys mapped to duplicate values are stored in `arrays`. 54 55 ```javascript 56 function transform( key, value ) { 57 return value; 58 } 59 var obj = { 60 'a': 'beep', 61 'b': 'beep' 62 }; 63 var out = invertBy( obj, transform ); 64 // returns { 'beep': [ 'a', 'b' ] } 65 ``` 66 67 To **not** allow duplicates, set the `duplicates` option to `false`. The output `key-value` pair will be the `key` most recently inserted into the input `object`. 68 69 ```javascript 70 function transform( key, value ) { 71 return value; 72 } 73 var obj = {}; 74 obj.a = 'beep'; 75 obj.b = 'boop'; 76 obj.c = 'beep'; // inserted after `a` 77 78 var opts = { 79 'duplicates': false 80 }; 81 var out = invertBy( obj, opts, transform ); 82 // returns { 'beep': 'c', 'boop': 'b' } 83 ``` 84 85 The `transform` function is provided three arguments: 86 87 - `key`: object key 88 - `value`: object value corresponding to `key` 89 - `obj`: input object 90 91 ```javascript 92 function transform( key, value, o ) { 93 if ( key === 'name' ) { 94 return value; 95 } 96 return o.name + ':' + value; 97 } 98 var obj = { 99 'name': 'foo', 100 'a': 'beep', 101 'b': 'boop' 102 }; 103 var out = invertBy( obj, transform ); 104 // returns { 'foo': 'name', 'foo:beep': 'a', 'foo:boop': 'b' } 105 ``` 106 107 </section> 108 109 <!-- /.usage --> 110 111 <section class="notes"> 112 113 ## Notes 114 115 - Beware when providing `objects` having values which are themselves `objects`. This function relies on native `object` serialization (`#toString`) when converting `transform` function return values to keys. 116 117 ```javascript 118 function transform( key, value ) { 119 return value; 120 } 121 var obj = { 122 'a': [ 1, 2, 3 ], 123 'b': { 124 'c': 'd' 125 } 126 }; 127 128 var out = invertBy( obj, transform ); 129 // returns { '1,2,3': 'a', '[object Object]': 'b' } 130 ``` 131 132 - Insertion 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 inversion. 133 134 </section> 135 136 <!-- /.notes --> 137 138 <section class="examples"> 139 140 ## Examples 141 142 <!-- eslint no-undef: "error" --> 143 144 ```javascript 145 var randu = require( '@stdlib/random/base/randu' ); 146 var round = require( '@stdlib/math/base/special/round' ); 147 var invertBy = require( '@stdlib/utils/object-inverse-by' ); 148 149 var keys; 150 var arr; 151 var out; 152 var i; 153 154 function transform( key, value ) { 155 return value; 156 } 157 158 // Create an array of random integers... 159 arr = new Array( 1000 ); 160 for ( i = 0; i < arr.length; i++ ) { 161 arr[ i ] = round( randu()*100.0 ); 162 } 163 // Invert the array to determine value frequency... 164 out = invertBy( arr, transform ); 165 keys = Object.keys( out ); 166 for ( i = 0; i < keys.length; i++ ) { 167 if ( out[ i ] ) { 168 out[ i ] = out[ i ].length; 169 } else { 170 out[ i ] = 0; 171 } 172 } 173 console.dir( out ); 174 ``` 175 176 </section> 177 178 <!-- /.examples --> 179 180 <section class="links"> 181 182 [ecma-262-for-in]: http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4 183 184 </section> 185 186 <!-- /.links -->