time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (4960B)


      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 # Flatten Object
     22 
     23 > Flatten an object.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var flattenObject = require( '@stdlib/utils/flatten-object' );
     31 ```
     32 
     33 <a name="flatten-object"></a>
     34 
     35 #### flattenObject( obj\[, options] )
     36 
     37 Flattens an `object`.
     38 
     39 ```javascript
     40 var obj = {
     41     'a': {
     42         'b': {
     43             'c': 'd'
     44         }
     45     }
     46 };
     47 
     48 var out = flattenObject( obj );
     49 // returns { 'a.b.c': 'd' }
     50 ```
     51 
     52 The function accepts the following `options`:
     53 
     54 -   **depth**: maximum depth to flatten.
     55 -   **copy**: `boolean` indicating whether to deep [copy][@stdlib/utils/copy] property values. Default: `false`.
     56 -   **delimiter**: key path delimiter. Default: `'.'`.
     57 -   **flattenArrays**: `boolean` indicating whether to flatten `arrays`. Default: `false`.
     58 
     59 To flatten to a specified depth, set the `depth` option.
     60 
     61 ```javascript
     62 var obj = {
     63     'a': {
     64         'b': {
     65             'c': 'd'
     66         }
     67     }
     68 };
     69 
     70 var out = flattenObject( obj, {
     71     'depth': 1
     72 });
     73 // returns { 'a.b': {'c': 'd'} }
     74 
     75 var bool = ( obj.a.b === out['a.b'] );
     76 // returns true
     77 ```
     78 
     79 To deep [copy][@stdlib/utils/copy] property values, set the `copy` option to `true`.
     80 
     81 ```javascript
     82 var obj = {
     83     'a': {
     84         'b': {
     85             'c': 'd'
     86         }
     87     }
     88 };
     89 
     90 var out = flattenObject( obj, {
     91     'depth': 1,
     92     'copy': true
     93 });
     94 // returns { 'a.b': { 'c': 'd' } }
     95 
     96 var bool = ( obj.a.b === out['a.b'] );
     97 // returns false
     98 ```
     99 
    100 To specify a custom key path delimiter, set the `delimiter` option.
    101 
    102 ```javascript
    103 var obj = {
    104     'a': {
    105         'b': {
    106             'c': 'd'
    107         }
    108     }
    109 };
    110 
    111 var out = flattenObject( obj, {
    112     'delimiter': '-|-'
    113 });
    114 // returns { 'a-|-b-|-c': 'd' }
    115 ```
    116 
    117 By default, the function does **not** flatten `arrays`. To flatten `arrays`, set the `flattenArrays` option to `true`.
    118 
    119 ```javascript
    120 var obj = {
    121     'a': {
    122         'b': [ 1, 2, 3 ]
    123     }
    124 };
    125 
    126 var out = flattenObject( obj, {
    127     'flattenArrays': true
    128 });
    129 // returns { 'a.b.0': 1, 'a.b.1': 2, 'a.b.2': 3 }
    130 ```
    131 
    132 #### flattenObject.factory( options )
    133 
    134 Returns a `function` to flatten an `object`.
    135 
    136 ```javascript
    137 var flatten = flattenObject.factory({
    138     'delimiter': '|',
    139     'flattenArrays': true
    140 });
    141 
    142 var obj = {
    143     'a': {
    144         'b': {
    145             'c': 'd'
    146         }
    147     }
    148 };
    149 
    150 var out = flatten( obj );
    151 // returns { 'a|b|c': 'd' }
    152 
    153 obj = {
    154     'a': {
    155         'b': [ 1, 2, 3 ]
    156     }
    157 };
    158 out = flatten( obj );
    159 // returns { 'a|b|0': 1, 'a|b|1': 2, 'a|b|2': 3 }
    160 ```
    161 
    162 The function accepts the same `options` as [`flattenObject()`](#flatten-object).
    163 
    164 </section>
    165 
    166 <!-- /.usage -->
    167 
    168 <section class="examples">
    169 
    170 ## Examples
    171 
    172 <!-- TODO: update once Buffer wrapper -->
    173 
    174 <!-- eslint no-undef: "error" -->
    175 
    176 <!-- eslint-disable no-buffer-constructor -->
    177 
    178 ```javascript
    179 var Float64Array = require( '@stdlib/array/float64' );
    180 var string2buffer = require( '@stdlib/buffer/from-string' );
    181 var flattenObject = require( '@stdlib/utils/flatten-object' );
    182 
    183 function noop() {
    184     // Do nothing...
    185 }
    186 
    187 var obj = {
    188     'a': {
    189         'b': {
    190             'beep': 'boop',
    191             'foo': 'bar'
    192         },
    193         'c': [ 1, 2, 3 ],
    194         'd': true,
    195         'e': null,
    196         'f': 3.14,
    197         'g': new Date(),
    198         'h': {
    199             'bip': 6,
    200             'bop': [ 4, 5, 6 ]
    201         },
    202         'i': [ null, true, {} ],
    203         'j': /.*/,
    204         'k': noop,
    205         'l': NaN,
    206         'm': [],
    207         'n': string2buffer( 'hello' ),
    208         'o': {
    209             'data': new Float64Array( 10 )
    210         },
    211         'p': {
    212             '1': {
    213                 '2': {
    214                     '3': {}
    215                 }
    216             }
    217         }
    218     }
    219 };
    220 
    221 var out = flattenObject( obj, {
    222     'depth': 5,
    223     'copy': true,
    224     'flattenArrays': false,
    225     'delimiter': '-|-'
    226 });
    227 /* returns
    228     {
    229         'a-|-b-|-beep': 'boop',
    230         'a-|-b-|-foo': 'bar',
    231         'a-|-c': [1,2,3],
    232         'a-|-d': true,
    233         'a-|-e': null,
    234         'a-|-f': 3.14,
    235         'a-|-g': <Date>,
    236         'a-|-h-|-bip': 6,
    237         'a-|-h-|-bop': [4,5,6],
    238         'a-|-i': [null,true,{}],
    239         'a-|-j': <RegExp>,
    240         'a-|-k': <Function>,
    241         'a-|-l': NaN,
    242         'a-|-m': [],
    243         'a-|-n': <Buffer>,
    244         'a-|-o-|-data': <Float64Array>,
    245         'a-|-p-|-1-|-2-|-3': {}
    246     }
    247 */
    248 ```
    249 
    250 </section>
    251 
    252 <!-- /.examples -->
    253 
    254 <section class="links">
    255 
    256 [@stdlib/utils/copy]: https://www.npmjs.com/package/@stdlib/utils/tree/main/copy
    257 
    258 </section>
    259 
    260 <!-- /.links -->