Skip to content

Commit

Permalink
feat: use dts-buddy to generate types (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatiusmb authored Jun 26, 2024
1 parent 8b24cc1 commit d077cfb
Show file tree
Hide file tree
Showing 42 changed files with 560 additions and 510 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules

*.js
*.d.ts
*.map
3 changes: 0 additions & 3 deletions .npmignore

This file was deleted.

45 changes: 28 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,41 @@
"type": "module",
"scripts": {
"clean": "git add * && git clean -dfx -e node_modules",
"watch": "pnpm pub:build --watch",
"format": "prettier -w . --plugin=prettier-plugin-sort-package-json",
"check": "pnpm check:code && pnpm check:style",
"check:code": "pnpm pub:build --noEmit",
"check:code": "pnpm build:files --noEmit",
"check:style": "prettier -c . --plugin=prettier-plugin-sort-package-json",
"test": "pnpm test:unit",
"test:unit": "uvu -r tsm src \"(spec\\.ts)\"",
"pub:build": "tsc --project src --declaration",
"pub:format": "prettier -wu **/*.{d.ts,js}",
"prepublishOnly": "pnpm pub:build && pnpm pub:format"
"build:types": "tsm scripts/compile-types.ts",
"build:files": "tsc --project src",
"prepublishOnly": "pnpm build:types && pnpm build:files"
},
"types": "./core/index.d.ts",
"typings": "./index.d.ts",
"exports": {
".": "./core/index.js",
"./api": "./api/index.js",
"./bits": "./bits/index.js",
"./guards": "./guards/index.js",
"./math": "./math/index.js",
"./std": "./std/index.js",
"./web": "./web/index.js",
"./typings": "./typings/index.d.ts",
".": "./src/core/index.js",
"./api": "./src/api/index.js",
"./bits": "./src/bits/index.js",
"./compare": "./src/compare/index.js",
"./csv": "./src/csv/index.js",
"./date": "./src/date/index.js",
"./guards": "./src/guards/index.js",
"./math": "./src/math/index.js",
"./random": "./src/random/index.js",
"./std": "./src/std/index.js",
"./web": "./src/web/index.js",
"./typings": "./src/typings/index.d.ts",
"./package.json": "./package.json",
"./prettier.json": "./prettier.json",
"./tsconfig.json": "./tsconfig.json"
},
"files": [
"src/**/*.js",
"index.d.ts",
"index.d.ts.map",
"prettier.json",
"tsconfig.json"
],
"packageManager": "[email protected]",
"prettier": "./prettier.json",
"keywords": [
Expand All @@ -51,11 +61,12 @@
"settings"
],
"devDependencies": {
"@types/node": "^20.11.0",
"prettier": "^3.1.1",
"@types/node": "^20.14.9",
"dts-buddy": "^0.5.0",
"prettier": "^3.3.2",
"prettier-plugin-sort-package-json": "^0.2.0",
"tsm": "^2.3.0",
"typescript": "^5.3.3",
"typescript": "^5.5.2",
"uvu": "^0.5.6"
}
}
128 changes: 110 additions & 18 deletions pnpm-lock.yaml

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

11 changes: 11 additions & 0 deletions scripts/compile-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createBundle } from 'dts-buddy';
import { exports } from '../package.json';

await createBundle({
output: 'index.d.ts',
project: 'src/tsconfig.json',
modules: Object.entries(exports).reduce((acc, [key, entry]) => {
if (key.slice(2).includes('.')) return acc; // skip non-modules
return { ...acc, ['mauss' + key.slice(1)]: entry };
}, {}),
});
8 changes: 2 additions & 6 deletions src/bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import { :util } from 'mauss/bits';
```

## `find`

A namespace with generic methods to help find items in an (sometimes sorted) array.

### `find.binary`
## `binary`

do a binary search on a sorted array with custom item checking and cutoff function.

### `find.minmax`
## `minmax`

find the minimum and maximum value in an array of numbers.

Expand Down
32 changes: 0 additions & 32 deletions src/bits/find.ts

This file was deleted.

33 changes: 32 additions & 1 deletion src/bits/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
export * from './find.js';
import type { UnaryFunction } from '../typings/helpers.js';

export function binary<T>(
sorted: T[],
check: {
item: UnaryFunction<T, false | UnaryFunction<T>>;
pointer: UnaryFunction<T, boolean>;
},
): T | undefined {
let start = 0, final = sorted.length - 1; // prettier-ignore
while (start <= final) {
const midpoint = (start + final) >> 1;
const current = sorted[midpoint];
const passes = check.item(current);
if (passes) return passes(current);
const flag = check.pointer(current);
start = flag ? start : midpoint + 1;
final = flag ? midpoint - 1 : final;
}
return;
}

export function minmax(array: number[]): [number, number] {
if (!array.length) return [0, 0];

let min = array[0], max = array[0]; // prettier-ignore
for (let i = 1; i < array.length; i++) {
min = array[i] < min ? array[i] : min;
max = array[i] > max ? array[i] : max;
}
return [min, max];
}
Loading

0 comments on commit d077cfb

Please sign in to comment.