Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

flexsearch

Package Overview
Dependencies
Maintainers
1
Versions
87
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flexsearch - npm Package Compare versions

Comparing version 0.1.29 to 0.2.0

255

flexsearch.js

@@ -43,2 +43,5 @@ ;/**!

// contextual depth
depth: 0,
// use on of built-in functions

@@ -301,2 +304,9 @@ // or pass custom encoding algorithm

this.depth = (
options['depth'] ||
this.depth ||
defaults.depth
);
/** @export */

@@ -339,3 +349,4 @@ this.encoder = (

{/* 8 */},
{/* 9 */}
{/* 9 */},
{/* ctx */}
];

@@ -393,3 +404,3 @@

value = this.encoder(value);
value = this.encoder.call(global_encoder, value);
}

@@ -480,3 +491,2 @@

var dupes = {};
var words = (

@@ -491,4 +501,15 @@

for(var i = 0; i < words.length; i++){
var dupes = {
'_ctx': {}
};
var threshold = this.threshold;
var map = this._map;
var word_length = words.length;
// tokenize
for(var i = 0; i < word_length; i++){
/** @type {string} */

@@ -514,12 +535,12 @@ var value = words[i];

this._map,
map,
dupes,
tmp,
id,
content,
this.threshold
/** @type {string} */ (content),
threshold
);
}
// Note: no break here, fallthrough to next case
// Note: no break here, fallthrough to next case

@@ -536,8 +557,8 @@ case 'forward':

this._map,
map,
dupes,
tmp,
id,
content,
this.threshold
/** @type {string} */ (content),
threshold
);

@@ -560,8 +581,8 @@ }

this._map,
map,
dupes,
tmp,
id,
content,
this.threshold
/** @type {string} */ (content),
threshold
);

@@ -573,15 +594,92 @@ }

case 'ngram':
// TODO
break;
case 'strict':
default:
addIndex(
var score = addIndex(
this._map,
map,
dupes,
value,
id,
content,
this.threshold
/** @type {string} */ (content),
threshold
);
if((word_length > 1) && this.depth && (score > threshold)){
var ctx_map = map[10];
var ctx_dupes = dupes['_ctx'][value] || (dupes['_ctx'][value] = {});
var ctx_tmp = ctx_map[value] || (ctx_map[value] = [
{/* 0 */},
{/* 1 */},
{/* 2 */},
{/* 3 */},
{/* 4 */},
{/* 5 */},
{/* 6 */},
{/* 7 */},
{/* 8 */},
{/* 9 */}
]);
if(i) {
if(i > 1) {
addIndex(
ctx_tmp,
ctx_dupes,
words[i - 2],
id,
/** @type {string} */ (content),
threshold
);
}
addIndex(
ctx_tmp,
ctx_dupes,
words[i - 1],
id,
/** @type {string} */ (content),
threshold
);
}
if(i < (word_length - 1)) {
addIndex(
ctx_tmp,
ctx_dupes,
words[i + 1],
id,
/** @type {string} */ (content),
threshold
);
if(i < (word_length - 2)) {
addIndex(
ctx_tmp,
ctx_dupes,
words[i + 2],
id,
/** @type {string} */ (content),
threshold
);
}
}
}
break;

@@ -750,2 +848,3 @@ }

var threshold;
var result = [];

@@ -811,3 +910,3 @@ if(query && (typeof query === 'object')){

return [];
return result;
}

@@ -847,3 +946,3 @@

return [];
return result;
}

@@ -857,3 +956,3 @@

return [];
return result;
}

@@ -873,65 +972,86 @@

var length = words.length;
var found = true;
var check = [];
var check_words = {};
// sort words by length
if(length > 1){
words.sort(sort_by_length_down);
if(this.depth){
var use_contextual = true;
var key = words[0];
check_words[key] = "1";
}
else{
// Note: sort words by length only in non-contextual mode
words.sort(sort_by_length_down);
}
}
// perform search
var ctx_map;
var result = [];
var found = true;
var check = [];
var check_words = {};
if(!use_contextual || (ctx_map = this._map[10])[key]){
for(var a = 0; a < length; a++){
for(var a = use_contextual ? 1 : 0; a < length; a++){
var value = words[a];
var value = words[a];
if(value && !check_words[value]){
if(value && !check_words[value]){
var map;
var map_found = false;
var map_check = [];
var count = 0;
var map;
var map_found = false;
var map_check = [];
var count = 0;
// loop scores top-down
for(var z = 9; z >= threshold; z--){
for(var z = 9; z >= threshold; z--){
map = (
if((map = this._map[z][value])){
use_contextual ?
//if(map.length){
ctx_map[key][z][value]
:
this._map[z][value]
);
if(map){
map_check[count++] = map;
map_found = true;
//}
}
}
}
if(!map_found){
if(!map_found){
found = false;
break;
}
else{
found = false;
break;
}
else{
// TODO: handle by intersection
// TODO: handle by intersection
check[check.length] = (
check[check.length] = (
count > 1 ?
count > 1 ?
check.concat.apply([], map_check)
:
map_check[0]
);
check.concat.apply([], map_check)
:
map_check[0]
);
}
check_words[value] = "1";
}
check_words[value] = "1";
key = value;
}
}
else{
found = false;
}
if(found /*&& check.length*/){

@@ -1063,2 +1183,3 @@

* Phonetic Encoders
* @dict
* @enum {Function}

@@ -1167,3 +1288,3 @@ * @private

return function(string, _skip_post_processing){
return /** @this {global_encoder} */ function(string, _skip_post_processing){

@@ -1176,3 +1297,3 @@ if(!string){

// perform simple encoding
string = global_encoder['simple'](string);
string = this['simple'](string);

@@ -1229,3 +1350,3 @@ // normalize special pairs

return function(str){
return /** @this {global_encoder} */ function(str){

@@ -1238,3 +1359,3 @@ if(!str){

// perform advanced encoding
str = global_encoder['advanced'](str, /* skip post processing? */ true);
str = this['advanced'](str, /* skip post processing? */ true);

@@ -1266,2 +1387,3 @@ if(str.length > 1){

* @param {!string} value
* @this {global_encoder}
* @returns {Array<?string>}

@@ -1280,3 +1402,3 @@ */

// perform advanced encoding
value = global_encoder['advanced'](value);
value = this['advanced'](value);

@@ -1468,16 +1590,17 @@ if(!value){

if(!dupes[tmp]){
if(typeof dupes[tmp] === 'undefined'){
dupes[tmp] = "1";
var score = calcScore(tmp, content);
dupes[tmp] = score;
if(score > threshold){
var arr = map[score];
arr = arr[tmp] || (arr[tmp] = []);
arr[arr.length] = id;
arr = arr[tmp] || (arr[tmp] = []);
arr[arr.length] = id;
}
}
return score || dupes[tmp];
}

@@ -1484,0 +1607,0 @@

@@ -6,21 +6,21 @@ /*

*/
'use strict';(function(k,z,e){var d;(d=e.define)&&d.amd?d([],function(){return z}):(d=e.modules)?d[k.toLowerCase()]=z:"undefined"!==typeof module?module.exports=z:e[k]=z})("FlexSearch",function D(k){function e(a){a||(a=r);this.id=a.id||E++;this.init(a);Object.defineProperty(this,"index",{get:function(){return this.a}});Object.defineProperty(this,"length",{get:function(){return Object.keys(this.a).length}})}function d(a){return new RegExp(a,"g")}function q(a,b,c){if("undefined"===typeof c){for(c=0;c<
b.length;c+=2)a=a.replace(b[c],b[c+1]);return a}return a.replace(b,c)}function v(a,b,c,g,f,m){b[c]||(b[c]="1",b=f.indexOf(c),f=3/f.length*(f.length-b)+6/(b-f.lastIndexOf(" ",b))+.5|0,f>m&&(a=a[f],a=a[c]||(a[c]=[]),a[a.length]=g))}function x(a){for(var b="",c="",g="",f=0;f<a.length;f++){var m=a[f];m!==c&&(0<f&&"h"===m?(g="a"===g||"e"===g||"i"===g||"o"===g||"u"===g||"y"===g,"a"!==c&&"e"!==c&&"i"!==c&&"o"!==c&&"u"!==c&&"y"!==c||!g||(b+=m)):b+=m);g=f===a.length-1?"":a[f+1];c=m}return b}function n(a,b){a=
a.length-b.length;return 0>a?1:0<a?-1:0}function F(a,b){a=a.length-b.length;return 0>a?-1:0<a?1:0}function G(a,b){var c=[],g=a.length;if(1<g){a.sort(F);for(var f={},m=a[0],d=0,l=m.length;d<l;++d)f[m[d]]=1;for(var h=0,e=1;e<g;++e){var k=a[e],n=!1;d=0;for(l=k.length;d<l;++d)if(f[m=k[d]]===e){if(e===g-1&&(c[h++]=m,b&&h===b))return c;n=!0;f[m]=e+1;break}if(!n)return[]}}else g&&(c=a[0],b&&c&&c.length>b&&(c=c.slice(0,b)));return c}function y(a){a.w||(a.w=A(function(){a.w=null;var b=a.async;b&&(a.async=
!1);if(a.f.length){for(var c=B(),g;(g=a.f.shift())||0===g;){var f=a.h[g];switch(f[0]){case w.add:a.add(f[1],f[2]);break;case w.update:a.update(f[1],f[2]);break;case w.remove:a.remove(f[1])}a.h[g]=null;delete a.h[g];if(100<B()-c)break}a.f.length&&y(a)}b&&(a.async=b)},1,"search-async-"+a.id))}function B(){return"undefined"!==typeof performance?performance.now():(new Date).getTime()}function H(a,b,c,g){a=k("flexsearch","id"+a,function(){var a,b;self.a=function(c){if(c=c.data)c.search?self.postMessage({result:b.search(c.content,
c.threshold?{limit:c.limit,threshold:c.threshold}:c.limit),id:a,content:c.content,limit:c.limit}):c.add?b.add(c.id,c.content):c.update?b.update(c.id,c.content):c.remove?b.remove(c.id):c.reset?b.reset():c.info?(c=b.info(),c.worker=a,b.A&&console.log(c)):c.register&&(a=c.id,c.options.cache=!1,c.options.async=!0,c.options.worker=!1,b=(new Function(c.register.substring(c.register.indexOf("{")+1,c.register.lastIndexOf("}"))))(),b=new b(c.options))}},function(a){(a=a.data)&&a.result&&g(a.id,a.content,a.result,
a.limit)},b);var f=D.toString();c.id=b;a.postMessage(b,{register:f,options:c,id:b});return a}var r={type:"integer",mode:"forward",cache:!1,async:!1,b:!1,threshold:0,encode:!1},p=[],E=0,w={add:0,update:1,remove:2},C=d("[ -/]");e.new=function(a){return new this(a)};e.create=function(a){return e.new(a)};e.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(p[p.length]=d(b),p[p.length]=a[b]);return this};e.register=function(a,b){u[a]=b;return this};e.encode=function(a,b){return u[a](b)};e.prototype.init=
function(a){this.m=[];if(a){if(a.worker)if("undefined"===typeof Worker)a.worker=!1,a.async=!0,this.j=null;else{var b=this,c=parseInt(a.worker,10)||4;b.s=-1;b.o=0;b.i=[];b.v=null;b.j=Array(c);for(var g=0;g<c;g++)b.j[g]=H(b.id,g,a||r,function(a,c,g,d){b.o!==b.b&&(b.i=b.i.concat(g),b.o++,d&&b.i.length>=d&&(b.o=b.b),b.v&&b.o===b.b&&(b.i.length?b.g="":b.g||(b.g=c),b.cache&&b.l.set(c,b.i),b.v(b.i),b.i=[]))})}this.mode=a.mode||this.mode||r.mode;this.cache=a.cache||this.cache||r.cache;this.async=a.async||
this.async||r.async;this.b=a.worker||this.b||r.b;this.threshold=a.threshold||this.threshold||r.threshold;this.encoder=a.encode&&u[a.encode]||this.encoder||a.encode;this.A=a.debug||this.A;a.matcher&&this.addMatcher(a.matcher)}this.c=[{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.f=[];this.w=null;this.g="";this.u=!0;this.l=this.cache?new I(3E4,50,!0):!1;return this};e.prototype.encode=function(a){a&&p.length&&(a=q(a,p));a&&this.m.length&&(a=q(a,this.m));a&&this.encoder&&(a=this.encoder(a));
return a};e.prototype.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(this.m[this.m.length]=d(b),this.m[this.m.length]=a[b]);return this};e.prototype.add=function(a,b){if("string"===typeof b&&b&&(a||0===a))if(this.a[a])this.update(a,b);else{if(this.b)return++this.s>=this.j.length&&(this.s=0),this.j[this.s].postMessage(this.s,{add:!0,id:a,content:b}),this.a[a]=""+this.s,this;if(this.async)return this.h[a]||(this.f[this.f.length]=a),this.h[a]=[w.add,a,b],y(this),this;b=this.encode(b);if(!b.length)return this;
for(var c={},g=b.constructor===Array?b:b.split(C),f=0;f<g.length;f++){var d=g[f];if(d){var e=d.length;switch(this.mode){case "reverse":case "both":for(var l="",h=e-1;1<=h;h--)l=d[h]+l,v(this.c,c,l,a,b,this.threshold);case "forward":l="";for(h=0;h<e;h++)l+=d[h],v(this.c,c,l,a,b,this.threshold);break;case "full":for(h=0;h<e;h++)for(var k=e;k>h;k--)l=d.substring(h,k),v(this.c,c,l,a,b,this.threshold);break;default:v(this.c,c,d,a,b,this.threshold)}}}this.a[a]="1";this.u=!1}return this};e.prototype.update=
function(a,b){if("string"===typeof b&&(a||0===a)&&this.a[a]){if(this.b){var c=parseInt(this.a[a],10);this.j[c].postMessage(c,{update:!0,id:a,content:b});return this}if(this.async)return this.h[a]||(this.f[this.f.length]=a),this.h[a]=[w.update,a,b],y(this),this;this.remove(a);b&&this.add(a,b)}return this};e.prototype.remove=function(a){if(this.a[a]){if(this.b){var b=parseInt(this.a[a],10);this.j[b].postMessage(b,{remove:!0,id:a});delete this.a[a];return this}if(this.async)return this.h[a]||(this.f[this.f.length]=
a),this.h[a]=[w.remove,a],y(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.c[b]),d=0;d<c.length;d++){var f=c[d],e=this.c[b];if((e=e&&e[f])&&e.length)for(var t=0;t<e.length;t++)if(e[t]===a){e.splice(t,1);break}e.length||delete this.c[b][f]}delete this.a[a];this.u=!1}return this};e.prototype.search=function(a,b,c){if(a&&"object"===typeof a){c=a.callback||b;b=a.limit;var d=a.threshold;a=a.query}d||(d=0);"function"===typeof b?(c=b,b=1E3):b||(b=1E3);if(this.b){this.v=c;this.o=0;this.i=[];for(var f=
0;f<this.b;f++)this.j[f].postMessage(f,{search:!0,limit:b,threshold:d,content:a});return null}if(c){var e=this;A(function(){c(e.search(a,b));e=null},1,"search-"+this.id);return null}if(!a||"string"!==typeof a)return[];f=a;if(!this.u)this.cache&&(this.g="",this.l.reset()),this.u=!0;else if(this.cache){var t=this.l.get(a);if(t)return t}else if(this.g&&0===a.indexOf(this.g))return[];f=this.encode(f);if(!f.length)return[];f=f.constructor===Array?f:f.split(C);t=f.length;1<t&&f.sort(n);for(var l=[],h=!0,
k=[],r={},q=0;q<t;q++){var p=f[q];if(p&&!r[p]){for(var v,w=!1,u=[],x=0,y=9;y>=d;y--)if(v=this.c[y][p])u[x++]=v,w=!0;if(w)k[k.length]=1<x?k.concat.apply([],u):u[0];else{h=!1;break}r[p]="1"}}h&&(l=G(k,b));l.length?this.g="":this.g||(this.g=a);this.cache&&this.l.set(a,l);return l};e.prototype.info=function(){if(this.b)for(var a=0;a<this.b;a++)this.j[a].postMessage(a,{info:!0,id:this.id});else{for(var b,c,d=0,f=0,e=0,k=0;10>k;k++)for(b=Object.keys(this.c[k]),a=0;a<b.length;a++)c=this.c[k][b[a]].length,
d+=c+2*b[a].length+4,f+=c,e+=2*b[a].length;b=Object.keys(this.a);c=b.length;for(a=0;a<c;a++)d+=2*b[a].length+2;return{id:this.id,memory:d,items:c,sequences:f,chars:e,status:this.u,cache:this.f.length,matcher:p.length,worker:this.b}}};e.prototype.reset=function(){this.destroy();return this.init()};e.prototype.destroy=function(){this.cache&&this.l.reset();this.c=this.a=this.l=null;return this};var u={icase:function(a){return a.toLowerCase()},simple:function(){var a=[d("[\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5]"),
"a",d("[\u00e8\u00e9\u00ea\u00eb]"),"e",d("[\u00ec\u00ed\u00ee\u00ef]"),"i",d("[\u00f2\u00f3\u00f4\u00f5\u00f6\u0151]"),"o",d("[\u00f9\u00fa\u00fb\u00fc\u0171]"),"u",d("[\u00fd\u0177\u00ff]"),"y",d("\u00f1"),"n",d("\u00e7"),"c",d("\u00df"),"s",d("[-/]")," ",d("[^a-z0-9 ]"),""];return function(b){return q(b.toLowerCase(),a)}}(),advanced:function(){var a=[d("ae"),"a",d("ai"),"ei",d("ay"),"ei",d("ey"),"ei",d("oe"),"o",d("ue"),"u",d("ie"),"i",d("sz"),"s",d("zs"),"s",d("sh"),"s",d("ck"),"k",d("cc"),"k",
d("dt"),"t",d("ph"),"f",d("pf"),"f",d("ou"),"o",d("uo"),"u"];return function(b,c){if(!b)return b;b=u.simple(b);2<b.length&&(b=q(b,a));c||1<b.length&&(b=x(b));return b}}(),extra:function(){var a=[d("p"),"b",d("z"),"s",d("[cgq]"),"k",d("n"),"m",d("d"),"t",d("[vw]"),"f",d("[aeiouy]"),""];return function(b){if(!b)return b;b=u.advanced(b,!0);if(1<b.length){b=b.split(" ");for(var c=0;c<b.length;c++){var d=b[c];1<d.length&&(b[c]=d[0]+q(d.substring(1),a))}b=b.join("");b=x(b)}return b}}(),ngram:function(a){var b=
[];if(!a)return b;a=u.advanced(a);if(!a)return b;for(var c=0,d=0,f=-1,e="",k=a.length,l=0;l<k;l++){var h=a[l];"a"===h||"e"===h||"i"===h||"o"===h||"u"===h||"y"===h?c++:d++;" "!==h&&(e+=h);if(" "===h||2<=c&&2<=d||l===k-1){if(e){c=e.length;if(2<c||" "===h||l===k-1){if(h=e.charCodeAt(0),1<c||48<=h||57>=h)b[++f]=e}else b[f]&&(b[f]+=e);e=""}d=c=0}}return b}},A=function(){var a={};return function(b,c,d){var f=a[d];f&&clearTimeout(f);return a[d]=setTimeout(b,c)}}(),I=function(){function a(){this.cache={}}
a.prototype.reset=function(){this.cache={}};a.prototype.set=function(a,c){this.cache[a]=c};a.prototype.get=function(a){return this.cache[a]};return a}();return e}(function(){var k={},z=!("undefined"===typeof Blob||"undefined"===typeof URL||!URL.createObjectURL);return function(e,d,q,v,x){var n=e;e=z?URL.createObjectURL(new Blob(["("+q.toString()+")()"],{type:"text/javascript"})):"../"+n+".js";n+="-"+d;k[n]||(k[n]=[]);k[n][x]=new Worker(e);k[n][x].onmessage=v;return{postMessage:function(d,e){k[n][d].postMessage(e)}}}}()),
this);
'use strict';(function(l,C,e){var d;(d=e.define)&&d.amd?d([],function(){return C}):(d=e.modules)?d[l.toLowerCase()]=C:"undefined"!==typeof module?module.exports=C:e[l]=C})("FlexSearch",function G(l){function e(a){a||(a=u);this.id=a.id||H++;this.init(a);Object.defineProperty(this,"index",{get:function(){return this.a}});Object.defineProperty(this,"length",{get:function(){return Object.keys(this.a).length}})}function d(a){return new RegExp(a,"g")}function q(a,b,c){if("undefined"===typeof c){for(c=0;c<
b.length;c+=2)a=a.replace(b[c],b[c+1]);return a}return a.replace(b,c)}function r(a,b,c,f,h,t){if("undefined"===typeof b[c]){var g=h.indexOf(c);g=3/h.length*(h.length-g)+6/(g-h.lastIndexOf(" ",g))+.5|0;b[c]=g;g>t&&(a=a[g],a=a[c]||(a[c]=[]),a[a.length]=f)}return g||b[c]}function w(a){for(var b="",c="",f="",h=0;h<a.length;h++){var t=a[h];t!==c&&(0<h&&"h"===t?(f="a"===f||"e"===f||"i"===f||"o"===f||"u"===f||"y"===f,"a"!==c&&"e"!==c&&"i"!==c&&"o"!==c&&"u"!==c&&"y"!==c||!f||(b+=t)):b+=t);f=h===a.length-
1?"":a[h+1];c=t}return b}function x(a,b){a=a.length-b.length;return 0>a?1:0<a?-1:0}function I(a,b){a=a.length-b.length;return 0>a?-1:0<a?1:0}function J(a,b){var c=[],f=a.length;if(1<f){a.sort(I);for(var h={},t=a[0],g=0,y=t.length;g<y;++g)h[t[g]]=1;for(var d=0,e=1;e<f;++e){var n=a[e],m=!1;g=0;for(y=n.length;g<y;++g)if(h[t=n[g]]===e){if(e===f-1&&(c[d++]=t,b&&d===b))return c;m=!0;h[t]=e+1;break}if(!m)return[]}}else f&&(c=a[0],b&&c&&c.length>b&&(c=c.slice(0,b)));return c}function B(a){a.w||(a.w=D(function(){a.w=
null;var b=a.async;b&&(a.async=!1);if(a.c.length){for(var c=E(),f;(f=a.c.shift())||0===f;){var h=a.h[f];switch(h[0]){case z.add:a.add(h[1],h[2]);break;case z.update:a.update(h[1],h[2]);break;case z.remove:a.remove(h[1])}a.h[f]=null;delete a.h[f];if(100<E()-c)break}a.c.length&&B(a)}b&&(a.async=b)},1,"search-async-"+a.id))}function E(){return"undefined"!==typeof performance?performance.now():(new Date).getTime()}function K(a,b,c,f){a=l("flexsearch","id"+a,function(){var a,b;self.a=function(c){if(c=
c.data)c.search?self.postMessage({result:b.search(c.content,c.threshold?{limit:c.limit,threshold:c.threshold}:c.limit),id:a,content:c.content,limit:c.limit}):c.add?b.add(c.id,c.content):c.update?b.update(c.id,c.content):c.remove?b.remove(c.id):c.reset?b.reset():c.info?(c=b.info(),c.worker=a,b.A&&console.log(c)):c.register&&(a=c.id,c.options.cache=!1,c.options.async=!0,c.options.worker=!1,b=(new Function(c.register.substring(c.register.indexOf("{")+1,c.register.lastIndexOf("}"))))(),b=new b(c.options))}},
function(a){(a=a.data)&&a.result&&f(a.id,a.content,a.result,a.limit)},b);var h=G.toString();c.id=b;a.postMessage(b,{register:h,options:c,id:b});return a}var u={type:"integer",mode:"forward",cache:!1,async:!1,b:!1,threshold:0,depth:0,encode:!1},v=[],H=0,z={add:0,update:1,remove:2},F=d("[ -/]");e.new=function(a){return new this(a)};e.create=function(a){return e.new(a)};e.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(v[v.length]=d(b),v[v.length]=a[b]);return this};e.register=function(a,
b){A[a]=b;return this};e.encode=function(a,b){return A[a](b)};e.prototype.init=function(a){this.m=[];if(a){if(a.worker)if("undefined"===typeof Worker)a.worker=!1,a.async=!0,this.j=null;else{var b=this,c=parseInt(a.worker,10)||4;b.s=-1;b.o=0;b.i=[];b.v=null;b.j=Array(c);for(var f=0;f<c;f++)b.j[f]=K(b.id,f,a||u,function(a,c,f,d){b.o!==b.b&&(b.i=b.i.concat(f),b.o++,d&&b.i.length>=d&&(b.o=b.b),b.v&&b.o===b.b&&(b.i.length?b.f="":b.f||(b.f=c),b.cache&&b.l.set(c,b.i),b.v(b.i),b.i=[]))})}this.mode=a.mode||
this.mode||u.mode;this.cache=a.cache||this.cache||u.cache;this.async=a.async||this.async||u.async;this.b=a.worker||this.b||u.b;this.threshold=a.threshold||this.threshold||u.threshold;this.depth=a.depth||this.depth||u.depth;this.encoder=a.encode&&A[a.encode]||this.encoder||a.encode;this.A=a.debug||this.A;a.matcher&&this.addMatcher(a.matcher)}this.g=[{},{},{},{},{},{},{},{},{},{},{}];this.a={};this.h={};this.c=[];this.w=null;this.f="";this.u=!0;this.l=this.cache?new L(3E4,50,!0):!1;return this};e.prototype.encode=
function(a){a&&v.length&&(a=q(a,v));a&&this.m.length&&(a=q(a,this.m));a&&this.encoder&&(a=this.encoder.call(A,a));return a};e.prototype.addMatcher=function(a){for(var b in a)a.hasOwnProperty(b)&&(this.m[this.m.length]=d(b),this.m[this.m.length]=a[b]);return this};e.prototype.add=function(a,b){if("string"===typeof b&&b&&(a||0===a))if(this.a[a])this.update(a,b);else{if(this.b)return++this.s>=this.j.length&&(this.s=0),this.j[this.s].postMessage(this.s,{add:!0,id:a,content:b}),this.a[a]=""+this.s,this;
if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[z.add,a,b],B(this),this;b=this.encode(b);if(!b.length)return this;for(var c=b.constructor===Array?b:b.split(F),f={_ctx:{}},h=this.threshold,d=this.g,g=c.length,e=0;e<g;e++){var k=c[e];if(k){var p=k.length;switch(this.mode){case "reverse":case "both":for(var n="",m=p-1;1<=m;m--)n=k[m]+n,r(d,f,n,a,b,h);case "forward":n="";for(m=0;m<p;m++)n+=k[m],r(d,f,n,a,b,h);break;case "full":for(m=0;m<p;m++)for(var l=p;l>m;l--)n=k.substring(m,l),
r(d,f,n,a,b,h);break;case "ngram":break;default:p=r(d,f,k,a,b,h),1<g&&this.depth&&p>h&&(n=d[10],p=f._ctx[k]||(f._ctx[k]={}),k=n[k]||(n[k]=[{},{},{},{},{},{},{},{},{},{}]),e&&(1<e&&r(k,p,c[e-2],a,b,h),r(k,p,c[e-1],a,b,h)),e<g-1&&(r(k,p,c[e+1],a,b,h),e<g-2&&r(k,p,c[e+2],a,b,h)))}}}this.a[a]="1";this.u=!1}return this};e.prototype.update=function(a,b){if("string"===typeof b&&(a||0===a)&&this.a[a]){if(this.b){var c=parseInt(this.a[a],10);this.j[c].postMessage(c,{update:!0,id:a,content:b});return this}if(this.async)return this.h[a]||
(this.c[this.c.length]=a),this.h[a]=[z.update,a,b],B(this),this;this.remove(a);b&&this.add(a,b)}return this};e.prototype.remove=function(a){if(this.a[a]){if(this.b){var b=parseInt(this.a[a],10);this.j[b].postMessage(b,{remove:!0,id:a});delete this.a[a];return this}if(this.async)return this.h[a]||(this.c[this.c.length]=a),this.h[a]=[z.remove,a],B(this),this;for(b=0;10>b;b++)for(var c=Object.keys(this.g[b]),f=0;f<c.length;f++){var d=c[f],e=this.g[b];if((e=e&&e[d])&&e.length)for(var g=0;g<e.length;g++)if(e[g]===
a){e.splice(g,1);break}e.length||delete this.g[b][d]}delete this.a[a];this.u=!1}return this};e.prototype.search=function(a,b,c){var f=[];if(a&&"object"===typeof a){c=a.callback||b;b=a.limit;var d=a.threshold;a=a.query}d||(d=0);"function"===typeof b?(c=b,b=1E3):b||(b=1E3);if(this.b){this.v=c;this.o=0;this.i=[];for(f=0;f<this.b;f++)this.j[f].postMessage(f,{search:!0,limit:b,threshold:d,content:a});return null}if(c){var e=this;D(function(){c(e.search(a,b));e=null},1,"search-"+this.id);return null}if(!a||
"string"!==typeof a)return f;var g=a;if(!this.u)this.cache&&(this.f="",this.l.reset()),this.u=!0;else if(this.cache){var y=this.l.get(a);if(y)return y}else if(this.f&&0===a.indexOf(this.f))return f;g=this.encode(g);if(!g.length)return f;g=g.constructor===Array?g:g.split(F);y=g.length;var k=!0,p=[],n={};if(1<y)if(this.depth){var m=!0,l=g[0];n[l]="1"}else g.sort(x);var r;if(!m||(r=this.g[10])[l])for(var u=m?1:0;u<y;u++){var q=g[u];if(q&&!n[q]){for(var v,z=!1,w=[],B=0,A=9;A>=d;A--)if(v=m?r[l][A][q]:
this.g[A][q])w[B++]=v,z=!0;if(z)p[p.length]=1<B?p.concat.apply([],w):w[0];else{k=!1;break}n[q]="1"}l=q}else k=!1;k&&(f=J(p,b));f.length?this.f="":this.f||(this.f=a);this.cache&&this.l.set(a,f);return f};e.prototype.info=function(){if(this.b)for(var a=0;a<this.b;a++)this.j[a].postMessage(a,{info:!0,id:this.id});else{for(var b,c,f=0,d=0,e=0,g=0;10>g;g++)for(b=Object.keys(this.g[g]),a=0;a<b.length;a++)c=this.g[g][b[a]].length,f+=c+2*b[a].length+4,d+=c,e+=2*b[a].length;b=Object.keys(this.a);c=b.length;
for(a=0;a<c;a++)f+=2*b[a].length+2;return{id:this.id,memory:f,items:c,sequences:d,chars:e,status:this.u,cache:this.c.length,matcher:v.length,worker:this.b}}};e.prototype.reset=function(){this.destroy();return this.init()};e.prototype.destroy=function(){this.cache&&this.l.reset();this.g=this.a=this.l=null;return this};var A={icase:function(a){return a.toLowerCase()},simple:function(){var a=[d("[\u00e0\u00e1\u00e2\u00e3\u00e4\u00e5]"),"a",d("[\u00e8\u00e9\u00ea\u00eb]"),"e",d("[\u00ec\u00ed\u00ee\u00ef]"),
"i",d("[\u00f2\u00f3\u00f4\u00f5\u00f6\u0151]"),"o",d("[\u00f9\u00fa\u00fb\u00fc\u0171]"),"u",d("[\u00fd\u0177\u00ff]"),"y",d("\u00f1"),"n",d("\u00e7"),"c",d("\u00df"),"s",d("[-/]")," ",d("[^a-z0-9 ]"),""];return function(b){return q(b.toLowerCase(),a)}}(),advanced:function(){var a=[d("ae"),"a",d("ai"),"ei",d("ay"),"ei",d("ey"),"ei",d("oe"),"o",d("ue"),"u",d("ie"),"i",d("sz"),"s",d("zs"),"s",d("sh"),"s",d("ck"),"k",d("cc"),"k",d("dt"),"t",d("ph"),"f",d("pf"),"f",d("ou"),"o",d("uo"),"u"];return function(b,
c){if(!b)return b;b=this.simple(b);2<b.length&&(b=q(b,a));c||1<b.length&&(b=w(b));return b}}(),extra:function(){var a=[d("p"),"b",d("z"),"s",d("[cgq]"),"k",d("n"),"m",d("d"),"t",d("[vw]"),"f",d("[aeiouy]"),""];return function(b){if(!b)return b;b=this.advanced(b,!0);if(1<b.length){b=b.split(" ");for(var c=0;c<b.length;c++){var f=b[c];1<f.length&&(b[c]=f[0]+q(f.substring(1),a))}b=b.join("");b=w(b)}return b}}(),ngram:function(a){var b=[];if(!a)return b;a=this.advanced(a);if(!a)return b;for(var c=0,f=
0,d=-1,e="",g=a.length,l=0;l<g;l++){var k=a[l];"a"===k||"e"===k||"i"===k||"o"===k||"u"===k||"y"===k?c++:f++;" "!==k&&(e+=k);if(" "===k||2<=c&&2<=f||l===g-1){if(e){c=e.length;if(2<c||" "===k||l===g-1){if(k=e.charCodeAt(0),1<c||48<=k||57>=k)b[++d]=e}else b[d]&&(b[d]+=e);e=""}f=c=0}}return b}},D=function(){var a={};return function(b,c,d){var e=a[d];e&&clearTimeout(e);return a[d]=setTimeout(b,c)}}(),L=function(){function a(){this.cache={}}a.prototype.reset=function(){this.cache={}};a.prototype.set=function(a,
c){this.cache[a]=c};a.prototype.get=function(a){return this.cache[a]};return a}();return e}(function(){var l={},C=!("undefined"===typeof Blob||"undefined"===typeof URL||!URL.createObjectURL);return function(e,d,q,r,w){var x=e;e=C?URL.createObjectURL(new Blob(["("+q.toString()+")()"],{type:"text/javascript"})):"../"+x+".js";x+="-"+d;l[x]||(l[x]=[]);l[x][w]=new Worker(e);l[x][w].onmessage=r;return{postMessage:function(d,e){l[x][d].postMessage(e)}}}}()),this);
{
"name": "flexsearch",
"version": "0.1.29",
"version": "0.2.0",
"description": "Superfast, lightweight and memory efficient full text search library.",

@@ -5,0 +5,0 @@ "keywords": [],

@@ -40,2 +40,3 @@ <p align="center">

<li>Relevance-based Scoring</li>
<li>Contextual Indexes</li>
<li>Limit Results</li>

@@ -47,4 +48,17 @@ <li>Caching</li>

</ul>
<a name="contextual"></a>
#### Contextual Search
FlexSearch introduce a new scoring mechanism called __Contextual Search__ which __incredibly boost up queries to a new level__.
The basic idea of this concept is to limit relevance by context instead of calculating relevance through the whole text distance.
Imagine you add a short text block of some sentences to an index ID. Assuming the query includes a combination of first and last word from this text block, are they really relevant to each other?
In this way contextual search also improves the results of relevance-based queries on large amount of text data.
<img src="https://raw.githubusercontent.com/nextapps-de/flexsearch/master/contextual_index.svg">
<br>
__Note:__ This feature is actually not enabled by default.
<a name="webworker"></a>
#### Web-Worker Support

@@ -96,2 +110,8 @@

<tr>
<td>Contextual Index</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr></tr>
<tr>
<td>WebWorker</td>

@@ -482,2 +502,11 @@ <td>No</td>

</tr>
<tr></tr>
<tr>
<td align="top">depth<br><br></td>
<td>
false<br>
{number}
</td>
<td>Enable/Disable contextual indexing and also sets relevance depth (experimental).<br><br></td>
</tr>
</table>

@@ -484,0 +513,0 @@

@@ -822,2 +822,41 @@ if(typeof module !== 'undefined'){

// ------------------------------------------------------------------------
// Contextual Indexing
// ------------------------------------------------------------------------
describe('Context', function(){
it('Should have been added properly to the context', function(){
var flexsearch_depth = new FlexSearch({
encode: 'icase',
mode: 'strict',
depth: 2,
async: false,
worker: false
});
flexsearch_depth.add(0, "zero one two three four five six seven eight nine ten");
expect(flexsearch_depth.length).to.equal(1);
expect(flexsearch_depth.search("zero one")).to.include(0);
expect(flexsearch_depth.search("zero two")).to.include(0);
expect(flexsearch_depth.search("zero three").length).to.equal(0);
expect(flexsearch_depth.search("three seven").length).to.equal(0);
expect(flexsearch_depth.search("three five seven")).to.include(0);
// TODO
// expect(flexsearch_depth.search("three seven five")).to.include(0);
expect(flexsearch_depth.search("three foobar seven").length).to.equal(0);
expect(flexsearch_depth.search("eight ten")).to.include(0);
expect(flexsearch_depth.search("seven ten").length).to.equal(0);
flexsearch_depth.add(1, "1 2 3 1 4 2 5 1");
expect(flexsearch_depth.search("1")).to.include(1);
expect(flexsearch_depth.search("1 5")).to.include(1);
expect(flexsearch_depth.search("2 4 1")).to.include(1);
});
});
// ------------------------------------------------------------------------
// Encoding Tests

@@ -824,0 +863,0 @@ // ------------------------------------------------------------------------

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc