Skip to content

Infinity and early conditions #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ os:
language: node_js
node_js:
- node
- '6'
- '4'
- '0.12'
- '0.10'
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ 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;

var max = str.length * num;

if (max === Infinity) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. Please move this check to be just after the string check (line 46 or so) and change it to if (num === Infinity).

Copy link
Author

@customcommander customcommander Apr 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I put it on that line was to leverage the implicit "parseFloat()" when str.length and num are multiplied together e.g.

5 * Infinity;
//=> Infinity

5 * 'Infinity';
//=> Infinity

5 * '    Infinity   ';
//=> Infinity

That was also motivated by some of the tests that seem to suggest that num can be given as a string too. I'm happy to make the change but should I still care about the case of Infinity as a string then?

By the way it seems that if num can be given as a string then the two shortcuts at L50,51 are most likely ignored. Should this be addressed?

throw new TypeError('cannot repeat indefinitely');
}

if (cache !== str || typeof cache === 'undefined') {
cache = str;
res = '';
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -74,4 +74,4 @@
"verb"
]
}
}
}
24 changes: 22 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
'use strict';

require('mocha');
var assert = require('assert');
var repeat = require('./');
const assert = require('assert');
const { it } = require('mocha');
const repeat = require('./');

describe('repeat', function() {
it('should return an empty string when a number is not given:', function() {
Expand All @@ -23,6 +24,18 @@ 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 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), '');
Expand Down Expand Up @@ -59,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'); });
});
});