From 87b19da9a900cbc9de8af666612bbae7aedff900 Mon Sep 17 00:00:00 2001 From: Dan Percic Date: Thu, 16 Mar 2023 23:47:30 +0200 Subject: [PATCH] update pipes --- src/index.ts | 2 +- src/lib/common-ui.module.ts | 7 ++---- .../{standalone => }/pipes/capitalize.pipe.ts | 2 +- .../pipes/humanize-camel-case.pipe.ts | 2 +- .../{standalone => }/pipes/humanize.pipe.ts | 2 +- src/lib/pipes/index.ts | 4 ++++ src/lib/{standalone => }/pipes/log.pipe.ts | 0 src/lib/sorting/functions.ts | 21 ++++++++++++++++ src/lib/sorting/index.ts | 1 + src/lib/sorting/sort-by.pipe.ts | 6 ++--- src/lib/sorting/sorting.service.ts | 24 +++---------------- src/lib/standalone/index.ts | 4 ---- 12 files changed, 38 insertions(+), 37 deletions(-) rename src/lib/{standalone => }/pipes/capitalize.pipe.ts (85%) rename src/lib/{standalone => }/pipes/humanize-camel-case.pipe.ts (84%) rename src/lib/{standalone => }/pipes/humanize.pipe.ts (87%) create mode 100644 src/lib/pipes/index.ts rename src/lib/{standalone => }/pipes/log.pipe.ts (100%) create mode 100644 src/lib/sorting/functions.ts delete mode 100644 src/lib/standalone/index.ts diff --git a/src/index.ts b/src/index.ts index c19f671..5313f98 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,5 +19,5 @@ export * from './lib/scrollbar'; export * from './lib/caching'; export * from './lib/users'; export * from './lib/translations'; -export * from './lib/standalone'; +export * from './lib/pipes'; export * from './lib/permissions'; diff --git a/src/lib/common-ui.module.ts b/src/lib/common-ui.module.ts index 1493707..b810439 100644 --- a/src/lib/common-ui.module.ts +++ b/src/lib/common-ui.module.ts @@ -3,7 +3,6 @@ import { CommonModule } from '@angular/common'; import { MatIconModule, MatIconRegistry } from '@angular/material/icon'; import { TranslateModule } from '@ngx-translate/core'; import { MatLegacyProgressSpinnerModule as MatProgressSpinnerModule } from '@angular/material/legacy-progress-spinner'; -import { SortByPipe } from './sorting'; import { CommonUiOptions, IqserAppConfig, ModuleOptions } from './utils'; import { HiddenActionComponent, ToastComponent } from './shared'; import { ConnectionStatusComponent, FullPageErrorComponent } from './error'; @@ -37,10 +36,8 @@ const matModules = [ const modules = [IqserListingModule, IqserFiltersModule, IqserInputsModule, IqserScrollbarModule, IqserSkeletonModule, HttpClientModule]; const components = [ConnectionStatusComponent, FullPageErrorComponent, HiddenActionComponent, ConfirmationDialogComponent, ToastComponent]; -const pipes = [SortByPipe]; - @NgModule({ - declarations: [...components, ...pipes], + declarations: [...components], imports: [ CommonModule, ...matModules, @@ -51,7 +48,7 @@ const pipes = [SortByPipe]; IconButtonComponent, CircleButtonComponent, ], - exports: [...components, ...pipes, ...modules], + exports: [...components, ...modules], providers: [ { provide: HTTP_INTERCEPTORS, diff --git a/src/lib/standalone/pipes/capitalize.pipe.ts b/src/lib/pipes/capitalize.pipe.ts similarity index 85% rename from src/lib/standalone/pipes/capitalize.pipe.ts rename to src/lib/pipes/capitalize.pipe.ts index 9d6999d..6ea45d7 100644 --- a/src/lib/standalone/pipes/capitalize.pipe.ts +++ b/src/lib/pipes/capitalize.pipe.ts @@ -1,5 +1,5 @@ import { Pipe, PipeTransform } from '@angular/core'; -import { capitalize } from '../../utils'; +import { capitalize } from '../utils'; @Pipe({ name: 'capitalize', diff --git a/src/lib/standalone/pipes/humanize-camel-case.pipe.ts b/src/lib/pipes/humanize-camel-case.pipe.ts similarity index 84% rename from src/lib/standalone/pipes/humanize-camel-case.pipe.ts rename to src/lib/pipes/humanize-camel-case.pipe.ts index 0d8ed0b..11136f8 100644 --- a/src/lib/standalone/pipes/humanize-camel-case.pipe.ts +++ b/src/lib/pipes/humanize-camel-case.pipe.ts @@ -1,5 +1,5 @@ import { Pipe, PipeTransform } from '@angular/core'; -import { humanizeCamelCase } from '../../utils'; +import { humanizeCamelCase } from '../utils'; @Pipe({ name: 'humanizeCamelCase', diff --git a/src/lib/standalone/pipes/humanize.pipe.ts b/src/lib/pipes/humanize.pipe.ts similarity index 87% rename from src/lib/standalone/pipes/humanize.pipe.ts rename to src/lib/pipes/humanize.pipe.ts index cb1c6f4..2535904 100644 --- a/src/lib/standalone/pipes/humanize.pipe.ts +++ b/src/lib/pipes/humanize.pipe.ts @@ -1,5 +1,5 @@ import { Pipe, PipeTransform } from '@angular/core'; -import { humanize } from '../../utils'; +import { humanize } from '../utils'; @Pipe({ name: 'humanize', diff --git a/src/lib/pipes/index.ts b/src/lib/pipes/index.ts new file mode 100644 index 0000000..d81973d --- /dev/null +++ b/src/lib/pipes/index.ts @@ -0,0 +1,4 @@ +export * from './log.pipe'; +export * from './humanize-camel-case.pipe'; +export * from './capitalize.pipe'; +export * from './humanize.pipe'; diff --git a/src/lib/standalone/pipes/log.pipe.ts b/src/lib/pipes/log.pipe.ts similarity index 100% rename from src/lib/standalone/pipes/log.pipe.ts rename to src/lib/pipes/log.pipe.ts diff --git a/src/lib/sorting/functions.ts b/src/lib/sorting/functions.ts new file mode 100644 index 0000000..7edcd44 --- /dev/null +++ b/src/lib/sorting/functions.ts @@ -0,0 +1,21 @@ +import { SortingOrder, SortingOrders } from './models/sorting-order.type'; +import { KeysOf } from '../utils'; +import { orderBy } from 'lodash-es'; + +export function sort(values: T[], order?: SortingOrder, column?: KeysOf): T[] { + if (!values || values.length <= 1 || !order) { + return values; + } + + if (!column) { + /** sort 1D array */ + const result = [...values].sort(); + return order === SortingOrders.asc ? result : result.reverse(); + } + + return orderBy( + values, + [(entity: T) => (typeof entity[column] === 'string' ? (entity[column] as unknown as string).toLowerCase() : entity[column])], + [order], + ); +} diff --git a/src/lib/sorting/index.ts b/src/lib/sorting/index.ts index ce59a22..85a9107 100644 --- a/src/lib/sorting/index.ts +++ b/src/lib/sorting/index.ts @@ -1,3 +1,4 @@ +export * from './functions'; export * from './sort-by.pipe'; export * from './sorting.service'; export * from './models/sorting-option.model'; diff --git a/src/lib/sorting/sort-by.pipe.ts b/src/lib/sorting/sort-by.pipe.ts index 522a916..ff4eb03 100644 --- a/src/lib/sorting/sort-by.pipe.ts +++ b/src/lib/sorting/sort-by.pipe.ts @@ -1,11 +1,11 @@ import { Pipe, PipeTransform } from '@angular/core'; -import { SortingService } from './sorting.service'; import { SortingOrder } from './models/sorting-order.type'; import { KeysOf } from '../utils'; +import { sort } from './functions'; -@Pipe({ name: 'sortBy' }) +@Pipe({ name: 'sortBy', standalone: true }) export class SortByPipe implements PipeTransform { transform(values: T[], order: SortingOrder, column: KeysOf): T[] { - return SortingService.sort(values, order, column); + return sort(values, order, column); } } diff --git a/src/lib/sorting/sorting.service.ts b/src/lib/sorting/sorting.service.ts index 9cee406..0970316 100644 --- a/src/lib/sorting/sorting.service.ts +++ b/src/lib/sorting/sorting.service.ts @@ -1,10 +1,10 @@ import { Injectable } from '@angular/core'; import { BehaviorSubject } from 'rxjs'; import { SortingOption } from './models/sorting-option.model'; -import { SortingOrder, SortingOrders } from './models/sorting-order.type'; +import { SortingOrders } from './models/sorting-order.type'; import { KeysOf, shareDistinctLast } from '../utils'; import { Id, IListable } from '../listing'; -import { orderBy } from 'lodash-es'; +import { sort } from './functions'; @Injectable() export class SortingService, PrimaryKey extends Id = T['id']> { @@ -18,30 +18,12 @@ export class SortingService, PrimaryKey extends return this._sortingOption$.getValue(); } - static sort(values: T[], order?: SortingOrder, column?: KeysOf): T[] { - if (!values || values.length <= 1 || !order) { - return values; - } - - if (!column) { - /** sort 1D array */ - const result = [...values].sort(); - return order === SortingOrders.asc ? result : result.reverse(); - } - - return orderBy( - values, - [(entity: T) => (typeof entity[column] === 'string' ? (entity[column] as unknown as string).toLowerCase() : entity[column])], - [order], - ); - } - setSortingOption(value: SortingOption): void { this._sortingOption$.next(value); } defaultSort(values: T[]): T[] { - return SortingService.sort(values, this.sortingOption?.order, this.sortingOption?.column); + return sort(values, this.sortingOption?.order, this.sortingOption?.column); } toggleSort(column: KeysOf): void { diff --git a/src/lib/standalone/index.ts b/src/lib/standalone/index.ts deleted file mode 100644 index 0a4a2be..0000000 --- a/src/lib/standalone/index.ts +++ /dev/null @@ -1,4 +0,0 @@ -export * from './pipes/log.pipe'; -export * from './pipes/humanize-camel-case.pipe'; -export * from './pipes/capitalize.pipe'; -export * from './pipes/humanize.pipe';