From 6d31917da8927bbdfefd78686fba5856e8085cc3 Mon Sep 17 00:00:00 2001 From: Alexey Mulyukin Date: Thu, 20 Aug 2020 12:48:43 +0300 Subject: [PATCH] - [Fixed] Fix issue #6 (Build a correct component name from a file name) --- CHANGELOG.md | 4 +++ lib/utils.js | 23 +++++++++++++ lib/v3/parser.js | 2 +- test/svelte3/integration/basic/basic.spec.js | 2 +- test/unit/helpers/utils.spec.js | 34 ++++++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/unit/helpers/utils.spec.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 323610a..9c91280 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## UNRELEASED +- 🛠 [Fixed] Fix issue #6 (Build a correct component name from a file name) +``` +round.button.svelte -> RoundButton +``` - 🛠 [Fixed] Fix issue #27 (Events is not exposed from exported functions and arrow functions) ## [3.0.1] 17.08.2020 diff --git a/lib/utils.js b/lib/utils.js index 0377a80..ebf2912 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -226,6 +226,28 @@ const unCamelcase = (text) => { return chars.join(''); }; +const buildCamelCase = (text) => { + return text.split('').reduce((state, char) => { + const isLegal = /[a-zA-Z]/.test(char); + const isNumeric = /[0-9]/.test(char); + + if (isLegal || isNumeric) { + if (state.chars.length === 0 || !state.prevIsLegal) { + state.chars.push(char.toUpperCase()); + } else { + state.chars.push(char); + } + } + + state.prevIsLegal = isLegal; + + return state; + }, { + chars: [], + prevIsLegal: false + }).chars.join(''); +}; + const getDependencies = (ast, source) => { const dependencies = []; @@ -277,3 +299,4 @@ module.exports.getDependencies = getDependencies; module.exports.escapeImportKeyword = escapeImportKeyword; module.exports.inferTypeFromVariableDeclaration = inferTypeFromVariableDeclaration; module.exports.isTopLevelComment = isTopLevelComment; +module.exports.buildCamelCase = buildCamelCase; diff --git a/lib/v3/parser.js b/lib/v3/parser.js index 3bb4c95..58652c5 100644 --- a/lib/v3/parser.js +++ b/lib/v3/parser.js @@ -94,7 +94,7 @@ class Parser extends EventEmitter { } if (this.componentName) { - this.emit('name', utils.unCamelcase(this.componentName)); + this.emit('name', utils.buildCamelCase(this.componentName)); } } diff --git a/test/svelte3/integration/basic/basic.spec.js b/test/svelte3/integration/basic/basic.spec.js index ff37e78..841986f 100644 --- a/test/svelte3/integration/basic/basic.spec.js +++ b/test/svelte3/integration/basic/basic.spec.js @@ -13,7 +13,7 @@ describe('SvelteDoc v3 - Basic', () => { ignoredVisibilities: [] }).then((doc) => { expect(doc, 'Document should be provided').to.exist; - expect(doc.name, 'Document should have proper name').to.equal('basic.name'); + expect(doc.name, 'Document should have proper name').to.equal('BasicName'); done(); }).catch(e => { diff --git a/test/unit/helpers/utils.spec.js b/test/unit/helpers/utils.spec.js new file mode 100644 index 0000000..a6fd618 --- /dev/null +++ b/test/unit/helpers/utils.spec.js @@ -0,0 +1,34 @@ +const utils = require('../../../lib/utils'); +const { expect } = require('chai'); + +describe('"utils.js" module', () => { + describe('"buildCamelCase" method', () => { + it('when input is already camel cased then should return same value', done => { + const result = utils.buildCamelCase('CamelCasedTestMethodName12'); + + expect(result).be.equal('CamelCasedTestMethodName12'); + done(); + }); + + it('when spaces used in name then should remove them and make next char uppercased', done => { + const result = utils.buildCamelCase('Spaces In the name'); + + expect(result).be.equal('SpacesInTheName'); + done(); + }); + + it('when first letter is lowercased then should be changed to upper case', done => { + const result = utils.buildCamelCase('lowercasedFirstLetter'); + + expect(result).be.equal('LowercasedFirstLetter'); + done(); + }); + + it('when illegal chars in name then should remove then and make next char uppercased', done => { + const result = utils.buildCamelCase('Illegal-chars-In-the-name'); + + expect(result).to.be.equal('IllegalCharsInTheName'); + done(); + }); + }); +});