Skip to content

Commit 609682c

Browse files
feat: add assert/is-negative-finite
PR-URL: #1356 This commit adds the assert/is-negative-finite module for handling negative finite values. Signed-off-by: GUNJ JOSHI <gunjjoshi8372@gmail.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 50616dc commit 609682c

File tree

15 files changed

+1311
-0
lines changed

15 files changed

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

0 commit comments

Comments
 (0)