@adecore/utils 0.62.1 → 0.63.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/errors.js +0 -1
- package/dist/fs.d.ts +1 -0
- package/dist/fs.js +6 -0
- package/dist/geometry.d.ts +16 -0
- package/dist/geometry.js +44 -0
- package/dist/path.js +0 -1
- package/dist/single-flight.d.ts +3 -0
- package/dist/single-flight.js +10 -1
- package/dist/text.js +0 -1
- package/package.json +7 -2
- package/src/errors.ts +0 -1
- package/src/fs.ts +8 -0
- package/src/geometry.ts +66 -0
- package/src/path.ts +0 -1
- package/src/single-flight.ts +13 -1
- package/src/text.ts +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @adecore/utils
|
|
2
2
|
|
|
3
|
-
Small helpers without dependencies: atomic file writes, path containment, the order of async work
|
|
3
|
+
Small helpers without dependencies: atomic file writes, path containment, the order of async work, reading a thrown value and the math on points and boxes. Each lives at its own subpath. `async`, `errors`, `geometry`, `record` and `text` run in a page; `fs` and `path` need Node or Bun.
|
|
4
4
|
|
|
5
5
|
```sh
|
|
6
6
|
bun add @adecore/utils
|
package/dist/errors.js
CHANGED
|
@@ -22,7 +22,6 @@ export function describeError(error, debug) {
|
|
|
22
22
|
}
|
|
23
23
|
return error.message === '' ? error.name : error.message;
|
|
24
24
|
}
|
|
25
|
-
/* What was thrown, as an Error a rejection can carry. */
|
|
26
25
|
export function asError(thrown) {
|
|
27
26
|
return thrown instanceof Error ? thrown : new Error(String(thrown));
|
|
28
27
|
}
|
package/dist/fs.d.ts
CHANGED
package/dist/fs.js
CHANGED
|
@@ -19,6 +19,12 @@ export function isBusyRename(e) {
|
|
|
19
19
|
export function tempNameFor(target) {
|
|
20
20
|
return `${target}.${process.pid}-${randomUUID()}.tmp`;
|
|
21
21
|
}
|
|
22
|
+
// Before 0.53.3 the part after the process id was a counter, not a UUID, and files of those writes can still lie around.
|
|
23
|
+
const TEMP_NAME = /\.\d+-[0-9a-f-]+\.tmp$/;
|
|
24
|
+
/* Whether a file name is one `tempNameFor` gave, for a sweep of the writes a crash left behind. */
|
|
25
|
+
export function isTempName(name) {
|
|
26
|
+
return TEMP_NAME.test(name);
|
|
27
|
+
}
|
|
22
28
|
async function createTemp(target, mode) {
|
|
23
29
|
for (let attempt = 0;; attempt++) {
|
|
24
30
|
const path = tempNameFor(target);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface Point {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
export interface Rect {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
w: number;
|
|
9
|
+
h: number;
|
|
10
|
+
}
|
|
11
|
+
export declare function centerOf(rect: Rect): Point;
|
|
12
|
+
export declare function roundPoint(point: Point): Point;
|
|
13
|
+
export declare function rotatePoint(point: Point, around: Point, angle: number): Point;
|
|
14
|
+
export declare function unionOf(rects: readonly Rect[]): Rect | null;
|
|
15
|
+
export declare function intersects(a: Rect, b: Rect): boolean;
|
|
16
|
+
export declare function rectFromPoints(from: Point, to: Point): Rect;
|
package/dist/geometry.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export function centerOf(rect) {
|
|
2
|
+
return { x: rect.x + rect.w / 2, y: rect.y + rect.h / 2 };
|
|
3
|
+
}
|
|
4
|
+
/* A point on the whole pixel, for a layout that lines its boxes and its lines up on a pixel grid. */
|
|
5
|
+
export function roundPoint(point) {
|
|
6
|
+
return { x: Math.round(point.x), y: Math.round(point.y) };
|
|
7
|
+
}
|
|
8
|
+
export function rotatePoint(point, around, angle) {
|
|
9
|
+
if (angle === 0) {
|
|
10
|
+
return point;
|
|
11
|
+
}
|
|
12
|
+
const cos = Math.cos(angle);
|
|
13
|
+
const sin = Math.sin(angle);
|
|
14
|
+
const dx = point.x - around.x;
|
|
15
|
+
const dy = point.y - around.y;
|
|
16
|
+
return { x: around.x + dx * cos - dy * sin, y: around.y + dx * sin + dy * cos };
|
|
17
|
+
}
|
|
18
|
+
export function unionOf(rects) {
|
|
19
|
+
if (rects.length === 0) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
let left = Infinity;
|
|
23
|
+
let top = Infinity;
|
|
24
|
+
let right = -Infinity;
|
|
25
|
+
let bottom = -Infinity;
|
|
26
|
+
for (const rect of rects) {
|
|
27
|
+
left = Math.min(left, rect.x, rect.x + rect.w);
|
|
28
|
+
top = Math.min(top, rect.y, rect.y + rect.h);
|
|
29
|
+
right = Math.max(right, rect.x, rect.x + rect.w);
|
|
30
|
+
bottom = Math.max(bottom, rect.y, rect.y + rect.h);
|
|
31
|
+
}
|
|
32
|
+
return { x: left, y: top, w: right - left, h: bottom - top };
|
|
33
|
+
}
|
|
34
|
+
export function intersects(a, b) {
|
|
35
|
+
return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
|
|
36
|
+
}
|
|
37
|
+
export function rectFromPoints(from, to) {
|
|
38
|
+
return {
|
|
39
|
+
x: Math.min(from.x, to.x),
|
|
40
|
+
y: Math.min(from.y, to.y),
|
|
41
|
+
w: Math.abs(to.x - from.x),
|
|
42
|
+
h: Math.abs(to.y - from.y)
|
|
43
|
+
};
|
|
44
|
+
}
|
package/dist/path.js
CHANGED
|
@@ -9,7 +9,6 @@ import { isAbsolute, relative, sep } from 'node:path';
|
|
|
9
9
|
export function climbsOut(rel) {
|
|
10
10
|
return rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel);
|
|
11
11
|
}
|
|
12
|
-
/* Whether `path` is `folder` or lies below it. */
|
|
13
12
|
export function isWithin(folder, path) {
|
|
14
13
|
return !climbsOut(relative(folder, path));
|
|
15
14
|
}
|
package/dist/single-flight.d.ts
CHANGED
|
@@ -2,5 +2,8 @@ export declare class SingleFlight<T> {
|
|
|
2
2
|
private readonly flights;
|
|
3
3
|
get(key: string): Promise<T> | undefined;
|
|
4
4
|
has(key: string): boolean;
|
|
5
|
+
keys(): IterableIterator<string>;
|
|
6
|
+
forget(key: string): void;
|
|
7
|
+
clear(): void;
|
|
5
8
|
run(key: string, work: () => Promise<T>): Promise<T>;
|
|
6
9
|
}
|
package/dist/single-flight.js
CHANGED
|
@@ -4,13 +4,22 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export class SingleFlight {
|
|
6
6
|
flights = new Map();
|
|
7
|
-
/* The run in flight for `key`, if any. */
|
|
8
7
|
get(key) {
|
|
9
8
|
return this.flights.get(key);
|
|
10
9
|
}
|
|
11
10
|
has(key) {
|
|
12
11
|
return this.flights.has(key);
|
|
13
12
|
}
|
|
13
|
+
keys() {
|
|
14
|
+
return this.flights.keys();
|
|
15
|
+
}
|
|
16
|
+
/* Lets the next caller of `key` start a run of its own. The run in flight carries on for the callers that hold it. */
|
|
17
|
+
forget(key) {
|
|
18
|
+
this.flights.delete(key);
|
|
19
|
+
}
|
|
20
|
+
clear() {
|
|
21
|
+
this.flights.clear();
|
|
22
|
+
}
|
|
14
23
|
run(key, work) {
|
|
15
24
|
const running = this.flights.get(key);
|
|
16
25
|
if (running !== undefined) {
|
package/dist/text.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adecore/utils",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Small helpers without dependencies: atomic file writes, path containment, async ordering and
|
|
3
|
+
"version": "0.63.0",
|
|
4
|
+
"description": "Small helpers without dependencies: atomic file writes, path containment, async ordering, error reading and point and box math.",
|
|
5
5
|
"license": "FSL-1.1-MIT",
|
|
6
6
|
"author": "Bas Milius",
|
|
7
7
|
"repository": {
|
|
@@ -36,6 +36,11 @@
|
|
|
36
36
|
"types": "./dist/fs.d.ts",
|
|
37
37
|
"default": "./dist/fs.js"
|
|
38
38
|
},
|
|
39
|
+
"./geometry": {
|
|
40
|
+
"source": "./src/geometry.ts",
|
|
41
|
+
"types": "./dist/geometry.d.ts",
|
|
42
|
+
"default": "./dist/geometry.js"
|
|
43
|
+
},
|
|
39
44
|
"./path": {
|
|
40
45
|
"source": "./src/path.ts",
|
|
41
46
|
"types": "./dist/path.d.ts",
|
package/src/errors.ts
CHANGED
|
@@ -28,7 +28,6 @@ export function describeError(error: unknown, debug: boolean): string {
|
|
|
28
28
|
return error.message === '' ? error.name : error.message;
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
/* What was thrown, as an Error a rejection can carry. */
|
|
32
31
|
export function asError(thrown: unknown): Error {
|
|
33
32
|
return thrown instanceof Error ? thrown : new Error(String(thrown));
|
|
34
33
|
}
|
package/src/fs.ts
CHANGED
|
@@ -24,6 +24,14 @@ export function tempNameFor(target: string): string {
|
|
|
24
24
|
return `${target}.${process.pid}-${randomUUID()}.tmp`;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
// Before 0.53.3 the part after the process id was a counter, not a UUID, and files of those writes can still lie around.
|
|
28
|
+
const TEMP_NAME = /\.\d+-[0-9a-f-]+\.tmp$/;
|
|
29
|
+
|
|
30
|
+
/* Whether a file name is one `tempNameFor` gave, for a sweep of the writes a crash left behind. */
|
|
31
|
+
export function isTempName(name: string): boolean {
|
|
32
|
+
return TEMP_NAME.test(name);
|
|
33
|
+
}
|
|
34
|
+
|
|
27
35
|
export interface WriteAtomicOptions {
|
|
28
36
|
/* Flushed to the disk before and after the rename, for a file whose loss costs a person their work. */
|
|
29
37
|
durable?: boolean;
|
package/src/geometry.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
export interface Point {
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface Rect {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
w: number;
|
|
10
|
+
h: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function centerOf(rect: Rect): Point {
|
|
14
|
+
return { x: rect.x + rect.w / 2, y: rect.y + rect.h / 2 };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/* A point on the whole pixel, for a layout that lines its boxes and its lines up on a pixel grid. */
|
|
18
|
+
export function roundPoint(point: Point): Point {
|
|
19
|
+
return { x: Math.round(point.x), y: Math.round(point.y) };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function rotatePoint(point: Point, around: Point, angle: number): Point {
|
|
23
|
+
if (angle === 0) {
|
|
24
|
+
return point;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const cos = Math.cos(angle);
|
|
28
|
+
const sin = Math.sin(angle);
|
|
29
|
+
const dx = point.x - around.x;
|
|
30
|
+
const dy = point.y - around.y;
|
|
31
|
+
|
|
32
|
+
return { x: around.x + dx * cos - dy * sin, y: around.y + dx * sin + dy * cos };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function unionOf(rects: readonly Rect[]): Rect | null {
|
|
36
|
+
if (rects.length === 0) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let left = Infinity;
|
|
41
|
+
let top = Infinity;
|
|
42
|
+
let right = -Infinity;
|
|
43
|
+
let bottom = -Infinity;
|
|
44
|
+
|
|
45
|
+
for (const rect of rects) {
|
|
46
|
+
left = Math.min(left, rect.x, rect.x + rect.w);
|
|
47
|
+
top = Math.min(top, rect.y, rect.y + rect.h);
|
|
48
|
+
right = Math.max(right, rect.x, rect.x + rect.w);
|
|
49
|
+
bottom = Math.max(bottom, rect.y, rect.y + rect.h);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return { x: left, y: top, w: right - left, h: bottom - top };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function intersects(a: Rect, b: Rect): boolean {
|
|
56
|
+
return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function rectFromPoints(from: Point, to: Point): Rect {
|
|
60
|
+
return {
|
|
61
|
+
x: Math.min(from.x, to.x),
|
|
62
|
+
y: Math.min(from.y, to.y),
|
|
63
|
+
w: Math.abs(to.x - from.x),
|
|
64
|
+
h: Math.abs(to.y - from.y)
|
|
65
|
+
};
|
|
66
|
+
}
|
package/src/path.ts
CHANGED
|
@@ -12,7 +12,6 @@ export function climbsOut(rel: string): boolean {
|
|
|
12
12
|
return rel === '..' || rel.startsWith(`..${sep}`) || isAbsolute(rel);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
/* Whether `path` is `folder` or lies below it. */
|
|
16
15
|
export function isWithin(folder: string, path: string): boolean {
|
|
17
16
|
return !climbsOut(relative(folder, path));
|
|
18
17
|
}
|
package/src/single-flight.ts
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
export class SingleFlight<T> {
|
|
6
6
|
private readonly flights = new Map<string, Promise<T>>();
|
|
7
7
|
|
|
8
|
-
/* The run in flight for `key`, if any. */
|
|
9
8
|
get(key: string): Promise<T> | undefined {
|
|
10
9
|
return this.flights.get(key);
|
|
11
10
|
}
|
|
@@ -14,6 +13,19 @@ export class SingleFlight<T> {
|
|
|
14
13
|
return this.flights.has(key);
|
|
15
14
|
}
|
|
16
15
|
|
|
16
|
+
keys(): IterableIterator<string> {
|
|
17
|
+
return this.flights.keys();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* Lets the next caller of `key` start a run of its own. The run in flight carries on for the callers that hold it. */
|
|
21
|
+
forget(key: string): void {
|
|
22
|
+
this.flights.delete(key);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
clear(): void {
|
|
26
|
+
this.flights.clear();
|
|
27
|
+
}
|
|
28
|
+
|
|
17
29
|
run(key: string, work: () => Promise<T>): Promise<T> {
|
|
18
30
|
const running = this.flights.get(key);
|
|
19
31
|
|
package/src/text.ts
CHANGED