diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 134775e..67f9d75 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -39,6 +39,9 @@ jobs: - name: Install dependencies run: pnpm install + - name: TypeScript + run: pnpm tsc + - name: Test run: pnpm test diff --git a/README.md b/README.md index 1e2d8c8..1a89ef7 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,6 @@ Logo by Pravin Unagar from >( hits: idKey === "objectID" ? (response.items as any) - : response.items.map(adaptHit(idKey || "id")), + : response.items.map(adaptHit(idKey || "id")), page: response.pagination.page, hitsPerPage: response.pagination.perPage, nbHits: response.pagination.total, @@ -25,17 +25,17 @@ export function adaptResponse>( } export function adaptHit(key: string) { - return (item: I): Hit => ({ + return (item: any): Hit => ({ objectID: item[key], ...item, }); } -export function adaptFacets(facets): Record> { +export function adaptFacets(facets: any): Record> { const instantsearchFacets = Object.create(null); Object.keys(facets).forEach((field) => { instantsearchFacets[field] = Object.create(null); - facets[field].items.forEach(([value, frequency]) => { + facets[field].items.forEach(([value, frequency]: [string, number]) => { instantsearchFacets[field][value] = frequency; }); }); @@ -44,7 +44,7 @@ export function adaptFacets(facets): Record> { } export function adaptFacetsStats( - facets + facets: any ): Record { const instantsearchFacetsStats = Object.create(null); Object.keys(facets).forEach((field) => { diff --git a/packages/facets-instantsearch/tsconfig.json b/packages/facets-instantsearch/tsconfig.json new file mode 100644 index 0000000..d306be6 --- /dev/null +++ b/packages/facets-instantsearch/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "moduleResolution": "Node" + }, + "include": ["src"] +} diff --git a/packages/facets/package.json b/packages/facets/package.json index 2723c96..3847808 100644 --- a/packages/facets/package.json +++ b/packages/facets/package.json @@ -26,7 +26,8 @@ "prepublishOnly": "npm run build", "build": "rm -rf dist && microbundle", "dev": "microbundle watch", - "clean": "rm -rf dist" + "clean": "rm -rf dist", + "tsc": "tsc" }, "dependencies": { "mnemonist": "^0.39.5", @@ -45,6 +46,7 @@ "minisearch": "^6.2.0", "quick-score": "^0.2.0", "microbundle": "^0.15.1", + "typescript": "^5.2.2", "vitest": "^0.34.6" } } diff --git a/packages/facets/src/Facets.ts b/packages/facets/src/Facets.ts index f59bd9e..6bb0608 100644 --- a/packages/facets/src/Facets.ts +++ b/packages/facets/src/Facets.ts @@ -150,10 +150,14 @@ export type SearchResults> = { }; export class Facets> { + #config: FacetsConfig; + // @ts-expect-error it is assigned later #items: I[]; + // @ts-expect-error it is assigned later #indexes: Record>; - #config: FacetsConfig; + // @ts-expect-error it is assigned later #textIndex: InstanceType; + // @ts-expect-error it is assigned later #fullFacets: Record< string, Array<[SupportedFieldTypes, number, SparseTypedFastBitSet]> @@ -279,7 +283,7 @@ export class Facets> { const to: number = selected.to || Infinity; (this.#indexes[field] as InvertedIndex) .values() - .forEach(([x, y, z]) => { + .forEach(([x, _, z]) => { if (x == null || x > to || x < from) return; if (!ids) { ids = z.clone(); diff --git a/packages/facets/src/ITrieMapIndex.ts b/packages/facets/src/ITrieMapIndex.ts index 11e3f7d..8f8106f 100644 --- a/packages/facets/src/ITrieMapIndex.ts +++ b/packages/facets/src/ITrieMapIndex.ts @@ -10,8 +10,8 @@ export class ITrieMapIndex extends InvertedIndexMaplike { this.index = new TrieMap(); } - like(prefix) { - return this.index.find(prefix).reduce((p, [k, v]) => { + like(prefix: K) { + return this.index.find(prefix).reduce((p, [_, v]) => { p.union(v); return p; }, new SparseTypedFastBitSet()); diff --git a/packages/facets/src/InvertedIndex.ts b/packages/facets/src/InvertedIndex.ts index ea56463..129d913 100644 --- a/packages/facets/src/InvertedIndex.ts +++ b/packages/facets/src/InvertedIndex.ts @@ -3,10 +3,10 @@ import { SparseTypedFastBitSet } from "typedfastbitset"; export type FacetValue = [K, number, SparseTypedFastBitSet]; export class InvertedIndex { - add(value: K, id: number): void { + add(_value: K, _id: number): void { throw new Error("Not implemented"); } - get(value: K): SparseTypedFastBitSet { + get(_value: K): SparseTypedFastBitSet { throw new Error("Not implemented"); } values(): Array> { @@ -22,6 +22,7 @@ interface Maplike { } export class InvertedIndexMaplike extends InvertedIndex { + // @ts-expect-error fix later index: Maplike; add(value: K, id: number) { diff --git a/packages/facets/src/TFlexsearchIndex.ts b/packages/facets/src/TFlexsearchIndex.ts index 1b787e1..2afb0ac 100644 --- a/packages/facets/src/TFlexsearchIndex.ts +++ b/packages/facets/src/TFlexsearchIndex.ts @@ -25,7 +25,7 @@ export class TFlexsearchIndex extends TextIndexBase { this.#index.add(id, item); } - search(query, options?: SearchOptions & TextSearchOptions) { + search(query: string, options?: SearchOptions & TextSearchOptions) { let { page, perPage, ...rest } = options || {}; page = page || 0; perPage = perPage || 20; @@ -33,10 +33,14 @@ export class TFlexsearchIndex extends TextIndexBase { ...rest, offset: 0, limit: perPage * (page + 1), - }); - if (results.length === 1) return results[0].result; - - const resultsByRelevance = new Map(); + }) as Array<{ result: number[] }>; + if (results.length === 1) + return { + ids: results[0].result, + matches: new Map(), + }; + + const resultsByRelevance = new Map(); results.forEach(({ result }) => { const factor = 1; result.forEach((id, i) => { @@ -52,6 +56,7 @@ export class TFlexsearchIndex extends TextIndexBase { .sort((a, b) => a[1] - b[1]) .slice(page * perPage, (page + 1) * perPage) .map((a) => a[0]), + matches: new Map(), }; } } diff --git a/packages/facets/src/TFuseIndex.ts b/packages/facets/src/TFuseIndex.ts index b8636fd..641f543 100644 --- a/packages/facets/src/TFuseIndex.ts +++ b/packages/facets/src/TFuseIndex.ts @@ -16,6 +16,7 @@ export class TFuseIndex extends TextIndexBase { static canHighlight = false; #options: IFuseOptions; + // @ts-expect-error it is assigned later #index: Fuse; constructor({ fields, idKey }: TextIndexBaseOptions) { @@ -30,7 +31,7 @@ export class TFuseIndex extends TextIndexBase { this.#index = new Fuse(items, this.#options); } - search(query, options?: FuseSearchOptions & TextSearchOptions) { + search(query: string, options?: FuseSearchOptions & TextSearchOptions) { const matches = new Map>(); return { ids: this.#index.search(query, options).map((x) => { diff --git a/packages/facets/src/TMinisearchIndex.ts b/packages/facets/src/TMinisearchIndex.ts index 4efc6bc..d238132 100644 --- a/packages/facets/src/TMinisearchIndex.ts +++ b/packages/facets/src/TMinisearchIndex.ts @@ -27,7 +27,7 @@ export class TMinisearchIndex extends TextIndexBase { this.#index.addAll(items); } - search(query, options?: SearchOptions & TextSearchOptions) { + search(query: string, options?: SearchOptions & TextSearchOptions) { const matches = new Map(); console.log() return { diff --git a/packages/facets/src/TQuickscoreIndex.ts b/packages/facets/src/TQuickscoreIndex.ts index 3fe669b..a216669 100644 --- a/packages/facets/src/TQuickscoreIndex.ts +++ b/packages/facets/src/TQuickscoreIndex.ts @@ -9,6 +9,7 @@ export class TQuickscoreIndex extends TextIndexBase { static canHighlight = true; #fields: Array; + // @ts-expect-error it is assigned later #index: QuickScore; #idKey: string; @@ -22,7 +23,7 @@ export class TQuickscoreIndex extends TextIndexBase { this.#index = new QuickScore(items, this.#fields); } - search(query) { + search(query: string) { const matches = new Map>(); return { ids: (this.#index.search(query) as ScoredObject[]).map((x) => { diff --git a/packages/facets/src/TextIndex.ts b/packages/facets/src/TextIndex.ts index f455085..3f6eec5 100644 --- a/packages/facets/src/TextIndex.ts +++ b/packages/facets/src/TextIndex.ts @@ -17,18 +17,18 @@ export class TextIndexBase { static requiresId = false; static canHighlight = false; - constructor(opt: TextIndexBaseOptions) {} + constructor(_opt: TextIndexBaseOptions) {} - search(query: string, options?: TextSearchOptions): TextIndexBaseResults { + search(_query: string, _options?: TextSearchOptions): TextIndexBaseResults { throw new Error("not impelemted"); } - addAll(values: any[]): void { + addAll(_values: any[]): void { throw new Error("not impelemted"); } - addOne(id: number, value: any): void { + addOne(_id: number, _value: any): void { throw new Error("not impelemted"); } - highlight(matches: Map): (value: any) => Record { + highlight(_matches: Map): (value: any) => Record { throw new Error("not impelemted"); } } diff --git a/packages/facets/tsconfig.json b/packages/facets/tsconfig.json new file mode 100644 index 0000000..d306be6 --- /dev/null +++ b/packages/facets/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "moduleResolution": "Node" + }, + "include": ["src"] +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f67a4a9..765f2f0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: turbo: specifier: ^1.10.16 version: 1.10.16 + typescript: + specifier: ^5.2.2 + version: 5.2.2 packages/demo: dependencies: @@ -64,6 +67,9 @@ importers: quick-score: specifier: ^0.2.0 version: 0.2.0 + typescript: + specifier: ^5.2.2 + version: 5.2.2 vitest: specifier: ^0.34.6 version: 0.34.6 @@ -82,6 +88,9 @@ importers: microbundle: specifier: ^0.15.1 version: 0.15.1 + typescript: + specifier: ^5.2.2 + version: 5.2.2 vitest: specifier: ^0.34.6 version: 0.34.6 diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..a370656 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "target": "ES2020", + "useDefineForClassFields": true, + "module": "ESNext", + "lib": ["ES2020", "DOM", "DOM.Iterable"], + "skipLibCheck": true, + + /* Bundler mode */ + "moduleResolution": "bundler", + // "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + + /* Linting */ + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + } +} diff --git a/turbo.json b/turbo.json index 2905b46..a145b8d 100644 --- a/turbo.json +++ b/turbo.json @@ -3,6 +3,9 @@ "pipeline": { "test": {}, "clean": {}, + "tsc": { + "dependsOn": ["^build"] + }, "build": { "dependsOn": ["^build"], "outputs": ["dist/**"]