Skip to content

Display string enum pipe #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions projects/common/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export * from './utilities/formatters/numeric/display-number.pipe';
export * from './utilities/formatters/numeric/numeric-formatter';
export * from './utilities/formatters/string/string-formatter';
export * from './utilities/formatters/string/highlight.pipe';
export * from './utilities/formatters/enum/display-string-enum.pipe';
export * from './utilities/formatters/enum/display-string-enum';

// Http Param Encoder
export { HttpParamEncoder } from './utilities/http/http-param-encoder';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Pipe, PipeTransform } from '@angular/core';
import { displayStringEnum } from './display-string-enum';

@Pipe({
name: 'htDisplayStringEnum'
})
export class DisplayStringEnumPipe implements PipeTransform {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use displayString?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Not sure that will work, or at least it shouldn't. The displayString method shouldn't replace - or _. It should just basically String() any input but not format it... with some extra logic for null checks, arrays, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to overload display string with this behavior (for example "my-value" would become "My Value", which we don't want the main string pipe doing)

public transform(value: string): string {
return displayStringEnum(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { displayStringEnum } from './display-string-enum';

describe('Display string enum', () => {
const enum TestEnum {
Flat = 'flatcase',
UpperFlat = 'UPPERFLATCASE',
Camel = 'camelCase',
Pascal = 'PascalCase',
Snake = 'snake_case',
PascalSnake = 'Pascal_Snake_Case',
Kabob = 'kabob-case',
Train = 'Train-Case',
Macro = 'MACRO_CASE',
MacroTrain = 'MACRO-TRAIN-CASE'
}

test('can convert to display string', () => {
expect(displayStringEnum(undefined)).toBe('-');
expect(displayStringEnum('a')).toBe('A');
expect(displayStringEnum(TestEnum.Flat)).toBe('Flatcase');
expect(displayStringEnum(TestEnum.UpperFlat)).toBe('Upperflatcase');
expect(displayStringEnum(TestEnum.Camel)).toBe('Camel case');
expect(displayStringEnum(TestEnum.Pascal)).toBe('Pascal case');
expect(displayStringEnum(TestEnum.Snake)).toBe('Snake case');
expect(displayStringEnum(TestEnum.PascalSnake)).toBe('Pascal snake case');
expect(displayStringEnum(TestEnum.Kabob)).toBe('Kabob case');
expect(displayStringEnum(TestEnum.Train)).toBe('Train case');
expect(displayStringEnum(TestEnum.Macro)).toBe('Macro case');
expect(displayStringEnum(TestEnum.MacroTrain)).toBe('Macro train case');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isNil, startCase } from 'lodash-es';

export const displayStringEnum = (provided?: string): string => {
if (isNil(provided)) {
return '-';
}

// This removes dashes and underscores and gives all words initial caps
const startCased = startCase(provided);

// We only want the first word capitalized.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's what we want, what about capitalize(lowerCase(provided)) - believe that works the same way as startCase:

_.lowerCase('--Foo-Bar--');
// => 'foo bar'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does look cleaner, but I think this one fails for camelCase and PascalCase.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lodash should have ng pipes for each method. 'text' | lowercase | capitalize

return `${startCased[0]}${startCased.substr(1).toLowerCase()}`;
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NgModule } from '@angular/core';
import { DisplayDatePipe } from './date/display-date.pipe';
import { DisplayDurationPipe } from './duration/display-duration.pipe';
import { DisplayStringEnumPipe } from './enum/display-string-enum.pipe';
import { DisplayNumberPipe } from './numeric/display-number.pipe';
import { OrdinalPipe } from './ordinal/ordinal.pipe';
import { DisplayStringPipe } from './string/display-string.pipe';
Expand All @@ -17,7 +18,8 @@ import { DisplayTimeAgo } from './time/display-time-ago.pipe';
DisplayStringPipe,
HighlightPipe,
DisplayTitlePipe,
OrdinalPipe
OrdinalPipe,
DisplayStringEnumPipe
],
exports: [
DisplayNumberPipe,
Expand All @@ -27,7 +29,8 @@ import { DisplayTimeAgo } from './time/display-time-ago.pipe';
DisplayStringPipe,
HighlightPipe,
DisplayTitlePipe,
OrdinalPipe
OrdinalPipe,
DisplayStringEnumPipe
]
})
export class FormattingModule {}