Modify random.item to work with array-like objects and update tests
This commit is contained in:
parent
44d74b09bf
commit
28254a80de
2 changed files with 7 additions and 6 deletions
|
@ -50,10 +50,11 @@ var random = { // eslint-disable-line no-unused-vars
|
||||||
return Math.exp(this.float() * Math.log(limit))
|
return Math.exp(this.float() * Math.log(limit))
|
||||||
},
|
},
|
||||||
item: function (list) {
|
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()
|
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)]
|
return list[this.number(list.length)]
|
||||||
},
|
},
|
||||||
key: function (obj) {
|
key: function (obj) {
|
||||||
|
|
|
@ -179,10 +179,10 @@ QUnit.test("random.ludOneTo() distribution", function(assert) {
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("random.item() exception cases", function(assert) {
|
QUnit.test("random.item() exception cases", function(assert) {
|
||||||
assert.throws(random.item, /non array type/);
|
assert.throws(random.item, /received an invalid object/);
|
||||||
assert.throws(function(){ return random.item(1); }, /non array type/);
|
assert.throws(function(){ return random.item(1); }, /received an invalid object/);
|
||||||
assert.throws(function(){ return random.item("1"); }, /non array type/);
|
assert.throws(function(){ return random.item("1"); }, /received an invalid object/);
|
||||||
assert.throws(function(){ return random.item({}); }, /non array type/);
|
assert.throws(function(){ return random.item({}); }, /received an invalid object/);
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test("random.item() distribution with list", function(assert) {
|
QUnit.test("random.item() distribution with list", function(assert) {
|
||||||
|
|
Loading…
Reference in a new issue