Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 4 additions & 40 deletions lib/rules/name-property-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,15 @@
'use strict'

const utils = require('../utils')

function kebabCase (str) {
return str
.replace(/([a-z])([A-Z])/g, match => match[0] + '-' + match[1])
.replace(/[^a-zA-Z:]+/g, '-')
.toLowerCase()
}

function camelCase (str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => (
index === 0 ? letter.toLowerCase() : letter.toUpperCase())
)
.replace(/[^a-zA-Z:]+/g, '')
}

function pascalCase (str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => letter.toUpperCase())
.replace(/[^a-zA-Z:]+/g, '')
}

const allowedCaseOptions = [
'camelCase',
'kebab-case',
'PascalCase'
]

const convertersMap = {
'kebab-case': kebabCase,
'camelCase': camelCase,
'PascalCase': pascalCase
}

function getConverter (name) {
return convertersMap[name] || pascalCase
}
const casing = require('../utils/casing')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

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

// ----------------------------------------------------------------------
// Public
Expand All @@ -65,7 +29,7 @@ function create (context) {

if (!node) return

const value = getConverter(caseType)(node.value.value)
const value = casing.getConverter(caseType)(node.value.value)
if (value !== node.value.value) {
context.report({
node: node.value,
Expand All @@ -90,7 +54,7 @@ module.exports = {
fixable: 'code', // or "code" or "whitespace"
schema: [
{
enum: allowedCaseOptions
enum: casing.allowedCaseOptions
}
]
},
Expand Down
44 changes: 44 additions & 0 deletions lib/utils/casing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const assert = require('assert')

const invalidChars = /[^a-zA-Z0-9:]+/g

function kebabCase (str) {
return str
.replace(/([a-z])([A-Z])/g, match => match[0] + '-' + match[1])
.replace(invalidChars, '-')
.toLowerCase()
}

function camelCase (str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => (
index === 0 ? letter.toLowerCase() : letter.toUpperCase())
)
.replace(invalidChars, '')
}

function pascalCase (str) {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, (letter, index) => letter.toUpperCase())
.replace(invalidChars, '')
}

const convertersMap = {
'kebab-case': kebabCase,
'camelCase': camelCase,
'PascalCase': pascalCase
}

module.exports = {
allowedCaseOptions: [
'camelCase',
'kebab-case',
'PascalCase'
],

getConverter (name) {
assert(typeof name === 'string')

return convertersMap[name] || pascalCase
}
}
35 changes: 35 additions & 0 deletions tests/lib/utils/casing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict'

const casing = require('../../../lib/utils/casing')
const chai = require('chai')

const assert = chai.assert

describe('getConverter()', () => {
it('should conver string to camelCase', () => {
const converter = casing.getConverter('camelCase')

assert.ok(converter('fooBar') === 'fooBar')
assert.ok(converter('foo-bar') === 'fooBar')
assert.ok(converter('FooBar') === 'fooBar')
assert.ok(converter('Foo1Bar') === 'foo1Bar')
})

it('should conver string to PascalCase', () => {
const converter = casing.getConverter('PascalCase')

assert.ok(converter('fooBar') === 'FooBar')
assert.ok(converter('foo-bar') === 'FooBar')
assert.ok(converter('FooBar') === 'FooBar')
assert.ok(converter('Foo1Bar') === 'Foo1Bar')
})

it('should conver string to kebab-case', () => {
const converter = casing.getConverter('kebab-case')

assert.ok(converter('fooBar') === 'foo-bar')
assert.ok(converter('foo-bar') === 'foo-bar')
assert.ok(converter('FooBar') === 'foo-bar')
assert.ok(converter('Foo1Bar') === 'foo1bar')
})
})