UNPKG

@yookue/ts-lang-utils

Version:

Common lang utilities for typescript

12 lines 583 B
export function center(text) { var width = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : -1; var placeholder = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ' '; if (!text || width < 0 || width > 0 && width <= text.length) { return text; } if (width === 0) { return ''; } var left = Math.floor((width - text.length) / 2); return text.padStart(text.length + left, placeholder !== null && placeholder !== void 0 ? placeholder : ' ').padEnd(width, placeholder !== null && placeholder !== void 0 ? placeholder : ' '); }