Skip to content

Commit 023121c

Browse files
armano2michalsnik
authored andcommitted
Fix logic behind kabab case and snake case for propID (#641)
fix issue #550
1 parent d1cd06e commit 023121c

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

lib/utils/casing.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ const invalidChars = /[^a-zA-Z0-9:]+/g
99
*/
1010
function kebabCase (str) {
1111
return str
12-
.replace(/([a-z])([A-Z])/g, match => match[0] + '-' + match[1])
12+
.replace(/[A-Z]/g, match => '-' + match)
13+
.replace(/([^a-zA-Z])-([A-Z])/g, match => match[0] + match[2])
14+
.replace(/^-/, '')
1315
.replace(invalidChars, '-')
1416
.toLowerCase()
1517
}
@@ -21,7 +23,9 @@ function kebabCase (str) {
2123
*/
2224
function snakeCase (str) {
2325
return str
24-
.replace(/([a-z])([A-Z])/g, match => match[0] + '_' + match[1])
26+
.replace(/[A-Z]/g, match => '_' + match)
27+
.replace(/([^a-zA-Z])_([A-Z])/g, match => match[0] + match[2])
28+
.replace(/^_/, '')
2529
.replace(invalidChars, '_')
2630
.toLowerCase()
2731
}

tests/lib/rules/attribute-hyphenation.js

+11
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,17 @@ ruleTester.run('attribute-hyphenation', rule, {
150150
line: 1
151151
}]
152152
},
153+
{
154+
filename: 'test.vue',
155+
code: '<template><div><custom v-bind:propID="prop" :secondPropID="test"></custom></div></template>',
156+
output: '<template><div><custom v-bind:prop-i-d="prop" :secondPropID="test"></custom></div></template>',
157+
options: ['always', { ignore: ['secondPropID'] }],
158+
errors: [{
159+
message: "Attribute 'v-bind:propID' must be hyphenated.",
160+
type: 'VDirectiveKey',
161+
line: 1
162+
}]
163+
},
153164
{
154165
filename: 'test.vue',
155166
code: `

tests/lib/utils/casing.js

+35-20
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,54 @@ const chai = require('chai')
66
const assert = chai.assert
77

88
describe('getConverter()', () => {
9-
it('should conver string to camelCase', () => {
9+
it('should convert string to camelCase', () => {
1010
const converter = casing.getConverter('camelCase')
1111

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')
12+
assert.equal(converter('fooBar'), 'fooBar')
13+
assert.equal(converter('foo-bar'), 'fooBar')
14+
assert.equal(converter('foo_bar'), 'fooBar')
15+
assert.equal(converter('FooBar'), 'fooBar')
16+
assert.equal(converter('Foo1Bar'), 'foo1Bar')
17+
assert.equal(converter('FooBAR'), 'fooBAR')
18+
assert.equal(converter('Foo1BAZ'), 'foo1BAZ')
19+
assert.equal(converter('foo1b_a_z'), 'foo1bAZ')
1620
})
1721

18-
it('should conver string to PascalCase', () => {
22+
it('should convert string to PascalCase', () => {
1923
const converter = casing.getConverter('PascalCase')
2024

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+
assert.equal(converter('fooBar'), 'FooBar')
26+
assert.equal(converter('foo-bar'), 'FooBar')
27+
assert.equal(converter('foo_bar'), 'FooBar')
28+
assert.equal(converter('FooBar'), 'FooBar')
29+
assert.equal(converter('Foo1Bar'), 'Foo1Bar')
30+
assert.equal(converter('FooBAR'), 'FooBAR')
31+
assert.equal(converter('Foo1BAZ'), 'Foo1BAZ')
32+
assert.equal(converter('foo1b_a_z'), 'Foo1bAZ')
2533
})
2634

27-
it('should conver string to kebab-case', () => {
35+
it('should convert string to kebab-case', () => {
2836
const converter = casing.getConverter('kebab-case')
2937

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')
38+
assert.equal(converter('fooBar'), 'foo-bar')
39+
assert.equal(converter('foo-bar'), 'foo-bar')
40+
assert.equal(converter('foo_bar'), 'foo-bar')
41+
assert.equal(converter('FooBar'), 'foo-bar')
42+
assert.equal(converter('Foo1Bar'), 'foo1bar')
43+
assert.equal(converter('FooBAR'), 'foo-b-a-r')
44+
assert.equal(converter('Foo1BAZ'), 'foo1b-a-z')
45+
assert.equal(converter('foo1b_a_z'), 'foo1b-a-z')
3446
})
3547

36-
it('should conver string to snake_case', () => {
48+
it('should convert string to snake_case', () => {
3749
const converter = casing.getConverter('snake_case')
3850

39-
assert.ok(converter('fooBar') === 'foo_bar')
40-
assert.ok(converter('foo-bar') === 'foo_bar')
41-
assert.ok(converter('FooBar') === 'foo_bar')
42-
assert.ok(converter('Foo1Bar') === 'foo1bar')
51+
assert.equal(converter('fooBar'), 'foo_bar')
52+
assert.equal(converter('foo-bar'), 'foo_bar')
53+
assert.equal(converter('FooBar'), 'foo_bar')
54+
assert.equal(converter('Foo1Bar'), 'foo1bar')
55+
assert.equal(converter('FooBAR'), 'foo_b_a_r')
56+
assert.equal(converter('Foo1BAZ'), 'foo1b_a_z')
57+
assert.equal(converter('foo1b_a_z'), 'foo1b_a_z')
4358
})
4459
})

0 commit comments

Comments
 (0)