Skip to content

Commit f36bef4

Browse files
feat: add assert/is-nonpositive-finite
PR-URL: #1396 Closes: #1343 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 c1353fd commit f36bef4

File tree

15 files changed

+1219
-0
lines changed

15 files changed

+1219
-0
lines changed
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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+
# isNonPositiveFinite
22+
23+
> Test if a value is a number having a nonpositive finite value.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var isNonPositiveFinite = require( '@stdlib/assert/is-nonpositive-finite' );
31+
```
32+
33+
#### isNonPositiveFinite( value )
34+
35+
Tests if a `value` is a `number` having a nonpositive finite value.
36+
37+
<!-- eslint-disable no-new-wrappers -->
38+
39+
```javascript
40+
var Number = require( '@stdlib/number/ctor' );
41+
var NINF = require('@stdlib/constants/float64/ninf');
42+
43+
var bool = isNonPositiveFinite( -5.0 );
44+
// returns true
45+
46+
bool = isNonPositiveFinite( NINF );
47+
// returns false
48+
49+
bool = isNonPositiveFinite( new Number( -5.0 ) );
50+
// returns true
51+
52+
bool = isNonPositiveFinite( -3.14 );
53+
// returns true
54+
55+
bool = isNonPositiveFinite( 5.0 );
56+
// returns false
57+
58+
bool = isNonPositiveFinite( null );
59+
// returns false
60+
```
61+
62+
#### isNonPositiveFinite.isPrimitive( value )
63+
64+
Tests if a `value` is a primitive `number` having a nonpositive finite value.
65+
66+
<!-- eslint-disable no-new-wrappers -->
67+
68+
```javascript
69+
var Number = require( '@stdlib/number/ctor' );
70+
71+
var bool = isNonPositiveFinite.isPrimitive( -3.0 );
72+
// returns true
73+
74+
bool = isNonPositiveFinite.isPrimitive( new Number( -3.0 ) );
75+
// returns false
76+
```
77+
78+
#### isNonPositiveFinite.isObject( value )
79+
80+
Tests if a `value` is a `Number` object having a nonpositive finite value.
81+
82+
<!-- eslint-disable no-new-wrappers -->
83+
84+
```javascript
85+
var Number = require( '@stdlib/number/ctor' );
86+
87+
var bool = isNonPositiveFinite.isObject( -3.0 );
88+
// returns false
89+
90+
bool = isNonPositiveFinite.isObject( new Number( -3.0 ) );
91+
// returns true
92+
```
93+
94+
</section>
95+
96+
<!-- /.usage -->
97+
98+
<section class="examples">
99+
100+
## Examples
101+
102+
<!-- eslint-disable no-new-wrappers -->
103+
104+
<!-- eslint no-undef: "error" -->
105+
106+
```javascript
107+
var Number = require( '@stdlib/number/ctor' );
108+
var isNonPositiveFinite = require( '@stdlib/assert/is-nonpositive-finite' );
109+
var NINF = require('@stdlib/constants/float64/ninf');
110+
111+
var bool = isNonPositiveFinite( -5.0 );
112+
// returns true
113+
114+
bool = isNonPositiveFinite( NINF );
115+
// returns false
116+
117+
bool = isNonPositiveFinite( new Number( -5.0 ) );
118+
// returns true
119+
120+
bool = isNonPositiveFinite( 0.0 );
121+
// returns true
122+
123+
bool = isNonPositiveFinite( -3.14 );
124+
// returns true
125+
126+
bool = isNonPositiveFinite( 5.0 );
127+
// returns false
128+
129+
bool = isNonPositiveFinite( '-5' );
130+
// returns false
131+
132+
bool = isNonPositiveFinite( null );
133+
// returns false
134+
```
135+
136+
</section>
137+
138+
<!-- /.examples -->
139+
140+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141+
142+
<section class="related">
143+
144+
* * *
145+
146+
## See Also
147+
148+
- <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>
149+
150+
</section>
151+
152+
<!-- /.related -->
153+
154+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
155+
156+
<section class="links">
157+
158+
<!-- <related-links> -->
159+
160+
[@stdlib/assert/is-number]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/assert/is-number
161+
162+
<!-- </related-links> -->
163+
164+
</section>
165+
166+
<!-- /.links -->
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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 isNonPositiveFinite = 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+
];
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
bool = isNonPositiveFinite( values[ i % values.length ] );
55+
if ( typeof bool !== 'boolean' ) {
56+
b.fail( 'should return a boolean' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isBoolean( bool ) ) {
61+
b.fail( 'should return a boolean' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
});
66+
67+
bench( pkg+'::objects', function benchmark( b ) {
68+
var values;
69+
var bool;
70+
var i;
71+
72+
values = [
73+
[],
74+
{},
75+
function noop() {},
76+
new Number( 2.0 ),
77+
new Number( -3.0 ),
78+
new Number( 3.14 )
79+
];
80+
81+
b.tic();
82+
for ( i = 0; i < b.iterations; i++ ) {
83+
bool = isNonPositiveFinite( values[ i % values.length ] );
84+
if ( typeof bool !== 'boolean' ) {
85+
b.fail( 'should return a boolean' );
86+
}
87+
}
88+
b.toc();
89+
if ( !isBoolean( bool ) ) {
90+
b.fail( 'should return a boolean' );
91+
}
92+
b.pass( 'benchmark finished' );
93+
b.end();
94+
});
95+
96+
bench( pkg+'::primitives:isPrimitive', function benchmark( b ) {
97+
var values;
98+
var bool;
99+
var i;
100+
101+
values = [
102+
'5',
103+
5.0,
104+
4.0,
105+
3.14,
106+
-5.0,
107+
-4.0,
108+
NaN,
109+
true,
110+
false,
111+
null,
112+
undefined
113+
];
114+
115+
b.tic();
116+
for ( i = 0; i < b.iterations; i++ ) {
117+
bool = isNonPositiveFinite.isPrimitive( values[ i % values.length ] );
118+
if ( typeof bool !== 'boolean' ) {
119+
b.fail( 'should return a boolean' );
120+
}
121+
}
122+
b.toc();
123+
if ( !isBoolean( bool ) ) {
124+
b.fail( 'should return a boolean' );
125+
}
126+
b.pass( 'benchmark finished' );
127+
b.end();
128+
});
129+
130+
bench( pkg+'::objects:isPrimitive', function benchmark( b ) {
131+
var values;
132+
var bool;
133+
var i;
134+
135+
values = [
136+
[],
137+
{},
138+
function noop() {},
139+
new Number( 2.0 ),
140+
new Number( -3.0 ),
141+
new Number( 3.14 )
142+
];
143+
144+
b.tic();
145+
for ( i = 0; i < b.iterations; i++ ) {
146+
bool = isNonPositiveFinite.isPrimitive( values[ i % values.length ] );
147+
if ( typeof bool !== 'boolean' ) {
148+
b.fail( 'should return a boolean' );
149+
}
150+
}
151+
b.toc();
152+
if ( !isBoolean( bool ) ) {
153+
b.fail( 'should return a boolean' );
154+
}
155+
b.pass( 'benchmark finished' );
156+
b.end();
157+
});
158+
159+
bench( pkg+'::primitives:isObject', function benchmark( b ) {
160+
var values;
161+
var bool;
162+
var i;
163+
164+
values = [
165+
'5',
166+
5.0,
167+
4.0,
168+
3.14,
169+
-5.0,
170+
-4.0,
171+
NaN,
172+
true,
173+
false,
174+
null,
175+
undefined
176+
];
177+
178+
b.tic();
179+
for ( i = 0; i < b.iterations; i++ ) {
180+
bool = isNonPositiveFinite.isObject( values[ i % values.length ] );
181+
if ( typeof bool !== 'boolean' ) {
182+
b.fail( 'should return a boolean' );
183+
}
184+
}
185+
b.toc();
186+
if ( !isBoolean( bool ) ) {
187+
b.fail( 'should return a boolean' );
188+
}
189+
b.pass( 'benchmark finished' );
190+
b.end();
191+
});
192+
193+
bench( pkg+'::objects:isObject', function benchmark( b ) {
194+
var values;
195+
var bool;
196+
var i;
197+
198+
values = [
199+
[],
200+
{},
201+
function noop() {},
202+
new Number( 2.0 ),
203+
new Number( -3.0 ),
204+
new Number( 3.14 )
205+
];
206+
207+
b.tic();
208+
for ( i = 0; i < b.iterations; i++ ) {
209+
bool = isNonPositiveFinite.isObject( values[ i % values.length ] );
210+
if ( typeof bool !== 'boolean' ) {
211+
b.fail( 'should return a boolean' );
212+
}
213+
}
214+
b.toc();
215+
if ( !isBoolean( bool ) ) {
216+
b.fail( 'should return a boolean' );
217+
}
218+
b.pass( 'benchmark finished' );
219+
b.end();
220+
});

0 commit comments

Comments
 (0)