@tsdotnet/text-utility
Version:
A set of commonly used functions for manipulating and formatting strings.
49 lines • 1.77 kB
JavaScript
/*!
* @author electricessence / https://github.com/electricessence/
* Originally based upon: https://github.com/vwxyz/padding
* Licensing: MIT
*/
import { EMPTY, repeat } from './Utility';
const SPACE = ' ';
const ZERO = '0';
export function padStringLeft(source, minLength, pad = SPACE) {
return pad && minLength > 0 ? repeat(pad, minLength - source.length) + source : source;
}
export function padStringRight(source, minLength, pad = SPACE) {
return pad && minLength > 0 ? source + repeat(pad, minLength - source.length) : source;
}
export function padNumberLeft(source, minLength, pad = ZERO) {
// noinspection SuspiciousTypeOfGuard
if (typeof source != 'number')
throw new Error('Cannot pad non-number.');
if (!source)
source = 0;
return padStringLeft(source + EMPTY, minLength, pad + EMPTY);
}
export function padNumberRight(source, minLength, pad = ZERO) {
// noinspection SuspiciousTypeOfGuard
if (typeof source != 'number')
throw new Error('Cannot pad non-number.');
if (!source)
source = 0;
return padStringRight(source + EMPTY, minLength, pad + EMPTY);
}
export function padLeft(source, minLength, pad) {
switch (typeof source) {
case 'string':
return padStringLeft(source, minLength, pad);
case 'number':
return padNumberLeft(source, minLength, pad);
}
throw new Error('Invalid source type.');
}
export function padRight(source, minLength, pad) {
switch (typeof source) {
case 'string':
return padStringRight(source, minLength, pad);
case 'number':
return padNumberRight(source, minLength, pad);
}
throw new Error('Invalid source type.');
}
//# sourceMappingURL=Padding.js.map