@pistonite/pure 0.26.4 → 0.26.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pistonite/pure",
3
- "version": "0.26.4",
3
+ "version": "0.26.6",
4
4
  "type": "module",
5
5
  "description": "Pure TypeScript libraries for my projects",
6
6
  "homepage": "https://github.com/Pistonite/pure",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "devDependencies": {
30
30
  "vitest": "^3.2.4",
31
- "mono-dev": "0.2.2"
31
+ "mono-dev": "0.2.3"
32
32
  },
33
33
  "dependencies": {
34
34
  "denque": "^2.1.0"
@@ -1,3 +1,4 @@
1
+ import { ilog } from "../log/internal.ts";
1
2
  import { errstr } from "../result";
2
3
 
3
4
  import { fsErr, FsErr, fsFail, type FsVoid, type FsResult } from "./FsError.ts";
@@ -29,7 +30,7 @@ export class FsFileStandaloneImplFileAPI implements FsFileStandalone {
29
30
  const data = await this.file.arrayBuffer();
30
31
  return { val: new Uint8Array(data) };
31
32
  } catch (e) {
32
- console.error(e);
33
+ ilog.error(e);
33
34
  return { err: fsFail(errstr(e)) };
34
35
  }
35
36
  }
@@ -41,7 +42,7 @@ export class FsFileStandaloneImplFileAPI implements FsFileStandalone {
41
42
  const data = await this.file.text();
42
43
  return { val: data };
43
44
  } catch (e) {
44
- console.error(e);
45
+ ilog.error(e);
45
46
  return { err: fsFail(errstr(e)) };
46
47
  }
47
48
  }
@@ -1,3 +1,4 @@
1
+ import { ilog } from "../log/internal.ts";
1
2
  import { errstr } from "../result";
2
3
 
3
4
  import { fsErr, FsErr, fsFail, type FsVoid, type FsResult } from "./FsError.ts";
@@ -39,7 +40,7 @@ export class FsFileStandaloneImplHandleAPI implements FsFileStandalone {
39
40
  });
40
41
  return requestedPermission === "granted";
41
42
  } catch (e) {
42
- console.error(e);
43
+ ilog.error(e);
43
44
  return false;
44
45
  }
45
46
  }
@@ -58,7 +59,7 @@ export class FsFileStandaloneImplHandleAPI implements FsFileStandalone {
58
59
  return { err: fsErr(FsErr.NotFound, "File not found") };
59
60
  }
60
61
  }
61
- console.error(e);
62
+ ilog.error(e);
62
63
  return { err: fsFail(errstr(e)) };
63
64
  }
64
65
  }
@@ -79,7 +80,7 @@ export class FsFileStandaloneImplHandleAPI implements FsFileStandalone {
79
80
  const data = await file.val.arrayBuffer();
80
81
  return { val: new Uint8Array(data) };
81
82
  } catch (e) {
82
- console.error(e);
83
+ ilog.error(e);
83
84
  return { err: fsFail(errstr(e)) };
84
85
  }
85
86
  }
@@ -99,7 +100,7 @@ export class FsFileStandaloneImplHandleAPI implements FsFileStandalone {
99
100
  const data = await file.val.text();
100
101
  return { val: data };
101
102
  } catch (e) {
102
- console.error(e);
103
+ ilog.error(e);
103
104
  return { err: fsFail(errstr(e)) };
104
105
  }
105
106
  }
@@ -145,7 +146,7 @@ export class FsFileStandaloneImplHandleAPI implements FsFileStandalone {
145
146
  };
146
147
  }
147
148
  }
148
- console.error(e);
149
+ ilog.error(e);
149
150
  return { err: fsFail(errstr(e)) };
150
151
  }
151
152
  }
@@ -25,10 +25,7 @@ export class FsImplEntryAPI
25
25
  constructor(root: string, rootEntry: FileSystemDirectoryEntry) {
26
26
  this.root = root;
27
27
  this.rootEntry = rootEntry;
28
- this.capabilities = {
29
- write: false,
30
- live: true,
31
- };
28
+ this.capabilities = { write: false, live: true };
32
29
  this.mgr = new FsFileMgr();
33
30
  }
34
31
 
@@ -1,3 +1,5 @@
1
+ import { ilog } from "../log/internal.ts";
2
+
1
3
  import type { FsFile } from "./FsFile.ts";
2
4
  import type {
3
5
  FsFileSystem,
@@ -28,10 +30,7 @@ export class FsImplFileAPI
28
30
  constructor(files: FileList) {
29
31
  // this seems to also work for windows
30
32
  this.root = files[0].webkitRelativePath.split("/", 1)[0];
31
- this.capabilities = {
32
- write: false,
33
- live: false,
34
- };
33
+ this.capabilities = { write: false, live: false };
35
34
  this.files = {};
36
35
  this.directories = {};
37
36
  this.mgr = new FsFileMgr();
@@ -43,7 +42,7 @@ export class FsImplFileAPI
43
42
  const normalized = fsNormalize(path);
44
43
  if (normalized.err) {
45
44
  // shouldn't happen since the path is from the File API
46
- console.error("Invalid path: " + path);
45
+ ilog.error("invalid path: " + path);
47
46
  continue;
48
47
  }
49
48
  this.files[normalized.val] = file;
@@ -49,10 +49,7 @@ export class FsImplHandleAPI
49
49
  this.rootHandle = rootHandle;
50
50
  this.writeMode = write;
51
51
  this.permissionStatus = "prompt";
52
- this.capabilities = {
53
- write,
54
- live: true,
55
- };
52
+ this.capabilities = { write, live: true };
56
53
  this.mgr = new FsFileMgr();
57
54
  }
58
55
 
@@ -160,10 +160,7 @@ const fsOpenFileWithFileSystemAccessAPI = async (
160
160
  if (anyMimeType.length) {
161
161
  convertedAccept["*/*"] = anyMimeType;
162
162
  }
163
- return {
164
- description,
165
- accept: convertedAccept,
166
- };
163
+ return { description, accept: convertedAccept };
167
164
  });
168
165
  try {
169
166
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
package/src/fs/FsSave.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { ilog } from "../log/internal.ts";
2
+
1
3
  import { fsFail, type FsVoid } from "./FsError.ts";
2
4
 
3
5
  /** Save (download) a file using Blob */
@@ -11,7 +13,7 @@ export function fsSave(content: string | Uint8Array, filename: string): FsVoid {
11
13
  saveAs(blob, filename);
12
14
  return {};
13
15
  } catch (e) {
14
- console.error(e);
16
+ ilog.error(e);
15
17
  return { err: fsFail("save failed") };
16
18
  }
17
19
  }
@@ -35,9 +37,7 @@ type SaveAsFn = (
35
37
  filename?: string,
36
38
  options?: SaveAsFnOptions,
37
39
  ) => void;
38
- type SaveAsFnOptions = {
39
- autoBom: boolean;
40
- };
40
+ type SaveAsFnOptions = { autoBom: boolean };
41
41
 
42
42
  /* eslint-disable @typescript-eslint/no-explicit-any */
43
43
 
package/src/log/index.ts CHANGED
@@ -1,154 +1,13 @@
1
- /**
2
- * Client side log util
3
- *
4
- * This is rather simple logging stuff with the primary focus
5
- * being easy to debug.
6
- *
7
- * Use {@link logger} to create a logger with a name and a color,
8
- * then use one of the {@link LoggerFactory} methods to setup the logging level.
9
- *
10
- * There are 3 levels of logging:
11
- * 1. (Default) Warnings and Errors only
12
- * 2. Also log info messages
13
- * 3. Also log debug messages.
14
- *
15
- * Each logger can turn on info and debug logging separately, or it can
16
- * be turned on at the global level for debugging.
17
- *
18
- * You can also turn off each logger individually or at global level for debugging.
19
- *
20
- * Due to the nature of JS, all logging calls, even when turned off, will incur
21
- * some small runtime overhead. While we could remove debug calls
22
- * for release build, that's currently not done (and it would require
23
- * bundler to inline the call to remove the call completely, which might
24
- * not be the case)
25
- *
26
- * @module
27
- */
28
-
29
- import { errstr } from "../result/index.ts";
30
-
31
- export const LogLevel = {
32
- Off: 0,
33
- High: 1,
34
- Info: 2,
35
- Debug: 3,
36
- } as const;
37
- export type LogLevel = (typeof LogLevel)[keyof typeof LogLevel];
38
-
39
- let globalLevel: LogLevel = LogLevel.High;
40
- /**
41
- * Suppress ALL logging.
42
- *
43
- * This overrides logger-level settings
44
- */
45
- export const globalLogOff = () => (globalLevel = 0);
46
- /**
47
- * Enable +info logging for ALL loggers
48
- *
49
- * This overrides logger-level settings
50
- */
51
- export const globalLogInfo = () => (globalLevel = 2);
52
- /**
53
- * Enable +debug logging for ALL loggers
54
- *
55
- * This overrides logger-level settings
56
- */
57
- export const globalLogDebug = () => (globalLevel = 3);
58
-
59
- /** Create a logger creator. Use the factory methods to finish making the logger */
60
- export const logger = (name: string, color?: string): LoggerFactory => {
61
- return {
62
- default: () => new LoggerImpl(name, color, LogLevel.High),
63
- debug: () => new LoggerImpl(name, color, LogLevel.Debug),
64
- info: () => new LoggerImpl(name, color, LogLevel.Info),
65
- off: () => new LoggerImpl(name, color, LogLevel.Off),
66
- };
67
- };
68
-
69
- export type LoggerFactory = {
70
- /** Standard important logger (warning and errors) */
71
- default(): Logger;
72
- /** Enable +info +debug logging for this logger */
73
- debug(): Logger;
74
- /** Enable +info logging for this logger */
75
- info(): Logger;
76
- /** Stop all logging, including warn and error */
77
- off(): Logger;
78
- };
79
-
80
- export type Logger = {
81
- /** Log a debug message */
82
- debug(obj: unknown): void;
83
- /** Log an info message */
84
- info(obj: unknown): void;
85
- /** Log a warning message */
86
- warn(obj: unknown): void;
87
- /** Log an error message */
88
- error(obj: unknown): void;
89
- };
90
-
91
- class LoggerImpl implements Logger {
92
- name: string;
93
- color: string | undefined;
94
- level: LogLevel;
95
-
96
- constructor(name: string, color: string | undefined, level: LogLevel) {
97
- this.name = name;
98
- this.color =
99
- "padding:0 3x;color:white" + (color ? `;background:${color}` : "");
100
- this.level = level;
101
- }
102
-
103
- debug(obj: unknown) {
104
- if (globalLevel !== LogLevel.Debug && this.level !== LogLevel.Debug) {
105
- return;
106
- }
107
- console.log(
108
- `%cDEBUG%c${this.name}%c ${obj}`,
109
- "background:gray;color:white;padding:0 3px",
110
- this.color,
111
- "color:inherit;background:inherit",
112
- );
113
- }
114
-
115
- info(obj: unknown) {
116
- if (globalLevel < LogLevel.Info && this.level < LogLevel.Info) {
117
- return;
118
- }
119
- console.log(
120
- `%cINFO%c${this.name}%c ${obj}`,
121
- "background:green;color:white;padding:0 3px",
122
- this.color,
123
- "color:inherit;background:inherit",
124
- );
125
- }
126
-
127
- warn(obj: unknown) {
128
- if (globalLevel < LogLevel.High || this.level < LogLevel.High) {
129
- return;
130
- }
131
- console.warn(
132
- `%cWARN%c${this.name}%c ${obj}`,
133
- "background:orange;color:white;padding:0 3px",
134
- this.color,
135
- "color:inherit;background:inherit",
136
- );
137
- }
138
-
139
- error(obj: unknown) {
140
- if (globalLevel < LogLevel.High || this.level < LogLevel.High) {
141
- return;
142
- }
143
- const msg = errstr(obj);
144
- console.error(
145
- `%cERROR%c${this.name}%c ${msg}`,
146
- "background:orange;color:white;padding:0 3px",
147
- this.color,
148
- "color:inherit;background:inherit",
149
- );
150
- if (msg !== obj) {
151
- console.error(obj);
152
- }
153
- }
154
- }
1
+ export {
2
+ globalLogOff,
3
+ globalLogInfo,
4
+ globalLogDebug,
5
+ logger,
6
+ type LoggerFactory,
7
+ type Logger,
8
+ } from "./logger.ts";
9
+ export {
10
+ internalLogOff,
11
+ internalLogDebug,
12
+ internalLogInfo,
13
+ } from "./internal.ts";
@@ -0,0 +1,18 @@
1
+ import { LoggerImpl, LogLevel } from "./logger.ts";
2
+
3
+ export const ilog: LoggerImpl = new LoggerImpl("pure", "gray", LogLevel.High);
4
+
5
+ /** Set the internal log level of calls in this library to off */
6
+ export const internalLogOff = () => {
7
+ ilog.level = LogLevel.Off;
8
+ };
9
+
10
+ /** Set the internal log level of calls in this library to debug */
11
+ export const internalLogDebug = () => {
12
+ ilog.level = LogLevel.Debug;
13
+ };
14
+
15
+ /** Set the internal log level of calls in this library to info */
16
+ export const internalLogInfo = () => {
17
+ ilog.level = LogLevel.Info;
18
+ };
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Client side log util
3
+ *
4
+ * This is rather simple logging stuff with the primary focus
5
+ * being easy to debug.
6
+ *
7
+ * Use {@link logger} to create a logger with a name and a color,
8
+ * then use one of the {@link LoggerFactory} methods to setup the logging level.
9
+ *
10
+ * There are 3 levels of logging:
11
+ * 1. (Default) Warnings and Errors only
12
+ * 2. Also log info messages
13
+ * 3. Also log debug messages.
14
+ *
15
+ * Each logger can turn on info and debug logging separately, or it can
16
+ * be turned on at the global level for debugging.
17
+ *
18
+ * You can also turn off each logger individually or at global level for debugging.
19
+ *
20
+ * Due to the nature of JS, all logging calls, even when turned off, will incur
21
+ * some small runtime overhead. While we could remove debug calls
22
+ * for release build, that's currently not done (and it would require
23
+ * bundler to inline the call to remove the call completely, which might
24
+ * not be the case)
25
+ *
26
+ * @module
27
+ */
28
+
29
+ import { errstr } from "../result/index.ts";
30
+
31
+ export const LogLevel = { Off: 0, High: 1, Info: 2, Debug: 3 } as const;
32
+ export type LogLevel = (typeof LogLevel)[keyof typeof LogLevel];
33
+
34
+ let globalLevel: LogLevel = LogLevel.High;
35
+ /**
36
+ * Suppress ALL logging.
37
+ *
38
+ * This overrides logger-level settings
39
+ */
40
+ export const globalLogOff = () => (globalLevel = 0);
41
+ /**
42
+ * Enable +info logging for ALL loggers
43
+ *
44
+ * This overrides logger-level settings
45
+ */
46
+ export const globalLogInfo = () => (globalLevel = 2);
47
+ /**
48
+ * Enable +debug logging for ALL loggers
49
+ *
50
+ * This overrides logger-level settings
51
+ */
52
+ export const globalLogDebug = () => (globalLevel = 3);
53
+
54
+ /** Create a logger creator. Use the factory methods to finish making the logger */
55
+ export const logger = (name: string, color?: string): LoggerFactory => {
56
+ return {
57
+ default: () => new LoggerImpl(name, color, LogLevel.High),
58
+ debug: () => new LoggerImpl(name, color, LogLevel.Debug),
59
+ info: () => new LoggerImpl(name, color, LogLevel.Info),
60
+ off: () => new LoggerImpl(name, color, LogLevel.Off),
61
+ };
62
+ };
63
+
64
+ export type LoggerFactory = {
65
+ /** Standard important logger (warning and errors) */
66
+ default(): Logger;
67
+ /** Enable +info +debug logging for this logger */
68
+ debug(): Logger;
69
+ /** Enable +info logging for this logger */
70
+ info(): Logger;
71
+ /** Stop all logging, including warn and error */
72
+ off(): Logger;
73
+ };
74
+
75
+ export type Logger = {
76
+ /** Log a debug message */
77
+ debug(obj: unknown): void;
78
+ /** Log an info message */
79
+ info(obj: unknown): void;
80
+ /** Log a warning message */
81
+ warn(obj: unknown): void;
82
+ /** Log an error message */
83
+ error(obj: unknown): void;
84
+ };
85
+
86
+ export class LoggerImpl implements Logger {
87
+ name: string;
88
+ color: string | undefined;
89
+ level: LogLevel;
90
+
91
+ constructor(name: string, color: string | undefined, level: LogLevel) {
92
+ this.name = name;
93
+ this.color =
94
+ "padding:0 3x;color:white" + (color ? `;background:${color}` : "");
95
+ this.level = level;
96
+ }
97
+
98
+ debug(obj: unknown) {
99
+ if (globalLevel !== LogLevel.Debug && this.level !== LogLevel.Debug) {
100
+ return;
101
+ }
102
+ console.debug(
103
+ `%cDEBUG%c${this.name}%c ${obj}`,
104
+ "background:gray;color:white;padding:0 3px",
105
+ this.color,
106
+ "color:inherit;background:inherit",
107
+ );
108
+ }
109
+
110
+ info(obj: unknown) {
111
+ if (globalLevel < LogLevel.Info && this.level < LogLevel.Info) {
112
+ return;
113
+ }
114
+ console.info(
115
+ `%cINFO%c${this.name}%c ${obj}`,
116
+ "background:green;color:white;padding:0 3px",
117
+ this.color,
118
+ "color:inherit;background:inherit",
119
+ );
120
+ }
121
+
122
+ warn(obj: unknown) {
123
+ if (globalLevel < LogLevel.High || this.level < LogLevel.High) {
124
+ return;
125
+ }
126
+ console.warn(
127
+ `%cWARN%c${this.name}%c ${obj}`,
128
+ "background:orange;color:white;padding:0 3px",
129
+ this.color,
130
+ "color:inherit;background:inherit",
131
+ );
132
+ }
133
+
134
+ error(obj: unknown) {
135
+ if (globalLevel < LogLevel.High || this.level < LogLevel.High) {
136
+ return;
137
+ }
138
+ const msg = errstr(obj);
139
+ console.error(
140
+ `%cERROR%c${this.name}%c ${msg}`,
141
+ "background:darkred;color:white;padding:0 3px",
142
+ this.color,
143
+ "color:inherit;background:inherit",
144
+ );
145
+ if (msg !== obj) {
146
+ console.error(obj);
147
+ }
148
+ }
149
+ }
@@ -2,10 +2,7 @@ import { describe, afterEach, expect, it } from "vitest";
2
2
 
3
3
  import { type Erc, makeErcType } from "./erc.ts";
4
4
 
5
- type Rc = {
6
- value: string;
7
- refCount: number;
8
- };
5
+ type Rc = { value: string; refCount: number };
9
6
  const Marker = Symbol("test");
10
7
  type Marker = typeof Marker;
11
8
  class Allocator {
@@ -74,10 +71,7 @@ class Allocator {
74
71
  }
75
72
 
76
73
  allocValue(value: string): number {
77
- const rc: Rc = {
78
- value,
79
- refCount: 1,
80
- };
74
+ const rc: Rc = { value, refCount: 1 };
81
75
  for (let i = 0; i < this.mockMemory.length; i++) {
82
76
  if (this.mockMemory[i] === undefined) {
83
77
  this.mockMemory[i] = rc;
@@ -6,7 +6,7 @@ export const safeidgen = (n: number): (() => number) => {
6
6
  let x = 1;
7
7
  return () => {
8
8
  x += 1;
9
- if (x === n) {
9
+ if (x >= n) {
10
10
  x = 1;
11
11
  }
12
12
  return x;
@@ -8,3 +8,4 @@ export { cell, type CellConstructor, type Cell } from "./cell.ts";
8
8
  export { persist, type PersistConstructor, type Persist } from "./persist.ts";
9
9
  export * from "./erc.ts";
10
10
  export * from "./async_erc.ts";
11
+ export * from "./idgen.ts";
package/src/sync/batch.ts CHANGED
@@ -166,12 +166,7 @@ class BatchImpl<TFn extends AnyFn> {
166
166
  return this.execute(...args);
167
167
  }
168
168
  const { promise, resolve, reject } = makePromise<AwaitRet<TFn>>();
169
- this.scheduled.push({
170
- input: args,
171
- promise,
172
- resolve,
173
- reject,
174
- });
169
+ this.scheduled.push({ input: args, promise, resolve, reject });
175
170
 
176
171
  return promise;
177
172
  }
@@ -120,9 +120,7 @@ export type DebounceConstructor<TFn> = {
120
120
 
121
121
  class DebounceImpl<TFn extends AnyFn> {
122
122
  private idle: boolean;
123
- private next?: PromiseHandle<AwaitRet<TFn>> & {
124
- args: Parameters<TFn>;
125
- };
123
+ private next?: PromiseHandle<AwaitRet<TFn>> & { args: Parameters<TFn> };
126
124
  constructor(
127
125
  private fn: TFn,
128
126
  private interval: number,
@@ -47,9 +47,7 @@ test("passing in arguments", async () => {
47
47
  });
48
48
 
49
49
  test("current serial number", async () => {
50
- const execute = serial({
51
- fn: (_, serial) => () => serial,
52
- });
50
+ const execute = serial({ fn: (_, serial) => () => serial });
53
51
 
54
52
  const one = await execute();
55
53
  expect(one).toStrictEqual({ val: 1n });
package/src/sync/util.ts CHANGED
@@ -14,11 +14,7 @@ export const makePromise = <T>(): {
14
14
  "Promise callbacks not set. This is a bug in the JS engine!",
15
15
  );
16
16
  }
17
- return {
18
- promise,
19
- resolve,
20
- reject,
21
- };
17
+ return { promise, resolve, reject };
22
18
  };
23
19
 
24
20
  export type PromiseHandle<T> = ReturnType<typeof makePromise<T>>;