UNPKG

@feugene/mu

Version:

Helpful TS utilities without dependencies

38 lines 1.31 kB
import isNil from '../is/isNil.mjs'; /** * This function add symbols to string in start or end * * @param {string | number | undefined} value * @param {int} targetLength * @param {string} padString * @param {boolean} leading If TRUE add symbols before string, else - after * @returns {string} */ export default function pad(value, targetLength, padString = ' ', leading = true) { targetLength = Math.trunc(targetLength); //floor if number or convert non-number to 0; if (isNil(value)) { return ''; } const str = String(value); if (str.length > targetLength) { return str; } targetLength = targetLength - str.length; if (targetLength > padString.length) { padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed } return leading ? padString.slice(0, targetLength) + str : str + padString.slice(0, targetLength); } /** * This function add leading symbols */ export function padStart(value, targetLength, padString = ' ') { return pad(value, targetLength, padString); } /** * This function add ending symbols */ export function padEnd(value, targetLength, padString = ' ') { return pad(value, targetLength, padString, false); } //# sourceMappingURL=pad.mjs.map