File tree Expand file tree Collapse file tree 5 files changed +62
-2
lines changed Expand file tree Collapse file tree 5 files changed +62
-2
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,8 @@ export * from './utilities/formatters/numeric/display-number.pipe';
5151export * from './utilities/formatters/numeric/numeric-formatter' ;
5252export * from './utilities/formatters/string/string-formatter' ;
5353export * from './utilities/formatters/string/highlight.pipe' ;
54+ export * from './utilities/formatters/enum/display-string-enum.pipe' ;
55+ export * from './utilities/formatters/enum/display-string-enum' ;
5456
5557// Http Param Encoder
5658export { HttpParamEncoder } from './utilities/http/http-param-encoder' ;
Original file line number Diff line number Diff line change 1+ import { Pipe , PipeTransform } from '@angular/core' ;
2+ import { displayStringEnum } from './display-string-enum' ;
3+
4+ @Pipe ( {
5+ name : 'htDisplayStringEnum'
6+ } )
7+ export class DisplayStringEnumPipe implements PipeTransform {
8+ public transform ( value : string ) : string {
9+ return displayStringEnum ( value ) ;
10+ }
11+ }
Original file line number Diff line number Diff line change 1+ import { displayStringEnum } from './display-string-enum' ;
2+
3+ describe ( 'Display string enum' , ( ) => {
4+ const enum TestEnum {
5+ Flat = 'flatcase' ,
6+ UpperFlat = 'UPPERFLATCASE' ,
7+ Camel = 'camelCase' ,
8+ Pascal = 'PascalCase' ,
9+ Snake = 'snake_case' ,
10+ PascalSnake = 'Pascal_Snake_Case' ,
11+ Kabob = 'kabob-case' ,
12+ Train = 'Train-Case' ,
13+ Macro = 'MACRO_CASE' ,
14+ MacroTrain = 'MACRO-TRAIN-CASE'
15+ }
16+
17+ test ( 'can convert to display string' , ( ) => {
18+ expect ( displayStringEnum ( undefined ) ) . toBe ( '-' ) ;
19+ expect ( displayStringEnum ( 'a' ) ) . toBe ( 'A' ) ;
20+ expect ( displayStringEnum ( TestEnum . Flat ) ) . toBe ( 'Flatcase' ) ;
21+ expect ( displayStringEnum ( TestEnum . UpperFlat ) ) . toBe ( 'Upperflatcase' ) ;
22+ expect ( displayStringEnum ( TestEnum . Camel ) ) . toBe ( 'Camel case' ) ;
23+ expect ( displayStringEnum ( TestEnum . Pascal ) ) . toBe ( 'Pascal case' ) ;
24+ expect ( displayStringEnum ( TestEnum . Snake ) ) . toBe ( 'Snake case' ) ;
25+ expect ( displayStringEnum ( TestEnum . PascalSnake ) ) . toBe ( 'Pascal snake case' ) ;
26+ expect ( displayStringEnum ( TestEnum . Kabob ) ) . toBe ( 'Kabob case' ) ;
27+ expect ( displayStringEnum ( TestEnum . Train ) ) . toBe ( 'Train case' ) ;
28+ expect ( displayStringEnum ( TestEnum . Macro ) ) . toBe ( 'Macro case' ) ;
29+ expect ( displayStringEnum ( TestEnum . MacroTrain ) ) . toBe ( 'Macro train case' ) ;
30+ } ) ;
31+ } ) ;
Original file line number Diff line number Diff line change 1+ import { isNil , startCase } from 'lodash-es' ;
2+
3+ export const displayStringEnum = ( provided ?: string ) : string => {
4+ if ( isNil ( provided ) ) {
5+ return '-' ;
6+ }
7+
8+ // This removes dashes and underscores and gives all words initial caps
9+ const startCased = startCase ( provided ) ;
10+
11+ // We only want the first word capitalized.
12+ return `${ startCased [ 0 ] } ${ startCased . substr ( 1 ) . toLowerCase ( ) } ` ;
13+ } ;
Original file line number Diff line number Diff line change 11import { NgModule } from '@angular/core' ;
22import { DisplayDatePipe } from './date/display-date.pipe' ;
33import { DisplayDurationPipe } from './duration/display-duration.pipe' ;
4+ import { DisplayStringEnumPipe } from './enum/display-string-enum.pipe' ;
45import { DisplayNumberPipe } from './numeric/display-number.pipe' ;
56import { OrdinalPipe } from './ordinal/ordinal.pipe' ;
67import { DisplayStringPipe } from './string/display-string.pipe' ;
@@ -17,7 +18,8 @@ import { DisplayTimeAgo } from './time/display-time-ago.pipe';
1718 DisplayStringPipe ,
1819 HighlightPipe ,
1920 DisplayTitlePipe ,
20- OrdinalPipe
21+ OrdinalPipe ,
22+ DisplayStringEnumPipe
2123 ] ,
2224 exports : [
2325 DisplayNumberPipe ,
@@ -27,7 +29,8 @@ import { DisplayTimeAgo } from './time/display-time-ago.pipe';
2729 DisplayStringPipe ,
2830 HighlightPipe ,
2931 DisplayTitlePipe ,
30- OrdinalPipe
32+ OrdinalPipe ,
33+ DisplayStringEnumPipe
3134 ]
3235} )
3336export class FormattingModule { }
You can’t perform that action at this time.
0 commit comments