@goodbyenjn/utils 26.4.0 → 26.4.2

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.
@@ -159,6 +159,7 @@ declare class Result<T = unknown, E = unknown> {
159
159
  }
160
160
  declare const ok: typeof Result.ok;
161
161
  declare const err: typeof Result.err;
162
+ declare const isResult: typeof Result.is;
162
163
  //#endregion
163
164
  //#region src/result/error.d.ts
164
165
  declare class ResultError extends Error {
@@ -171,4 +172,4 @@ declare class ResultError extends Error {
171
172
  toString(): string;
172
173
  }
173
174
  //#endregion
174
- export { err as a, ExtractOkTypes as c, ResultAll as d, Result as i, InferErrType as l, Err as n, ok as o, Ok as r, ExtractErrTypes as s, ResultError as t, InferOkType as u };
175
+ export { err as a, ExtractErrTypes as c, InferOkType as d, ResultAll as f, Result as i, ExtractOkTypes as l, Err as n, isResult as o, Ok as r, ok as s, ResultError as t, InferErrType as u };
package/dist/common.d.ts CHANGED
@@ -1,187 +1,2 @@
1
- import { i as Result } from "./chunks/chunk-704a1835.js";
2
- import { l as Fn, p as TemplateFn, s as AsyncFn } from "./chunks/chunk-3c6f28c7.js";
3
-
4
- //#region src/common/error.d.ts
5
- declare const normalizeError: (error: unknown, caller?: Function) => Error;
6
- declare const getErrorMessage: (error: unknown, message?: string) => string;
7
- //#endregion
8
- //#region node_modules/.pnpm/safe-stable-stringify@2.5.0/node_modules/safe-stable-stringify/index.d.ts
9
- type Replacer = (number | string)[] | null | undefined | ((key: string, value: unknown) => string | number | boolean | null | object);
10
- declare function stringify$1(value: undefined | symbol | ((...args: unknown[]) => unknown), replacer?: Replacer, space?: string | number): undefined;
11
- declare function stringify$1(value: string | number | unknown[] | null | boolean | object, replacer?: Replacer, space?: string | number): string;
12
- declare function stringify$1(value: unknown, replacer?: ((key: string, value: unknown) => unknown) | (number | string)[] | null | undefined, space?: string | number): string | undefined;
13
- interface StringifyOptions {
14
- bigint?: boolean;
15
- circularValue?: string | null | TypeErrorConstructor | ErrorConstructor;
16
- deterministic?: boolean | ((a: string, b: string) => number);
17
- maximumBreadth?: number;
18
- maximumDepth?: number;
19
- strict?: boolean;
20
- }
21
- declare namespace stringify$1 {
22
- export function configure(options: StringifyOptions): typeof stringify$1;
23
- }
24
- //#endregion
25
- //#region src/common/json.d.ts
26
- type Stringify = typeof stringify$1;
27
- declare const stringify: Stringify;
28
- declare const safeParse: <T = any>(text: string, reviver?: (this: any, key: string, value: any) => any) => Result<T, Error>;
29
- declare const unsafeParse: <T = any>(text: string, reviver?: (this: any, key: string, value: any) => any) => T | undefined;
30
- //#endregion
31
- //#region src/common/math.d.ts
32
- /**
33
- * @example
34
- * ```
35
- * const value = linear(0.5, [0, 2]) // value: 1
36
- * ```
37
- */
38
- declare const linear: (value: number, range: [min: number, max: number]) => number;
39
- /**
40
- * @example
41
- * ```
42
- * const value = scale(0.5, [0, 1], [200, 400]) // value: 300
43
- * ```
44
- */
45
- declare const scale: (value: number, inRange: [min: number, max: number], outRange: [min: number, max: number]) => number;
46
- //#endregion
47
- //#region src/common/parse.d.ts
48
- declare const parseKeyValuePairs: (input: string) => Record<string, string>;
49
- declare const parseValueToBoolean: <T>(value: unknown, defaultValue: T | boolean) => T | boolean;
50
- //#endregion
51
- //#region src/common/promise.d.ts
52
- interface Singleton<T> {
53
- (): Promise<T>;
54
- reset: () => Promise<void>;
55
- }
56
- interface Lock {
57
- run: <T = void>(fn: AsyncFn<T>) => Promise<T>;
58
- wait: () => Promise<void>;
59
- isWaiting: () => boolean;
60
- clear: () => void;
61
- }
62
- interface PromiseWithResolvers<T> {
63
- promise: Promise<T>;
64
- resolve: (value: T | PromiseLike<T>) => void;
65
- reject: (reason?: any) => void;
66
- }
67
- declare const sleep: (ms: number, callback?: Fn) => Promise<void>;
68
- declare const createSingleton: <T>(fn: AsyncFn<T>) => Singleton<T>;
69
- /**
70
- * @example
71
- * ```
72
- * const lock = createLock()
73
- *
74
- * lock.run(async () => {
75
- * await doSomething()
76
- * })
77
- *
78
- * // in anther context:
79
- * await lock.wait() // it will wait all tasking finished
80
- * ```
81
- */
82
- declare const createLock: () => Lock;
83
- declare const createPromiseWithResolvers: <T>() => PromiseWithResolvers<T>;
84
- //#endregion
85
- //#region src/common/string.d.ts
86
- declare const addPrefix: (prefix: string, str: string) => string;
87
- declare const addSuffix: (suffix: string, str: string) => string;
88
- declare const removePrefix: (prefix: string, str: string) => string;
89
- declare const removeSuffix: (suffix: string, str: string) => string;
90
- declare const join: (separator: string, ...paths: string[]) => string;
91
- declare const split: (separator: string, path: string) => string[];
92
- declare const toForwardSlash: (str: string) => string;
93
- declare const joinWithSlash: (...paths: string[]) => string;
94
- declare const splitWithSlash: (path: string) => string[];
95
- declare const splitByLineBreak: (str: string) => string[];
96
- declare const concatTemplateStrings: (template: TemplateStringsArray, values: any[]) => string;
97
- /**
98
- * @example
99
- * ```ts
100
- * // Default behavior: trim both start and end
101
- * const str1 = unindent`
102
- * if (a) {
103
- * b()
104
- * }
105
- * `;
106
- *
107
- * // Factory function: custom trim behavior
108
- * const str2 = unindent(false, false)`
109
- * if (a) {
110
- * b()
111
- * }
112
- * `;
113
- *
114
- * // Only trim start, keep end
115
- * const str3 = unindent(true, false)(" hello\n world\n");
116
- * ```
117
- */
118
- declare function unindent(str: string): string;
119
- declare function unindent(template: TemplateStringsArray, ...values: any[]): string;
120
- declare function unindent(trimStart?: boolean, trimEnd?: boolean): TemplateFn<string> & ((str: string) => string);
121
- /**
122
- * @example
123
- * ```ts
124
- * // Using indent count with default space character
125
- * const str1 = indent(2)`
126
- * if (a) {
127
- * b()
128
- * }
129
- * `;
130
- *
131
- * // Using custom indent string directly
132
- * const str2 = indent(">>")`
133
- * if (a) {
134
- * b()
135
- * }
136
- * `;
137
- *
138
- * // Only trim start, keep end
139
- * const str3 = indent(2, true, false)("hello\nworld\n");
140
- * ```
141
- */
142
- declare function indent(indentNumber: number, trimStart?: boolean, trimEnd?: boolean): TemplateFn<string> & ((str: string) => string);
143
- declare function indent(indentString: string, trimStart?: boolean, trimEnd?: boolean): TemplateFn<string> & ((str: string) => string);
144
- /**
145
- * @example
146
- * ```
147
- * const result = template(
148
- * 'Hello {0}! My name is {1}.',
149
- * 'World',
150
- * 'Alice'
151
- * ) // Hello World! My name is Alice.
152
- * ```
153
- *
154
- * ```
155
- * const result = template(
156
- * '{greet}! My name is {name}.',
157
- * { greet: 'Hello', name: 'Alice' }
158
- * ) // Hello! My name is Alice.
159
- * ```
160
- *
161
- * const result = template(
162
- * '{greet}! My name is {name}.',
163
- * { greet: 'Hello' }, // name isn't passed hence fallback will be used for name
164
- * 'placeholder'
165
- * ) // Hello! My name is placeholder.
166
- * ```
167
- */
168
- declare function template(str: string, mapping: Record<string | number, any>, fallback?: string | ((key: string) => string)): string;
169
- declare function template(str: string, ...args: (string | number | bigint | undefined | null)[]): string;
170
- //#endregion
171
- //#region src/common/throttle.d.ts
172
- interface WrappedFn<T extends Fn> {
173
- (...args: Parameters<T>): void;
174
- cancel: Fn<void>;
175
- }
176
- interface Options {
177
- leading?: boolean;
178
- trailing?: boolean;
179
- }
180
- type DebouncedFn<T extends Fn> = WrappedFn<T>;
181
- type ThrottledFn<T extends Fn> = WrappedFn<T>;
182
- type DebounceOptions = Options;
183
- type ThrottleOptions = Options;
184
- declare const debounce: <T extends Fn>(fn: T, wait?: number, options?: DebounceOptions) => DebouncedFn<T>;
185
- declare const throttle: <T extends Fn>(fn: T, wait?: number, options?: ThrottleOptions) => ThrottledFn<T>;
186
- //#endregion
187
- export { type DebounceOptions, type DebouncedFn, type Lock, type PromiseWithResolvers, type Singleton, type Stringify, type ThrottleOptions, type ThrottledFn, addPrefix, addSuffix, concatTemplateStrings, createLock, createPromiseWithResolvers, createSingleton, debounce, getErrorMessage, indent, join, joinWithSlash, linear, normalizeError, unsafeParse as parse, parseKeyValuePairs, parseValueToBoolean, removePrefix, removeSuffix, safeParse, scale, sleep, split, splitByLineBreak, splitWithSlash, stringify, template, throttle, toForwardSlash, unindent };
1
+ import { A as Nil, C as Singleton, D as sleep, E as createSingleton, F as getErrorMessage, I as normalizeError, M as nil, N as linear, O as parseKeyValuePairs, P as scale, S as PromiseWithResolvers, T as createPromiseWithResolvers, _ as splitWithSlash, a as debounce, b as unindent, c as addSuffix, d as join, f as joinWithSlash, g as splitByLineBreak, h as split, i as ThrottledFn, j as isNil, k as parseValueToBoolean, l as concatTemplateStrings, m as removeSuffix, n as DebouncedFn, o as throttle, p as removePrefix, r as ThrottleOptions, s as addPrefix, t as DebounceOptions, u as indent, v as template, w as createLock, x as Lock, y as toForwardSlash } from "./chunks/chunk-71e0c144.js";
2
+ export { DebounceOptions, DebouncedFn, Lock, Nil, PromiseWithResolvers, Singleton, ThrottleOptions, ThrottledFn, addPrefix, addSuffix, concatTemplateStrings, createLock, createPromiseWithResolvers, createSingleton, debounce, getErrorMessage, indent, isNil, join, joinWithSlash, linear, nil, normalizeError, parseKeyValuePairs, parseValueToBoolean, removePrefix, removeSuffix, scale, sleep, split, splitByLineBreak, splitWithSlash, template, throttle, toForwardSlash, unindent };
package/dist/common.js CHANGED
@@ -1,3 +1,2 @@
1
- import { C as linear, D as safeParse, E as normalizeError, O as stringify, S as parseValueToBoolean, T as getErrorMessage, _ as createLock, a as concatTemplateStrings, b as sleep, c as joinWithSlash, d as split, f as splitByLineBreak, g as unindent, h as toForwardSlash, i as addSuffix, k as unsafeParse, l as removePrefix, m as template, n as throttle, o as indent, p as splitWithSlash, r as addPrefix, s as join, t as debounce, u as removeSuffix, v as createPromiseWithResolvers, w as scale, x as parseKeyValuePairs, y as createSingleton } from "./chunks/chunk-486f65b0.js";
2
-
3
- export { addPrefix, addSuffix, concatTemplateStrings, createLock, createPromiseWithResolvers, createSingleton, debounce, getErrorMessage, indent, join, joinWithSlash, linear, normalizeError, unsafeParse as parse, parseKeyValuePairs, parseValueToBoolean, removePrefix, removeSuffix, safeParse, scale, sleep, split, splitByLineBreak, splitWithSlash, stringify, template, throttle, toForwardSlash, unindent };
1
+ import { C as isNil, D as getErrorMessage, E as scale, O as normalizeError, S as parseValueToBoolean, T as linear, _ as createLock, a as concatTemplateStrings, b as sleep, c as joinWithSlash, d as split, f as splitByLineBreak, g as unindent, h as toForwardSlash, i as addSuffix, l as removePrefix, m as template, n as throttle, o as indent, p as splitWithSlash, r as addPrefix, s as join, t as debounce, u as removeSuffix, v as createPromiseWithResolvers, w as nil, x as parseKeyValuePairs, y as createSingleton } from "./chunks/chunk-11b9216b.js";
2
+ export { addPrefix, addSuffix, concatTemplateStrings, createLock, createPromiseWithResolvers, createSingleton, debounce, getErrorMessage, indent, isNil, join, joinWithSlash, linear, nil, normalizeError, parseKeyValuePairs, parseValueToBoolean, removePrefix, removeSuffix, scale, sleep, split, splitByLineBreak, splitWithSlash, template, throttle, toForwardSlash, unindent };
package/dist/exec.d.ts ADDED
@@ -0,0 +1,129 @@
1
+ import { o as Nullable, p as TemplateFn, t as index_d_exports } from "./chunks/chunk-3c6f28c7.js";
2
+ import { i as Result } from "./chunks/chunk-bd9f56dd.js";
3
+ import { ChildProcess, SpawnOptions } from "node:child_process";
4
+ import { Readable } from "node:stream";
5
+
6
+ //#region src/exec/types.d.ts
7
+ type KillSignal = Parameters<ChildProcess["kill"]>[0];
8
+ interface Output {
9
+ stdout: string;
10
+ stderr: string;
11
+ exitCode: number | undefined;
12
+ }
13
+ interface BaseProcessOptions {
14
+ stdin: BaseProcessInstance | string;
15
+ signal: AbortSignal;
16
+ spawnOptions: SpawnOptions;
17
+ timeout: number;
18
+ persist: boolean;
19
+ throwOnError: boolean;
20
+ }
21
+ interface BaseProcessInstance extends PromiseLike<any>, AsyncIterable<any> {
22
+ get process(): ChildProcess | undefined;
23
+ get aborted(): boolean;
24
+ get killed(): boolean;
25
+ get pid(): number | undefined;
26
+ get exitCode(): number | undefined;
27
+ exec: (...params: any[]) => any;
28
+ pipe: (...params: any[]) => any;
29
+ kill: (signal?: KillSignal) => boolean;
30
+ }
31
+ type ExecParams = ExecSpawnParams | ExecCommandTemplateParams | ExecCommandStringParams | ExecFactoryParams;
32
+ type BaseExec = BaseExecSpawn & BaseExecCommandTemplate & BaseExecCommandString & BaseExecFactory;
33
+ type ExecSpawnParams = [command: string, args?: string[], options?: Partial<BaseProcessOptions>];
34
+ type BaseExecSpawn = (...params: ExecSpawnParams) => unknown;
35
+ type ExecCommandTemplateParams = Parameters<TemplateFn>;
36
+ type BaseExecCommandTemplate = (...params: ExecCommandTemplateParams) => unknown;
37
+ type ExecCommandStringParams = [command: string];
38
+ type BaseExecCommandString = (...params: ExecCommandStringParams) => unknown;
39
+ type ExecFactoryParams = [options: Partial<BaseProcessOptions>];
40
+ type BaseExecFactory = (...params: ExecFactoryParams) => BaseExecCommandTemplate & BaseExecCommandString;
41
+ //#endregion
42
+ //#region src/exec/error.d.ts
43
+ declare class NonZeroExitError extends Error {
44
+ static is(value: unknown): value is NonZeroExitError;
45
+ readonly result: BaseProcessInstance;
46
+ readonly output?: Output;
47
+ get exitCode(): number | undefined;
48
+ constructor(result: BaseProcessInstance, output?: Output);
49
+ }
50
+ //#endregion
51
+ //#region src/exec/process.d.ts
52
+ declare abstract class BaseProcess implements BaseProcessInstance {
53
+ protected static execImpl(self: BaseProcess, params: ExecParams): BaseExec;
54
+ protected _process: ChildProcess | undefined;
55
+ protected _aborted: boolean;
56
+ protected options: Partial<BaseProcessOptions>;
57
+ protected streamOut: Nullable<Readable>;
58
+ protected streamErr: Nullable<Readable>;
59
+ protected thrownError?: Error;
60
+ protected closeProcess: () => void;
61
+ protected processClosed: Promise<void>;
62
+ get process(): ChildProcess | undefined;
63
+ get aborted(): boolean;
64
+ get killed(): boolean;
65
+ get pid(): number | undefined;
66
+ get exitCode(): number | undefined;
67
+ constructor(options?: Partial<BaseProcessOptions>);
68
+ exec: any;
69
+ pipe: any;
70
+ kill(signal?: KillSignal): boolean;
71
+ then<TResult1 = any, TResult2 = never>(onfulfilled?: ((value: any) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
72
+ protected spawn(command: string, args: string[]): this;
73
+ protected cleanup(): Promise<void>;
74
+ abstract [Symbol.asyncIterator](): AsyncIterator<any>;
75
+ protected abstract thenImpl(): Promise<any>;
76
+ protected abstract construct(): BaseProcess;
77
+ }
78
+ //#endregion
79
+ //#region src/exec/safe/types.d.ts
80
+ type ProcessOptions$1 = BaseProcessOptions & ExtendProcessOptions$1;
81
+ interface ExtendProcessOptions$1 {
82
+ stdin: ProcessInstance$1 | string;
83
+ }
84
+ type ProcessInstance$1 = BaseProcessInstance & ExtendProcessInstance$1;
85
+ interface ExtendProcessInstance$1 extends PromiseLike<Result<Output, Error>>, AsyncIterable<Result<string, Error>> {
86
+ exec: Exec$1;
87
+ pipe: Exec$1;
88
+ }
89
+ type Exec$1 = index_d_exports.SetReturnType<BaseExecSpawn, ProcessInstance$1> & index_d_exports.SetReturnType<BaseExecCommandTemplate, ProcessInstance$1> & index_d_exports.SetReturnType<BaseExecCommandString, ProcessInstance$1> & index_d_exports.SetReturnType<BaseExecFactory, index_d_exports.SetReturnType<BaseExecCommandTemplate, ProcessInstance$1> & index_d_exports.SetReturnType<BaseExecCommandString, ProcessInstance$1>>;
90
+ //#endregion
91
+ //#region src/exec/safe/index.d.ts
92
+ declare class Process$1 extends BaseProcess implements ProcessInstance$1 {
93
+ exec: Exec$1;
94
+ pipe: Exec$1;
95
+ protected options: Partial<ProcessOptions$1>;
96
+ constructor(options?: Partial<ProcessOptions$1>);
97
+ [Symbol.asyncIterator](): AsyncIterator<Result<string, Error>>;
98
+ protected thenImpl(): Promise<Result<Output, Error>>;
99
+ protected maybeThrow(output?: Output): Result<void, Error>;
100
+ protected construct(): Process$1;
101
+ }
102
+ declare const exec$1: Exec$1;
103
+ //#endregion
104
+ //#region src/exec/unsafe/types.d.ts
105
+ type ProcessOptions = BaseProcessOptions & ExtendProcessOptions;
106
+ interface ExtendProcessOptions {
107
+ stdin: ProcessInstance;
108
+ }
109
+ type ProcessInstance = BaseProcessInstance & ExtendProcessInstance;
110
+ interface ExtendProcessInstance extends PromiseLike<Output>, AsyncIterable<string> {
111
+ exec: Exec;
112
+ pipe: Exec;
113
+ }
114
+ type Exec = index_d_exports.SetReturnType<BaseExecSpawn, ProcessInstance> & index_d_exports.SetReturnType<BaseExecCommandTemplate, ProcessInstance> & index_d_exports.SetReturnType<BaseExecCommandString, ProcessInstance> & index_d_exports.SetReturnType<BaseExecFactory, index_d_exports.SetReturnType<BaseExecCommandTemplate, ProcessInstance> & index_d_exports.SetReturnType<BaseExecCommandString, ProcessInstance>>;
115
+ //#endregion
116
+ //#region src/exec/unsafe/index.d.ts
117
+ declare class Process extends BaseProcess implements ProcessInstance {
118
+ exec: Exec;
119
+ pipe: Exec;
120
+ protected options: Partial<ProcessOptions>;
121
+ constructor(options?: Partial<ProcessOptions>);
122
+ [Symbol.asyncIterator](): AsyncIterator<string>;
123
+ protected thenImpl(): Promise<Output>;
124
+ protected maybeThrow(output?: Output): void;
125
+ protected construct(): Process;
126
+ }
127
+ declare const exec: Exec;
128
+ //#endregion
129
+ export { type Exec, NonZeroExitError, type Output, Process, type ProcessInstance, type ProcessOptions, type Exec$1 as SafeExec, Process$1 as SafeProcess, type ProcessInstance$1 as SafeProcessInstance, type ProcessOptions$1 as SafeProcessOptions, exec, exec$1 as safeExec };