* master: Stats subtitle css fix use ng-template for drop preview and placeholder fix show add button when multi select active fix virtual scroll drag&drop preview and placeholder add virtual scroll in workflow
72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import { tap } from 'rxjs/operators';
|
|
import { ITrackable } from '../listing/models/trackable';
|
|
import { MonoTypeOperatorFunction } from 'rxjs';
|
|
import moment from 'moment';
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
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;
|
|
}
|
|
|
|
export function hasFormChanged(form: FormGroup, initialFormValue): boolean {
|
|
|
|
if (form && initialFormValue) {
|
|
for (const key of Object.keys(form.getRawValue())) {
|
|
const initialValue = initialFormValue[key];
|
|
const updatedValue = form.get(key).value;
|
|
|
|
if (initialValue == null && updatedValue != null) {
|
|
const updatedValueType = typeof updatedValue;
|
|
if (updatedValueType !== 'string' && updatedValueType !== 'boolean') {
|
|
return true;
|
|
} else if (updatedValueType === 'string' && updatedValue.length > 0) {
|
|
return true;
|
|
} else if (updatedValueType === 'boolean' && updatedValue === true) {
|
|
return true;
|
|
}
|
|
} else if (initialValue !== updatedValue) {
|
|
if (Array.isArray(updatedValue)) {
|
|
if (JSON.stringify(initialValue) !== JSON.stringify(updatedValue)) {
|
|
return true;
|
|
}
|
|
} else if (updatedValue instanceof moment) {
|
|
if (!moment(updatedValue).isSame(moment(initialValue))) {
|
|
return true;
|
|
}
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|