Skip to content

Commit

Permalink
fix(cdk): added defaultsTo pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamChelman committed Jan 4, 2024
1 parent 6e4aa67 commit a0c695b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions libs/cdk/defaults-to/defaults-to.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Inject, InjectionToken, Pipe, PipeTransform } from '@angular/core';
import { isEmpty } from '@cognizone/model-utils';

export interface DefaultsToPipeOptions {
defaultValue: unknown;
}

export const DEFAULTS_TO_PIPE_OPTIONS = new InjectionToken<DefaultsToPipeOptions>('DEFAULT_TO_PIPE_OPTIONS', {
factory: () => ({ defaultValue: '-' }),
});

@Pipe({
name: 'defaultsTo',
standalone: true,
})
export class DefaultsToPipe implements PipeTransform {
constructor(@Inject(DEFAULTS_TO_PIPE_OPTIONS) private options: DefaultsToPipeOptions) {}

transform<T>(value: T, defaultValue?: T | string): T | string {
return isEmpty(value) ? defaultValue ?? (this.options.defaultValue as T | string) : value;
}
}
1 change: 1 addition & 0 deletions libs/cdk/defaults-to/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './defaults-to.pipe';
5 changes: 5 additions & 0 deletions libs/cdk/defaults-to/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "index.ts"
}
}

0 comments on commit a0c695b

Please sign in to comment.