From ce112e57c0bba37447be9635b6c485382bf59471 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Fri, 6 Aug 2021 18:26:04 +0300 Subject: [PATCH] add custom condition to required decorator --- src/lib/tables/models/table-column-config.model.ts | 2 +- .../table-column-name/table-column-name.component.ts | 7 +++++-- src/lib/utils/decorators/required.decorator.ts | 6 ++++-- src/lib/utils/functions.ts | 4 ++-- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/lib/tables/models/table-column-config.model.ts b/src/lib/tables/models/table-column-config.model.ts index 83762f4..4100bb0 100644 --- a/src/lib/tables/models/table-column-config.model.ts +++ b/src/lib/tables/models/table-column-config.model.ts @@ -1,8 +1,8 @@ import { KeysOf } from '../../utils/types/utility-types'; export interface TableColumnConfig { - readonly column?: KeysOf; readonly label: string; + readonly column?: KeysOf; readonly withSort?: boolean; readonly class?: string; readonly leftIcon?: string; diff --git a/src/lib/tables/table-column-name/table-column-name.component.ts b/src/lib/tables/table-column-name/table-column-name.component.ts index 7caabe8..f003baf 100644 --- a/src/lib/tables/table-column-name/table-column-name.component.ts +++ b/src/lib/tables/table-column-name/table-column-name.component.ts @@ -4,6 +4,9 @@ import { Required } from '../../utils/decorators/required.decorator'; import { KeysOf } from '../../utils/types/utility-types'; import { SortingService } from '../../sorting/sorting.service'; +const ifNeedsSort = (thisArg: TableColumnNameComponent) => thisArg.withSort; +const ifHasRightIcon = (thisArg: TableColumnNameComponent) => !!thisArg.rightIcon; + @Component({ selector: 'iqser-table-column-name', templateUrl: './table-column-name.component.html', @@ -14,12 +17,12 @@ export class TableColumnNameComponent { readonly sortingOrders = SortingOrders; @Input() @Required() label!: string; - @Input() column?: KeysOf; + @Input() @Required(ifNeedsSort) column?: KeysOf; @Input() withSort = false; @Input() class?: string; @Input() leftIcon?: string; @Input() rightIcon?: string; - @Input() rightIconTooltip?: string; + @Input() @Required(ifHasRightIcon) rightIconTooltip?: string; constructor(@Optional() readonly sortingService: SortingService) {} } diff --git a/src/lib/utils/decorators/required.decorator.ts b/src/lib/utils/decorators/required.decorator.ts index 3af62a3..4200891 100644 --- a/src/lib/utils/decorators/required.decorator.ts +++ b/src/lib/utils/decorators/required.decorator.ts @@ -1,8 +1,10 @@ -export function Required(message?: string): PropertyDecorator { +export type Condition = (thisArg: T) => boolean; + +export function Required(condition: Condition = () => true): PropertyDecorator { return function (target: Object, propertyKey: PropertyKey) { Object.defineProperty(target, propertyKey, { get() { - throw new Error(message || `Attribute ${String(propertyKey)} is required`); + if (condition(this)) throw new Error(`Attribute ${String(propertyKey)} is required`); }, set(value) { Object.defineProperty(this, propertyKey, { diff --git a/src/lib/utils/functions.ts b/src/lib/utils/functions.ts index d5bcefe..8169318 100644 --- a/src/lib/utils/functions.ts +++ b/src/lib/utils/functions.ts @@ -3,7 +3,7 @@ export function capitalize(value: string): string { } export function humanize(value: string, lowercase = true): string { - const frags = (lowercase ? value.toLowerCase() : value).split(/[ \-_]+/); + const words = (lowercase ? value.toLowerCase() : value).split(/[ \-_]+/); - return frags.map(capitalize).join(' '); + return words.map(capitalize).join(' '); }