Skip to content

Commit ea24598

Browse files
committed
Fix: RangeError on Number.padLeft on large numbers
1 parent ec5ca47 commit ea24598

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

spec/models/time.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const config = require('./../../src/models/time');
2+
const expect = require('chai').expect;
3+
4+
describe('time class', () => {
5+
it('Formats numbers', () => {
6+
expect((new Number(1)).padLeft(2, "0")).to.equal('01');
7+
expect((new Number(12)).padLeft(2, "0")).to.equal('12');
8+
expect((new Number(123)).padLeft(2, "0")).to.equal('123');
9+
expect((new Number(1234)).padLeft(2, "0")).to.equal('1234');
10+
});
11+
});
12+

src/models/time.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ const roundedRegex = /(\[\%([^\>\]]*)\:([^\]]*)\])/ig;
99
const conditionalSimpleRegex = /([0-9]*)\>(.*)/ig;
1010
const defaultRegex = /(\[\%([^\]]*)\])/ig;
1111

12+
/**
13+
* Adds leading zeroes to the number
14+
*
15+
* @param {Number} n
16+
* @param {String} str
17+
* @returns {String}
18+
*/
1219
Number.prototype.padLeft = function (n, str) {
13-
return Array(n - String(this).length + 1).join(str || '0') + this;
20+
return Array(Math.max(0, n - String(this).length + 1)).join(str || '0') + this;
1421
};
1522

1623
/**

0 commit comments

Comments
 (0)