mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-09-09 13:41:13 +02:00
replace "indexOf" with "includes"
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
<!-- TODO External Stemmer Lib -->
|
||||
|
||||
<h1 align="center">
|
||||
<p></p>
|
||||
<img src="https://cdn.jsdelivr.net/gh/nextapps-de/flexsearch@master/doc/flexsearch-logo-glass.svg?v2" alt="FlexSearch.js: Next-Generation full text search library for Browser and Node.js">
|
||||
|
@@ -160,7 +160,7 @@ CacheClass.prototype.del = function(id){
|
||||
key = this.queue[i];
|
||||
item = this.cache[key];
|
||||
|
||||
if(item.indexOf(id) !== -1){
|
||||
if(item.includes(id)){
|
||||
|
||||
this.queue.splice(i--, 1);
|
||||
delete this.cache[key];
|
||||
|
@@ -58,6 +58,8 @@ function Document(options){
|
||||
|
||||
if(SUPPORT_TAGS){
|
||||
|
||||
// TODO case-insensitive tags
|
||||
|
||||
this.tag = ((opt = document["tag"]) && parse_tree(opt, this.marker));
|
||||
this.tagindex = opt && create_object();
|
||||
}
|
||||
@@ -185,6 +187,8 @@ function parse_tree(key, marker){
|
||||
return count > 1 ? tree : tree[0];
|
||||
}
|
||||
|
||||
// TODO support generic function created from string when tree depth > 1
|
||||
|
||||
function parse_simple(obj, tree){
|
||||
|
||||
if(is_string(tree)){
|
||||
@@ -202,6 +206,8 @@ function parse_simple(obj, tree){
|
||||
return obj;
|
||||
}
|
||||
|
||||
// TODO support generic function created from string when tree depth > 1
|
||||
|
||||
function store_value(obj, store, tree, pos, key){
|
||||
|
||||
obj = obj[key];
|
||||
@@ -345,7 +351,7 @@ Document.prototype.add = function(id, content, _append){
|
||||
dupes[key] = 1;
|
||||
arr = this.tagindex[key] || (this.tagindex[key] = []);
|
||||
|
||||
if(!_append || (arr.indexOf(id) === -1)){
|
||||
if(!_append || !arr.includes(id)){
|
||||
|
||||
arr[arr.length] = id;
|
||||
|
||||
|
14
src/index.js
14
src/index.js
@@ -117,6 +117,10 @@ Index.prototype.append = function(id, content){
|
||||
return this.add(id, content, true);
|
||||
};
|
||||
|
||||
// TODO:
|
||||
// string + number as text
|
||||
// boolean, null, undefined as ?
|
||||
|
||||
/**
|
||||
* @param {!number|string} id
|
||||
* @param {!string} content
|
||||
@@ -133,7 +137,7 @@ Index.prototype.add = function(id, content, _append, _skip_update){
|
||||
return this.update(id, content);
|
||||
}
|
||||
|
||||
content = this.encode(content);
|
||||
content = this.encode("" + content);
|
||||
const length = content.length;
|
||||
|
||||
if(length){
|
||||
@@ -353,7 +357,7 @@ Index.prototype.push_index = function(dupes, value, score, id, append, keyword){
|
||||
arr = arr[score] || (arr[score] = []);
|
||||
}
|
||||
|
||||
if(!append || (arr.indexOf(id) === -1)){
|
||||
if(!append || !arr.includes(id)){
|
||||
|
||||
arr[arr.length] = id;
|
||||
|
||||
@@ -396,16 +400,16 @@ Index.prototype.search = function(query, limit, options){
|
||||
|
||||
if(options){
|
||||
|
||||
query = options["query"] || query;
|
||||
limit = options["limit"];
|
||||
offset = options["offset"] || 0;
|
||||
context = options["context"];
|
||||
suggest = SUPPORT_SUGGESTION && options["suggest"];
|
||||
query = options["query"] || query;
|
||||
}
|
||||
|
||||
if(query){
|
||||
|
||||
query = /** @type {Array} */ (this.encode(query));
|
||||
query = /** @type {Array} */ (this.encode("" + query));
|
||||
length = query.length;
|
||||
|
||||
// TODO: solve this in one single loop below
|
||||
@@ -421,7 +425,7 @@ Index.prototype.search = function(query, limit, options){
|
||||
|
||||
if(term && (term.length >= this.minlength) && !dupes[term]){
|
||||
|
||||
// this fast path just could applied when not in memory-optimized mode
|
||||
// this fast path can just apply when not in memory-optimized mode
|
||||
|
||||
if(!this.optimize && !suggest && !this.map[term]){
|
||||
|
||||
|
@@ -1,7 +1,7 @@
|
||||
import { create_object, concat } from "./common.js";
|
||||
|
||||
/**
|
||||
* Implementation based on Array.indexOf() provides better performance,
|
||||
* Implementation based on Array.includes() provides better performance,
|
||||
* but it needs at least one word in the query which is less frequent.
|
||||
* Also on large indexes it does not scale well performance-wise.
|
||||
* This strategy also lacks of suggestion capabilities (matching & sorting).
|
||||
@@ -97,7 +97,7 @@ import { create_object, concat } from "./common.js";
|
||||
//
|
||||
// if(arr.length){
|
||||
//
|
||||
// found = arr.indexOf(id) !== -1;
|
||||
// found = arr.includes(id);
|
||||
//
|
||||
// if(found){
|
||||
//
|
||||
@@ -280,7 +280,8 @@ export function intersect(arrays, limit, offset, suggest) {
|
||||
|
||||
if(suggest){
|
||||
|
||||
check_suggest[id] = (check_idx = check_suggest[id]) ? ++check_idx : check_idx = 1;
|
||||
check_idx = (check_suggest[id] || 0) + 1;
|
||||
check_suggest[id] = check_idx;
|
||||
|
||||
// do not adding IDs which are already included in the result (saves one loop)
|
||||
// the first intersection match has the check index 2, so shift by -2
|
||||
@@ -373,7 +374,7 @@ export function intersect_union(mandatory, arrays) {
|
||||
check[mandatory[x]] = 1;
|
||||
}
|
||||
|
||||
for(let x = 0, arr; x < arrays.length; x++){
|
||||
for(let x = 0, arr; x < arrays.length; x++){
|
||||
|
||||
arr = arrays[x];
|
||||
|
||||
|
@@ -45,6 +45,8 @@ export function pipeline(str, normalize, split, _collapse){
|
||||
return str;
|
||||
}
|
||||
|
||||
// TODO improve normalize + remove non-delimited chars like in "I'm" + split on whitespace+
|
||||
|
||||
export const regex_whitespace = /[\p{Z}\p{S}\p{P}\p{C}]+/u;
|
||||
const regex_normalize = /[\u0300-\u036f]/g;
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
// TODO return promises instead of inner await
|
||||
|
||||
import { IndexInterface, DocumentInterface } from "./type.js";
|
||||
import { create_object, is_string } from "./common.js";
|
||||
|
||||
|
Reference in New Issue
Block a user