README.md (2819B)
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 # ifthen 22 23 > If a condition is truthy, invoke `x`; otherwise, invoke `y`. 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 ifthen = require( '@stdlib/utils/if-then' ); 41 ``` 42 43 #### ifthen( bool, x, y ) 44 45 If a condition is truthy, invokes `x`; otherwise, invokes `y`. 46 47 ```javascript 48 function x() { 49 return 1.0; 50 } 51 52 function y() { 53 return -1.0; 54 } 55 56 var z = ifthen( true, x, y ); 57 // returns 1.0 58 59 z = ifthen( false, x, y ); 60 // returns -1.0 61 ``` 62 63 </section> 64 65 <!-- /.usage --> 66 67 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 68 69 <section class="notes"> 70 71 ## Notes 72 73 - The function is similar to [`ifelse()`][@stdlib/utils/if-else], but allows deferred argument evaluation. 74 75 </section> 76 77 <!-- /.notes --> 78 79 <!-- Package usage examples. --> 80 81 <section class="examples"> 82 83 ## Examples 84 85 <!-- eslint no-undef: "error" --> 86 87 ```javascript 88 var randu = require( '@stdlib/random/base/randu' ); 89 var ceil = require( '@stdlib/math/base/special/ceil' ); 90 var repeatString = require( '@stdlib/string/repeat' ); 91 var ifthen = require( '@stdlib/utils/if-then' ); 92 93 var z; 94 var i; 95 96 function x() { 97 return repeatString( 'BOOP', ceil( randu()*3.0 ) ); 98 } 99 100 function y() { 101 return repeatString( 'beep', ceil( randu()*5.0 ) ); 102 } 103 104 for ( i = 0; i < 100; i++ ) { 105 z = ifthen( randu() > 0.9, x, y ); 106 console.log( z ); 107 } 108 ``` 109 110 </section> 111 112 <!-- /.examples --> 113 114 <!-- 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. --> 115 116 <section class="references"> 117 118 </section> 119 120 <!-- /.references --> 121 122 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 123 124 <section class="links"> 125 126 [@stdlib/utils/if-else]: https://www.npmjs.com/package/@stdlib/utils/tree/main/if-else 127 128 </section> 129 130 <!-- /.links -->