Skip to content

Commit

Permalink
- [Fixed] Fix issue #6 (Build a correct component name from a file name)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprey committed Aug 20, 2020
1 parent 6726d21 commit 6d31917
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -277,3 +299,4 @@ module.exports.getDependencies = getDependencies;
module.exports.escapeImportKeyword = escapeImportKeyword;
module.exports.inferTypeFromVariableDeclaration = inferTypeFromVariableDeclaration;
module.exports.isTopLevelComment = isTopLevelComment;
module.exports.buildCamelCase = buildCamelCase;
2 changes: 1 addition & 1 deletion lib/v3/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/svelte3/integration/basic/basic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/helpers/utils.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});

0 comments on commit 6d31917

Please sign in to comment.