@rg-dev/stdlib 1.0.18 → 1.0.20
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/lib/common-env.cjs +29 -0
- package/lib/common-env.d.cts +39 -2
- package/lib/common-env.d.ts +39 -2
- package/lib/common-env.js +29 -0
- package/package.json +10 -4
package/lib/common-env.cjs
CHANGED
|
@@ -20,8 +20,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
var common_env_exports = {};
|
|
21
21
|
__export(common_env_exports, {
|
|
22
22
|
Optional: () => Optional,
|
|
23
|
+
StringBuilder: () => StringBuilder,
|
|
23
24
|
catchInline: () => catchInline,
|
|
24
25
|
doSafe: () => doSafe,
|
|
26
|
+
isNonEmptyString: () => isNonEmptyString,
|
|
25
27
|
isNumber: () => isNumber,
|
|
26
28
|
isRunningOnServer: () => isRunningOnServer,
|
|
27
29
|
promiseRetry: () => promiseRetry,
|
|
@@ -59,6 +61,30 @@ function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
|
59
61
|
);
|
|
60
62
|
}
|
|
61
63
|
|
|
64
|
+
// src/StringBuilder.ts
|
|
65
|
+
var StringBuilder = class {
|
|
66
|
+
_chunks;
|
|
67
|
+
constructor() {
|
|
68
|
+
this._chunks = [];
|
|
69
|
+
}
|
|
70
|
+
/** {@inheritDoc IStringBuilder.append} */
|
|
71
|
+
append(text) {
|
|
72
|
+
this._chunks.push(text);
|
|
73
|
+
}
|
|
74
|
+
/** {@inheritDoc IStringBuilder.toString} */
|
|
75
|
+
toString() {
|
|
76
|
+
if (this._chunks.length === 0) {
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
if (this._chunks.length > 1) {
|
|
80
|
+
const joined = this._chunks.join("");
|
|
81
|
+
this._chunks.length = 1;
|
|
82
|
+
this._chunks[0] = joined;
|
|
83
|
+
}
|
|
84
|
+
return this._chunks[0];
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
62
88
|
// src/common-env.ts
|
|
63
89
|
var Optional = class _Optional {
|
|
64
90
|
value;
|
|
@@ -165,3 +191,6 @@ async function useServer(fn, onError) {
|
|
|
165
191
|
function isPromise(value) {
|
|
166
192
|
return Boolean(value && typeof value.then === "function");
|
|
167
193
|
}
|
|
194
|
+
function isNonEmptyString(str) {
|
|
195
|
+
return typeof str == "string" && str.trim().length > 0;
|
|
196
|
+
}
|
package/lib/common-env.d.cts
CHANGED
|
@@ -5,6 +5,42 @@ type Options = {
|
|
|
5
5
|
};
|
|
6
6
|
declare function promiseRetry<T>(func: (attempt: number) => Promise<T>, options?: Partial<Options>, attempt?: number): Promise<T>;
|
|
7
7
|
|
|
8
|
+
interface IStringBuilder {
|
|
9
|
+
/**
|
|
10
|
+
* Append the specified text to the buffer.
|
|
11
|
+
*/
|
|
12
|
+
append(text: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a single string containing all the text that was appended to the buffer so far.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
*
|
|
18
|
+
* This is a potentially expensive operation.
|
|
19
|
+
*/
|
|
20
|
+
toString(): string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* This class allows a large text string to be constructed incrementally by appending small chunks. The final
|
|
24
|
+
* string can be obtained by calling StringBuilder.toString().
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* A naive approach might use the `+=` operator to append strings: This would have the downside of copying
|
|
28
|
+
* the entire string each time a chunk is appended, resulting in `O(n^2)` bytes of memory being allocated
|
|
29
|
+
* (and later freed by the garbage collector), and many of the allocations could be very large objects.
|
|
30
|
+
* StringBuilder avoids this overhead by accumulating the chunks in an array, and efficiently joining them
|
|
31
|
+
* when `getText()` is finally called.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
declare class StringBuilder implements IStringBuilder {
|
|
36
|
+
private _chunks;
|
|
37
|
+
constructor();
|
|
38
|
+
/** {@inheritDoc IStringBuilder.append} */
|
|
39
|
+
append(text: string): void;
|
|
40
|
+
/** {@inheritDoc IStringBuilder.toString} */
|
|
41
|
+
toString(): string;
|
|
42
|
+
}
|
|
43
|
+
|
|
8
44
|
type MyFn<T extends Record<string, (...args: any[]) => any>> = {
|
|
9
45
|
[K in keyof T]: (...args: Parameters<T[K]>) => void;
|
|
10
46
|
};
|
|
@@ -31,6 +67,7 @@ declare function catchInline<T>(cb: Promise<T>): Promise<{
|
|
|
31
67
|
}>;
|
|
32
68
|
declare function doSafe(safe: () => Promise<void> | void, onError?: (error: Error) => void): void;
|
|
33
69
|
declare function isRunningOnServer(): boolean;
|
|
34
|
-
declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
|
|
70
|
+
declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
|
|
71
|
+
declare function isNonEmptyString(str?: string): boolean;
|
|
35
72
|
|
|
36
|
-
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, catchInline, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
|
73
|
+
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
package/lib/common-env.d.ts
CHANGED
|
@@ -5,6 +5,42 @@ type Options = {
|
|
|
5
5
|
};
|
|
6
6
|
declare function promiseRetry<T>(func: (attempt: number) => Promise<T>, options?: Partial<Options>, attempt?: number): Promise<T>;
|
|
7
7
|
|
|
8
|
+
interface IStringBuilder {
|
|
9
|
+
/**
|
|
10
|
+
* Append the specified text to the buffer.
|
|
11
|
+
*/
|
|
12
|
+
append(text: string): void;
|
|
13
|
+
/**
|
|
14
|
+
* Returns a single string containing all the text that was appended to the buffer so far.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
*
|
|
18
|
+
* This is a potentially expensive operation.
|
|
19
|
+
*/
|
|
20
|
+
toString(): string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* This class allows a large text string to be constructed incrementally by appending small chunks. The final
|
|
24
|
+
* string can be obtained by calling StringBuilder.toString().
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* A naive approach might use the `+=` operator to append strings: This would have the downside of copying
|
|
28
|
+
* the entire string each time a chunk is appended, resulting in `O(n^2)` bytes of memory being allocated
|
|
29
|
+
* (and later freed by the garbage collector), and many of the allocations could be very large objects.
|
|
30
|
+
* StringBuilder avoids this overhead by accumulating the chunks in an array, and efficiently joining them
|
|
31
|
+
* when `getText()` is finally called.
|
|
32
|
+
*
|
|
33
|
+
* @public
|
|
34
|
+
*/
|
|
35
|
+
declare class StringBuilder implements IStringBuilder {
|
|
36
|
+
private _chunks;
|
|
37
|
+
constructor();
|
|
38
|
+
/** {@inheritDoc IStringBuilder.append} */
|
|
39
|
+
append(text: string): void;
|
|
40
|
+
/** {@inheritDoc IStringBuilder.toString} */
|
|
41
|
+
toString(): string;
|
|
42
|
+
}
|
|
43
|
+
|
|
8
44
|
type MyFn<T extends Record<string, (...args: any[]) => any>> = {
|
|
9
45
|
[K in keyof T]: (...args: Parameters<T[K]>) => void;
|
|
10
46
|
};
|
|
@@ -31,6 +67,7 @@ declare function catchInline<T>(cb: Promise<T>): Promise<{
|
|
|
31
67
|
}>;
|
|
32
68
|
declare function doSafe(safe: () => Promise<void> | void, onError?: (error: Error) => void): void;
|
|
33
69
|
declare function isRunningOnServer(): boolean;
|
|
34
|
-
declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
|
|
70
|
+
declare function useServer(fn: () => (Promise<any> | void), onError?: (err: Error) => void): Promise<any>;
|
|
71
|
+
declare function isNonEmptyString(str?: string): boolean;
|
|
35
72
|
|
|
36
|
-
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, catchInline, doSafe, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
|
73
|
+
export { type AsyncReturnType, type MaybeFunction, type MyFn, Optional, StringBuilder, catchInline, doSafe, isNonEmptyString, isNumber, isRunningOnServer, promiseRetry, promiseWithTimeout, sleep, useServer };
|
package/lib/common-env.js
CHANGED
|
@@ -26,6 +26,30 @@ function promiseRetry(func, options = defaultOptions, attempt = 1) {
|
|
|
26
26
|
);
|
|
27
27
|
}
|
|
28
28
|
|
|
29
|
+
// src/StringBuilder.ts
|
|
30
|
+
var StringBuilder = class {
|
|
31
|
+
_chunks;
|
|
32
|
+
constructor() {
|
|
33
|
+
this._chunks = [];
|
|
34
|
+
}
|
|
35
|
+
/** {@inheritDoc IStringBuilder.append} */
|
|
36
|
+
append(text) {
|
|
37
|
+
this._chunks.push(text);
|
|
38
|
+
}
|
|
39
|
+
/** {@inheritDoc IStringBuilder.toString} */
|
|
40
|
+
toString() {
|
|
41
|
+
if (this._chunks.length === 0) {
|
|
42
|
+
return "";
|
|
43
|
+
}
|
|
44
|
+
if (this._chunks.length > 1) {
|
|
45
|
+
const joined = this._chunks.join("");
|
|
46
|
+
this._chunks.length = 1;
|
|
47
|
+
this._chunks[0] = joined;
|
|
48
|
+
}
|
|
49
|
+
return this._chunks[0];
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
29
53
|
// src/common-env.ts
|
|
30
54
|
var Optional = class _Optional {
|
|
31
55
|
value;
|
|
@@ -132,10 +156,15 @@ async function useServer(fn, onError) {
|
|
|
132
156
|
function isPromise(value) {
|
|
133
157
|
return Boolean(value && typeof value.then === "function");
|
|
134
158
|
}
|
|
159
|
+
function isNonEmptyString(str) {
|
|
160
|
+
return typeof str == "string" && str.trim().length > 0;
|
|
161
|
+
}
|
|
135
162
|
export {
|
|
136
163
|
Optional,
|
|
164
|
+
StringBuilder,
|
|
137
165
|
catchInline,
|
|
138
166
|
doSafe,
|
|
167
|
+
isNonEmptyString,
|
|
139
168
|
isNumber,
|
|
140
169
|
isRunningOnServer,
|
|
141
170
|
promiseRetry,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rg-dev/stdlib",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.20",
|
|
4
4
|
"description": "",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"files": [
|
|
12
12
|
"lib"
|
|
13
13
|
],
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
14
17
|
"typesVersions": {
|
|
15
18
|
"*": {
|
|
16
19
|
"lib/common-env": [
|
|
@@ -29,15 +32,18 @@
|
|
|
29
32
|
"exports": {
|
|
30
33
|
"./lib/node-env": {
|
|
31
34
|
"import": "./lib/node-env.js",
|
|
32
|
-
"require": "./lib/node-env.cjs"
|
|
35
|
+
"require": "./lib/node-env.cjs",
|
|
36
|
+
"types": "./lib/node-env.d.ts"
|
|
33
37
|
},
|
|
34
38
|
"./lib/common-env": {
|
|
35
39
|
"import": "./lib/common-env.js",
|
|
36
|
-
"require": "./lib/common-env.cjs"
|
|
40
|
+
"require": "./lib/common-env.cjs",
|
|
41
|
+
"types": "./lib/common-env.d.ts"
|
|
37
42
|
},
|
|
38
43
|
"./lib/node-download": {
|
|
39
44
|
"import": "./lib/node-download.js",
|
|
40
|
-
"require": "./lib/node-download.cjs"
|
|
45
|
+
"require": "./lib/node-download.cjs",
|
|
46
|
+
"types": "./lib/node-download.d.ts"
|
|
41
47
|
}
|
|
42
48
|
},
|
|
43
49
|
"dependencies": {
|