Skip to content

Commit

Permalink
[Breaking] Take the *first* part of the fillStr when truncating.
Browse files Browse the repository at this point in the history
Per tc39/proposal-string-pad-start-end#6, and to be consistent with Ruby and PHP.

Fixes #1.
  • Loading branch information
ljharb committed Sep 25, 2015
1 parent dd69333 commit 44cad4b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Most common usage:
```js
var padLeft = require('string.prototype.padleft');

assert(padLeft('foo', 5, 'bar') === 'arfoo');
assert(padLeft('foo', 5, 'bar') === 'bafoo');

padLeft.shim();

Expand Down
2 changes: 1 addition & 1 deletion implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function padLeft(maxLength) {
stringFiller += fillStr;
}
if (stringFiller.length > fillLen) {
stringFiller = slice(stringFiller, -fillLen);
stringFiller = slice(stringFiller, 0, fillLen);
}

return stringFiller + S;
Expand Down
4 changes: 2 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = function (padLeft, t) {
st.equal(padLeft('a', 3, 'b'), 'bba', 'string pads left with single character');
st.equal(padLeft('abc', 3, 'd'), 'abc', 'string already of maximum length noops');
st.equal(padLeft('abc', -3, 'd'), 'abc', 'string already larger than maximum length noops');
st.equal(padLeft('cd', 3, 'ab'), 'bcd', 'string with maximum length equal to length plus filler length, pads');
st.equal(padLeft('cd', 3, 'ab'), 'acd', 'string with maximum length equal to length plus filler length, pads');
st.equal(padLeft('abc'), 'abc', 'absent maximum length is noop');
st.equal(padLeft('a', 3), ' a', 'absent fillStr defaults to a space');
st.equal(padLeft('ed', 6, null), 'nulled', 'non-string fillStr gets stringified');
Expand All @@ -14,7 +14,7 @@ module.exports = function (padLeft, t) {
});

t.test('truncated fill string', function (st) {
st.equal(padLeft('a', 2, 'bc'), 'ca', 'truncates at the provided max length');
st.equal(padLeft('a', 2, 'bc'), 'ba', 'truncates at the provided max length');

st.end();
});
Expand Down

0 comments on commit 44cad4b

Please sign in to comment.