fast-matcher
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -11,15 +11,58 @@ (function() { | ||
function FastMatcher(list, options) { | ||
var source = this.source = this.wrapList(list); | ||
this.options = options || {}; | ||
this.matches = this.options.matches || []; | ||
this.source = list.slice(0); | ||
this.options = options || {}; | ||
this.matches = this.options.matches || []; | ||
this.selectors = this.createSelectors(); | ||
var selectors = this.selectors = this.createSelectors(); | ||
this.lists = selectors.map(function(selector) { | ||
var list = source.slice(0); | ||
list.sort(function(x, y) { | ||
return compare(selector(x.val), selector(y.val)); | ||
var source = this.source, | ||
options = this.options, | ||
lists = []; | ||
this.selectors.forEach(function(selector) { | ||
var maxSegments = 0; | ||
var list = source.map(function(e, i) { | ||
var item = { | ||
i: i, | ||
val: e, | ||
selectedVal: selector(e) | ||
}; | ||
if (options.anyWord) { | ||
item.segments = getSubstrings(item.selectedVal); | ||
maxSegments = Math.max(maxSegments, item.segments.length); | ||
} | ||
return item; | ||
}); | ||
list.selector = selector; | ||
return list; | ||
if (options.anyWord) { | ||
for (var i = 0; i < maxSegments; ++i) { | ||
lists.push( | ||
list | ||
.filter(function(item) { | ||
return i < item.segments.length; | ||
}) | ||
.map(function(item) { | ||
return { | ||
i: item.i, | ||
val: item.val, | ||
selectedVal: item.segments[i] | ||
}; | ||
}) | ||
.sort(function(x, y) { | ||
return compare(x.selectedVal, y.selectedVal); | ||
})); | ||
} | ||
} else { | ||
list.sort(function(x, y) { | ||
return compare(x.selectedVal, y.selectedVal); | ||
}); | ||
lists.push(list); | ||
} | ||
}); | ||
this.lists = lists; | ||
} | ||
@@ -29,14 +72,2 @@ | ||
* @example | ||
* function wrapList(list) { | ||
* return new FastMatcher([]).wrapList(list); | ||
* } | ||
* | ||
* wrapList([5, 3, 4]); // => [{i:0,val:5},{i:1,val:3},{i:2,val:4}] | ||
*/ | ||
FastMatcher.prototype.wrapList = function wrapList(list) { | ||
return list.map(function(e, i) { return { i: i, val: e }; }); | ||
}; | ||
/** | ||
* @example | ||
* function createSelectors(selector) { | ||
@@ -110,2 +141,11 @@ * return new FastMatcher([], { selector: selector }).createSelectors(); | ||
* // => [{x:'a',y:'a'},{x:'a',y:'b'},{x:'b',y:'a'}] | ||
* | ||
* getMatches(['a', 'a b', 'a c', 'b', 'c a b'], 'b', { anyWord: true }); | ||
* // => ['b', 'a b', 'c a b'] | ||
* | ||
* getMatches(['a', 'a b', 'a c', 'b', 'c a b'], 'b', { anyWord: true, preserveOrder: true }); | ||
* // => ['a b', 'b', 'c a b'] | ||
* | ||
* getMatches(['a', 'a b', 'c a b'], 'a b', { anyWord: true }); | ||
* // => ['a b', 'c a b'] | ||
*/ | ||
@@ -122,3 +162,3 @@ FastMatcher.prototype.getMatches = function getMatches(prefix) { | ||
items = [], | ||
list, index, item, itemsAdded; | ||
list, index, value, itemsAdded; | ||
@@ -139,8 +179,8 @@ for (var i = 0; i < lists.length; ++i) { | ||
item = list.selector(list[index].val); | ||
value = list[index].selectedVal; | ||
if (this.options.caseInsensitive) { | ||
item = item.toLowerCase(); | ||
value = value.toLowerCase(); | ||
} | ||
if (!startsWith(item, prefix)) { | ||
if (!startsWith(value, prefix)) { | ||
break; | ||
@@ -170,4 +210,3 @@ } | ||
FastMatcher.prototype.findIndex = function findIndex(list, prefix) { | ||
var selector = list.selector, | ||
lower = 0, | ||
var lower = 0, | ||
upper = list.length; | ||
@@ -179,3 +218,3 @@ | ||
value = selector(list[i].val); | ||
value = list[i].selectedVal; | ||
if (this.options.caseInsensitive) { | ||
@@ -222,2 +261,25 @@ value = value.toLowerCase(); | ||
* @example | ||
* getSubstrings('of the night'); | ||
* // => ['of the night', 'the night', 'night'] | ||
* | ||
* getSubstrings(' foo bar '); | ||
* // => ['foo bar ', 'bar '] | ||
*/ | ||
function getSubstrings(string) { | ||
string = string.replace(/^\s+/, ''); | ||
var substrings = [string], | ||
startOfWord = /\s[^\s]/g, | ||
match; | ||
while (match = startOfWord.exec(string)) { | ||
substrings.push(string.substring(match.index + 1)); | ||
} | ||
return substrings; | ||
} | ||
/** | ||
* @private | ||
* @example | ||
* var arr = []; | ||
@@ -224,0 +286,0 @@ * |
{ | ||
"name": "fast-matcher", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Find matches fast", | ||
@@ -5,0 +5,0 @@ "main": "fastMatcher.js", |
@@ -44,2 +44,5 @@ # fast-matcher | ||
// whether to match against any word (not just first) for each string | ||
anyWord: false, | ||
// how many matches to find at a time | ||
@@ -46,0 +49,0 @@ limit: 25 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
64
1
18622
9
372
1