Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 65c9793

Browse files
Merge pull request #9 from kentcdodds/pr/padLeft
WIP: Cannot get coverage quite there
2 parents 3dc204a + 0d6749a commit 65c9793

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import flatten from './flatten'
22
import getQueryStringParam from './get-query-string-param'
33
import snakeToCamel from './snake-to-camel'
4+
import padLeft from './pad-left'
5+
46

57
export {
68
flatten,
79
snakeToCamel,
810
getQueryStringParam,
11+
padLeft,
912
}
13+

src/pad-left.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export default padLeft
2+
3+
/**
4+
* Original Source: http://stackoverflow.com/a/34083277/971592
5+
*
6+
* This method will pad the left of the given string by
7+
* the given size with the given character
8+
*
9+
* @param {String} str - The string to pad
10+
* @param {Number} size - The total size to pad
11+
* @param {String} padWith - The character to use for padding
12+
* @return {String} - The padded string
13+
*/
14+
function padLeft(str, size, padWith) {
15+
if (size <= str.length) {
16+
return str
17+
} else {
18+
return Array(size - str.length + 1).join(padWith || '0') + str
19+
}
20+
}
21+

test/pad-left.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import test from 'ava'
2+
import {padLeft} from '../src'
3+
4+
test('pads left of the given string', t => {
5+
const original = '123'
6+
const expected = 'zz123'
7+
const padLength = 5
8+
const padWith = 'z'
9+
const actual = padLeft(original, padLength, padWith)
10+
t.same(actual, expected)
11+
})
12+
13+
test('defaults to pad a zero', t => {
14+
const original = '123'
15+
const expected = '00123'
16+
const padLength = 5
17+
const actual = padLeft(original, padLength)
18+
t.same(actual, expected)
19+
})
20+
21+
test('does not pad a string longer than the pad length', t => {
22+
const original = '1234'
23+
const expected = '1234'
24+
const padLength = 3
25+
const actual = padLeft(original, padLength)
26+
t.same(actual, expected)
27+
})
28+

0 commit comments

Comments
 (0)