From 28254a80de578e97522074765c48b728bba44e5e Mon Sep 17 00:00:00 2001 From: pyoor Date: Thu, 20 Jul 2017 15:29:37 -0400 Subject: [PATCH] Modify random.item to work with array-like objects and update tests --- lib/random/random.js | 5 +++-- tests/random/random.js | 8 ++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/random/random.js b/lib/random/random.js index cac4191..9e482c1 100644 --- a/lib/random/random.js +++ b/lib/random/random.js @@ -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) { diff --git a/tests/random/random.js b/tests/random/random.js index b0bedc5..bc0a4eb 100644 --- a/tests/random/random.js +++ b/tests/random/random.js @@ -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) {