time-to-botec

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

README.md (5254B)


      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 # Deep Set
     22 
     23 > Set a nested property value.
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var deepSet = require( '@stdlib/utils/deep-set' );
     31 ```
     32 
     33 #### deepSet( obj, path, value\[, options] )
     34 
     35 Sets a nested property value.
     36 
     37 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
     38 
     39 ```javascript
     40 var obj = { 'a': { 'b': { 'c': 'd' } } };
     41 
     42 var bool = deepSet( obj, 'a.b.c', 'beep' );
     43 // returns true
     44 
     45 console.log( obj );
     46 // => { 'a': { 'b': { 'c': 'beep' } } }
     47 ```
     48 
     49 If the function is able to deep set a nested property, the function returns `true`; otherwise, the function returns `false`.
     50 
     51 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
     52 
     53 ```javascript
     54 var obj = { 'a': { 'b': { 'c': 'd' } } };
     55 
     56 var bool = deepSet( obj, 'a.b.c', 'woot' );
     57 // returns true
     58 
     59 bool = deepSet( obj, 'a.beep.c', 'boop' );
     60 // returns false
     61 
     62 bool = deepSet( null, 'a.beep.c', 'boop' );
     63 // returns false
     64 
     65 bool = deepSet( 'bap', 'a.beep.c', 'boop' );
     66 // returns false
     67 ```
     68 
     69 For `paths` including `arrays`, specify the numeric index.
     70 
     71 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
     72 
     73 ```javascript
     74 var arr = [
     75     { 'a': [ { 'x': 5 } ] },
     76     { 'a': [ { 'x': 10 } ] }
     77 ];
     78 
     79 var bool = deepSet( arr, '1.a.0.x', 25 );
     80 // returns true
     81 
     82 console.log( arr );
     83 /* =>
     84     [
     85         { 'a': [ { 'x': 5 } ] },
     86         { 'a': [ { 'x': 25 } ] }
     87     ]
     88 */
     89 ```
     90 
     91 The key `path` may be specified as either a delimited `string` or a key `array`.
     92 
     93 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
     94 
     95 ```javascript
     96 var obj = { 'a': { 'b': { 'c': 'd' } } };
     97 
     98 var bool = deepSet( obj, [ 'a', 'b', 'c' ], 'beep' ); // obj => { 'a': { 'b': { 'c': 'beep' } } }
     99 // returns true
    100 ```
    101 
    102 If `value` is a `function`, the argument is treated as a `callback` and should return a value to set.
    103 
    104 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
    105 
    106 ```javascript
    107 function set( val ) {
    108     var ch = val;
    109     var i;
    110     for ( i = 0; i < 4; i++ ) {
    111         val += ch;
    112     }
    113     return val;
    114 }
    115 var obj = { 'a': { 'b': { 'c': 'd' } } };
    116 
    117 var bool = deepSet( obj, 'a.b.c', set ); // obj => { 'a': { 'b': { 'c': 'ddddd' } } }
    118 // returns true
    119 ```
    120 
    121 The function accepts the following `options`:
    122 
    123 -   **sep**: key path separator. Default: `'.'`.
    124 -   **create**: `boolean` indicating whether to create a path if the key path does not already exist. Default: `false`.
    125 
    126 By default, the function assumes `dot` separated key values. To specify an alternative separator, set the `sep` option.
    127 
    128 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
    129 
    130 ```javascript
    131 var obj = { 'a': { 'b': { 'c': 'd' } } };
    132 
    133 var bool = deepSet( obj, 'a/b/c', 'beep', {
    134     'sep': '/'
    135 });
    136 // returns true
    137 
    138 console.log( obj );
    139 // => { 'a': { 'b': { 'c': 'beep' } } }
    140 ```
    141 
    142 To create a key path which does not already exist, set the `create` option to `true`.
    143 
    144 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
    145 
    146 ```javascript
    147 var obj = { 'a': { 'b': { 'c': 'd' } } };
    148 
    149 var bool = deepSet( obj, 'a.e.c', 'boop', {
    150     'create': true
    151 });
    152 // returns true
    153 
    154 console.log( obj );
    155 /* =>
    156     {
    157         'a': {
    158             'b': {
    159                 'c': 'beep'
    160             },
    161             'e': {
    162                 'c': 'boop'
    163             }
    164         }
    165     }
    166 */
    167 ```
    168 
    169 #### deepSet.factory( path\[, options] )
    170 
    171 Creates a reusable deep set function. The factory method ensures a `deepSet` function is configured identically by using the same set of provided `options`.
    172 
    173 ```javascript
    174 var dset = deepSet.factory( 'a/b/c', {
    175     'create': true,
    176     'sep': '/'
    177 });
    178 ```
    179 
    180 #### dset( obj, value )
    181 
    182 Sets a nested property value.
    183 
    184 <!-- eslint-disable object-curly-newline, object-curly-spacing -->
    185 
    186 ```javascript
    187 var dset = deepSet.factory( 'a.b.c' );
    188 
    189 var obj = { 'a': { 'b': { 'c': 'd' } } };
    190 
    191 var bool = dset( obj, 'beep' );
    192 // returns true
    193 
    194 console.log( obj );
    195 // => { 'a': { 'b': { 'c': 'beep' } } }
    196 ```
    197 
    198 </section>
    199 
    200 <!-- /.usage -->
    201 
    202 <section class="examples">
    203 
    204 ## Examples
    205 
    206 <!-- eslint no-undef: "error" -->
    207 
    208 ```javascript
    209 var randu = require( '@stdlib/random/base/randu' );
    210 var deepSet = require( '@stdlib/utils/deep-set' );
    211 
    212 var data;
    213 var bool;
    214 var keys;
    215 var i;
    216 
    217 function set( val ) {
    218     return val * 10.0;
    219 }
    220 
    221 data = new Array( 100 );
    222 for ( i = 0; i < data.length; i++ ) {
    223     data[ i ] = {
    224         'x': Date.now(),
    225         'y': [ randu(), randu(), i ]
    226     };
    227 }
    228 
    229 keys = [ 0, 'y', 2 ];
    230 for ( i = 0; i < data.length; i++ ) {
    231     keys[ 0 ] = i;
    232     bool = deepSet( data, keys, set );
    233     if ( !bool ) {
    234         console.error( 'Unable to deep set value.' );
    235     }
    236 }
    237 console.log( data );
    238 ```
    239 
    240 </section>
    241 
    242 <!-- /.examples -->
    243 
    244 <section class="links">
    245 
    246 </section>
    247 
    248 <!-- /.links -->