|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# noneBy |
| 22 | + |
| 23 | +> Test whether all elements in an array fail a test implemented by 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 noneBy = require( '@stdlib/array/base/none-by' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### noneBy( x, predicate\[, thisArg] ) |
| 44 | + |
| 45 | +Tests whether all elements in an array fail a test implemented by a `predicate` function. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function isPositive( value ) { |
| 49 | + return ( value > 0 ); |
| 50 | +} |
| 51 | + |
| 52 | +var x = [ 0, 0, 0, 0 ]; |
| 53 | + |
| 54 | +var bool = noneBy( x, isPositive ); |
| 55 | +// returns true |
| 56 | +``` |
| 57 | + |
| 58 | +If a `predicate` function returns a truthy value, the function **immediately** returns `false`. |
| 59 | + |
| 60 | +```javascript |
| 61 | +function isPositive( value ) { |
| 62 | + return ( value > 0 ); |
| 63 | +} |
| 64 | + |
| 65 | +var x = [ -1, -2, 3, -4 ]; |
| 66 | + |
| 67 | +var bool = noneBy( x, isPositive ); |
| 68 | +// returns false |
| 69 | +``` |
| 70 | + |
| 71 | +The `predicate` function is provided three arguments: |
| 72 | + |
| 73 | +- **value**: current array element. |
| 74 | +- **index**: current array element index. |
| 75 | +- **arr**: input array. |
| 76 | + |
| 77 | +To set the `predicate` function execution context, provide a `thisArg`. |
| 78 | + |
| 79 | +```javascript |
| 80 | +function predicate( value ) { |
| 81 | + this.count += 1; |
| 82 | + return ( value < 0 ); |
| 83 | +} |
| 84 | + |
| 85 | +var x = [ 1, 2, -3, 4 ]; |
| 86 | + |
| 87 | +var context = { |
| 88 | + 'count': 0 |
| 89 | +}; |
| 90 | + |
| 91 | +var bool = noneBy( x, predicate, context ); |
| 92 | +// returns false |
| 93 | + |
| 94 | +var cnt = context.count; |
| 95 | +// returns 3 |
| 96 | +``` |
| 97 | + |
| 98 | +</section> |
| 99 | + |
| 100 | +<!-- /.usage --> |
| 101 | + |
| 102 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 103 | + |
| 104 | +<section class="notes"> |
| 105 | + |
| 106 | +## Notes |
| 107 | + |
| 108 | +- The function performs a linear scan and returns immediately upon encountering a truthy return value. Unlike [`Array.prototype.every`][mdn-array-every], when performing a linear scan, the function does **not** skip `undefined` elements. |
| 109 | +- If provided an empty array, the function returns `true`. |
| 110 | + |
| 111 | +</section> |
| 112 | + |
| 113 | +<!-- /.notes --> |
| 114 | + |
| 115 | +<!-- Package usage examples. --> |
| 116 | + |
| 117 | +<section class="examples"> |
| 118 | + |
| 119 | +## Examples |
| 120 | + |
| 121 | +<!-- eslint no-undef: "error" --> |
| 122 | + |
| 123 | +```javascript |
| 124 | +var bernoulli = require( '@stdlib/random/array/bernoulli' ); |
| 125 | +var isPositiveInteger = require( '@stdlib/assert/is-positive-integer' ).isPrimitive; |
| 126 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 127 | +var noneBy = require( '@stdlib/array/base/none-by' ); |
| 128 | + |
| 129 | +var x = bernoulli( 10, 0.1, { |
| 130 | + 'dtype': 'int8' |
| 131 | +} ); |
| 132 | +// returns <Int8Array> |
| 133 | + |
| 134 | +var out = noneBy( x, naryFunction( isPositiveInteger, 1 ) ); |
| 135 | +// returns <boolean> |
| 136 | +``` |
| 137 | + |
| 138 | +</section> |
| 139 | + |
| 140 | +<!-- /.examples --> |
| 141 | + |
| 142 | +<!-- 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. --> |
| 143 | + |
| 144 | +<section class="references"> |
| 145 | + |
| 146 | +</section> |
| 147 | + |
| 148 | +<!-- /.references --> |
| 149 | + |
| 150 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 151 | + |
| 152 | +<section class="related"> |
| 153 | + |
| 154 | +</section> |
| 155 | + |
| 156 | +<!-- /.related --> |
| 157 | + |
| 158 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 159 | + |
| 160 | +<section class="links"> |
| 161 | + |
| 162 | +[mdn-array-every]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.links --> |
0 commit comments