Skip to content

Commit 419c260

Browse files
committed
chore: add isProbabilityArray (issue stdlib-js#6976)
1 parent cee6330 commit 419c260

File tree

2 files changed

+42
-59
lines changed
  • lib/node_modules/@stdlib/assert/is-probability-array

2 files changed

+42
-59
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
function isProbability(x) {
4+
return typeof x === 'number' && x >= 0 && x <= 1;
5+
}
6+
7+
/**
8+
* Tests if a value is an array-like object containing only probabilities (numbers between 0 and 1).
9+
*
10+
* @param {*} value - value to test
11+
* @returns {boolean} boolean indicating whether value is a probability array
12+
*/
13+
function isProbabilityArray(value) {
14+
var i;
15+
if (!Array.isArray(value)) {
16+
return false;
17+
}
18+
for (i = 0; i < value.length; i++) {
19+
if (!isProbability(value[i])) {
20+
return false;
21+
}
22+
}
23+
return true;
24+
}
25+
26+
// EXPORTS //
27+
module.exports = isProbabilityArray;

lib/node_modules/@stdlib/assert/is-probability-array/test/test.js

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,75 +20,31 @@
2020

2121
// MODULES //
2222

23-
var tape = require( 'tape' );
24-
var Float64Array = require( '@stdlib/array/float64' );
25-
var Number = require( '@stdlib/number/ctor' );
26-
var isProbabilityArray = require( './../lib' );
27-
23+
'use strict';
2824

29-
// TESTS //
25+
var tape = require('tape');
26+
var isProbabilityArray = require('./../lib/main.js');
3027

31-
tape( 'main export is a function', function test( t ) {
32-
t.ok( true, __filename );
33-
t.strictEqual( typeof isProbabilityArray, 'function', 'main export is a function' );
28+
tape('main export is a function', function test(t) {
29+
t.ok(typeof isProbabilityArray === 'function', 'main export is a function');
3430
t.end();
3531
});
3632

37-
tape( 'the function tests for an array-like object containing only probabilities', function test( t ) {
38-
var arr;
39-
40-
arr = [ 0.9, new Number( 0.8 ), 0.0 ]; // eslint-disable-line no-new-wrappers
41-
t.equal( isProbabilityArray( arr ), true, 'returns true' );
42-
43-
arr = new Float64Array( [ 0.9, 0.5, 0.3 ] );
44-
t.equal( isProbabilityArray( arr ), true, 'returns true' );
45-
46-
arr = {
47-
'length': 2,
48-
'0': 0.3,
49-
'1': 0.8
50-
};
51-
t.equal( isProbabilityArray( arr ), true, 'returns true' );
52-
53-
arr = [ 0.9, '3', null ];
54-
t.equal( isProbabilityArray( arr ), false, 'returns false' );
55-
56-
arr = new Float64Array( [ 0.9, NaN, 0.3 ] );
57-
t.equal( isProbabilityArray( arr ), false, 'returns false' );
58-
33+
tape('returns true for arrays of probabilities', function test(t) {
34+
t.equal(isProbabilityArray([0, 0.5, 1]), true);
35+
t.equal(isProbabilityArray([0.2, 0.8]), true);
5936
t.end();
6037
});
6138

62-
tape( 'the function provides a method to test for an array-like object containing only primitive probabilities', function test( t ) {
63-
var arr;
64-
65-
arr = [ 1.0, 0.0 ];
66-
t.equal( isProbabilityArray.primitives( arr ), true, 'returns true' );
67-
68-
arr = new Float64Array( [ 0.9, 0.5, 0.3 ] );
69-
t.equal( isProbabilityArray( arr ), true, 'returns true' );
70-
71-
arr = [ new Number( 5 ), 1.0, 1.0 ]; // eslint-disable-line no-new-wrappers
72-
t.equal( isProbabilityArray.primitives( arr ), false, 'returns false' );
73-
39+
tape('returns false for arrays with out-of-range numbers', function test(t) {
40+
t.equal(isProbabilityArray([0, -0.1, 0.5]), false);
41+
t.equal(isProbabilityArray([1.2, 0.3]), false);
7442
t.end();
7543
});
7644

77-
tape( 'the function provides a method to test for an array-like object containing only containing only Number objects whose values are probabilities', function test( t ) {
78-
var arr;
79-
80-
arr = [ new Number( 0.5 ), new Number( 0.5 ) ]; // eslint-disable-line no-new-wrappers
81-
t.equal( isProbabilityArray.objects( arr ), true, 'returns true' );
82-
83-
arr = {
84-
'length': 2,
85-
'0': new Number( 0.3 ), // eslint-disable-line no-new-wrappers
86-
'1': new Number( 0.8 ) // eslint-disable-line no-new-wrappers
87-
};
88-
t.equal( isProbabilityArray( arr ), true, 'returns true' );
89-
90-
arr = [ 0.5, 0.0 ];
91-
t.equal( isProbabilityArray.objects( arr ), false, 'returns false' );
92-
45+
tape('returns false for non-arrays', function test(t) {
46+
t.equal(isProbabilityArray('not an array'), false);
47+
t.equal(isProbabilityArray(0.5), false);
48+
t.equal(isProbabilityArray(null), false);
9349
t.end();
9450
});

0 commit comments

Comments
 (0)