trie-search
Advanced tools
Comparing version 1.2.4 to 1.2.5
@@ -6,3 +6,3 @@ { | ||
"description": "A trie implementation that maps keys to objects for rapid retrieval by phrases. Most common use will be for typeahead searches.", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"main": "index.js", | ||
@@ -9,0 +9,0 @@ "url": "https://github.com/joshjung/trie-search", |
@@ -5,2 +5,4 @@ var HashArray = require('hasharray'); | ||
var IS_WHITESPACE = /^[\s]*$/; | ||
var DEFAULT_INTERNATIONALIZE_EXPAND_REGEXES = [ | ||
@@ -45,2 +47,3 @@ { | ||
this.options.splitOnRegEx = this.options.hasOwnProperty('splitOnRegEx') ? this.options.splitOnRegEx : /\s/g; | ||
this.options.splitOnGetRegEx = this.options.splitOnGetRegEx || this.options.splitOnRegEx; | ||
this.options.min = this.options.min || 1; | ||
@@ -166,13 +169,18 @@ this.options.keepAll = this.options.hasOwnProperty('keepAll') ? this.options.keepAll : false; | ||
var phrases = key.split(this.options.splitOnRegEx); | ||
var emptySplitMatch = phrases.filter(p => IS_WHITESPACE.test(p)); | ||
var selfMatch = phrases.filter(p => p === key); | ||
var selfIsOnlyMatch = selfMatch.length + emptySplitMatch.length === phrases.length; | ||
for (var i = 0, l = phrases.length; i < l; i++) { | ||
// There is an edge case that a RegEx with a positive lookeahed like: | ||
// /?=[A-Z]/ // Split on capital letters for a camelcase sentence | ||
// Will then match again when we call map, creating an infinite stack loop. | ||
if (phrases[i] && phrases[i] !== key) { | ||
this.map(phrases[i], value); | ||
// There is an edge case that a RegEx with a positive lookeahed like: | ||
// /?=[A-Z]/ // Split on capital letters for a camelcase sentence | ||
// Will then match again when we call map, creating an infinite stack loop. | ||
if (!selfIsOnlyMatch) { | ||
for (var i = 0, l = phrases.length; i < l; i++) { | ||
if (!IS_WHITESPACE.test(phrases[i])) { | ||
this.map(phrases[i], value); | ||
} | ||
} | ||
return; | ||
} | ||
return; | ||
} | ||
@@ -253,3 +261,3 @@ | ||
haKeyFields = this.options.indexField ? [this.options.indexField] : this.keyFields, | ||
words = this.options.splitOnRegEx ? phrase.split(this.options.splitOnRegEx) : [phrase]; | ||
words = this.options.splitOnGetRegEx ? phrase.split(this.options.splitOnGetRegEx) : [phrase]; | ||
@@ -256,0 +264,0 @@ for (var w = 0, l = words.length; w < l; w++) |
@@ -681,10 +681,12 @@ var assert = require('assert'), | ||
describe('TrieSearch::map(...) works with RegEx with positive lookahead', function() { | ||
var ts = new TrieSearch('key', { | ||
splitOnRegEx: /([.\-\s]|(?=[A-Z]))/ | ||
}), | ||
item = {}; | ||
describe('TrieSearch::map(...) works with RegEx with positive lookahead (e.g. split on capital letters)', function() { | ||
it('should not error', function() { | ||
try { | ||
var ts = new TrieSearch('key', { | ||
splitOnRegEx: /([.\-\s']|(?=[A-Z]))/, | ||
splitOnGetRegEx: /[.\-\s']/, | ||
}), | ||
item = {someValue: 12345}, | ||
item2 = {someValue: 67890}; | ||
ts.map('This IsSome.Phrase-Whatever', item); | ||
@@ -695,3 +697,29 @@ } catch (error) { | ||
}); | ||
it('should match capital letter breaks', function() { | ||
var ts = new TrieSearch('key', { | ||
splitOnRegEx: /([.\-\s']|(?=[A-Z]))/, | ||
splitOnGetRegEx: /[.\-\s']/, | ||
}), | ||
item = {someValue: 12345}, | ||
item2 = {someValue: 67890}; | ||
ts.map('It\'sOnlyAFlesh Wound', item); | ||
ts.map('WhatIsYourFavoriteColor', item2); | ||
assert(ts.get('It')[0] === item, 'Did not properly match It'); | ||
assert(ts.get('s')[0] === item, 'Did not properly match s'); | ||
assert(ts.get('Only')[0] === item, 'Did not properly match Only'); | ||
assert(ts.get('A')[0] === item, 'Did not properly match A'); | ||
assert(ts.get('Flesh')[0] === item, 'Did not properly match Flesh'); | ||
assert(ts.get('Wound')[0] === item, 'Did not properly match Wound'); | ||
assert(ts.get('What')[0] === item2, 'Did not properly match What'); | ||
assert(ts.get('Is')[0] === item2, 'Did not properly match Is'); | ||
assert(ts.get('Your')[0] === item2, 'Did not properly match Your'); | ||
assert(ts.get('Fav')[0] === item2, 'Did not properly match Fav'); | ||
assert(ts.get('Favorite')[0] === item2, 'Did not properly match Favorite'); | ||
assert(ts.get('Color')[0] === item2, 'Did not properly match Color'); | ||
}); | ||
}); | ||
}); |
9007088
1898