Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modernize codebase #343

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
202 changes: 63 additions & 139 deletions index.d.ts
@@ -1,147 +1,71 @@
declare module "flexsearch" {
export interface Index<T> {
readonly id: string;
readonly index: string;
readonly length: number;

init(options?: CreateOptions): this;
info(): {
id: any;
items: any;
cache: any;
matcher: number;
worker: any;
threshold: any;
depth: any;
resolution: any;
contextual: boolean;
};
add(o: T): this;
add(id: number, o: string): this;

// Result without pagination -> T[]
search(
query: string,
options: number | SearchOptions,
callback: (results: T[]) => void
): void;
search(query: string, options?: number | SearchOptions): Promise<T[]>;
search(
options: SearchOptions & { query: string },
callback: (results: T[]) => void
): void;
search(options: SearchOptions & { query: string }): Promise<T[]>;

// Result with pagination -> SearchResults<T>
search(
query: string,
options: number | (SearchOptions & { page?: boolean | Cursor }),
callback: (results: SearchResults<T>) => void
): void;
search(
query: string,
options?: number | (SearchOptions & { page?: boolean | Cursor })
): Promise<SearchResults<T>>;
search(
options: SearchOptions & { query: string; page?: boolean | Cursor },
callback: (results: SearchResults<T>) => void
): void;
search(
options: SearchOptions & { query: string; page?: boolean | Cursor }
): Promise<SearchResults<T>>;

update(id: number, o: T): this;
remove(id: number): this;
clear(): this;
destroy(): this;
addMatcher(matcher: Matcher): this;

where(whereObj: { [key: string]: string } | ((o: T) => boolean)): T[];
encode(str: string): string;
export(
callback: (key: string, data: any) => any,
self?: this,
field?: string,
index_doc?: Number,
index?: Number
): Promise<boolean>;
import(exported: string): this;
}
interface SearchOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this file so much smaller now? Is it just because of formatting? It's hard to tell what changed

limit?: number;
suggest?: boolean;
where?: { [key: string]: string };
field?: string | string[];
bool?: 'and' | 'or' | 'not';
}

interface SearchOptions {
limit?: number;
suggest?: boolean;
where?: { [key: string]: string };
field?: string | string[];
bool?: "and" | "or" | "not";
//TODO: Sorting
}
interface SearchResults<T> {
page?: string;
next?: string;
result: T[];
}

interface SearchResults<T> {
page?: Cursor;
next?: Cursor;
result: T[];
}
type DefaultEncoder = 'icase' | 'simple' | 'advanced' | 'extra' | 'balance';
type DefaultTokenizer = 'strict' | 'forward' | 'reverse' | 'full';
type EncoderFunction = (str: string) => string;
type FilterFunction = (str: string) => boolean;
type TokenizerFunction = (str: string) => string[];

interface Document {
id: string;
field: any;
}
export interface CreateOptions {
async?: boolean;
boost?: any; // TODO: what?
cache?: number | boolean;
charset?: string;
context?: {
bidirectional?: boolean;
depth?: number;
resolution?: any; // TODO: what?
};
depth?: number | false;
doc?: { id: string; field: any };
encode?: false | DefaultEncoder | ((input: string | number) => ThisType);
fastupdate?: any; // TODO: what?
filter?: string | false | FilterFunction;
lang?: string; // TODO: define lang
matcher?: any; // TODO: what?
minlength?: number;
optimize?: boolean;
profile?: 'memory' | 'speed' | 'match' | 'score' | 'balance' | 'fast';
resolution?: number;
rtl?: boolean;
split?: RegExp;
stemmer?: string | false | { [key: string]: string };
threshold?: number | false;
tokenize?: DefaultTokenizer | TokenizerFunction;
worker?: number | false;
}

export type CreateOptions = {
profile?: IndexProfile;
tokenize?: DefaultTokenizer | TokenizerFn;
split?: RegExp;
encode?: DefaultEncoder | EncoderFn | false;
cache?: boolean | number;
async?: boolean;
worker?: false | number;
depth?: false | number;
threshold?: false | number;
resolution?: number;
stemmer?: Stemmer | string | false;
filter?: FilterFn | string | false;
rtl?: boolean;
doc?: Document;
};
export default class FlexSearch {
constructor(options?: CreateOptions);

// limit number Sets the limit of results.
// suggest true, false Enables suggestions in results.
// where object Use a where-clause for non-indexed fields.
// field string, Array<string> Sets the document fields which should be searched. When no field is set, all fields will be searched. Custom options per field are also supported.
// bool "and", "or" Sets the used logical operator when searching through multiple fields.
// page true, false, cursor Enables paginated results.
readonly id: string;
readonly index: string;
readonly length: number;

type IndexProfile =
| "memory"
| "speed"
| "match"
| "score"
| "balance"
| "fast";
type DefaultTokenizer = "strict" | "forward" | "reverse" | "full";
type TokenizerFn = (str: string) => string[];
type DefaultEncoder = "icase" | "simple" | "advanced" | "extra" | "balance";
type EncoderFn = (str: string) => string;
type Stemmer = { [key: string]: string };
type Matcher = { [key: string]: string };
type FilterFn = (str: string) => boolean;
type Cursor = string;
static encode(name: string, str: string): string;
static registerLanguage(
lang: string,
options: {
stemmer?: { [key: string]: string };
filter?: string[];
}
): typeof FlexSearch;

export default class FlexSearch {
static create<T>(options?: CreateOptions): Index<T>;
static registerMatcher(matcher: Matcher): typeof FlexSearch;
static registerEncoder(name: string, encoder: EncoderFn): typeof FlexSearch;
static registerLanguage(
lang: string,
options: { stemmer?: Stemmer; filter?: string[] }
): typeof FlexSearch;
static encode(name: string, str: string): string;
}
add(id: number, content: string): this;
append(id: number, content: string): this;
remove(id: number): this;
search(query: string, limit: number, options: SearchOptions): this;
update(id: number, content: string): this;
}

// FlexSearch.create(<options>)
// FlexSearch.registerMatcher({KEY: VALUE})
// FlexSearch.registerEncoder(name, encoder)
// FlexSearch.registerLanguage(lang, {stemmer:{}, filter:[]})
// FlexSearch.encode(name, string)
8 changes: 7 additions & 1 deletion package.json
Expand Up @@ -44,7 +44,9 @@
"build:module": "npx babel src -d dist/module && exit 0",
"build:all": "npm run build && npm run build:light && npm run build:compact && npm run build:es5 && npm run build:debug && npm run build:module",
"test": "cd test && npm install && npm run test",
"server": "node task/server"
"server": "node task/server",
"format": "prettier --write src",
"check:style": "prettier --check src"
},
"files": [
"dist/**",
Expand All @@ -56,6 +58,7 @@
"LICENSE"
],
"readme": "README.md",
"prettier": "mauss/prettier.json",
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-plugin-minify-constant-folding": "^0.5.0",
Expand All @@ -75,7 +78,10 @@
"babel-plugin-transform-undefined-to-void": "^6.9.4",
"cpx": "^1.5.0",
"google-closure-compiler": "^20220905.0.0",
"mauss": "^0.3.3",
"prettier": "^2.7.1",
"shx": "^0.3.3",
"typescript": "^4.8.4",
"web-servo": "^0.5.1"
}
}