diff --git a/src/flatten.js b/src/flatten.js index 23e9df70..7f6b8513 100644 --- a/src/flatten.js +++ b/src/flatten.js @@ -9,7 +9,8 @@ export default flatten * @return {Array} - The flattened array */ function flatten(...args) { - return args.reduce(function flattenReducer(flat, toFlatten) { - return flat.concat(Array.isArray(toFlatten) ? flatten(...toFlatten) : toFlatten) - }, []) -} + return args.reduce(function flattenReducer(flat, toFlatten) { + + return flat.concat(Array.isArray(toFlatten) ? flatten(...toFlatten) : toFlatten) + }, []) +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index 6352cf11..2a35678f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ - import reduce from './reduce-to-tally' import flatten from './flatten' import getQueryStringParam from './get-query-string-param' import snakeToCamel from './snake-to-camel' import padLeft from './pad-left' +import padRight from './pad-right' import randomInteger from './random-integer' import arrayFill from './array-fill' import sortObjectsArray from './sort-objects-array' @@ -26,29 +26,30 @@ import dec2bin from './dec2bin' import searchAndReplace from './search-and-replace' export { - reduce, - flatten, - snakeToCamel, - getQueryStringParam, - padLeft, - randomInteger, - arrayFill, - sortObjectsArray, - objectValuesToString, - getObjectSize, - isArray, - validateEmail, - hex2rgb, - isNullOrWhitespace, - isToday, - startsWith, - removeDuplicates, - add, - subtract, - divide, - multiply, - square, - sum, - dec2bin, - searchAndReplace, -} + reduce, + flatten, + snakeToCamel, + getQueryStringParam, + padLeft, + padRight, + randomInteger, + arrayFill, + sortObjectsArray, + objectValuesToString, + getObjectSize, + isArray, + validateEmail, + hex2rgb, + isNullOrWhitespace, + isToday, + startsWith, + removeDuplicates, + add, + subtract, + divide, + multiply, + square, + sum, + dec2bin, + searchAndReplace, +} \ No newline at end of file diff --git a/src/pad-right.js b/src/pad-right.js new file mode 100644 index 00000000..c351f64a --- /dev/null +++ b/src/pad-right.js @@ -0,0 +1,20 @@ +export default padRight + +/** + * 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 padRight(str, size, padWith) { + if (size <= str.length) { + return str + } else { + return str + Array(size - str.length + 1).join(padWith || '0') + } +} \ No newline at end of file diff --git a/test/pad-right.test.js b/test/pad-right.test.js new file mode 100644 index 00000000..a8a58246 --- /dev/null +++ b/test/pad-right.test.js @@ -0,0 +1,27 @@ +import test from 'ava' +import { padRight } from '../src' + +test('pads left of the given string', t => { + const original = '123' + const expected = '123zz' + const padLength = 5 + const padWith = 'z' + const actual = padRight(original, padLength, padWith) + t.deepEqual(actual, expected) +}) + +test('defaults to pad a zero', t => { + const original = '123' + const expected = '12300' + const padLength = 5 + const actual = padRight(original, padLength) + t.deepEqual(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 = padRight(original, padLength) + t.deepEqual(actual, expected) +}) \ No newline at end of file