Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix of the crash when a string contains '/' #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ TranslationManager.prototype.getSuggestedKey = async function (pathToFile, text,

var words = text.trim().split(' ')
if (words.length > 4) words = words.slice(0, 3)

let word = camelCase(words.join(' ').replace(/[^a-zA-Z ]/g, ''))
let word = camelCase(words.join(' ').replace(/[^a-zA-Z ]/g, '').replace(/\s\s+/g, ' '))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a chained .replace to remove whitespaces that caused the crash

if (!word) word = Math.floor(Math.random() * 10000)
let proposedKey = await this.getCompatibleKey(`${prefix}.${word}`, usedKeys)

Expand Down
7 changes: 7 additions & 0 deletions test/test-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ test('getSuggestedKey', async function (t) {
var key2 = await m.getSuggestedKey(pathToFile2, 'Little text here with a lot of words that hopefully won\'t appear in the key')
t.equal(key2, 'header.titlebar.littleTextHere', 'key is correct')

var pathToFile3 = path.join(__dirname, 'data/test-1/src/components/header/Titlebar.vue')
var key3 = await m.getSuggestedKey(pathToFile3, 'One / three')
t.equal(key3, 'header.titlebar.oneThree', 'key is correct')

cleanupTmp()
t.end()
})
Expand All @@ -92,6 +96,9 @@ test('getSuggestedKey with list of used keys', async function (t) {
var keyThree = await m.getSuggestedKey(pathToFile, 'Create Group and a lot of text that stays the same Three', ['test.createGroupAnd'])
t.equal(keyThree, 'test.createGroupAnd1', 'key is correct')

var keyFour = await m.getSuggestedKey(pathToFile, 'One / three and a lot of text that stays the same', ['test.oneThree'])
t.equal(keyFour, 'test.oneThree1', 'key is correct')

cleanupTmp()
t.end()
})
Expand Down