Skip to content

Commit 50616dc

Browse files
feat: add @stdlib/assert/is-nonnegative-finite
PR-URL: #1354 Signed-off-by: Spandan Barve <114365550+marsian83@users.noreply.github.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 4bfbed8 commit 50616dc

File tree

15 files changed

+1322
-0
lines changed

15 files changed

+1322
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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+
# isNonNegativeFinite
22+
23+
> Test if a value is a number having a nonnegative finite value.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isNonNegativeFinite = require( '@stdlib/assert/is-nonnegative-finite' );
31+
```
32+
33+
#### isNonNegativeFinite( value )
34+
35+
Tests if a `value` is a `number` having a nonnegative finite value.
36+
37+
<!-- eslint-disable no-new-wrappers -->
38+
39+
```javascript
40+
var Number = require( '@stdlib/number/ctor' );
41+
42+
var bool = isNonNegativeFinite( 5.0 );
43+
// returns true
44+
45+
bool = isNonNegativeFinite( new Number( 5.0 ) );
46+
// returns true
47+
48+
bool = isNonNegativeFinite( 3.14 );
49+
// returns true
50+
51+
bool = isNonNegativeFinite( -5.0 );
52+
// returns false
53+
54+
bool = isNonNegativeFinite( null );
55+
// returns false
56+
57+
bool = isNonNegativeFinite( Infinity );
58+
// returns false
59+
```
60+
61+
#### isNonNegativeFinite.isPrimitive( value )
62+
63+
Tests if a `value` is a primitive `number` having a nonnegative finite value.
64+
65+
<!-- eslint-disable no-new-wrappers -->
66+
67+
```javascript
68+
var Number = require( '@stdlib/number/ctor' );
69+
70+
var bool = isNonNegativeFinite.isPrimitive( 3.0 );
71+
// returns true
72+
73+
bool = isNonNegativeFinite.isPrimitive( new Number( 3.0 ) );
74+
// returns false
75+
```
76+
77+
#### isNonNegativeFinite.isObject( value )
78+
79+
Tests if a `value` is a `Number` object having a nonnegative finite value.
80+
81+
<!-- eslint-disable no-new-wrappers -->
82+
83+
```javascript
84+
var Number = require( '@stdlib/number/ctor' );
85+
86+
var bool = isNonNegativeFinite.isObject( 3.0 );
87+
// returns false
88+
89+
bool = isNonNegativeFinite.isObject( new Number( 3.0 ) );
90+
// returns true
91+
```
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<section class="examples">
98+
99+
## Examples
100+
101+
<!-- eslint-disable no-new-wrappers -->
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var Number = require( '@stdlib/number/ctor' );
107+
var isNonNegativeFinite = require( '@stdlib/assert/is-nonnegative-finite' );
108+
109+
var bool = isNonNegativeFinite( 5.0 );
110+
// returns true
111+
112+
bool = isNonNegativeFinite( new Number( 5.0 ) );
113+
// returns true
114+
115+
bool = isNonNegativeFinite( 0.0 );
116+
// returns true
117+
118+
bool = isNonNegativeFinite( 3.14 );
119+
// returns true
120+
121+
bool = isNonNegativeFinite( -5.0 );
122+
// returns false
123+
124+
bool = isNonNegativeFinite( '5' );
125+
// returns false
126+
127+
bool = isNonNegativeFinite( null );
128+
// returns false
129+
```
130+
131+
</section>
132+
133+
<!-- /.examples -->
134+
135+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
136+
137+
<section class="related">
138+
139+
* * *
140+
141+
## See Also
142+
143+
- <span class="package-name">[`@stdlib/assert/is-number`][@stdlib/assert/is-number]</span><span class="delimiter">: </span><span class="description">test if a value is a number.</span>
144+
- <span class="package-name">[`@stdlib/assert/is-finite`][@stdlib/assert/is-finite]</span><span class="delimiter">: </span><span class="description">test if a value is a finite number.</span>
145+
146+
</section>
147+
148+
<!-- /.related -->
149+
150+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
151+
152+
<section class="links">
153+
154+
<!-- <related-links> -->
155+
156+
[@stdlib/assert/is-number]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-number
157+
[@stdlib/assert/is-finite]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-finite
158+
159+
<!-- </related-links> -->
160+
161+
</section>
162+
163+
<!-- /.links -->
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2024 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* eslint-disable no-undefined, no-empty-function */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var bench = require( '@stdlib/bench' );
26+
var Number = require( '@stdlib/number/ctor' );
27+
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
28+
var pkg = require( './../package.json' ).name;
29+
var isNonNegativeFinite = require( './../lib' );
30+
31+
32+
// MAIN //
33+
34+
bench( pkg+'::primitives', function benchmark( b ) {
35+
var values;
36+
var bool;
37+
var i;
38+
39+
values = [
40+
'5',
41+
5.0,
42+
4.0,
43+
3.14,
44+
-5.0,
45+
-4.0,
46+
NaN,
47+
true,
48+
false,
49+
null,
50+
Infinity,
51+
-Infinity,
52+
undefined
53+
];
54+
55+
b.tic();
56+
for ( i = 0; i < b.iterations; i++ ) {
57+
bool = isNonNegativeFinite( values[ i % values.length ] );
58+
if ( typeof bool !== 'boolean' ) {
59+
b.fail( 'should return a boolean' );
60+
}
61+
}
62+
b.toc();
63+
if ( !isBoolean( bool ) ) {
64+
b.fail( 'should return a boolean' );
65+
}
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
});
69+
70+
bench( pkg+'::objects', function benchmark( b ) {
71+
var values;
72+
var bool;
73+
var i;
74+
75+
values = [
76+
[],
77+
{},
78+
function noop() {},
79+
new Number( 2.0 ),
80+
new Number( -3.0 ),
81+
new Number( 3.14 )
82+
];
83+
84+
b.tic();
85+
for ( i = 0; i < b.iterations; i++ ) {
86+
bool = isNonNegativeFinite( values[ i % values.length ] );
87+
if ( typeof bool !== 'boolean' ) {
88+
b.fail( 'should return a boolean' );
89+
}
90+
}
91+
b.toc();
92+
if ( !isBoolean( bool ) ) {
93+
b.fail( 'should return a boolean' );
94+
}
95+
b.pass( 'benchmark finished' );
96+
b.end();
97+
});
98+
99+
bench( pkg+'::primitives:isPrimitive', function benchmark( b ) {
100+
var values;
101+
var bool;
102+
var i;
103+
104+
values = [
105+
'5',
106+
5.0,
107+
4.0,
108+
3.14,
109+
-5.0,
110+
-4.0,
111+
NaN,
112+
true,
113+
false,
114+
null,
115+
undefined,
116+
Infinity,
117+
-Infinity
118+
];
119+
120+
b.tic();
121+
for ( i = 0; i < b.iterations; i++ ) {
122+
bool = isNonNegativeFinite.isPrimitive( values[ i % values.length ] );
123+
if ( typeof bool !== 'boolean' ) {
124+
b.fail( 'should return a boolean' );
125+
}
126+
}
127+
b.toc();
128+
if ( !isBoolean( bool ) ) {
129+
b.fail( 'should return a boolean' );
130+
}
131+
b.pass( 'benchmark finished' );
132+
b.end();
133+
});
134+
135+
bench( pkg+'::objects:isPrimitive', function benchmark( b ) {
136+
var values;
137+
var bool;
138+
var i;
139+
140+
values = [
141+
[],
142+
{},
143+
function noop() {},
144+
new Number( 2.0 ),
145+
new Number( -3.0 ),
146+
new Number( 3.14 )
147+
];
148+
149+
b.tic();
150+
for ( i = 0; i < b.iterations; i++ ) {
151+
bool = isNonNegativeFinite.isPrimitive( values[ i % values.length ] );
152+
if ( typeof bool !== 'boolean' ) {
153+
b.fail( 'should return a boolean' );
154+
}
155+
}
156+
b.toc();
157+
if ( !isBoolean( bool ) ) {
158+
b.fail( 'should return a boolean' );
159+
}
160+
b.pass( 'benchmark finished' );
161+
b.end();
162+
});
163+
164+
bench( pkg+'::primitives:isObject', function benchmark( b ) {
165+
var values;
166+
var bool;
167+
var i;
168+
169+
values = [
170+
'5',
171+
5.0,
172+
4.0,
173+
3.14,
174+
-5.0,
175+
-4.0,
176+
NaN,
177+
true,
178+
false,
179+
null,
180+
undefined,
181+
Infinity,
182+
-Infinity
183+
];
184+
185+
b.tic();
186+
for ( i = 0; i < b.iterations; i++ ) {
187+
bool = isNonNegativeFinite.isObject( values[ i % values.length ] );
188+
if ( typeof bool !== 'boolean' ) {
189+
b.fail( 'should return a boolean' );
190+
}
191+
}
192+
b.toc();
193+
if ( !isBoolean( bool ) ) {
194+
b.fail( 'should return a boolean' );
195+
}
196+
b.pass( 'benchmark finished' );
197+
b.end();
198+
});
199+
200+
bench( pkg+'::objects:isObject', function benchmark( b ) {
201+
var values;
202+
var bool;
203+
var i;
204+
205+
values = [
206+
[],
207+
{},
208+
function noop() {},
209+
new Number( 2.0 ),
210+
new Number( -3.0 ),
211+
new Number( 3.14 ),
212+
new Number( Infinity )
213+
];
214+
215+
b.tic();
216+
for ( i = 0; i < b.iterations; i++ ) {
217+
bool = isNonNegativeFinite.isObject( values[ i % values.length ] );
218+
if ( typeof bool !== 'boolean' ) {
219+
b.fail( 'should return a boolean' );
220+
}
221+
}
222+
b.toc();
223+
if ( !isBoolean( bool ) ) {
224+
b.fail( 'should return a boolean' );
225+
}
226+
b.pass( 'benchmark finished' );
227+
b.end();
228+
});

0 commit comments

Comments
 (0)