Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinity and early conditions #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

customcommander
Copy link

@customcommander customcommander commented Apr 16, 2021

This pull request has a few things going on:

  1. Mocha was a bit out of date. Upgraded it but that meant having to stop testing on Node.js 6 and below. These versions of Node.js aren't supported anymore so probably not a big deal anyway.
  2. Added a few missing tests: numbers aren't rounded and negative numbers produce empty strings
  3. There were var and const. No need to have both so replaced with const.
  4. Early exit condition when the string to repeat is empty
  5. Breaking change: can't repeat a string when number is Infinity

Infinity

The native String#repeat method does throw an error when Infinity is given as a parameter. So this changes aligns with the native behaviour. Also there's an issue with how Infinity works right now: with the same string to repeat, it will produce a different string:

const repeat = require('repeat-string');

repeat('馃尟', Infinity)
//=> '馃尟馃尟'
repeat('馃尟', Infinity)
//=> '馃尟馃尟馃尟馃尟'
repeat('馃尟', Infinity)
//=> '馃尟馃尟馃尟馃尟馃尟馃尟' 

This didn't seem right to me so took the liberty to throw an error instead.

@customcommander
Copy link
Author

@jonschlinkert 馃憢

// 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

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?

@jonschlinkert
Copy link
Owner

@customcommander nice work! I only had the one comment, everything else is great. thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants