This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from kentcdodds/pr/padLeft
WIP: Cannot get coverage quite there
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
|