This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Expand file tree Collapse file tree 3 files changed +53
-0
lines changed Original file line number Diff line number Diff line change 1
1
import flatten from './flatten'
2
2
import getQueryStringParam from './get-query-string-param'
3
3
import snakeToCamel from './snake-to-camel'
4
+ import padLeft from './pad-left'
5
+
4
6
5
7
export {
6
8
flatten ,
7
9
snakeToCamel ,
8
10
getQueryStringParam ,
11
+ padLeft ,
9
12
}
13
+
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments