From 45dc0f0f1199bc510d477b1e132e098c4c605e3f Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Wed, 29 Jun 2022 13:28:20 +0300 Subject: [PATCH] add some augmented functions --- .../progress-loading.component.scss | 2 +- src/lib/utils/functions.ts | 39 ++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/lib/loading/progress-loading/progress-loading.component.scss b/src/lib/loading/progress-loading/progress-loading.component.scss index d87a066..02e347e 100644 --- a/src/lib/loading/progress-loading/progress-loading.component.scss +++ b/src/lib/loading/progress-loading/progress-loading.component.scss @@ -1,4 +1,4 @@ -@use "../../../assets/styles/common-mixins"; +@use '../../../assets/styles/common-mixins'; :host { display: flex; diff --git a/src/lib/utils/functions.ts b/src/lib/utils/functions.ts index 14b0839..a208c33 100644 --- a/src/lib/utils/functions.ts +++ b/src/lib/utils/functions.ts @@ -3,7 +3,7 @@ import { UntypedFormGroup } from '@angular/forms'; import { forOwn, has, isEqual, isPlainObject, transform } from 'lodash-es'; import dayjs, { Dayjs } from 'dayjs'; -export function capitalize(value: string): string { +export function capitalize(value: string | String): string { if (!value) { return ''; } @@ -20,7 +20,7 @@ export function humanize(value: string, lowercase = true): string { } export function _log(value: unknown, message = '') { - console.log(`%c[${dayjs().format('HH:mm:ss.SSS')}] ${message}`, 'color: yellow;', value); + console.log(`%c[${dayjs().format('mm:ss.SSS')}] ${message}`, 'color: yellow;', value); } export function toNumber(str: string): number { @@ -130,3 +130,38 @@ export function bool(value: unknown): boolean { return Boolean(_value); } + +declare global { + interface String { + capitalize(): string; + } + + interface Array { + /** + * Returns a new array with all falsy values removed. + * The values false, null, 0, "", undefined, and NaN are considered falsy. + * @param and - Additional function that is called for each truthy element in the array. + * The value returned from the function determines whether the element is kept or removed. + */ + filterTruthy(and?: (value: T) => boolean): T[]; + } + + interface Console { + /** + * Logs a beautifully formatted message to the console. + * @param value - The object to log. + * @param message - Additional message. + */ + write(value: unknown, message?: string): void; + } +} + +console.write = _log; + +String.prototype.capitalize = function _capitalize(this: string): string { + return capitalize(this); +}; + +Array.prototype.filterTruthy = function (this: T[], predicate: (value: T) => boolean = () => true): T[] { + return this.filter(value => !!value && predicate(value)); +};