Skip to content

Commit

Permalink
Update types for CreateOptions
Browse files Browse the repository at this point in the history
update types
  • Loading branch information
ryanyue123 authored Oct 12, 2021
2 parents 45747b7 + 260650a commit c93d5d6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
12 changes: 8 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ export class Document<T> {
options?: SearchOptions
): any // The shape of the resulting object can vary widely,
// so we will put off typing it for now
search(
query: string,
options?: SearchOptions
): Promise<any> // The shape of the resulting object can vary widely,

contain(id: any): boolean;

// TODO add async methods
Expand All @@ -66,6 +63,7 @@ interface SearchOptions {
bool?: "and" | "or" | "not";
query?: string;
enrich?: boolean;
context?: boolean;
//TODO: Sorting
}

Expand Down Expand Up @@ -104,6 +102,12 @@ export type CreateOptions = {
filter?: FilterFn | string | false;
rtl?: boolean;
document?: CreateDocumentOptions;
context?: {
bidirectional?: boolean;
resolution?: number;
depth?: number
}
minlength?: number;
};

// limit number Sets the limit of results.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@skiff-org/trawler",
"version": "0.1.2",
"version": "0.1.6",
"description": "A modern search library for Skiff",
"homepage": "https://github.com/skiff-org/trawler/",
"author": "Thomas Wilkerling",
Expand Down
32 changes: 7 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { encode as default_encoder } from './lang/latin/default.js';
import { create_object, create_object_array, concat, sort_by_length_down, is_array, is_object, parse_option } from './common.js';
import { create_object, create_object_array, concat, sort_by_length_down, is_array, parse_option } from './common.js';
import { init_stemmer_or_matcher, init_filter } from './lang.js';
import apply_async from './async.js';
import { intersect } from './intersect.js';
Expand Down Expand Up @@ -212,24 +212,14 @@ export class Index {
}
/**
* @param {string|Object} query
* @param {number|Object=} limit
* @param {Object=} options
* @returns {Array<number|string>}
*/
search(query, limit, options) {
if (!options) {
if (!limit && is_object(query)) {
options = /** @type {Object} */ (query);
query = options.query;
}
else if (is_object(limit)) {
options = /** @type {Object} */ (limit);
}
}

search(query, options) {
let result = [];
let length;
let context, suggest, offset = 0;
let limit = 100;

if (options) {
limit = options.limit;
Expand Down Expand Up @@ -272,19 +262,14 @@ export class Index {
return result;
}

limit || (limit = 100);

let depth = this.depth && (length > 1) && (context !== false);
let index = 0, keyword;

if (depth) {
keyword = query[0];
index = 1;
}
else {
if (length > 1) {
query.sort(sort_by_length_down);
}
} else if (length > 1) {
query.sort(sort_by_length_down);
}

for (let arr, term; index < length; index++) {
Expand All @@ -303,8 +288,7 @@ export class Index {
if (!suggest || (arr !== false) || !result.length) {
keyword = term;
}
}
else {
} else {
arr = this.add_result(result, suggest, limit, offset, length === 1, term);
}

Expand All @@ -321,13 +305,11 @@ export class Index {
// fallback to non-contextual search when no result was found
depth = 0;
index = -1;

continue;
}

return result;
}
else if (length === 1) {
} else if (length === 1) {
// fast path optimization
return single_result(result[0], limit, offset);
}
Expand Down

0 comments on commit c93d5d6

Please sign in to comment.