From 66a215ad59192db2833fde40b37aaec8b2760cb7 Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Thu, 20 May 2021 14:04:29 +0200 Subject: [PATCH] fix circular module dependencies --- dist/flexsearch.min.js | 40 +++++++++++++------------- doc/0.7.0.md | 12 +------- src/async.js | 2 -- src/cache.js | 5 ++-- src/document.js | 4 ++- src/engine.js | 2 -- src/index.js | 6 ++-- src/lang.js | 4 +-- src/lang/arabic/default.js | 6 ---- src/lang/cjk/default.js | 6 ---- src/lang/cyrillic/default.js | 6 ---- src/lang/latin/advanced.js | 2 +- src/lang/latin/extra.js | 2 +- src/{presets.js => preset.js} | 10 +++---- src/serialize.js | 11 ++++--- src/type.js | 54 +++++++++++++++++++++++++++++++++++ 16 files changed, 98 insertions(+), 74 deletions(-) rename src/{presets.js => preset.js} (87%) create mode 100644 src/type.js diff --git a/dist/flexsearch.min.js b/dist/flexsearch.min.js index 2db82f7..86f0c83 100644 --- a/dist/flexsearch.min.js +++ b/dist/flexsearch.min.js @@ -1,31 +1,31 @@ /**! - * FlexSearch.js v0.7.0-beta (Bundle) + * FlexSearch.js v0.7.0-beta * Copyright 2019 Nextapps GmbH * Author: Thomas Wilkerling * Licence: Apache-2.0 * https://github.com/nextapps-de/flexsearch */ -(function(){'use strict';var t;function aa(a){function b(c){return a.next(c)}function d(c){return a.throw(c)}return new Promise(function(c,e){function f(g){g.done?c(g.value):Promise.resolve(g.value).then(b,d).then(f,e)}f(a.next())})};const v=/[\W_]+/;function ba(a){if(a=a.toLowerCase())if(a&&this.B&&(a=ca(a,this.B)),this.F&&1=b)return f.concat(l.slice(0,b-h));f=f.concat(l);h+=r}return f} -;function N(a){this.h=!0!==a&&a;this.cache=A();this.g=[]}function la(a,b,d){G(a)&&(a=a.query);let c=this.cache.get(a);c||(c=this.search(a,b,d),this.cache.set(a,c));return c}N.prototype.set=function(a,b){if(!this.cache[a]){var d=this.g.length;d===this.h?delete this.cache[this.g[d-1]]:d++;for(--d;0=e&&(f=e-1);this.s=e;this.threshold=f;this.u=b=d&&d.I||a.tokenize||"strict";this.depth="strict"===b&&h.depth;this.v=y(h.bidirectional,!0);this.l=g="memory"=== -a.optimize;this.i=y(a.fastupdate,!0);this.o=a.minlength||1;this.g=g?da(e-f):A();e=h.resolution||e;f=h.threshold||f;f>=e&&(f=e-1);this.j=e;this.m=f;this.h=g?da(e-f):A();this.D=d&&d.D||a.rtl;this.B=(b=a.matcher||c&&c.B)&&fa(b,!1);this.F=(b=a.stemmer||c&&c.F)&&fa(b,!0);if(d=b=a.filter||c&&c.filter){d=b;c=A();for(let k=0,l=d.length;k=this.o&&(r||!u[p])){var e=Math.min(this.s/l*x|0,x);if(eg;k--)f=p.substring(g,k),f.length>=this.o&&P(this,u,f,h,a,d)}break}case "reverse":if(2< -c){for(g=c-1;0=this.o&&P(this,u,f,e,a,d);f=""}case "forward":if(1=this.o&&P(this,u,f,e,a,d);break;default:if(P(this,u,p,e,a,d),r&&1=this.o&&!e[p]){if(e[p]=1,k=Math.min((this.j-g)/l*x+h|0,x+(h-1)),kf;P(this,m,z?f:p,k,a,d,z?p:f)}}else g=Math.min(g+1,l-x)}}}}this.i||(this.register[a]=1)}}return this}; -function P(a,b,d,c,e,f,g){let h=g?a.h:a.g;if(!b[d]||g&&!b[d][g])a.l&&(h=h[c]),g?(b[d]||(b[d]=A()),b[d][g]=1,h=h[g]||(h[g]=A())):b[d]=1,h=h[d]||(h[d]=[]),a.l||(h=h[c]||(h[c]=[])),f&&-1!==h.indexOf(e)||(h[h.length]=e,a.i&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=h))} -t.search=function(a,b,d){G(a)?(d=a,a=d.query):G(b)&&(d=b);let c=[],e;var f=this.threshold;let g,h=0;if(d){b=d.limit;h=d.offset||0;f=y(d.threshold,f);var k=d.context;g=d.suggest}if(a&&(a=this.encode(a),e=a.length,1=this.o&&!d[m])if(this.l||g||this.g[m])l[u++]=m,d[m]=1;else return c;a=l;e=a.length}if(!e)return c;b||(b=100);d=this.s-f;f=this.j-f;k=this.depth&&1=b)return f.concat(l.slice(0,b-h));f=f.concat(l);h+=r}return f} +;function N(a){this.h=!0!==a&&a;this.cache=A();this.g=[]}function la(a,b,d){G(a)&&(a=a.query);let c=this.cache.get(a);c||(c=this.search(a,b,d),this.cache.set(a,c));return c}N.prototype.set=function(a,b){if(!this.cache[a]){var d=this.g.length;d===this.h?delete this.cache[this.g[d-1]]:d++;for(--d;0=e&&(f=e-1);this.o=e;this.threshold=f;this.s=b=d&&d.H||a.tokenize||"strict";this.depth="strict"===b&&h.depth;this.u=y(h.bidirectional,!0);this.j=g="memory"=== +a.optimize;this.i=y(a.fastupdate,!0);this.m=a.minlength||1;this.map=g?da(e-f):A();e=h.resolution||e;f=h.threshold||f;f>=e&&(f=e-1);this.h=e;this.l=f;this.g=g?da(e-f):A();this.C=d&&d.C||a.rtl;this.B=(b=a.matcher||c&&c.B)&&fa(b,!1);this.D=(b=a.stemmer||c&&c.D)&&fa(b,!0);if(d=b=a.filter||c&&c.filter){d=b;c=A();for(let k=0,l=d.length;k=this.m&&(r||!u[p])){var e=Math.min(this.o/l*x|0,x);if(eg;k--)f=p.substring(g,k),f.length>=this.m&&P(this,u,f,h,a,d)}break}case "reverse":if(2< +c){for(g=c-1;0=this.m&&P(this,u,f,e,a,d);f=""}case "forward":if(1=this.m&&P(this,u,f,e,a,d);break;default:if(P(this,u,p,e,a,d),r&&1=this.m&&!e[p]){if(e[p]=1,k=Math.min((this.h-g)/l*x+h|0,x+(h-1)),kf;P(this,m,z?f:p,k,a,d,z?p:f)}}else g=Math.min(g+1,l-x)}}}}this.i||(this.register[a]=1)}}return this}; +function P(a,b,d,c,e,f,g){let h=g?a.g:a.map;if(!b[d]||g&&!b[d][g])a.j&&(h=h[c]),g?(b[d]||(b[d]=A()),b[d][g]=1,h=h[g]||(h[g]=A())):b[d]=1,h=h[d]||(h[d]=[]),a.j||(h=h[c]||(h[c]=[])),f&&-1!==h.indexOf(e)||(h[h.length]=e,a.i&&(a=a.register[e]||(a.register[e]=[]),a[a.length]=h))} +t.search=function(a,b,d){G(a)?(d=a,a=d.query):G(b)&&(d=b);let c=[],e;var f=this.threshold;let g,h=0;if(d){b=d.limit;h=d.offset||0;f=y(d.threshold,f);var k=d.context;g=d.suggest}if(a&&(a=this.encode(a),e=a.length,1=this.m&&!d[m])if(this.j||g||this.map[m])l[u++]=m,d[m]=1;else return c;a=l;e=a.length}if(!e)return c;b||(b=100);d=this.o-f;f=this.h-f;k=this.depth&&1b?c.slice(0,b):c}}return ka(c,b,h,g)};function pa(a,b,d,c){d?(c=c&&b>d,a=(a=a[c?b:d])&&a[c?d:b]):a=a[b];return a} -function oa(a,b,d,c,e,f,g,h){let k=[],l=h?a.h:a.g;a.l||(l=pa(l,g,h,a.v));if(l){let r=0;c=Math.min(l.length,c);for(let q=0,u=0,m;q=e)));q++);if(r){if(f)return k=1===r?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;b[b.length]=k;return}}return!d&&k}t.contain=function(a){return!!this.register[a]};t.update=function(a,b){return this.remove(a).add(a,b)}; -t.remove=function(a,b){const d=this.register[a];if(d){if(this.i)for(let c=0,e;c=e)));q++);if(r){if(f)return k=1===r?k[0]:[].concat.apply([],k),k.length>e?k.slice(0,e):k;b[b.length]=k;return}}return!d&&k}t.contain=function(a){return!!this.register[a]};t.update=function(a,b){return this.remove(a).add(a,b)}; +t.remove=function(a,b){const d=this.register[a];if(d){if(this.i)for(let c=0,e;cb||q)g=g.slice(q,q+b);h&&(g=sa.call(c,g)); -n={tag:f,result:g}}else n=void 0;n&&(e[e.length]=n,u++)}return u?e:[]}m||(m=c.g);r=r&&(1} */ diff --git a/src/document.js b/src/document.js index 9659ba1..179ca82 100644 --- a/src/document.js +++ b/src/document.js @@ -18,6 +18,7 @@ import { } from "./config.js"; import Index from "./index.js"; +import { DocumentInterface } from "./type.js"; import Cache, { searchCache } from "./cache.js"; import { create_object, is_array, is_string, is_object, parse_option } from "./common.js"; import apply_async from "./async.js"; @@ -26,9 +27,10 @@ import { exportDocument, importDocument } from "./serialize.js"; import WorkerAdapter from "./adapter.js"; /** + * @constructor + * @implements DocumentInterface * @param {Object=} options * @return {Document} - * @constructor */ function Document(options){ diff --git a/src/engine.js b/src/engine.js index cc70df9..57da76b 100644 --- a/src/engine.js +++ b/src/engine.js @@ -1,5 +1,3 @@ -import { addAsync, appendAsync, removeAsync, searchAsync, updateAsync } from "./async.js"; -import Document from "./document"; import { DEBUG, SUPPORT_ASYNC, SUPPORT_CACHE } from "./config"; import { searchCache } from "./cache"; diff --git a/src/index.js b/src/index.js index 1b52f0d..0ad00ce 100644 --- a/src/index.js +++ b/src/index.js @@ -16,6 +16,7 @@ import { } from "./config.js"; +import { IndexInterface } from "./type.js"; import { encode as default_encoder } from "./lang/latin/default.js"; import { create_object, create_object_array, concat, sort_by_length_down, is_array, is_string, is_object, parse_option } from "./common.js"; import { pipeline, init_stemmer_or_matcher, init_filter } from "./lang.js"; @@ -23,14 +24,15 @@ import { global_lang, global_charset } from "./global.js"; import apply_async from "./async.js"; import { intersect } from "./intersect.js"; import Cache, { searchCache } from "./cache.js"; -import apply_preset from "./presets.js"; +import apply_preset from "./preset.js"; import { exportIndex, importIndex } from "./serialize.js"; /** + * @constructor + * @implements IndexInterface * @param {Object=} options * @param {Object=} _register * @return {Index} - * @constructor */ function Index(options, _register){ diff --git a/src/lang.js b/src/lang.js index 45e0aa6..b607a15 100644 --- a/src/lang.js +++ b/src/lang.js @@ -1,4 +1,4 @@ -import Index from "./index.js"; +import { IndexInterface, DocumentInterface } from "./type.js"; import { create_object } from "./common.js"; /** @@ -7,7 +7,7 @@ import { create_object } from "./common.js"; * @param {boolean|string|RegExp=} split * @param {boolean=} _collapse * @returns {string|Array} - * @this Index + * @this IndexInterface */ export function pipeline(str, normalize, split, _collapse){ diff --git a/src/lang/arabic/default.js b/src/lang/arabic/default.js index 91a0726..1ed5765 100644 --- a/src/lang/arabic/default.js +++ b/src/lang/arabic/default.js @@ -1,5 +1,3 @@ -import Document from "../../flexsearch.js"; - export const rtl = true; export const tokenize = ""; export default { @@ -9,10 +7,6 @@ export default { const regex = /[\x00-\x7F]+/g; -/** - * @this Document - */ - export function encode(str){ return this.pipeline( diff --git a/src/lang/cjk/default.js b/src/lang/cjk/default.js index 78cd6ce..b5f3076 100644 --- a/src/lang/cjk/default.js +++ b/src/lang/cjk/default.js @@ -1,5 +1,3 @@ -import Document from "../../flexsearch.js"; - export const rtl = false; export const tokenize = "strict"; export default { @@ -10,10 +8,6 @@ export default { const regex = /[\x00-\x7F]+/g; -/** - * @this Document - */ - export function encode(str){ return this.pipeline( diff --git a/src/lang/cyrillic/default.js b/src/lang/cyrillic/default.js index b3bc8c9..b86b6a6 100644 --- a/src/lang/cyrillic/default.js +++ b/src/lang/cyrillic/default.js @@ -1,5 +1,3 @@ -import Document from "../../flexsearch.js"; - export const rtl = false; export const tokenize = ""; export default { @@ -9,10 +7,6 @@ export default { const regex = /[\x00-\x7F]+/g; -/** - * @this Document - */ - export function encode(str){ return this.pipeline( diff --git a/src/lang/latin/advanced.js b/src/lang/latin/advanced.js index 49090b5..d336ac6 100644 --- a/src/lang/latin/advanced.js +++ b/src/lang/latin/advanced.js @@ -54,7 +54,7 @@ export function encode(str, self, _skip_postprocessing){ if(str){ - str = encode_simple(str, /** @type {Document} */ (self || this)).join(" "); + str = encode_simple(str, self || this).join(" "); if(str.length > 2){ diff --git a/src/lang/latin/extra.js b/src/lang/latin/extra.js index 6311e6f..93fa286 100644 --- a/src/lang/latin/extra.js +++ b/src/lang/latin/extra.js @@ -34,7 +34,7 @@ export function encode(str){ if(str){ - str = encode_advanced(str, /** @type {Document} */ (this), /* skip post-processing: */ true); + str = encode_advanced(str, this, /* skip post-processing: */ true); if(str.length > 1){ diff --git a/src/presets.js b/src/preset.js similarity index 87% rename from src/presets.js rename to src/preset.js index c39f5dc..cbfb6f2 100644 --- a/src/presets.js +++ b/src/preset.js @@ -6,7 +6,7 @@ import { is_string } from "./common.js"; * @const */ -const presets = { +const preset = { "memory": { charset: "latin:extra", @@ -70,12 +70,12 @@ export default function apply_preset(options){ if(is_string(options)){ - if(DEBUG && !presets[options]){ + if(DEBUG && !preset[options]){ console.warn("Preset not found: " + options); } - options = presets[options]; + options = preset[options]; } else{ @@ -83,12 +83,12 @@ export default function apply_preset(options){ if(preset){ - if(DEBUG && !presets[preset]){ + if(DEBUG && !preset[preset]){ console.warn("Preset not found: " + preset); } - options = Object.assign({}, presets[preset], /** @type {Object} */ (options)); + options = Object.assign({}, preset[preset], /** @type {Object} */ (options)); } } diff --git a/src/serialize.js b/src/serialize.js index 33d20db..14a00c6 100644 --- a/src/serialize.js +++ b/src/serialize.js @@ -1,5 +1,4 @@ -import Index from "./index.js"; -import Document from "./document.js"; +import { IndexInterface, DocumentInterface } from "./type.js"; import { create_object, is_string } from "./common.js"; function async(callback, self, key, index_doc, index, data){ @@ -25,7 +24,7 @@ function async(callback, self, key, index_doc, index, data){ } /** - * @this Index + * @this IndexInterface */ export function exportIndex(callback, self, field, index_doc, index){ @@ -89,7 +88,7 @@ export function exportIndex(callback, self, field, index_doc, index){ } /** - * @this Index + * @this IndexInterface */ export function importIndex(key, data){ @@ -132,7 +131,7 @@ export function importIndex(key, data){ } /** - * @this Document + * @this DocumentInterface */ export function exportDocument(callback, self, field, index_doc, index){ @@ -192,7 +191,7 @@ export function exportDocument(callback, self, field, index_doc, index){ } /** - * @this Document + * @this DocumentInterface */ export function importDocument(key, data){ diff --git a/src/type.js b/src/type.js new file mode 100644 index 0000000..10772bd --- /dev/null +++ b/src/type.js @@ -0,0 +1,54 @@ +/** + * @interface + */ + +export function IndexInterface(){ + + this.cache = null; +} + +/** + * @param {!number|string} id + * @param {!string} content + */ + +IndexInterface.prototype.add; + +/** + * @param {!number|string} id + * @param {!string} content + */ + +IndexInterface.prototype.append; + +/** + * @param {!string|Object} query + * @param {number|Object=} limit + * @param {Object=} options + * @returns {Array} + */ + +IndexInterface.prototype.search; + +/** + * @param {!number|string} id + * @param {!string} content + */ + +IndexInterface.prototype.update; + +/** + * @param {!number|string} id + */ + +IndexInterface.prototype.remove; + +/** + * @interface + */ + +export function DocumentInterface(){ + + this.field = null; + this.index = null; +}