Skip to content

Commit 646384c

Browse files
committed
Add auto imports
1 parent faa5914 commit 646384c

11 files changed

+258
-8
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,7 @@ dist
173173

174174
# Finder (MacOS) folder config
175175
.DS_Store
176+
177+
# project specific
178+
auto-import.d.ts
179+
input.txt

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

bun.lockb

696 Bytes
Binary file not shown.

bunfig.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
preload = ["@danielx/civet/bun-civet"]
1+
preload = ["./civet.loader.ts"]

civet.loader.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { plugin, file, write } from 'bun';
2+
import lodash from 'lodash';
3+
4+
await plugin({
5+
name: 'AoC Civet loader',
6+
async setup(builder) {
7+
const { compile } = await import('@danielx/civet');
8+
9+
const utilsSrc = await file('./src/utils.civet').text();
10+
const utilsExports = getExports(utilsSrc);
11+
12+
const lodashExports = Object.keys(lodash);
13+
const autoDts = createAutoImportDts(utilsExports, lodashExports);
14+
15+
await write('./src/auto-import.d.ts', autoDts);
16+
17+
return builder.onLoad({ filter: /\.civet$/ }, async ({ path }) => {
18+
let source = await file(path).text();
19+
20+
source =
21+
`input from ./input.txt
22+
{ ${utilsExports.join(', ')} } from ../utils.civet
23+
{ ${lodashExports.join(', ')} } from lodash
24+
25+
` + source;
26+
27+
return {
28+
contents: await compile(source),
29+
loader: 'tsx',
30+
};
31+
});
32+
},
33+
});
34+
35+
function getExports(source: string) {
36+
const regex = /export\s+\{.*?as\s+(\w+)\s*\}|\bexport\s+function\s+(\w+)/g;
37+
38+
const exports: string[] = [];
39+
40+
let match;
41+
while ((match = regex.exec(source)) !== null) {
42+
exports.push(match[1] || match[2]);
43+
}
44+
45+
return exports;
46+
}
47+
48+
function createAutoImportDts(utilsExports: string[], lodashExports: string[]) {
49+
return `import * as utils from "./utils.civet"
50+
import lodash from "lodash"
51+
52+
declare global {
53+
// Utils
54+
${utilsExports.map((e) => ` const ${e}: typeof utils.${e}`).join(';\n')}
55+
56+
// Lodash
57+
${lodashExports.map((e) => ` const ${e}: typeof lodash.${e}`).join(';\n')}
58+
}
59+
60+
export {}`;
61+
}

index.ts

-1
This file was deleted.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
{
22
"name": "advent-of-code-2024",
33
"module": "index.ts",
4-
"type": "module",
54
"devDependencies": {
65
"@danielx/civet": "^0.8.16",
7-
"@types/bun": "latest"
6+
"@types/bun": "^1.1.14",
7+
"@types/lodash": "^4.17.13"
88
},
99
"peerDependencies": {
1010
"typescript": "^5.0.0"
11+
},
12+
"type": "module",
13+
"dependencies": {
14+
"lodash": "^4.17.21"
1115
}
1216
}

src/01/index.civet

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
console.log 'hello world!'
1+
input |> getNumbers |> sum |> log

src/globals.d.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
declare module '*.txt' {
2+
const text: string;
3+
export = text;
4+
}
5+
6+
declare global {
7+
const input: string;
8+
9+
interface Array2d<T> extends Array<T[]> {}
10+
interface Array3d<T> extends Array<T[][]> {}
11+
12+
interface Point {
13+
x: number;
14+
y: number;
15+
}
16+
17+
interface Point3d extends Point {
18+
z: number;
19+
}
20+
21+
type DeepReadonly<T> = {
22+
readonly [P in keyof T]: DeepReadonly<T[P]>;
23+
};
24+
}
25+
26+
export {};

src/utils.civet

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
export function getLines<T extends string>(input: string): T[]
2+
lines := input.split '\n'
3+
lines.pop() if lines.-1 is ''
4+
lines as T[]
5+
6+
export function getNumbers(input: string): number[]
7+
lines := getLines input
8+
lines.length is 1
9+
? lines.0.split ',' |> .map +&
10+
: lines.map +&
11+
12+
export function getArray2d<T extends string>(
13+
input: string,
14+
splitBy = ''
15+
): T[][]
16+
getLines(input).map &.split(splitBy) as T[]
17+
18+
export function asc(a: any, b: any): number
19+
a - b
20+
21+
export function desc(a: any, b: any): number
22+
b - a
23+
24+
export function inRange(min: number, value: number, max: number): boolean
25+
min <= value <= max
26+
27+
export function getManhattanDistance(
28+
a: Point,
29+
b: Point = x: 0, y: 0
30+
): number
31+
Math.abs(a.x - b.x) + Math.abs(a.y - b.y)
32+
33+
export function getDistance(a: Point, b: Point): number
34+
Math.hypot a.x - b.x, a.y - b.y
35+
36+
export function match<
37+
Variant extends PropertyKey,
38+
Options extends Record<Variant, () => any>
39+
>(variant: Variant, options: Options): ReturnType<Options[Variant]>
40+
options[variant]()
41+
42+
export function getColumn<T>(array: T[][], columnNumber: number): T[]
43+
for row of array
44+
row[columnNumber]
45+
46+
export function loop2d<T>(
47+
array: T[][],
48+
callback: (y: number, x: number, item: T) => void
49+
): void
50+
for y of [0...array.length]
51+
for x of [0...array[y]!.length]
52+
callback y, x, array[y]![x]!
53+
54+
export function toNumber(str: string): number
55+
+(str.match(/\-?\d+((.|,)\d+)?/)?.[0] or '')
56+
57+
export function toNumbers(str: string): number[]
58+
(str.match(/\-?\d+(\.\d+)?/g) ?? []).map +&
59+
60+
export function divisible(a: number, b: number): boolean
61+
a % b is 0
62+
63+
/** Greatest common divisor */
64+
export function getGcd(a: number, b: number): number
65+
!b ? a : getGcd(b, a % b)
66+
67+
/** Least Common Multiple */
68+
export function getLcm(a: number[]): number
69+
export function getLcm(a: number, b: number): number
70+
export function getLcm(a: number | number[], b?: number): number
71+
return Array.isArray(a)
72+
? a.reduce getLcm, 1
73+
: // @ts-ignore
74+
a * (b / getGcd a, b)
75+
76+
export function adjacentPoints<T>(arr: T[][], x: number, y: number)
77+
[
78+
item: arr[y - 1]?.[x]
79+
x: x,
80+
y: y - 1
81+
,
82+
item: arr[y - 1]?.[x + 1]
83+
x: x + 1,
84+
y: y - 1
85+
,
86+
item: arr[y - 1]?.[x - 1]
87+
x: x - 1,
88+
y: y - 1
89+
,
90+
item: arr[y + 1]?.[x]
91+
x: x,
92+
y: y + 1
93+
,
94+
item: arr[y + 1]?.[x + 1]
95+
x: x + 1,
96+
y: y + 1
97+
,
98+
item: arr[y + 1]?.[x - 1]
99+
x: x - 1,
100+
y: y + 1
101+
,
102+
item: arr[y][x - 1]
103+
x: x - 1,
104+
y: y
105+
,
106+
item: arr[y][x + 1]
107+
x: x + 1,
108+
y: y
109+
]
110+
111+
export function toKeys<Item, Key extends string>(...keys: Key[])
112+
function (item: Item[])
113+
obj := {} as Record<Key, Item>
114+
for i of [0...keys.length]
115+
obj[keys[i]] = item[i]
116+
obj
117+
118+
export function printArray(arr: any[], empty = '.'): void
119+
for y of [0...arr.length]
120+
for x of [0...arr[y]!.length]
121+
process.stdout.write arr[y]![x] ?? empty
122+
console.log()
123+
124+
export function createArray<T>(y: number, x: number, filler: T): T[][]
125+
new Array(y).fill(0).map
126+
() => new Array(x).fill filler
127+
128+
export function exhaustiveCheck(arg: never): void
129+
arg;
130+
131+
export function isPoint (arg: any): arg is Point
132+
arg and arg.x <? 'number' and arg.y <? 'number'
133+
134+
135+
export function isPoint3d (arg: any): arg is Point3d
136+
arg and arg.x <? 'number' and arg.y <? 'number' and arg.z <? 'number'
137+
138+
export function log (...args: any[]): void
139+
console.log ...args
140+
141+
export function int (nr: string): number
142+
parseInt nr
143+
144+
export function float (nr: string): number
145+
parseFloat nr
146+
147+
export function multiply(arr: number[]): number
148+
arr.reduce (a, b) => a * b
149+
150+
export function countChars(str: string)
151+
counter: Record<string, number> := {}
152+
for c of str.split ''
153+
(counter[c] ?= 0)++
154+
counter
155+

tsconfig.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
"target": "ESNext",
66
"module": "ESNext",
77
"moduleDetection": "force",
8-
"jsx": "react-jsx",
9-
"allowJs": true,
8+
"types": ["node"],
109

1110
// Bundler mode
1211
"moduleResolution": "bundler",
@@ -18,7 +17,6 @@
1817
"strict": true,
1918
"skipLibCheck": true,
2019
"noFallthroughCasesInSwitch": true,
21-
2220
// Some stricter flags (disabled by default)
2321
"noUnusedLocals": false,
2422
"noUnusedParameters": false,

0 commit comments

Comments
 (0)