Modify random.item to work with array-like objects and update tests

This commit is contained in:
pyoor 2017-07-20 15:29:37 -04:00
parent 44d74b09bf
commit 28254a80de
2 changed files with 7 additions and 6 deletions

View File

@ -50,10 +50,11 @@ var random = { // eslint-disable-line no-unused-vars
return Math.exp(this.float() * Math.log(limit))
},
item: function (list) {
if (!(Array.isArray(list) || (list !== undefined && typeof list !== 'string' && list.hasOwnProperty('length')))) {
if (list === undefined || typeof list === 'string' || !list.hasOwnProperty('length')) {
logger.traceback()
throw new TypeError('this.item() received a non array type: \'' + list + '\'')
throw new TypeError('random.item() received an invalid object: \'' + list + '\'')
}
return list[this.number(list.length)]
},
key: function (obj) {

View File

@ -179,10 +179,10 @@ QUnit.test("random.ludOneTo() distribution", function(assert) {
});
QUnit.test("random.item() exception cases", function(assert) {
assert.throws(random.item, /non array type/);
assert.throws(function(){ return random.item(1); }, /non array type/);
assert.throws(function(){ return random.item("1"); }, /non array type/);
assert.throws(function(){ return random.item({}); }, /non array type/);
assert.throws(random.item, /received an invalid object/);
assert.throws(function(){ return random.item(1); }, /received an invalid object/);
assert.throws(function(){ return random.item("1"); }, /received an invalid object/);
assert.throws(function(){ return random.item({}); }, /received an invalid object/);
});
QUnit.test("random.item() distribution with list", function(assert) {