-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If that's what we want, what about
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lodash should have ng pipes for each method. |
||
return `${startCased[0]}${startCased.substr(1).toLowerCase()}`; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use displayString?
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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)