Skip to content

Commit

Permalink
typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
stereobooster committed Nov 19, 2023
1 parent 8e4ff16 commit 79fda58
Show file tree
Hide file tree
Showing 21 changed files with 103 additions and 46 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:
- name: Install dependencies
run: pnpm install

- name: TypeScript
run: pnpm tsc

- name: Test
run: pnpm test

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ Logo by Pravin Unagar from <a href="https://thenounproject.com/browse/icons/term

## TODO

- prettier, eslint, typescript
- publish
- prettier, eslint
- add example to [faceted-search demo](https://github.com/stereobooster/faceted-search)
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"test": "turbo run test",
"build": "turbo run build",
"dev": "turbo run dev",
"clean": "turbo run clean"
"clean": "turbo run clean",
"tsc": "turbo run tsc"
},
"devDependencies": {
"turbo": "^1.10.16"
"turbo": "^1.10.16",
"typescript": "^5.2.2"
}
}
3 changes: 2 additions & 1 deletion packages/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview",
"clean": "rm -rf dist"
"clean": "rm -rf dist",
"tsc": "tsc"
},
"dependencies": {
"@stereobooster/facets": "workspace:*",
Expand Down
17 changes: 1 addition & 16 deletions packages/demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
{
"extends": "../../tsconfig.json",
"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
},
"include": ["src"]
}
4 changes: 3 additions & 1 deletion packages/facets-instantsearch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
"peerDependencies": {
Expand All @@ -38,6 +39,7 @@
"@algolia/client-search": "^4.20.0",
"instantsearch.js": "4.60.0",
"microbundle": "^0.15.1",
"typescript": "^5.2.2",
"vitest": "^0.34.6"
}
}
10 changes: 5 additions & 5 deletions packages/facets-instantsearch/src/adaptResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function adaptResponse<S extends Schema, I extends Item<S>>(
hits:
idKey === "objectID"
? (response.items as any)
: response.items.map(adaptHit(idKey || "id")),
: response.items.map(adaptHit<I>(idKey || "id")),
page: response.pagination.page,
hitsPerPage: response.pagination.perPage,
nbHits: response.pagination.total,
Expand All @@ -25,17 +25,17 @@ export function adaptResponse<S extends Schema, I extends Item<S>>(
}

export function adaptHit<I>(key: string) {
return (item: I): Hit<I> => ({
return (item: any): Hit<I> => ({
objectID: item[key],
...item,
});
}

export function adaptFacets(facets): Record<string, Record<string, number>> {
export function adaptFacets(facets: any): Record<string, Record<string, number>> {
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;
});
});
Expand All @@ -44,7 +44,7 @@ export function adaptFacets(facets): Record<string, Record<string, number>> {
}

export function adaptFacetsStats(
facets
facets: any
): Record<string, { min: number; max: number; avg: number; sum: number }> {
const instantsearchFacetsStats = Object.create(null);
Object.keys(facets).forEach((field) => {
Expand Down
8 changes: 8 additions & 0 deletions packages/facets-instantsearch/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node"
},
"include": ["src"]
}
4 changes: 3 additions & 1 deletion packages/facets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
}
8 changes: 6 additions & 2 deletions packages/facets/src/Facets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ export type SearchResults<S extends Schema, I extends Item<S>> = {
};

export class Facets<S extends Schema, I extends Item<S>> {
#config: FacetsConfig<S>;
// @ts-expect-error it is assigned later
#items: I[];
// @ts-expect-error it is assigned later
#indexes: Record<string, InvertedIndex<SupportedFieldTypes>>;
#config: FacetsConfig<S>;
// @ts-expect-error it is assigned later
#textIndex: InstanceType<TextIndex>;
// @ts-expect-error it is assigned later
#fullFacets: Record<
string,
Array<[SupportedFieldTypes, number, SparseTypedFastBitSet]>
Expand Down Expand Up @@ -279,7 +283,7 @@ export class Facets<S extends Schema, I extends Item<S>> {
const to: number = selected.to || Infinity;
(this.#indexes[field] as InvertedIndex<number>)
.values()
.forEach(([x, y, z]) => {
.forEach(([x, _, z]) => {
if (x == null || x > to || x < from) return;
if (!ids) {
ids = z.clone();
Expand Down
4 changes: 2 additions & 2 deletions packages/facets/src/ITrieMapIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export class ITrieMapIndex<K> extends InvertedIndexMaplike<K> {
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());
Expand Down
5 changes: 3 additions & 2 deletions packages/facets/src/InvertedIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { SparseTypedFastBitSet } from "typedfastbitset";
export type FacetValue<K = unknown> = [K, number, SparseTypedFastBitSet];

export class InvertedIndex<K = unknown> {
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<FacetValue<K>> {
Expand All @@ -22,6 +22,7 @@ interface Maplike<K, V> {
}

export class InvertedIndexMaplike<K = unknown> extends InvertedIndex<K> {
// @ts-expect-error fix later
index: Maplike<K, SparseTypedFastBitSet>;

add(value: K, id: number) {
Expand Down
15 changes: 10 additions & 5 deletions packages/facets/src/TFlexsearchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ 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;
const results = this.#index.search(query, {
...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<number, any>(),
};

const resultsByRelevance = new Map<number, number>();
results.forEach(({ result }) => {
const factor = 1;
result.forEach((id, i) => {
Expand All @@ -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<number, any>(),
};
}
}
3 changes: 2 additions & 1 deletion packages/facets/src/TFuseIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class TFuseIndex extends TextIndexBase {
static canHighlight = false;

#options: IFuseOptions<string>;
// @ts-expect-error it is assigned later
#index: Fuse<any>;

constructor({ fields, idKey }: TextIndexBaseOptions) {
Expand All @@ -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<number, ReadonlyArray<FuseResultMatch>>();
return {
ids: this.#index.search(query, options).map((x) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/facets/src/TMinisearchIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, MatchInfo>();
console.log()
return {
Expand Down
3 changes: 2 additions & 1 deletion packages/facets/src/TQuickscoreIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export class TQuickscoreIndex extends TextIndexBase {
static canHighlight = true;

#fields: Array<string>;
// @ts-expect-error it is assigned later
#index: QuickScore<any>;
#idKey: string;

Expand All @@ -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<number, Record<string, RangeTuple[]>>();
return {
ids: (this.#index.search(query) as ScoredObject<any>[]).map((x) => {
Expand Down
10 changes: 5 additions & 5 deletions packages/facets/src/TextIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, any>): (value: any) => Record<string, string> {
highlight(_matches: Map<number, any>): (value: any) => Record<string, string> {
throw new Error("not impelemted");
}
}
Expand Down
8 changes: 8 additions & 0 deletions packages/facets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Node"
},
"include": ["src"]
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"pipeline": {
"test": {},
"clean": {},
"tsc": {
"dependsOn": ["^build"]
},
"build": {
"dependsOn": ["^build"],
"outputs": ["dist/**"]
Expand Down

0 comments on commit 79fda58

Please sign in to comment.