Skip to content

Commit

Permalink
Upgrade Prettier; allow using from both CommonJS and ESM code (#4)
Browse files Browse the repository at this point in the history
## PRs in the Stack
- ➡ #4

(The stack is managed by
[git-grok](https://github.com/dimikot/git-grok).)
  • Loading branch information
dimikot authored Jan 13, 2024
2 parents 3e33bd2 + 6de42d0 commit 842d96e
Show file tree
Hide file tree
Showing 19 changed files with 72 additions and 54 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.base.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ module.exports = (projectRoot) => ({
},
ignorePatterns: [
"node_modules",
"dist",
"webpack.config.ts",
"**/bin/**",
"*.d.ts",
"**/jest.config.js",
"*.js",
],
rules: {
// TODO: slowly enable no-extraneous-dependencies rule below. For now, it's
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run build
- run: npm run test
- run: npm run lint
- run: npm install -g pnpm --force
- run: pnpm install
- run: pnpm run build
- run: pnpm run lint
- run: pnpm run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist
node_modules
package-lock.json
yarn.lock
pnpm-lock.yaml
.DS_Store
*.log
*.tmp
Expand Down
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
__tests__
.npmrc
tsconfig.tsbuildinfo
.github

node_modules
package-lock.json
yarn.lock
pnpm-lock.yaml
.DS_Store
*.log
*.tmp
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

See also [Full API documentation](https://github.com/clickup/magination/blob/master/docs/modules.md).

![CI run](https://github.com/clickup/magination/actions/workflows/ci.yml/badge.svg?branch=main)

The problem: for some search request (e.g. search-by-keywords), we have multiple
search queries (sources) with different performance that deliver results (hits)
with different relevancy. We want to merge that search hits using "cursor based
Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

See also [Full API documentation](https://github.com/clickup/magination/blob/master/docs/modules.md).

![CI run](https://github.com/clickup/magination/actions/workflows/ci.yml/badge.svg?branch=main)

The problem: for some search request (e.g. search-by-keywords), we have multiple
search queries (sources) with different performance that deliver results (hits)
with different relevancy. We want to merge that search hits using "cursor based
Expand Down
22 changes: 13 additions & 9 deletions docs/classes/Magination.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@clickup/magination](../README.md) / [Exports](../modules.md) / Magination

# Class: Magination<TSource, THit\>
# Class: Magination\<TSource, THit\>

Represents a union of multiple pagination sources into one continuous pages
stream with cursor.
Expand All @@ -9,28 +9,32 @@ stream with cursor.

| Name | Type |
| :------ | :------ |
| `TSource` | extends [`Source`](Source.md)<`any`\> |
| `THit` | `TSource` extends [`Source`](Source.md)<infer THit\> ? `THit` : `never` |
| `TSource` | extends [`Source`](Source.md)\<`any`\> |
| `THit` | `TSource` extends [`Source`](Source.md)\<infer THit\> ? `THit` : `never` |

## Constructors

### constructor

**new Magination**<`TSource`, `THit`\>(`sources`)
**new Magination**\<`TSource`, `THit`\>(`sources`): [`Magination`](Magination.md)\<`TSource`, `THit`\>

#### Type parameters

| Name | Type |
| :------ | :------ |
| `TSource` | extends [`Source`](Source.md)<`any`\> |
| `THit` | `TSource` extends [`Source`](Source.md)<`THit`\> ? `THit` : `never` |
| `TSource` | extends [`Source`](Source.md)\<`any`\> |
| `THit` | `TSource` extends [`Source`](Source.md)\<`THit`\> ? `THit` : `never` |

#### Parameters

| Name | Type |
| :------ | :------ |
| `sources` | readonly `TSource`[] |

#### Returns

[`Magination`](Magination.md)\<`TSource`, `THit`\>

#### Defined in

[src/Magination.ts:30](https://github.com/clickup/magination/blob/master/src/Magination.ts#L30)
Expand All @@ -39,7 +43,7 @@ stream with cursor.

### load

**load**(`«destructured»`): `AsyncGenerator`<[`Page`](../interfaces/Page.md)<`THit`\> & { `prevCursor`: ``null`` \| `string` ; `source`: `TSource` }, `any`, `unknown`\>
**load**(`«destructured»`): `AsyncGenerator`\<[`Page`](../interfaces/Page.md)\<`THit`\> & \{ `prevCursor`: ``null`` \| `string` ; `source`: `TSource` }, `any`, `unknown`\>

Returns a finite generator of pages which runs all of the sources in
parallel and then return a page of resulting hits. Basically, loads the
Expand Down Expand Up @@ -76,12 +80,12 @@ hits along with the new cursor.
| :------ | :------ |
| `«destructured»` | `Object` |
| › `cache` | [`Cache`](../interfaces/Cache.md) |
| › `hasher` | [`Hasher`](../interfaces/Hasher.md)<`THit`\> |
| › `hasher` | [`Hasher`](../interfaces/Hasher.md)\<`THit`\> |
| › `cursor` | ``null`` \| `string` |

#### Returns

`AsyncGenerator`<[`Page`](../interfaces/Page.md)<`THit`\> & { `prevCursor`: ``null`` \| `string` ; `source`: `TSource` }, `any`, `unknown`\>
`AsyncGenerator`\<[`Page`](../interfaces/Page.md)\<`THit`\> & \{ `prevCursor`: ``null`` \| `string` ; `source`: `TSource` }, `any`, `unknown`\>

#### Defined in

Expand Down
16 changes: 10 additions & 6 deletions docs/classes/Source.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@clickup/magination](../README.md) / [Exports](../modules.md) / Source

# Class: Source<THit\>
# Class: Source\<THit\>

Represents a single stream of hits subject for pagination.

Expand All @@ -20,7 +20,7 @@ do exclusion on the hits returned, so they are never repeated.

### constructor

**new Source**<`THit`\>(`name`, `options`)
**new Source**\<`THit`\>(`name`, `options`): [`Source`](Source.md)\<`THit`\>

#### Type parameters

Expand All @@ -33,7 +33,11 @@ do exclusion on the hits returned, so they are never repeated.
| Name | Type |
| :------ | :------ |
| `name` | `string` |
| `options` | [`SourceOptions`](../interfaces/SourceOptions.md)<`THit`\> |
| `options` | [`SourceOptions`](../interfaces/SourceOptions.md)\<`THit`\> |

#### Returns

[`Source`](Source.md)\<`THit`\>

#### Defined in

Expand All @@ -53,7 +57,7 @@ do exclusion on the hits returned, so they are never repeated.

### load

**load**(`«destructured»`): `Promise`<[`Page`](../interfaces/Page.md)<`THit`\>\>
**load**(`«destructured»`): `Promise`\<[`Page`](../interfaces/Page.md)\<`THit`\>\>

#### Parameters

Expand All @@ -63,11 +67,11 @@ do exclusion on the hits returned, so they are never repeated.
| › `cache` | [`Cache`](../interfaces/Cache.md) |
| › `cursor` | ``null`` \| `string` |
| › `excludeHits` | `THit`[] |
| › `hasher` | [`Hasher`](../interfaces/Hasher.md)<`THit`\> |
| › `hasher` | [`Hasher`](../interfaces/Hasher.md)\<`THit`\> |

#### Returns

`Promise`<[`Page`](../interfaces/Page.md)<`THit`\>\>
`Promise`\<[`Page`](../interfaces/Page.md)\<`THit`\>\>

#### Defined in

Expand Down
8 changes: 4 additions & 4 deletions docs/interfaces/Cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A key-value store which allows to store various metadata for a cursor.

### read

**read**(`key`): `Promise`<``null`` \| `object`\>
**read**(`key`): `Promise`\<``null`` \| `object`\>

#### Parameters

Expand All @@ -18,7 +18,7 @@ A key-value store which allows to store various metadata for a cursor.

#### Returns

`Promise`<``null`` \| `object`\>
`Promise`\<``null`` \| `object`\>

#### Defined in

Expand All @@ -28,7 +28,7 @@ ___

### write

**write**(`key`, `value`): `Promise`<`unknown`\>
**write**(`key`, `value`): `Promise`\<`unknown`\>

#### Parameters

Expand All @@ -39,7 +39,7 @@ ___

#### Returns

`Promise`<`unknown`\>
`Promise`\<`unknown`\>

#### Defined in

Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/Hasher.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@clickup/magination](../README.md) / [Exports](../modules.md) / Hasher

# Interface: Hasher<THit\>
# Interface: Hasher\<THit\>

## Type parameters

Expand Down
2 changes: 1 addition & 1 deletion docs/interfaces/Page.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@clickup/magination](../README.md) / [Exports](../modules.md) / Page

# Interface: Page<THit\>
# Interface: Page\<THit\>

A chunk of hits and a cursor which allows to fetch more hits. If the cursor
is null, there are no more hits left to fetch.
Expand Down
8 changes: 4 additions & 4 deletions docs/interfaces/SourceOptions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[@clickup/magination](../README.md) / [Exports](../modules.md) / SourceOptions

# Interface: SourceOptions<THit\>
# Interface: SourceOptions\<THit\>

## Type parameters

Expand Down Expand Up @@ -32,11 +32,11 @@ ___

### search

**search**: (`cursor`: ``null`` \| `string`, `excludeHits`: `THit`[], `count`: `number`) => `Promise`<[`Page`](Page.md)<`THit`\>\>
**search**: (`cursor`: ``null`` \| `string`, `excludeHits`: `THit`[], `count`: `number`) => `Promise`\<[`Page`](Page.md)\<`THit`\>\>

#### Type declaration

▸ (`cursor`, `excludeHits`, `count`): `Promise`<[`Page`](Page.md)<`THit`\>\>
▸ (`cursor`, `excludeHits`, `count`): `Promise`\<[`Page`](Page.md)\<`THit`\>\>

##### Parameters

Expand All @@ -48,7 +48,7 @@ ___

##### Returns

`Promise`<[`Page`](Page.md)<`THit`\>\>
`Promise`\<[`Page`](Page.md)\<`THit`\>\>

#### Defined in

Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@clickup/magination",
"description": "An opinionated framework to build cursor-based pagination over multiple streams of hits",
"version": "2.10.292",
"version": "2.10.296",
"license": "MIT",
"keywords": [
"pagination",
Expand All @@ -16,7 +16,7 @@
"lint": "eslint . --ext .ts --cache --cache-location dist/.eslintcache",
"test": "jest",
"docs": "rm -rf docs && typedoc --plugin typedoc-plugin-markdown --plugin typedoc-plugin-merge-modules && sed -i '' -E 's#packages/[^/]+/##g' $(find docs -type f -name '*.md')",
"clean": "rm -rf dist node_modules yarn.lock package-lock.json",
"clean": "rm -rf dist node_modules yarn.lock package-lock.json pnpm-lock.yaml *.log",
"copy-package-to-public-dir": "copy-package-to-public-dir.sh",
"backport-package-from-public-dir": "backport-package-from-public-dir.sh",
"deploy": "npm run build && npm run lint && npm run test && npm publish --access=public"
Expand All @@ -28,6 +28,7 @@
"devDependencies": {
"@types/jest": "^29.5.5",
"@types/lodash": "^4.14.175",
"@types/node": "^20.4.1",
"@types/uniqid": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^5.59.6",
"@typescript-eslint/parser": "^5.59.6",
Expand All @@ -41,6 +42,8 @@
"eslint-plugin-typescript-sort-keys": "^2.3.0",
"eslint-plugin-unused-imports": "^2.0.0",
"eslint": "^8.40.0",
"jest": "^29.7.0",
"prettier": "3.2.1",
"ts-jest": "^29.1.1",
"typedoc-plugin-markdown": "^3.16.0",
"typedoc-plugin-merge-modules": "^5.1.0",
Expand Down
8 changes: 4 additions & 4 deletions src/Magination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type MaginationSlot = {
*/
export default class Magination<
TSource extends Source<any>,
THit = TSource extends Source<infer THit> ? THit : never
THit = TSource extends Source<infer THit> ? THit : never,
> {
private sources;

Expand Down Expand Up @@ -119,12 +119,12 @@ export default class Magination<
cursor,
excludeHits: [],
hasher,
})
)
}),
),
);

const excludeHashes = new Set(
flatten(slot.frames.slice(0, num + 1).map((frame) => frame.hitHashes))
flatten(slot.frames.slice(0, num + 1).map((frame) => frame.hitHashes)),
);

// Yield pages in order of sources. On each yield, remember all of the
Expand Down
14 changes: 7 additions & 7 deletions src/Source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface SourceOptions<THit> {
search: (
cursor: string | null,
excludeHits: THit[],
count: number
count: number,
) => Promise<Page<THit>>;
}

Expand All @@ -32,7 +32,7 @@ export interface SourceOptions<THit> {
export default class Source<THit> {
constructor(
public readonly name: string,
private options: SourceOptions<THit>
private options: SourceOptions<THit>,
) {}

async load({
Expand All @@ -50,8 +50,8 @@ export default class Source<THit> {
typeof this.options.preloadSize === "function"
? this.options.preloadSize(offset)
: typeof this.options.preloadSize === "number"
? this.options.preloadSize
: this.options.pageSize + 1; // +1 to efficiently detect whether we have more pages
? this.options.preloadSize
: this.options.pageSize + 1; // +1 to efficiently detect whether we have more pages
let [slotKey, pos] = parseCursor(cursor);

// Load cache slot or create a new empty one.
Expand All @@ -70,7 +70,7 @@ export default class Source<THit> {
pos = Math.min(pos, slot.hits.length);

const excludeHashes = new Set<string>(
[...slot.hits.slice(0, pos), ...excludeHits].map((hit) => hasher(hit))
[...slot.hits.slice(0, pos), ...excludeHits].map((hit) => hasher(hit)),
);

// Extract hits from the cache starting from pos to form results page.
Expand All @@ -85,7 +85,7 @@ export default class Source<THit> {
const res = await this.options.search(
slot.cursor,
[...slot.hits, ...excludeHits],
preloadSize(pos)
preloadSize(pos),
);
slot.hits.push(...res.hits);
slot.cursor = res.cursor;
Expand Down Expand Up @@ -113,7 +113,7 @@ export default class Source<THit> {
inoutExcludeHashes: Set<string>,
hasher: Hasher<THit>,
cachedHits: THit[],
pos: number
pos: number,
): number {
while (outHits.length < this.options.pageSize && pos < cachedHits.length) {
const hit = cachedHits[pos];
Expand Down
Loading

0 comments on commit 842d96e

Please sign in to comment.