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 keyboard edits #42

Closed
wants to merge 7 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
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'],
'TODO: This test behaves unexpectedly, but is needed for coverage.'
)
Comment on lines +502 to +506
Copy link
Contributor Author

@tvquizphd tvquizphd Jan 10, 2021

Choose a reason for hiding this comment

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

This test is needed for the coverage to reach 100% including line #236 of suggest.js. Otherwise, no comparison is ever made when sorting suggestions of equal weight where element a has better casing than element b. This test is not needed by PR #45 or PR #39.


st.end()
})

Expand Down