Skip to content

fix: Trie.keysWithPrefix ignores case in a case-insensitive trie - #802

Open
OsamaAnsar wants to merge 1 commit into
NaturalNode:masterfrom
OsamaAnsar:fix/trie-keys-with-prefix-case
Open

OsamaAnsar wants to merge 1 commit into
NaturalNode:masterfrom
OsamaAnsar:fix/trie-keys-with-prefix-case

Conversation

@OsamaAnsar

Copy link
Copy Markdown
Contributor

A trie made with new Trie(false) is case-insensitive in contains, findPrefix and findMatchesOnPath, but not in keysWithPrefix:

const { Trie } = require('natural')

const trie = new Trie(false)
trie.addStrings(['Hello', 'help'])

trie.contains('HELLO')      // true
trie.keysWithPrefix('he')   // ['hello', 'help']
trie.keysWithPrefix('He')   // [], expected ['hello', 'help']

keysWithPrefix reads this.caseSensitive, which is never set. The constructor stores the flag as this.cs, and the other methods read that. The prefix is therefore never lower-cased.

The change reads this.cs. Two new specs cover the case-insensitive and the default case-sensitive trie; the first fails without the change. npm test (900 specs) and npm run lint pass.

keysWithPrefix read this.caseSensitive, which is never set, so it did not
lower-case the prefix the way contains, findPrefix and findMatchesOnPath do.
A prefix with an upper-case letter found nothing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant