Skip to content

Commit 3361366

Browse files
armano2michalsnik
authored andcommitted
Allow to use numbers in component names (#106)
* Allow to use numbers in component names * Move casing to separate file and add unit tests
1 parent 17aedbf commit 3361366

File tree

3 files changed

+83
-40
lines changed

3 files changed

+83
-40
lines changed

lib/rules/name-property-casing.js

+4-40
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,15 @@
55
'use strict'
66

77
const utils = require('../utils')
8-
9-
function kebabCase (str) {
10-
return str
11-
.replace(/([a-z])([A-Z])/g, match => match[0] + '-' + match[1])
12-
.replace(/[^a-zA-Z:]+/g, '-')
13-
.toLowerCase()
14-
}
15-
16-
function camelCase (str) {
17-
return str
18-
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => (
19-
index === 0 ? letter.toLowerCase() : letter.toUpperCase())
20-
)
21-
.replace(/[^a-zA-Z:]+/g, '')
22-
}
23-
24-
function pascalCase (str) {
25-
return str
26-
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => letter.toUpperCase())
27-
.replace(/[^a-zA-Z:]+/g, '')
28-
}
29-
30-
const allowedCaseOptions = [
31-
'camelCase',
32-
'kebab-case',
33-
'PascalCase'
34-
]
35-
36-
const convertersMap = {
37-
'kebab-case': kebabCase,
38-
'camelCase': camelCase,
39-
'PascalCase': pascalCase
40-
}
41-
42-
function getConverter (name) {
43-
return convertersMap[name] || pascalCase
44-
}
8+
const casing = require('../utils/casing')
459

4610
// ------------------------------------------------------------------------------
4711
// Rule Definition
4812
// ------------------------------------------------------------------------------
4913

5014
function create (context) {
5115
const options = context.options[0]
52-
const caseType = allowedCaseOptions.indexOf(options) !== -1 ? options : 'PascalCase'
16+
const caseType = casing.allowedCaseOptions.indexOf(options) !== -1 ? options : 'PascalCase'
5317

5418
// ----------------------------------------------------------------------
5519
// Public
@@ -65,7 +29,7 @@ function create (context) {
6529

6630
if (!node) return
6731

68-
const value = getConverter(caseType)(node.value.value)
32+
const value = casing.getConverter(caseType)(node.value.value)
6933
if (value !== node.value.value) {
7034
context.report({
7135
node: node.value,
@@ -90,7 +54,7 @@ module.exports = {
9054
fixable: 'code', // or "code" or "whitespace"
9155
schema: [
9256
{
93-
enum: allowedCaseOptions
57+
enum: casing.allowedCaseOptions
9458
}
9559
]
9660
},

lib/utils/casing.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const assert = require('assert')
2+
3+
const invalidChars = /[^a-zA-Z0-9:]+/g
4+
5+
function kebabCase (str) {
6+
return str
7+
.replace(/([a-z])([A-Z])/g, match => match[0] + '-' + match[1])
8+
.replace(invalidChars, '-')
9+
.toLowerCase()
10+
}
11+
12+
function camelCase (str) {
13+
return str
14+
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => (
15+
index === 0 ? letter.toLowerCase() : letter.toUpperCase())
16+
)
17+
.replace(invalidChars, '')
18+
}
19+
20+
function pascalCase (str) {
21+
return str
22+
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => letter.toUpperCase())
23+
.replace(invalidChars, '')
24+
}
25+
26+
const convertersMap = {
27+
'kebab-case': kebabCase,
28+
'camelCase': camelCase,
29+
'PascalCase': pascalCase
30+
}
31+
32+
module.exports = {
33+
allowedCaseOptions: [
34+
'camelCase',
35+
'kebab-case',
36+
'PascalCase'
37+
],
38+
39+
getConverter (name) {
40+
assert(typeof name === 'string')
41+
42+
return convertersMap[name] || pascalCase
43+
}
44+
}

tests/lib/utils/casing.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const casing = require('../../../lib/utils/casing')
4+
const chai = require('chai')
5+
6+
const assert = chai.assert
7+
8+
describe('getConverter()', () => {
9+
it('should conver string to camelCase', () => {
10+
const converter = casing.getConverter('camelCase')
11+
12+
assert.ok(converter('fooBar') === 'fooBar')
13+
assert.ok(converter('foo-bar') === 'fooBar')
14+
assert.ok(converter('FooBar') === 'fooBar')
15+
assert.ok(converter('Foo1Bar') === 'foo1Bar')
16+
})
17+
18+
it('should conver string to PascalCase', () => {
19+
const converter = casing.getConverter('PascalCase')
20+
21+
assert.ok(converter('fooBar') === 'FooBar')
22+
assert.ok(converter('foo-bar') === 'FooBar')
23+
assert.ok(converter('FooBar') === 'FooBar')
24+
assert.ok(converter('Foo1Bar') === 'Foo1Bar')
25+
})
26+
27+
it('should conver string to kebab-case', () => {
28+
const converter = casing.getConverter('kebab-case')
29+
30+
assert.ok(converter('fooBar') === 'foo-bar')
31+
assert.ok(converter('foo-bar') === 'foo-bar')
32+
assert.ok(converter('FooBar') === 'foo-bar')
33+
assert.ok(converter('Foo1Bar') === 'foo1bar')
34+
})
35+
})

0 commit comments

Comments
 (0)