Skip to content

Commit ba425f9

Browse files
committed
fix(default transform functions): fixed transform result and typing, also fixed export of token
1 parent b04af14 commit ba425f9

2 files changed

Lines changed: 50 additions & 19 deletions

File tree

src/formatterParser.ts

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,67 @@ export class FormatterParser {
88
let transformedValue = value;
99

1010
if (typeof value === 'string' || value instanceof String) {
11-
value = value.toString().toLowerCase().replace(/[^a-zA-Z]./g, function (str) {
12-
return str.toUpperCase();
13-
});
14-
transformedValue = value.charAt(0).toUpperCase() + value.slice(1);
11+
transformedValue = transformedValue
12+
.toLowerCase()
13+
.split(' ')
14+
.map(val => val.charAt(0).toUpperCase() + val.slice(1))
15+
.join(' ');
1516
}
1617

1718
return {
1819
name: 'toCapitalized',
19-
result: transformedValue
20+
result: transformedValue,
21+
previous: value
2022
};
2123

2224
}
2325

2426
static toUpperCase: IFormatterParserFn = (value: any): IFormatterParserResult => {
2527
let transformedValue = value;
2628
if (typeof value === 'string' || value instanceof String) {
27-
28-
transformedValue = value.toString().toLowerCase().replace(/[a-zA-Z]/g, function (str) {
29-
return str.toUpperCase();
30-
});
29+
transformedValue = value.toUpperCase();
3130
}
3231

3332
return {
3433
name: 'toUpperCase',
35-
result: transformedValue
34+
result: transformedValue,
35+
previous: value
36+
};
37+
}
38+
39+
static toLowerCase: IFormatterParserFn = (value: any): IFormatterParserResult => {
40+
let transformedValue = value;
41+
if (typeof transformedValue === 'string' || transformedValue instanceof String) {
42+
transformedValue = transformedValue.toLowerCase();
43+
}
44+
45+
return {
46+
name: 'toLowerCase',
47+
result: transformedValue,
48+
previous: value
49+
};
50+
}
51+
52+
static replaceString(searchValue: RegExp, replaceValue: string): IFormatterParserFn {
53+
54+
return (value: any) => {
55+
56+
let transformedValue = value;
57+
58+
if (typeof transformedValue === 'string' || transformedValue instanceof String) {
59+
transformedValue = transformedValue.replace(searchValue, replaceValue);
60+
}
61+
62+
const result: IFormatterParserResult = {
63+
name: 'replaceString',
64+
result: transformedValue,
65+
previous: value
66+
};
67+
68+
return result;
69+
3670
};
71+
3772
}
3873

3974
}

src/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,24 @@ import { FormatterParser } from './formatterParser';
88

99
export * from './formatter-parser.directive';
1010
export * from './formatter-parser.service';
11-
export * from './formatterParser'
12-
13-
const EXPORTS = [
14-
FormatterParserDirective,
15-
];
16-
11+
export * from './formatterParser';
12+
export * from './formatter-parser.injectionToken';
1713

1814
@NgModule({
1915
imports: [
2016
CommonModule,
2117
ReactiveFormsModule
2218
],
23-
declarations: [EXPORTS],
24-
exports: [EXPORTS]
19+
declarations: [FormatterParserDirective],
20+
exports: [FormatterParserDirective, ReactiveFormsModule]
2521
})
2622
export class FormatterParserModule {
2723

2824
static forRoot(): ModuleWithProviders {
2925
return {
3026
ngModule: FormatterParserModule,
3127
providers: [
32-
FormatterParserService,
28+
FormatterParserService,
3329
{provide: FormatterParser, useClass: FormatterParser}
3430
]
3531
};

0 commit comments

Comments
 (0)