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

Commit

Permalink
Merge pull request #9 from kentcdodds/pr/padLeft
Browse files Browse the repository at this point in the history
WIP: Cannot get coverage quite there
  • Loading branch information
silvestertomato committed Feb 3, 2016
2 parents 3dc204a + 0d6749a commit 65c9793
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import flatten from './flatten'
import getQueryStringParam from './get-query-string-param'
import snakeToCamel from './snake-to-camel'
import padLeft from './pad-left'


export {
flatten,
snakeToCamel,
getQueryStringParam,
padLeft,
}

21 changes: 21 additions & 0 deletions src/pad-left.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default padLeft

/**
* Original Source: http://stackoverflow.com/a/34083277/971592
*
* This method will pad the left of the given string by
* the given size with the given character
*
* @param {String} str - The string to pad
* @param {Number} size - The total size to pad
* @param {String} padWith - The character to use for padding
* @return {String} - The padded string
*/
function padLeft(str, size, padWith) {
if (size <= str.length) {
return str
} else {
return Array(size - str.length + 1).join(padWith || '0') + str
}
}

28 changes: 28 additions & 0 deletions test/pad-left.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import test from 'ava'
import {padLeft} from '../src'

test('pads left of the given string', t => {
const original = '123'
const expected = 'zz123'
const padLength = 5
const padWith = 'z'
const actual = padLeft(original, padLength, padWith)
t.same(actual, expected)
})

test('defaults to pad a zero', t => {
const original = '123'
const expected = '00123'
const padLength = 5
const actual = padLeft(original, padLength)
t.same(actual, expected)
})

test('does not pad a string longer than the pad length', t => {
const original = '1234'
const expected = '1234'
const padLength = 3
const actual = padLeft(original, padLength)
t.same(actual, expected)
})

0 comments on commit 65c9793

Please sign in to comment.