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 capitalization #43

Closed
wants to merge 2 commits into from
Closed
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
50 changes: 41 additions & 9 deletions lib/suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,12 @@ function suggest(value) {

push.apply(edits, values)

// Ensure the lower-cased, capitalised, and uppercase values are included.
// Ensure the capitalised and uppercase values are included.
values = [value]
replacement = value.toLowerCase()

if (value === replacement) {
if (value === replacement || currentCase === null) {
values.push(value.charAt(0).toUpperCase() + replacement.slice(1))
} else {
values.push(replacement)
}

replacement = value.toUpperCase()
Expand Down Expand Up @@ -248,6 +246,7 @@ function generate(context, memory, words, edits) {
var flags = context.flags
var result = []
var upper
var nextUpper
var length
var index
var word
Expand All @@ -261,6 +260,7 @@ function generate(context, memory, words, edits) {
var nextCharacter
var inject
var offset
var currentCase

// Check the pre-generated edits.
length = edits && edits.length
Expand All @@ -280,8 +280,10 @@ function generate(context, memory, words, edits) {
before = ''
character = ''
nextAfter = word
currentCase = casing(word)
nextNextAfter = word.slice(1)
nextCharacter = word.charAt(0)
nextUpper = nextCharacter.toLowerCase() !== nextCharacter
position = -1
count = word.length + 1

Expand All @@ -293,7 +295,23 @@ function generate(context, memory, words, edits) {
nextNextAfter = nextAfter.slice(1)
character = nextCharacter
nextCharacter = word.charAt(position + 1)
upper = character.toLowerCase() !== character
upper = nextUpper
if (nextCharacter) {
nextUpper = nextCharacter.toLowerCase() !== nextCharacter
}

if (nextAfter && upper !== nextUpper) {
// Remove.
check(before + switchCase(nextAfter))

// Switch.
check(
before +
switchCase(nextCharacter) +
switchCase(character) +
nextNextAfter
)
}

// Remove.
check(before + nextAfter)
Expand All @@ -309,14 +327,19 @@ function generate(context, memory, words, edits) {
while (++offset < characterLength) {
inject = characters[offset]

// Add and replace.
check(before + inject + after)
check(before + inject + nextAfter)

// Try upper-case if the original character was upper-cased.
if (upper && inject !== inject.toUpperCase()) {
if (currentCase !== 's') {
check(before + inject + after)
check(before + inject + nextAfter)
}

inject = inject.toUpperCase()

check(before + inject + after)
check(before + inject + nextAfter)
} else {
// Add and replace.
check(before + inject + after)
check(before + inject + nextAfter)
}
Expand Down Expand Up @@ -350,4 +373,13 @@ function generate(context, memory, words, edits) {
memory.weighted[value]++
}
}

function switchCase(fragment) {
var first = fragment.charAt(0)
if (first.toLowerCase() === first) {
return first.toUpperCase() + fragment.slice(1)
}

return first.toLowerCase() + fragment.slice(1)
}
}
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,30 @@ test('NSpell()', function (t) {
'should suggest alternatives including correct conjunction'
)

st.deepEqual(
us.suggest('Iffect'),
['Affect', 'Effect', 'Infect'],
'should suggest sentence-case with replaced first character'
)

st.deepEqual(
us.suggest('Acnada'),
['Canada'],
'should suggest sentence-case with swapped first character'
)

st.deepEqual(
us.suggest('abDUL'),
['Abdul'],
'should suggest sentence-case for funky case when sentence-case in dictionary'
)

st.deepEqual(
us.suggest('COLORFU'),
['COLORFUL'],
'should suggest alternatives for upper-case with added letter'
)

st.end()
})

Expand Down