update pipes

This commit is contained in:
Dan Percic 2023-03-16 23:47:30 +02:00
parent e1ebbb9ab2
commit 87b19da9a9
12 changed files with 38 additions and 37 deletions

View File

@ -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';

View File

@ -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,

View File

@ -1,5 +1,5 @@
import { Pipe, PipeTransform } from '@angular/core';
import { capitalize } from '../../utils';
import { capitalize } from '../utils';
@Pipe({
name: 'capitalize',

View File

@ -1,5 +1,5 @@
import { Pipe, PipeTransform } from '@angular/core';
import { humanizeCamelCase } from '../../utils';
import { humanizeCamelCase } from '../utils';
@Pipe({
name: 'humanizeCamelCase',

View File

@ -1,5 +1,5 @@
import { Pipe, PipeTransform } from '@angular/core';
import { humanize } from '../../utils';
import { humanize } from '../utils';
@Pipe({
name: 'humanize',

4
src/lib/pipes/index.ts Normal file
View File

@ -0,0 +1,4 @@
export * from './log.pipe';
export * from './humanize-camel-case.pipe';
export * from './capitalize.pipe';
export * from './humanize.pipe';

View File

@ -0,0 +1,21 @@
import { SortingOrder, SortingOrders } from './models/sorting-order.type';
import { KeysOf } from '../utils';
import { orderBy } from 'lodash-es';
export function sort<T>(values: T[], order?: SortingOrder, column?: KeysOf<T>): 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<T>(
values,
[(entity: T) => (typeof entity[column] === 'string' ? (entity[column] as unknown as string).toLowerCase() : entity[column])],
[order],
);
}

View File

@ -1,3 +1,4 @@
export * from './functions';
export * from './sort-by.pipe';
export * from './sorting.service';
export * from './models/sorting-option.model';

View File

@ -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<T>(values: T[], order: SortingOrder, column: KeysOf<T>): T[] {
return SortingService.sort(values, order, column);
return sort(values, order, column);
}
}

View File

@ -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<T extends IListable<PrimaryKey>, PrimaryKey extends Id = T['id']> {
@ -18,30 +18,12 @@ export class SortingService<T extends IListable<PrimaryKey>, PrimaryKey extends
return this._sortingOption$.getValue();
}
static sort<T>(values: T[], order?: SortingOrder, column?: KeysOf<T>): 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<T>(
values,
[(entity: T) => (typeof entity[column] === 'string' ? (entity[column] as unknown as string).toLowerCase() : entity[column])],
[order],
);
}
setSortingOption(value: SortingOption<T>): 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<T>): void {

View File

@ -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';