From 2158d0ebb07779c7a35228c61d6d6062d4160e00 Mon Sep 17 00:00:00 2001 From: customcommander Date: Fri, 16 Apr 2021 14:08:23 +0100 Subject: [PATCH 1/7] upgrade to mocha v8 --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index fc0d190..cb77aaf 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "benchmarked": "^0.2.5", "gulp-format-md": "^0.1.11", "isobject": "^2.1.0", - "mocha": "^3.1.2", + "mocha": "^8.3.2", "repeating": "^3.0.0", "text-table": "^0.2.0", "yargs-parser": "^4.0.2" @@ -74,4 +74,4 @@ "verb" ] } -} \ No newline at end of file +} From 945a312b9c9be0ec2e603d52467f3f5c155798dc Mon Sep 17 00:00:00 2001 From: customcommander Date: Fri, 16 Apr 2021 14:28:49 +0100 Subject: [PATCH 2/7] add test to make sure numbers arent rounded --- test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test.js b/test.js index a1249d3..1e4c781 100644 --- a/test.js +++ b/test.js @@ -9,6 +9,7 @@ require('mocha'); var assert = require('assert'); +const { it } = require('mocha'); var repeat = require('./'); describe('repeat', function() { @@ -23,6 +24,11 @@ describe('repeat', function() { assert.equal(repeat('a', null), ''); }); + it('shoud not round numbers:', function() { + assert.equal(repeat('A', 6.9), repeat('A', 6)); + assert.equal(repeat('A', '6.9'), repeat('A', 6)); + }); + it('should repeat the given string n times', function() { assert.equal(repeat(' ', 0), ''); assert.equal(repeat('a', 0), ''); From c8220092e27ef9beabaab21ee402a480d7134434 Mon Sep 17 00:00:00 2001 From: customcommander Date: Fri, 16 Apr 2021 14:31:26 +0100 Subject: [PATCH 3/7] add test for negative numbers --- test.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test.js b/test.js index 1e4c781..507c05e 100644 --- a/test.js +++ b/test.js @@ -29,6 +29,13 @@ describe('repeat', function() { assert.equal(repeat('A', '6.9'), repeat('A', 6)); }); + it('should return an empty string for negative numbers:', function () { + assert.equal(repeat('A', -42), ''); + assert.equal(repeat('A', '-42'), ''); + assert.equal(repeat('A', -Infinity), ''); + assert.equal(repeat('A', '-Infinity'), ''); + }); + it('should repeat the given string n times', function() { assert.equal(repeat(' ', 0), ''); assert.equal(repeat('a', 0), ''); From d22fda2540b2f70eefdf490db8ed71a44e2cb653 Mon Sep 17 00:00:00 2001 From: customcommander Date: Fri, 16 Apr 2021 21:09:12 +0100 Subject: [PATCH 4/7] use const vs var in tests --- test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test.js b/test.js index 507c05e..a630782 100644 --- a/test.js +++ b/test.js @@ -8,9 +8,9 @@ 'use strict'; require('mocha'); -var assert = require('assert'); +const assert = require('assert'); const { it } = require('mocha'); -var repeat = require('./'); +const repeat = require('./'); describe('repeat', function() { it('should return an empty string when a number is not given:', function() { From 9542213870b4edc163c4a11b6623960257b4cd7d Mon Sep 17 00:00:00 2001 From: customcommander Date: Fri, 16 Apr 2021 21:29:11 +0100 Subject: [PATCH 5/7] early exit condition when string is empty --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index 4459afd..50512c4 100644 --- a/index.js +++ b/index.js @@ -43,6 +43,9 @@ function repeat(str, num) { throw new TypeError('expected a string'); } + // no need to repeat an empty string + if (str === '') return ''; + // cover common, quick use cases if (num === 1) return str; if (num === 2) return str + str; From fd4f86a55168740c4d379c8d53afc3f9c1f9e286 Mon Sep 17 00:00:00 2001 From: customcommander Date: Fri, 16 Apr 2021 21:43:14 +0100 Subject: [PATCH 6/7] throws when attempting to repeat ad infinitum --- index.js | 5 +++++ test.js | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/index.js b/index.js index 50512c4..ad09b18 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,11 @@ function repeat(str, num) { if (num === 2) return str + str; var max = str.length * num; + + if (max === Infinity) { + throw new TypeError('cannot repeat indefinitely'); + } + if (cache !== str || typeof cache === 'undefined') { cache = str; res = ''; diff --git a/test.js b/test.js index a630782..d2c9563 100644 --- a/test.js +++ b/test.js @@ -72,4 +72,11 @@ describe('repeat', function() { assert.throws(function() {repeat(10); }, /expected a string/); assert.throws(function() {repeat(null); }, /expected a string/); }); + + it('should throw an error when attempting to repeat ad infinitum', function () { + assert.throws(function () { repeat('foo', Infinity); }); + assert.throws(function () { repeat('foo', 'Infinity'); }); + assert.doesNotThrow(function () { repeat('foo', -Infinity); }); + assert.doesNotThrow(function () { repeat('foo', '-Infinity'); }); + }); }); From 0e41e0d57ead6f0cbfc211c9bcb1b9c293312080 Mon Sep 17 00:00:00 2001 From: customcommander Date: Fri, 16 Apr 2021 22:31:55 +0100 Subject: [PATCH 7/7] test against latest stable node.js version --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8acd460..0300130 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,3 @@ os: language: node_js node_js: - node - - '6' - - '4' - - '0.12' - - '0.10'