1
- import getForcedCase from './get-forced-case ' ;
1
+ import { camelCase , kebabCase , snakeCase , upperFirst , startCase } from 'lodash ' ;
2
2
3
3
/**
4
4
* Get forced case for rule
@@ -7,18 +7,58 @@ import getForcedCase from './get-forced-case';
7
7
*/
8
8
export default function getForcedCaseFn ( rule ) {
9
9
const noop = input => input ;
10
- const lowerCase = input => String . prototype . toLowerCase . call ( input ) ;
11
- const upperCase = input => String . prototype . toUpperCase . call ( input ) ;
12
10
13
11
if ( ! rule ) {
14
12
return noop ;
15
13
}
16
14
17
- const forcedCase = getForcedCase ( rule ) ;
15
+ const [ config ] = rule ;
18
16
19
- if ( forcedCase === null ) {
17
+ if ( ! Array . isArray ( config ) ) {
20
18
return noop ;
21
19
}
22
20
23
- return forcedCase === 'lowerCase' ? lowerCase : upperCase ;
21
+ const [ level ] = config ;
22
+
23
+ if ( level === 0 ) {
24
+ return ;
25
+ }
26
+
27
+ const [ , when ] = config ;
28
+
29
+ if ( when === 'neve' ) {
30
+ return ;
31
+ }
32
+
33
+ const [ , , target ] = config ;
34
+
35
+ if ( Array . isArray ( target ) ) {
36
+ return noop ;
37
+ }
38
+
39
+ switch ( target ) {
40
+ case 'camel-case' :
41
+ return input => camelCase ( input ) ;
42
+ case 'kebab-case' :
43
+ return input => kebabCase ( input ) ;
44
+ case 'snake-case' :
45
+ return input => snakeCase ( input ) ;
46
+ case 'pascal-case' :
47
+ return input => upperFirst ( camelCase ( input ) ) ;
48
+ case 'start-case' :
49
+ return input => startCase ( input ) ;
50
+ case 'upper-case' :
51
+ case 'uppercase' :
52
+ return input => input . toUpperCase ( ) ;
53
+ case 'sentence-case' :
54
+ case 'sentencecase' :
55
+ return input =>
56
+ `${ input . charAt ( 0 ) . toUpperCase ( ) } ${ input . substring ( 1 ) . toLowerCase ( ) } ` ;
57
+ case 'lower-case' :
58
+ case 'lowercase' :
59
+ case 'lowerCase' : // Backwards compat config-angular v4
60
+ return input => input . toLowerCase ( ) === input ;
61
+ default :
62
+ throw new TypeError ( `Unknown target case "${ rule [ 2 ] } "` ) ;
63
+ }
24
64
}
0 commit comments