Write .pad()

This commit is contained in:
marleyrae 2023-06-27 18:34:53 -07:00
parent 76758547dc
commit fba7363fa4

View file

@ -1,4 +1,4 @@
let _ = {} const _ = {}
/* --------------------------------------------------------------- clamp ---- */ /* --------------------------------------------------------------- clamp ---- */
@ -13,7 +13,7 @@ _.inRange = function (num, start, end) {
} }
if (start > end) { if (start > end) {
let s = start const s = start
start = end start = end
end = s end = s
} }
@ -25,5 +25,18 @@ _.inRange = function (num, start, end) {
_.words = (string) => string.split(' ') _.words = (string) => string.split(' ')
/* ----------------------------------------------------------------- pad ---- */
_.pad = function (string, length) {
if (string.length >= length) {
return string
}
const addToStart = Math.floor((length - string.length) / 2)
const addToEnd = length - string.length - addToStart
return ' '.repeat(addToStart) + string + ' '.repeat(addToEnd)
}
// Do not write or modify code below this line. // Do not write or modify code below this line.
module.exports = _ module.exports = _