Skip to content

Commit

Permalink
replace "indexOf" with "includes"
Browse files Browse the repository at this point in the history
  • Loading branch information
ts-thomas committed Oct 3, 2022
1 parent 1298807 commit 811b7e1
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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">
Expand Down
2 changes: 1 addition & 1 deletion src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
8 changes: 7 additions & 1 deletion src/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)){
Expand All @@ -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];
Expand Down Expand Up @@ -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;

Expand Down
14 changes: 9 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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){
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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]){

Expand Down
9 changes: 5 additions & 4 deletions src/intersect.js
Original file line number Diff line number Diff line change
@@ -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).
Expand Down Expand Up @@ -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){
//
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];

Expand Down
2 changes: 2 additions & 0 deletions src/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 2 additions & 0 deletions src/serialize.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down

0 comments on commit 811b7e1

Please sign in to comment.