@nu-art/ts-common 0.500.0 → 0.500.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/core/error-handling.d.ts +2 -2
- package/core/module-manager.d.ts +3 -3
- package/core/module-manager.js +3 -1
- package/core/module.d.ts +13 -13
- package/core/module.js +7 -10
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/mem-cache/MemCache.js +2 -3
- package/package.json +7 -7
- package/testing/workspace-creator.d.ts +1 -1
- package/testing.d.ts +1 -1
- package/testing.js +1 -1
- package/utils/date-time-tools.d.ts +14 -0
- package/utils/date-time-tools.js +53 -0
- package/utils/query-params.d.ts +1 -1
- package/utils/query-params.js +4 -4
- package/utils/string-tools.d.ts +10 -0
- package/utils/string-tools.js +45 -0
- package/validator/type-validators.d.ts +2 -0
- package/validator/type-validators.js +2 -0
- package/mem-cache/index.d.ts +0 -1
- package/mem-cache/index.js +0 -6
- package/mem-storage/index.d.ts +0 -1
- package/mem-storage/index.js +0 -1
- package/testing/index.d.ts +0 -1
- package/testing/index.js +0 -1
- package/utils/index.d.ts +0 -27
- package/utils/index.js +0 -27
package/core/error-handling.d.ts
CHANGED
|
@@ -47,7 +47,7 @@ export interface OnApplicationNotification {
|
|
|
47
47
|
* Use this to notify all modules that implement `OnApplicationNotification`
|
|
48
48
|
* about application-level events.
|
|
49
49
|
*/
|
|
50
|
-
export declare const dispatch_onApplicationNotification: Dispatcher<OnApplicationNotification, "__processApplicationNotification", [errorLevel: ServerErrorSeverity, module: Module<any
|
|
50
|
+
export declare const dispatch_onApplicationNotification: Dispatcher<OnApplicationNotification, "__processApplicationNotification", [errorLevel: ServerErrorSeverity, module: Module<any>, message: ErrorMessage], void>;
|
|
51
51
|
/**
|
|
52
52
|
* Interface for modules that handle application exceptions.
|
|
53
53
|
*
|
|
@@ -69,4 +69,4 @@ export interface OnApplicationException {
|
|
|
69
69
|
* Use this to notify all modules that implement `OnApplicationException`
|
|
70
70
|
* about exceptions that occur at the application level.
|
|
71
71
|
*/
|
|
72
|
-
export declare const dispatch_onApplicationException: Dispatcher<OnApplicationException, "__processApplicationException", [e: CustomException, module: Module<any
|
|
72
|
+
export declare const dispatch_onApplicationException: Dispatcher<OnApplicationException, "__processApplicationException", [e: CustomException, module: Module<any>], void>;
|
package/core/module-manager.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { Logger } from './logger/index.js';
|
|
|
8
8
|
*
|
|
9
9
|
* @internal
|
|
10
10
|
*/
|
|
11
|
-
export declare function moduleResolver(): Module<any
|
|
11
|
+
export declare function moduleResolver(): Module<any>[];
|
|
12
12
|
/**
|
|
13
13
|
* Runtime function to access all registered modules.
|
|
14
14
|
*
|
|
@@ -21,7 +21,7 @@ export declare const RuntimeModules: () => {
|
|
|
21
21
|
map: <T, S>(processor: (item: T, index: number, array: T[]) => S) => S[];
|
|
22
22
|
forEach: <T>(processor: (item: T, index: number, array: T[]) => void) => void;
|
|
23
23
|
includes: <T>(module: T) => boolean;
|
|
24
|
-
all: Module<any
|
|
24
|
+
all: Module<any>[];
|
|
25
25
|
};
|
|
26
26
|
/**
|
|
27
27
|
* Runtime function to get the application version.
|
|
@@ -79,7 +79,7 @@ export declare class ModuleManager extends Logger {
|
|
|
79
79
|
map: <T, S>(processor: (item: T, index: number, array: T[]) => S) => S[];
|
|
80
80
|
forEach: <T>(processor: (item: T, index: number, array: T[]) => void) => void;
|
|
81
81
|
includes: <T>(module: T) => boolean;
|
|
82
|
-
all: Module<any
|
|
82
|
+
all: Module<any>[];
|
|
83
83
|
};
|
|
84
84
|
/** Singleton instance of ModuleManager */
|
|
85
85
|
static instance: ModuleManager;
|
package/core/module-manager.js
CHANGED
|
@@ -205,8 +205,10 @@ export class ModuleManager extends Logger {
|
|
|
205
205
|
* @throws {BadImplementationException} If any module is undefined (cyclic import issue)
|
|
206
206
|
*/
|
|
207
207
|
init() {
|
|
208
|
-
if (this.config.logLevel)
|
|
208
|
+
if (this.config.logLevel) {
|
|
209
209
|
this.setMinLevel(this.config.logLevel);
|
|
210
|
+
this.modules.forEach((module) => module.setMinLevel(this.config.logLevel));
|
|
211
|
+
}
|
|
210
212
|
this.logInfo(`--------- initializing app ---------`);
|
|
211
213
|
const undefinedModule = this.modules.some(module => !exists(module));
|
|
212
214
|
if (undefinedModule) {
|
package/core/module.d.ts
CHANGED
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import { ModuleManager } from './module-manager.js';
|
|
5
5
|
import { Logger, LogLevel } from './logger/index.js';
|
|
6
|
-
import {
|
|
6
|
+
import { TypeValidator } from '../validator/validator-core.js';
|
|
7
|
+
import { TS_Object } from '../utils/types.js';
|
|
8
|
+
type _ModuleConfig<C extends TS_Object> = C & {
|
|
9
|
+
minLogLevel?: LogLevel;
|
|
10
|
+
};
|
|
7
11
|
/**
|
|
8
12
|
* Base abstract class for all modules in the nu-art ecosystem.
|
|
9
13
|
*
|
|
@@ -20,9 +24,8 @@ import { ValidatorTypeResolver } from '../validator/validator-core.js';
|
|
|
20
24
|
* 3. `init()` - Override to perform initialization logic
|
|
21
25
|
* 4. `validate()` - Override to validate module state after initialization
|
|
22
26
|
*
|
|
23
|
-
* @template Config - The configuration type for this module
|
|
24
|
-
*
|
|
25
|
-
* @template ConfigValidator - Validator type resolver for config validation
|
|
27
|
+
* @template Config - The user-facing configuration type for this module.
|
|
28
|
+
* Internal fields (e.g. minLogLevel) are computed from Config and hidden from consumers.
|
|
26
29
|
*
|
|
27
30
|
* @example
|
|
28
31
|
* ```typescript
|
|
@@ -39,21 +42,17 @@ import { ValidatorTypeResolver } from '../validator/validator-core.js';
|
|
|
39
42
|
* export const MyModule = new MyModule_Class();
|
|
40
43
|
* ```
|
|
41
44
|
*/
|
|
42
|
-
export declare abstract class Module<Config = any
|
|
43
|
-
minLogLevel?: LogLevel;
|
|
44
|
-
} = Config & {
|
|
45
|
-
minLogLevel?: LogLevel;
|
|
46
|
-
}, ConfigValidator extends ValidatorTypeResolver<ModuleConfig> = ValidatorTypeResolver<ModuleConfig>> extends Logger {
|
|
45
|
+
export declare abstract class Module<Config extends TS_Object = any> extends Logger {
|
|
47
46
|
private readonly classStack;
|
|
48
47
|
private name;
|
|
49
48
|
/** Module configuration, merged from default config and ModuleManager-provided config */
|
|
50
|
-
|
|
49
|
+
protected config: _ModuleConfig<Config>;
|
|
51
50
|
/** Reference to the ModuleManager instance, injected during initialization */
|
|
52
51
|
protected readonly manager: ModuleManager;
|
|
53
52
|
/** Flag indicating whether the module has been initialized (set by ModuleManager) */
|
|
54
53
|
protected readonly initiated = false;
|
|
55
54
|
/** Optional config validator, set via setConfigValidator() */
|
|
56
|
-
protected
|
|
55
|
+
protected configValidator?: TypeValidator<_ModuleConfig<Config>>;
|
|
57
56
|
/**
|
|
58
57
|
* Creates a new Module instance.
|
|
59
58
|
*
|
|
@@ -75,7 +74,7 @@ export declare abstract class Module<Config = any, ModuleConfig extends Config &
|
|
|
75
74
|
*
|
|
76
75
|
* @param validator - Validator instance that implements ValidatorTypeResolver
|
|
77
76
|
*/
|
|
78
|
-
setConfigValidator(validator:
|
|
77
|
+
setConfigValidator(validator: TypeValidator<Config>): void;
|
|
79
78
|
/**
|
|
80
79
|
* Sets default configuration values that will be merged with any config
|
|
81
80
|
* provided by the ModuleManager.
|
|
@@ -85,7 +84,7 @@ export declare abstract class Module<Config = any, ModuleConfig extends Config &
|
|
|
85
84
|
*
|
|
86
85
|
* @param config - Partial config object to merge with existing config
|
|
87
86
|
*/
|
|
88
|
-
setDefaultConfig(config: Partial<
|
|
87
|
+
setDefaultConfig(config: Partial<_ModuleConfig<Config>>): void;
|
|
89
88
|
/**
|
|
90
89
|
* Gets the module name (class name without `_Class` suffix).
|
|
91
90
|
*/
|
|
@@ -162,3 +161,4 @@ export declare abstract class Module<Config = any, ModuleConfig extends Config &
|
|
|
162
161
|
protected validate(): void;
|
|
163
162
|
protected destroy(): Promise<void>;
|
|
164
163
|
}
|
|
164
|
+
export {};
|
package/core/module.js
CHANGED
|
@@ -17,8 +17,9 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import { BadImplementationException } from './exceptions/exceptions.js';
|
|
19
19
|
import { merge } from '../utils/merge-tools.js';
|
|
20
|
-
import { Logger } from './logger/index.js';
|
|
20
|
+
import { Logger, LogLevel } from './logger/index.js';
|
|
21
21
|
import { lastElement } from '../utils/array-tools.js';
|
|
22
|
+
import { tsValidateEnum } from '../validator/type-validators.js';
|
|
22
23
|
/**
|
|
23
24
|
* Base abstract class for all modules in the nu-art ecosystem.
|
|
24
25
|
*
|
|
@@ -35,9 +36,8 @@ import { lastElement } from '../utils/array-tools.js';
|
|
|
35
36
|
* 3. `init()` - Override to perform initialization logic
|
|
36
37
|
* 4. `validate()` - Override to validate module state after initialization
|
|
37
38
|
*
|
|
38
|
-
* @template Config - The configuration type for this module
|
|
39
|
-
*
|
|
40
|
-
* @template ConfigValidator - Validator type resolver for config validation
|
|
39
|
+
* @template Config - The user-facing configuration type for this module.
|
|
40
|
+
* Internal fields (e.g. minLogLevel) are computed from Config and hidden from consumers.
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* ```typescript
|
|
@@ -100,8 +100,7 @@ export class Module extends Logger {
|
|
|
100
100
|
* @param validator - Validator instance that implements ValidatorTypeResolver
|
|
101
101
|
*/
|
|
102
102
|
setConfigValidator(validator) {
|
|
103
|
-
|
|
104
|
-
this.configValidator = validator;
|
|
103
|
+
this.configValidator = { ...validator, minLogLevel: tsValidateEnum(LogLevel, false) };
|
|
105
104
|
}
|
|
106
105
|
/**
|
|
107
106
|
* Sets default configuration values that will be merged with any config
|
|
@@ -113,7 +112,6 @@ export class Module extends Logger {
|
|
|
113
112
|
* @param config - Partial config object to merge with existing config
|
|
114
113
|
*/
|
|
115
114
|
setDefaultConfig(config) {
|
|
116
|
-
// @ts-ignore
|
|
117
115
|
this.config = merge(this.config, config);
|
|
118
116
|
}
|
|
119
117
|
/**
|
|
@@ -140,8 +138,7 @@ export class Module extends Logger {
|
|
|
140
138
|
*/
|
|
141
139
|
// @ts-ignore
|
|
142
140
|
setConfig(config) {
|
|
143
|
-
|
|
144
|
-
this.config = this.config ? merge(this.config, config) : config;
|
|
141
|
+
this.config = (this.config ? merge(this.config, config) : config);
|
|
145
142
|
this.config.minLogLevel && this.setMinLevel(this.config.minLogLevel);
|
|
146
143
|
}
|
|
147
144
|
/**
|
|
@@ -155,7 +152,7 @@ export class Module extends Logger {
|
|
|
155
152
|
// @ts-ignore
|
|
156
153
|
setManager(manager) {
|
|
157
154
|
// @ts-ignore
|
|
158
|
-
this
|
|
155
|
+
this['manager'] = manager;
|
|
159
156
|
}
|
|
160
157
|
/**
|
|
161
158
|
* Executes an async function in a fire-and-forget manner, logging the execution.
|
package/index.d.ts
CHANGED
|
@@ -35,5 +35,5 @@ export * from './validator/validator-core.js';
|
|
|
35
35
|
export * from './validator/validators.js';
|
|
36
36
|
export * from './validator/type-validators.js';
|
|
37
37
|
export * from './consts/consts.js';
|
|
38
|
-
export * from './mem-cache/
|
|
38
|
+
export * from './mem-cache/MemCache.js';
|
|
39
39
|
export * from './modules/csv-serializer.js';
|
package/index.js
CHANGED
|
@@ -52,5 +52,5 @@ export * from './validator/validator-core.js';
|
|
|
52
52
|
export * from './validator/validators.js';
|
|
53
53
|
export * from './validator/type-validators.js';
|
|
54
54
|
export * from './consts/consts.js';
|
|
55
|
-
export * from './mem-cache/
|
|
55
|
+
export * from './mem-cache/MemCache.js';
|
|
56
56
|
export * from './modules/csv-serializer.js';
|
package/mem-cache/MemCache.js
CHANGED
|
@@ -84,9 +84,8 @@ export class MemCache {
|
|
|
84
84
|
onEntriesUpdated(itemsUpdated) {
|
|
85
85
|
const frozen = itemsUpdated.map(item => Object.freeze(item));
|
|
86
86
|
const ids = new Set(itemsUpdated.map(this.getId));
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
this.setCache(toCache);
|
|
87
|
+
const retained = this.filter(i => !ids.has(this.getId(i)));
|
|
88
|
+
this.setCache(retained.concat(frozen));
|
|
90
89
|
}
|
|
91
90
|
setCache(cacheArray) {
|
|
92
91
|
this._map = Object.freeze({ ...arrayToMap(cacheArray, (item) => this.getId(item)) });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nu-art/ts-common",
|
|
3
|
-
"version": "0.500.
|
|
3
|
+
"version": "0.500.6",
|
|
4
4
|
"description": "Core TypeScript infrastructure library for building modular applications with lifecycle management, logging, validation, and utilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"keywords": [
|
|
@@ -37,8 +37,8 @@
|
|
|
37
37
|
"run-tests": "ts-mocha --timeout 50000 -p src/test/tsconfig.json src/test/run-all-tests.ts"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@nu-art/testalot": "0.500.
|
|
41
|
-
"@nu-art/logger": "0.500.
|
|
40
|
+
"@nu-art/testalot": "0.500.6",
|
|
41
|
+
"@nu-art/logger": "0.500.6",
|
|
42
42
|
"fast-csv": "^5.0.2",
|
|
43
43
|
"export-to-csv": "0.2.1",
|
|
44
44
|
"moment": "^2.29.4",
|
|
@@ -61,12 +61,12 @@
|
|
|
61
61
|
"import": "./index.js"
|
|
62
62
|
},
|
|
63
63
|
"./utils": {
|
|
64
|
-
"types": "./utils
|
|
65
|
-
"import": "./utils
|
|
64
|
+
"types": "./utils-public-exports.d.ts",
|
|
65
|
+
"import": "./utils-public-exports.js"
|
|
66
66
|
},
|
|
67
67
|
"./mem-storage": {
|
|
68
|
-
"types": "./mem-storage/
|
|
69
|
-
"import": "./mem-storage/
|
|
68
|
+
"types": "./mem-storage/MemStorage.d.ts",
|
|
69
|
+
"import": "./mem-storage/MemStorage.js"
|
|
70
70
|
},
|
|
71
71
|
"./testing": {
|
|
72
72
|
"types": "./testing.d.ts",
|
package/testing.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from '
|
|
1
|
+
export * from '@nu-art/testalot';
|
|
2
2
|
export * from './testing/workspace-creator.js';
|
package/testing.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from '
|
|
1
|
+
export * from '@nu-art/testalot';
|
|
2
2
|
export * from './testing/workspace-creator.js';
|
|
@@ -7,6 +7,20 @@ export declare const Day: number;
|
|
|
7
7
|
export declare const Week: number;
|
|
8
8
|
export declare const Year: number;
|
|
9
9
|
export declare const Month: number;
|
|
10
|
+
/**
|
|
11
|
+
* Parse and format durations using compact notation (s/m/h/d/w).
|
|
12
|
+
*
|
|
13
|
+
* - `parse("3h1m30s")` → `10890000` (ms)
|
|
14
|
+
* - `format(10890000)` → `"3h1m30s"`
|
|
15
|
+
*/
|
|
16
|
+
export declare const StringFormat_Duration: {
|
|
17
|
+
format(ms: number): string;
|
|
18
|
+
parse(duration: string): number;
|
|
19
|
+
};
|
|
20
|
+
/** @see StringFormat_Duration.format */
|
|
21
|
+
export declare const formatDuration: (ms: number) => string;
|
|
22
|
+
/** @see StringFormat_Duration.parse */
|
|
23
|
+
export declare const parseDuration: (duration: string) => number;
|
|
10
24
|
/** Predefined timestamp format strings */
|
|
11
25
|
export declare const Format_HHmmss_DDMMYYYY = "HH:mm:ss_DD-MM-YYYY";
|
|
12
26
|
export declare const Format_YYYYMMDD_HHmmss = "YYYY-MM-DD_HH:mm:ss";
|
package/utils/date-time-tools.js
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
import utc from 'moment';
|
|
19
19
|
import { exists } from './tools.js';
|
|
20
20
|
import { TimeProxy } from './time-proxy.js';
|
|
21
|
+
import { BadImplementationException } from '../core/exceptions/exceptions.js';
|
|
21
22
|
/** Time constants in milliseconds */
|
|
22
23
|
export const Second = 1000;
|
|
23
24
|
export const Minute = Second * 60;
|
|
@@ -26,6 +27,58 @@ export const Day = Hour * 24;
|
|
|
26
27
|
export const Week = Day * 7;
|
|
27
28
|
export const Year = Day * 365;
|
|
28
29
|
export const Month = Year / 12;
|
|
30
|
+
const durationUnits = {
|
|
31
|
+
s: Second,
|
|
32
|
+
m: Minute,
|
|
33
|
+
h: Hour,
|
|
34
|
+
d: Day,
|
|
35
|
+
w: Week,
|
|
36
|
+
};
|
|
37
|
+
const durationPattern = /^(?:\d+[smhdw])+$/;
|
|
38
|
+
const durationSegment = /(\d+)([smhdw])/g;
|
|
39
|
+
const durationUnitOrder = [
|
|
40
|
+
{ unit: 'd', ms: Day },
|
|
41
|
+
{ unit: 'h', ms: Hour },
|
|
42
|
+
{ unit: 'm', ms: Minute },
|
|
43
|
+
{ unit: 's', ms: Second },
|
|
44
|
+
];
|
|
45
|
+
/**
|
|
46
|
+
* Parse and format durations using compact notation (s/m/h/d/w).
|
|
47
|
+
*
|
|
48
|
+
* - `parse("3h1m30s")` → `10890000` (ms)
|
|
49
|
+
* - `format(10890000)` → `"3h1m30s"`
|
|
50
|
+
*/
|
|
51
|
+
export const StringFormat_Duration = {
|
|
52
|
+
format(ms) {
|
|
53
|
+
if (ms <= 0)
|
|
54
|
+
return '0s';
|
|
55
|
+
let remaining = ms;
|
|
56
|
+
let result = '';
|
|
57
|
+
for (const { unit, ms: unitMs } of durationUnitOrder) {
|
|
58
|
+
const count = Math.floor(remaining / unitMs);
|
|
59
|
+
if (count > 0) {
|
|
60
|
+
result += `${count}${unit}`;
|
|
61
|
+
remaining -= count * unitMs;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return result || '0s';
|
|
65
|
+
},
|
|
66
|
+
parse(duration) {
|
|
67
|
+
if (!durationPattern.test(duration))
|
|
68
|
+
throw new BadImplementationException(`Invalid duration format: "${duration}" — expected segments like 5s, 1m, 3h1m30s`);
|
|
69
|
+
let total = 0;
|
|
70
|
+
let match;
|
|
71
|
+
durationSegment.lastIndex = 0;
|
|
72
|
+
while ((match = durationSegment.exec(duration)) !== null) {
|
|
73
|
+
total += parseInt(match[1]) * durationUnits[match[2]];
|
|
74
|
+
}
|
|
75
|
+
return total;
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
/** @see StringFormat_Duration.format */
|
|
79
|
+
export const formatDuration = StringFormat_Duration.format;
|
|
80
|
+
/** @see StringFormat_Duration.parse */
|
|
81
|
+
export const parseDuration = StringFormat_Duration.parse;
|
|
29
82
|
/** Predefined timestamp format strings */
|
|
30
83
|
export const Format_HHmmss_DDMMYYYY = 'HH:mm:ss_DD-MM-YYYY';
|
|
31
84
|
export const Format_YYYYMMDD_HHmmss = 'YYYY-MM-DD_HH:mm:ss';
|
package/utils/query-params.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type RouteParams = {
|
|
|
12
12
|
*
|
|
13
13
|
* Converts an object of parameters into a URL-encoded query string.
|
|
14
14
|
* - Functions are evaluated to get their return value
|
|
15
|
-
* - undefined/null values
|
|
15
|
+
* - undefined/null values are omitted entirely
|
|
16
16
|
* - All values are URI-encoded
|
|
17
17
|
*
|
|
18
18
|
* @param params - Object with parameter keys and values
|
package/utils/query-params.js
CHANGED
|
@@ -3,17 +3,17 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Converts an object of parameters into a URL-encoded query string.
|
|
5
5
|
* - Functions are evaluated to get their return value
|
|
6
|
-
* - undefined/null values
|
|
6
|
+
* - undefined/null values are omitted entirely
|
|
7
7
|
* - All values are URI-encoded
|
|
8
8
|
*
|
|
9
9
|
* @param params - Object with parameter keys and values
|
|
10
10
|
* @returns URL-encoded query string (e.g., "key1=value1&key2=value2")
|
|
11
11
|
*/
|
|
12
12
|
export function composeQueryParams(params = {}) {
|
|
13
|
-
return Object.keys(params)
|
|
13
|
+
return Object.keys(params)
|
|
14
|
+
.filter(key => params[key] !== undefined && params[key] !== null)
|
|
15
|
+
.map((paramKey) => {
|
|
14
16
|
let param = params[paramKey];
|
|
15
|
-
if (param === undefined || param === null)
|
|
16
|
-
return `${paramKey}=`;
|
|
17
17
|
if (typeof param === 'function')
|
|
18
18
|
param = param();
|
|
19
19
|
return `${paramKey}=${encodeURIComponent(param)}`;
|
package/utils/string-tools.d.ts
CHANGED
|
@@ -161,3 +161,13 @@ export declare function normalizeString(string: string): string;
|
|
|
161
161
|
* ```
|
|
162
162
|
*/
|
|
163
163
|
export declare function convertUpperCamelCase(upperCamelCase: string, delimiter?: string): string;
|
|
164
|
+
/**
|
|
165
|
+
* Parse and format numbers using volume shorthand notation (K/M/G/T).
|
|
166
|
+
*
|
|
167
|
+
* - `parse("76M")` → `76_000_000`
|
|
168
|
+
* - `format(76_000_000)` → `"76M"`
|
|
169
|
+
*/
|
|
170
|
+
export declare const StringFormat_Volume: {
|
|
171
|
+
parse(input: string): number;
|
|
172
|
+
format(value: number): string;
|
|
173
|
+
};
|
package/utils/string-tools.js
CHANGED
|
@@ -267,3 +267,48 @@ export function normalizeString(string) {
|
|
|
267
267
|
export function convertUpperCamelCase(upperCamelCase, delimiter = ' ') {
|
|
268
268
|
return upperCamelCase.replace(/([a-z0-9])([A-Z])/g, `$1${delimiter}$2`);
|
|
269
269
|
}
|
|
270
|
+
const _volumeSuffixes = [
|
|
271
|
+
{ suffix: 'T', value: 1e12 },
|
|
272
|
+
{ suffix: 'G', value: 1e9 },
|
|
273
|
+
{ suffix: 'M', value: 1e6 },
|
|
274
|
+
{ suffix: 'K', value: 1e3 },
|
|
275
|
+
];
|
|
276
|
+
const _volumeSuffixMap = Object.fromEntries(_volumeSuffixes.map(s => [s.suffix, s.value]));
|
|
277
|
+
const _volumePattern = /^(-?\d+\.?\d*)\s*([KMGT])?$/;
|
|
278
|
+
/**
|
|
279
|
+
* Parse and format numbers using volume shorthand notation (K/M/G/T).
|
|
280
|
+
*
|
|
281
|
+
* - `parse("76M")` → `76_000_000`
|
|
282
|
+
* - `format(76_000_000)` → `"76M"`
|
|
283
|
+
*/
|
|
284
|
+
export const StringFormat_Volume = {
|
|
285
|
+
parse(input) {
|
|
286
|
+
const trimmed = input.trim();
|
|
287
|
+
if (!trimmed)
|
|
288
|
+
throw new Error('Empty volume input');
|
|
289
|
+
const match = trimmed.match(_volumePattern);
|
|
290
|
+
if (!match)
|
|
291
|
+
throw new Error(`Invalid volume format: "${input}" — expected pattern like 76M, 1.5G, 300K`);
|
|
292
|
+
const coefficient = Number(match[1]);
|
|
293
|
+
const suffix = match[2];
|
|
294
|
+
if (!suffix)
|
|
295
|
+
return coefficient;
|
|
296
|
+
const multiplier = _volumeSuffixMap[suffix];
|
|
297
|
+
if (multiplier === undefined)
|
|
298
|
+
throw new Error(`Unknown volume suffix: "${suffix}"`);
|
|
299
|
+
return coefficient * multiplier;
|
|
300
|
+
},
|
|
301
|
+
format(value) {
|
|
302
|
+
if (value === 0)
|
|
303
|
+
return '0';
|
|
304
|
+
for (const { suffix, value: threshold } of _volumeSuffixes) {
|
|
305
|
+
if (Math.abs(value) < threshold)
|
|
306
|
+
continue;
|
|
307
|
+
const coefficient = value / threshold;
|
|
308
|
+
const rounded = Math.round(coefficient * 100) / 100;
|
|
309
|
+
if (rounded * threshold === value)
|
|
310
|
+
return `${rounded}${suffix}`;
|
|
311
|
+
}
|
|
312
|
+
return String(value);
|
|
313
|
+
},
|
|
314
|
+
};
|
|
@@ -78,6 +78,8 @@ export declare const tsValidateAnyNumber: Validator<number>;
|
|
|
78
78
|
export declare const tsValidateOptionalAnyNumber: Validator<number>;
|
|
79
79
|
export declare const tsValidateEnum: (enumType: TypedMap<number | string>, mandatory?: boolean) => Validator<number | string>;
|
|
80
80
|
export declare const tsValidateBoolean: (mandatory?: boolean) => Validator<boolean>;
|
|
81
|
+
export declare const tsValidateOptionalBoolean: Validator<boolean>;
|
|
82
|
+
export declare const tsValidateMandatoryBoolean: Validator<boolean>;
|
|
81
83
|
export declare const tsValidateValue: <T>(values: T[] | ReadonlyArray<T>, mandatory?: boolean) => Validator<any>;
|
|
82
84
|
export declare const tsValidateIsInRange: (ranges: [number, number][], mandatory?: boolean) => Validator<number>;
|
|
83
85
|
export declare const tsValidateRange: (mandatory?: boolean) => Validator<[number, number] | undefined>;
|
|
@@ -183,6 +183,8 @@ export const tsValidateBoolean = (mandatory = true) => {
|
|
|
183
183
|
return `Input is not a boolean! \nvalue: ${input}\ntype: ${typeof input}`;
|
|
184
184
|
}];
|
|
185
185
|
};
|
|
186
|
+
export const tsValidateOptionalBoolean = tsValidateBoolean(false);
|
|
187
|
+
export const tsValidateMandatoryBoolean = tsValidateBoolean();
|
|
186
188
|
export const tsValidateValue = (values, mandatory = true) => {
|
|
187
189
|
return [tsValidateExists(mandatory),
|
|
188
190
|
(input) => {
|
package/mem-cache/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './MemCache.js';
|
package/mem-cache/index.js
DELETED
package/mem-storage/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './MemStorage.js';
|
package/mem-storage/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './MemStorage.js';
|
package/testing/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@nu-art/testalot';
|
package/testing/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@nu-art/testalot';
|
package/utils/index.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export * from './array-tools.js';
|
|
2
|
-
export * from './conflict-tools.js';
|
|
3
|
-
export * from './crypto-tools.js';
|
|
4
|
-
export * from './date-time-tools.js';
|
|
5
|
-
export * from './db-object-tools.js';
|
|
6
|
-
export * from './exception-tools.js';
|
|
7
|
-
export * from './filter-tools.js';
|
|
8
|
-
export * from './hash-tools.js';
|
|
9
|
-
export * from './index.js';
|
|
10
|
-
export * from './json-tools.js';
|
|
11
|
-
export * from './merge-tools.js';
|
|
12
|
-
export * from './mimetype-tools.js';
|
|
13
|
-
export * from './number-tools.js';
|
|
14
|
-
export * from './object-tools.js';
|
|
15
|
-
export * from './promise-tools.js';
|
|
16
|
-
export * from './query-params.js';
|
|
17
|
-
export * from './queue.js';
|
|
18
|
-
export * from './queue-v2.js';
|
|
19
|
-
export * from './random-tools.js';
|
|
20
|
-
export * from './storage-capacity-tools.js';
|
|
21
|
-
export * from './string-tools.js';
|
|
22
|
-
export * from './time-proxy.js';
|
|
23
|
-
export * from './tools.js';
|
|
24
|
-
export * from './types.js';
|
|
25
|
-
export * from './ui-tools.js';
|
|
26
|
-
export * from './url-tools.js';
|
|
27
|
-
export * from './version-tools.js';
|
package/utils/index.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export * from './array-tools.js';
|
|
2
|
-
export * from './conflict-tools.js';
|
|
3
|
-
export * from './crypto-tools.js';
|
|
4
|
-
export * from './date-time-tools.js';
|
|
5
|
-
export * from './db-object-tools.js';
|
|
6
|
-
export * from './exception-tools.js';
|
|
7
|
-
export * from './filter-tools.js';
|
|
8
|
-
export * from './hash-tools.js';
|
|
9
|
-
export * from './index.js';
|
|
10
|
-
export * from './json-tools.js';
|
|
11
|
-
export * from './merge-tools.js';
|
|
12
|
-
export * from './mimetype-tools.js';
|
|
13
|
-
export * from './number-tools.js';
|
|
14
|
-
export * from './object-tools.js';
|
|
15
|
-
export * from './promise-tools.js';
|
|
16
|
-
export * from './query-params.js';
|
|
17
|
-
export * from './queue.js';
|
|
18
|
-
export * from './queue-v2.js';
|
|
19
|
-
export * from './random-tools.js';
|
|
20
|
-
export * from './storage-capacity-tools.js';
|
|
21
|
-
export * from './string-tools.js';
|
|
22
|
-
export * from './time-proxy.js';
|
|
23
|
-
export * from './tools.js';
|
|
24
|
-
export * from './types.js';
|
|
25
|
-
export * from './ui-tools.js';
|
|
26
|
-
export * from './url-tools.js';
|
|
27
|
-
export * from './version-tools.js';
|