Skip to content

chore: add isProbabilityArray (issue #6976) #6981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/node_modules/@stdlib/assert/is-probability-array/lib/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

function isProbability(x) {
return typeof x === 'number' && x >= 0 && x <= 1;
}

/**
* Tests if a value is an array-like object containing only probabilities (numbers between 0 and 1).
*
* @param {*} value - value to test
* @returns {boolean} boolean indicating whether value is a probability array
*/
function isProbabilityArray(value) {
var i;
if (!Array.isArray(value)) {
return false;
}
for (i = 0; i < value.length; i++) {
if (!isProbability(value[i])) {
return false;
}
}
return true;
}

// EXPORTS //
module.exports = isProbabilityArray;
74 changes: 15 additions & 59 deletions lib/node_modules/@stdlib/assert/is-probability-array/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,75 +20,31 @@

// MODULES //

var tape = require( 'tape' );
var Float64Array = require( '@stdlib/array/float64' );
var Number = require( '@stdlib/number/ctor' );
var isProbabilityArray = require( './../lib' );

'use strict';

// TESTS //
var tape = require('tape');
var isProbabilityArray = require('./../lib/main.js');

tape( 'main export is a function', function test( t ) {
t.ok( true, __filename );
t.strictEqual( typeof isProbabilityArray, 'function', 'main export is a function' );
tape('main export is a function', function test(t) {
t.ok(typeof isProbabilityArray === 'function', 'main export is a function');
t.end();
});

tape( 'the function tests for an array-like object containing only probabilities', function test( t ) {
var arr;

arr = [ 0.9, new Number( 0.8 ), 0.0 ]; // eslint-disable-line no-new-wrappers
t.equal( isProbabilityArray( arr ), true, 'returns true' );

arr = new Float64Array( [ 0.9, 0.5, 0.3 ] );
t.equal( isProbabilityArray( arr ), true, 'returns true' );

arr = {
'length': 2,
'0': 0.3,
'1': 0.8
};
t.equal( isProbabilityArray( arr ), true, 'returns true' );

arr = [ 0.9, '3', null ];
t.equal( isProbabilityArray( arr ), false, 'returns false' );

arr = new Float64Array( [ 0.9, NaN, 0.3 ] );
t.equal( isProbabilityArray( arr ), false, 'returns false' );

tape('returns true for arrays of probabilities', function test(t) {
t.equal(isProbabilityArray([0, 0.5, 1]), true);
t.equal(isProbabilityArray([0.2, 0.8]), true);
t.end();
});

tape( 'the function provides a method to test for an array-like object containing only primitive probabilities', function test( t ) {
var arr;

arr = [ 1.0, 0.0 ];
t.equal( isProbabilityArray.primitives( arr ), true, 'returns true' );

arr = new Float64Array( [ 0.9, 0.5, 0.3 ] );
t.equal( isProbabilityArray( arr ), true, 'returns true' );

arr = [ new Number( 5 ), 1.0, 1.0 ]; // eslint-disable-line no-new-wrappers
t.equal( isProbabilityArray.primitives( arr ), false, 'returns false' );

tape('returns false for arrays with out-of-range numbers', function test(t) {
t.equal(isProbabilityArray([0, -0.1, 0.5]), false);
t.equal(isProbabilityArray([1.2, 0.3]), false);
t.end();
});

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 ) {
var arr;

arr = [ new Number( 0.5 ), new Number( 0.5 ) ]; // eslint-disable-line no-new-wrappers
t.equal( isProbabilityArray.objects( arr ), true, 'returns true' );

arr = {
'length': 2,
'0': new Number( 0.3 ), // eslint-disable-line no-new-wrappers
'1': new Number( 0.8 ) // eslint-disable-line no-new-wrappers
};
t.equal( isProbabilityArray( arr ), true, 'returns true' );

arr = [ 0.5, 0.0 ];
t.equal( isProbabilityArray.objects( arr ), false, 'returns false' );

tape('returns false for non-arrays', function test(t) {
t.equal(isProbabilityArray('not an array'), false);
t.equal(isProbabilityArray(0.5), false);
t.equal(isProbabilityArray(null), false);
t.end();
});