randomise-array
Advanced tools
Comparing version 1.0.1 to 1.0.3
{ | ||
"name": "randomise-array", | ||
"version": "1.0.1", | ||
"version": "1.0.3", | ||
"description": "Randomise arrays and generate a new array with any given length", | ||
@@ -5,0 +5,0 @@ "main": "randomise-array.js", |
@@ -0,4 +1,4 @@ | ||
function randomArray(arr, options= {}){ | ||
//First perform type checks | ||
// return arr | ||
if (typeof options !== "object") { | ||
@@ -15,21 +15,28 @@ return new Error('The second argument must be an object') | ||
options.length = options.length || arr.length | ||
options.gte = options.gte || -Infinity | ||
options.lte = options.lte || Infinity | ||
if (typeof options.lte !== 'number') { | ||
if (typeof options.lte !== 'number' && typeof options.lte !== 'undefined') { | ||
return new Error('lte must be a number') | ||
} | ||
if (typeof options.gte !== 'number') { | ||
if (typeof options.gte !== 'number' && typeof options.lte !== 'undefined') { | ||
return new Error('gte must be a number') | ||
} | ||
var arr = arr.filter((val) => { | ||
return val >= options.gte && val <= options.lte | ||
}) | ||
if (options.lte) { | ||
arr = arr.filter((val) => { | ||
return val <= options.lte | ||
}) | ||
} | ||
if (options.gte) { | ||
arr = arr.filter((val) => { | ||
return val >= options.lte | ||
}) | ||
} | ||
if(options.length > arr.length && options.unique) { | ||
return new Error(`Cannot generate ${options.length} unique values from ${arr.length} values`) | ||
} | ||
for (currInd = 0; currInd < options.length; currInd++) { | ||
randInd= Math.floor(Math.random()* arr.length) | ||
newArr[currInd] = arr[randInd] | ||
newArr[currInd] = arr[randInd] | ||
if (options.unique) { | ||
@@ -41,2 +48,3 @@ arr.splice(randInd,1) | ||
} | ||
module.exports= exports = randomArray |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3244
46