common-ui/src/lib/utils/functions.ts
2022-01-19 10:37:00 +02:00

37 lines
1008 B
TypeScript

import { tap } from 'rxjs/operators';
import { ITrackable } from '../listing/models/trackable';
import { MonoTypeOperatorFunction } from 'rxjs';
import moment from 'moment';
export function capitalize(value: string): string {
if (!value) {
return '';
}
return value.charAt(0).toUpperCase() + value.slice(1);
}
export function humanize(value: string, lowercase = true): string {
if (!value) {
return '';
}
const words = (lowercase ? value.toLowerCase() : value).split(/[ \-_]+/);
return words.map(capitalize).join(' ');
}
export function log<T>(): MonoTypeOperatorFunction<T> {
return tap<T>(res => console.log(`%c[${moment().format('HH:mm:ss.SSS')}]`, 'color: yellow;', res));
}
export function toNumber(str: string): number {
try {
return parseInt(`${str}`.replace(/\D/g, ''), 10);
} catch (e) {
return 0;
}
}
export function trackByFactory<T extends ITrackable>() {
return (index: number, item: T): string => item.id;
}