Skip to content

Commit

Permalink
Fix caps, keyboard group logic
Browse files Browse the repository at this point in the history
Closes GH-37.
Closes GH-39.
Closes GH-41.
  • Loading branch information
tvquizphd committed Jan 17, 2021
1 parent bd92048 commit 9c002d2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
13 changes: 11 additions & 2 deletions lib/suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function suggest(value) {
var replacementTable = self.replacementTable
var conversion = self.conversion
var groups = self.flags.KEY
var charAdded = {}
var suggestions = []
var weighted = {}
var memory
Expand Down Expand Up @@ -77,8 +78,12 @@ function suggest(value) {

while (++index < length) {
character = value.charAt(index)
before = value.slice(0, index)
after = value.slice(index + 1)
insensitive = character.toLowerCase()
upper = insensitive !== character
charAdded = {}

offset = -1
count = groups.length

Expand All @@ -90,15 +95,19 @@ function suggest(value) {
continue
}

before = value.slice(0, position)
after = value.slice(position + 1)
otherOffset = -1
otherCount = group.length

while (++otherOffset < otherCount) {
if (otherOffset !== position) {
otherCharacter = group.charAt(otherOffset)

if (charAdded[otherCharacter]) {
continue
}

charAdded[otherCharacter] = true

if (upper) {
otherCharacter = otherCharacter.toUpperCase()
}
Expand Down
20 changes: 13 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ test('NSpell()', function (t) {

st.deepEqual(
us.suggest('propper'),
['dropper', 'proper', 'cropper', 'popper', 'propped', 'prosper'],
['proper', 'propped', 'cropper', 'dropper', 'popper', 'prosper'],
'should suggest alternatives'
)

st.deepEqual(
us.suggest('Ghandi'),
['shandy', 'Brandi', 'Ghana', 'Grand', 'Grandee', 'Grands', 'handy'],
['Brandi', 'Ghana', 'Grandee', 'Shanxi', 'hand', 'hands', 'handy'],
'should suggest alternatives'
)

Expand Down Expand Up @@ -301,7 +301,7 @@ test('NSpell()', function (t) {

st.deepEqual(
us.suggest('collor'),
['color', 'collar', 'colloq'],
['color', 'colloq', 'collar'],
'should suggest removals'
)

Expand Down Expand Up @@ -478,17 +478,17 @@ test('NSpell()', function (t) {
st.deepEqual(
us.suggest('dont'),
[
'dent',
'cont',
'font',
'wont',
'dent',
'dint',
'done',
'dong',
'wont',
'dolt',
'don',
"don't",
'dona',
'done',
'dong',
'dons',
'dost',
'dot',
Expand All @@ -499,6 +499,12 @@ test('NSpell()', function (t) {
'should suggest alternatives including correct conjunction'
)

st.deepEqual(
us.suggest('TODO'),
[`DODO`, `TOD`, `TODD`, `TOGO`, `TOJO`, `TOO`, `TOTO`],
'should suggest alternatives for uppercase input'
)

st.end()
})

Expand Down

0 comments on commit 9c002d2

Please sign in to comment.