1
0
Fork 0
js-utils/src/String.ts

25 lines
556 B
TypeScript

export const DEFAULT_TRIM = 8;
export function leftPad(val: string, min = DEFAULT_TRIM, fill = '0'): string {
if (val.length < min) {
const len = min - val.length;
const pre = Array(len).fill(fill).join('').slice(0, len);
return `${pre}${val}`;
} else {
return val;
}
}
export function trim(val: string, max: number, tail = '...'): string {
if (val.length <= max) {
return val;
}
if (max < tail.length) {
return val.substr(0, max);
}
const start = val.substr(0, max - tail.length);
return `${start}${tail}`;
}