diff --git a/lib/node_modules/@stdlib/assert/is-probability-array/lib/main.js b/lib/node_modules/@stdlib/assert/is-probability-array/lib/main.js new file mode 100644 index 000000000000..674668e94b3e --- /dev/null +++ b/lib/node_modules/@stdlib/assert/is-probability-array/lib/main.js @@ -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; diff --git a/lib/node_modules/@stdlib/assert/is-probability-array/test/test.js b/lib/node_modules/@stdlib/assert/is-probability-array/test/test.js index 8f8f31bd8bce..11ff137550da 100644 --- a/lib/node_modules/@stdlib/assert/is-probability-array/test/test.js +++ b/lib/node_modules/@stdlib/assert/is-probability-array/test/test.js @@ -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(); });