Skip to content

Commit 38a4e86

Browse files
committed
feat (pipe): Add new pipe - 'SnakeCasePipe'
1 parent 08889bb commit 38a4e86

File tree

6 files changed

+76
-21
lines changed

6 files changed

+76
-21
lines changed

src/string/index.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {PhonePipe} from './phone.pipe';
88
import {RepeatPipe} from './repeat.pipe';
99
import {RightTrimPipe} from './rtrim.pipe';
1010
import {SlugifyPipe} from './slugify.pipe';
11+
import {SnakeCasePipe} from './snake-case.pipe';
1112
import {SplitPipe} from './split.pipe';
1213
import {StartsWithPipe} from './starts-with.pipe';
1314
import {StringularPipe} from './stringular.pipe';
@@ -29,6 +30,7 @@ export * from './phone.pipe';
2930
export * from './repeat.pipe';
3031
export * from './rtrim.pipe';
3132
export * from './slugify.pipe';
33+
export * from './snake-case.pipe';
3234
export * from './split.pipe';
3335
export * from './starts-with.pipe';
3436
export * from './stringular.pipe';
@@ -44,22 +46,16 @@ export * from './wrap.pipe';
4446

4547
@NgModule({
4648
declarations: [
47-
EndsWithPipe, LatinizePipe, LeftTrimPipe,
48-
MatchPipe, PhonePipe, RepeatPipe,
49-
RightTrimPipe, SlugifyPipe, SplitPipe,
50-
StartsWithPipe, StringularPipe, StripTagsPipe,
51-
TestPipe, TitleizePipe, TrimPipe,
52-
TruncatePipe, UcfirstPipe, UriComponentEncodePipe,
53-
UriEncodePipe, WrapPipe
49+
EndsWithPipe, LatinizePipe, LeftTrimPipe, MatchPipe, PhonePipe, RepeatPipe,
50+
RightTrimPipe, SlugifyPipe, SnakeCasePipe, SplitPipe, StartsWithPipe, StringularPipe,
51+
StripTagsPipe, TestPipe, TitleizePipe, TrimPipe, TruncatePipe, UcfirstPipe,
52+
UriComponentEncodePipe, UriEncodePipe, WrapPipe
5453
],
5554
exports: [
56-
EndsWithPipe, LatinizePipe, LeftTrimPipe,
57-
MatchPipe, PhonePipe, RepeatPipe,
58-
RightTrimPipe, SlugifyPipe, SplitPipe,
59-
StartsWithPipe, StringularPipe, StripTagsPipe,
60-
TestPipe, TitleizePipe, TrimPipe,
61-
TruncatePipe, UcfirstPipe, UriComponentEncodePipe,
62-
UriEncodePipe, WrapPipe
55+
EndsWithPipe, LatinizePipe, LeftTrimPipe, MatchPipe, PhonePipe, RepeatPipe,
56+
RightTrimPipe, SlugifyPipe, SnakeCasePipe, SplitPipe, StartsWithPipe, StringularPipe,
57+
StripTagsPipe, TestPipe, TitleizePipe, TrimPipe, TruncatePipe, UcfirstPipe,
58+
UriComponentEncodePipe, UriEncodePipe, WrapPipe
6359
]
6460
})
6561
export class StringPipesModule {

src/string/phone.pipe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import {Pipe, PipeTransform} from '@angular/core';
22

3-
import {getPhone, IsNil} from '../utils/utils';
3+
import {getPhone, isNil} from '../utils/utils';
44

55
@Pipe({name: 'phone'})
66
export class PhonePipe implements PipeTransform {
77
transform(input: string, locale: string = 'en-US', showCountryCode: boolean = false): string {
8-
if (IsNil(input) || !/^[a-zA-Z]{2}-[a-zA-Z]{2}$/.test(locale)) return input;
8+
if (isNil(input) || !/^[a-zA-Z]{2}-[a-zA-Z]{2}$/.test(locale)) return input;
99

1010
const strInput: string = input.replace(/[^0-9]/g, '');
1111
const splittedLocale = locale.split('-');
1212
const formattedLocale: string =
1313
`${splittedLocale[0].toLowerCase()}-${splittedLocale[1].toUpperCase()}`;
1414
const phoneObj: any = getPhone(formattedLocale);
1515

16-
if (IsNil(phoneObj) || strInput.length < phoneObj.minLength) return input;
16+
if (isNil(phoneObj) || strInput.length < phoneObj.minLength) return input;
1717

1818
const pattern: RegExp = phoneObj.pattern;
1919
const matches: Array<string> = strInput.match(pattern);

src/string/snake-case.pipe.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Pipe, PipeTransform} from '@angular/core';
2+
3+
import {isString} from '../utils/utils';
4+
5+
@Pipe({name: 'snakeCase'})
6+
export class SnakeCasePipe implements PipeTransform {
7+
transform(input: string): string {
8+
if (!isString(input)) return input;
9+
10+
return input.trim()
11+
.replace(/\s+/g, '')
12+
.replace(
13+
/[A-Z]/g,
14+
(value, index) => index === 0 ? value.toLowerCase() : `_${value.toLowerCase()}`);
15+
}
16+
}

src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const isArray = (value: any): boolean => Array.isArray(value);
22

33
export const isFunction = (value: any): boolean => typeof value === 'function';
44

5-
export const IsNil = (value: any): boolean => value === null || typeof value === 'undefined';
5+
export const isNil = (value: any): boolean => value === null || typeof value === 'undefined';
66

77
export const isNull = (value: any): boolean => value === null;
88

test/string/snake-case.pipe.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {SnakeCasePipe} from '../../src/index';
2+
3+
describe('SnakeCasePipe', () => {
4+
let pipe: SnakeCasePipe;
5+
6+
beforeEach(() => {
7+
pipe = new SnakeCasePipe();
8+
});
9+
10+
const data: Array<any> = [
11+
{'value': 'ASimpleWord', 'toEqual': 'a_simple_word', 'valid': true},
12+
{'value': 'aMediumWordHere', 'toEqual': 'a_medium_word_here', 'valid': true}, {
13+
'value': 'ANUPPERCASEDWORDHERE',
14+
'toEqual': 'a_n_u_p_p_e_r_c_a_s_e_d_w_o_r_d_h_e_r_e',
15+
'valid': true
16+
},
17+
{'value': 'alowercasedword', 'toEqual': 'alowercasedword', 'valid': true},
18+
{'value': ' ', 'toEqual': '', 'valid': true},
19+
{'value': ' SOME WHITE SPACES ', 'toEqual': 's_o_m_e_w_h_i_t_e_s_p_a_c_e_s', 'valid': true},
20+
{'value': null, 'toEqual': 'a_simple_word', 'valid': false},
21+
{'value': undefined, 'toEqual': 'a_medium_word_here', 'valid': false}, {
22+
'value': 'ANUPPERCASEDWORDHERe',
23+
'toEqual': 'a_n_u_p_p_e_r_c_a_s_e_d_w_o_r_d_h_e_r_e',
24+
'valid': false
25+
},
26+
{'value': 'aloweRcasedword', 'toEqual': 'alowercasedword', 'valid': false}
27+
];
28+
29+
for (const element of data) {
30+
const testCase = `${element.value} should be ${element.valid ? 'equal' :
31+
'not equal'
32+
} to ${element.toEqual}`;
33+
34+
it(testCase, () => {
35+
if (element.valid) {
36+
expect(pipe.transform(element.value)).toEqual(element.toEqual);
37+
} else {
38+
expect(pipe.transform(element.value)).not.toEqual(element.toEqual);
39+
}
40+
})
41+
}
42+
43+
});

test/string/test.pipe.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ describe('TestPipe', () => {
88
});
99

1010
it('should test a string with given pattern', () => {
11-
expect(pipe.transform('15/12/2003', '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$', 'i')).toBeTruthy();
11+
expect(pipe.transform('15/12/2003', '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$', 'i'))
12+
.toBeTruthy();
1213
expect(pipe.transform('foobarbaz', '^[a-z]{3,}$')).toBeTruthy();
1314
expect(pipe.transform('FOOBARBAZ', '^[a-z]{3,}$', 'i')).toBeTruthy();
1415
expect(pipe.transform('FOOBARBAZ', '^[a-z]{3,}$')).toBeFalsy();
1516
expect(pipe.transform('foobarbaz', '\\W')).toBeFalsy();
1617
expect(pipe.transform('foobarbaz', '\\w')).toBeTruthy();
17-
expect(pipe.transform('1a/bb/2003', '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$', 'i'))
18-
.toBeFalsy();
18+
expect(pipe.transform('1a/bb/2003', '^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$', 'i')).toBeFalsy();
1919
});
2020

2121
});

0 commit comments

Comments
 (0)