@cripty2001/utils 0.0.232 → 0.0.233
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/dist/index.d.ts +6 -5
- package/dist/index.js +18 -12
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Whispr } from "@cripty2001/whispr";
|
|
2
|
-
import type { TSchema } from "@sinclair/typebox";
|
|
2
|
+
import type { Static, TSchema } from "@sinclair/typebox";
|
|
3
3
|
export type JSONEncodable = number | string | boolean | JSONEncodable[] | null | {
|
|
4
4
|
[key: string]: JSONEncodable;
|
|
5
5
|
};
|
|
@@ -121,12 +121,13 @@ export declare function getEnvInt(key: string, constraints?: {
|
|
|
121
121
|
max?: number;
|
|
122
122
|
}, defaultValue?: number): number;
|
|
123
123
|
/**
|
|
124
|
-
* @see getEnv, with the additional constraint that the environment variable must be
|
|
125
|
-
* Note: the default value is not used instead of
|
|
124
|
+
* @see getEnv, with the additional constraint that the environment variable must be valid JSON.
|
|
125
|
+
* Note: the default value is not used instead of invalid JSON or a schema mismatch. Those therefore throw.
|
|
126
126
|
*
|
|
127
|
-
* @param schema
|
|
127
|
+
* @param schema Optional TypeBox schema.
|
|
128
128
|
*/
|
|
129
|
-
export declare function getEnvJson<T>(key: string, defaultValue?: T
|
|
129
|
+
export declare function getEnvJson<T>(key: string, defaultValue?: T): T;
|
|
130
|
+
export declare function getEnvJson<S extends TSchema>(key: string, defaultValue: Static<S> | undefined, schema: S): Static<S>;
|
|
130
131
|
export declare const CURRENT_TS_MS: () => Whispr<number>;
|
|
131
132
|
export declare function getClock(rate: number): Whispr<number>;
|
|
132
133
|
export declare function timediff2HumanReadable(diffMs: number): string;
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,7 @@ exports.timediff2HumanReadable = timediff2HumanReadable;
|
|
|
24
24
|
exports.fn2promise = fn2promise;
|
|
25
25
|
exports.safeApply = safeApply;
|
|
26
26
|
const whispr_1 = require("@cripty2001/whispr");
|
|
27
|
+
const value_1 = require("@sinclair/typebox/value");
|
|
27
28
|
const lodash_1 = require("lodash");
|
|
28
29
|
const U32_LIMIT = 0x1_0000_0000;
|
|
29
30
|
function randomU32() {
|
|
@@ -285,8 +286,10 @@ function getEnvString(key, allowEmpty = false, defaultValue) {
|
|
|
285
286
|
*/
|
|
286
287
|
function getEnvInt(key, constraints = {}, defaultValue) {
|
|
287
288
|
return getEnv(key, defaultValue, (s) => {
|
|
288
|
-
|
|
289
|
-
|
|
289
|
+
if (!/^[+-]?\d+$/.test(s))
|
|
290
|
+
throw new GetenvHandledError(`Invalid integer.`);
|
|
291
|
+
const parsed = Number(s);
|
|
292
|
+
if (!Number.isInteger(parsed))
|
|
290
293
|
throw new GetenvHandledError(`Invalid integer.`);
|
|
291
294
|
const MIN = constraints.min ?? Number.MIN_SAFE_INTEGER;
|
|
292
295
|
if (parsed < MIN)
|
|
@@ -297,17 +300,20 @@ function getEnvInt(key, constraints = {}, defaultValue) {
|
|
|
297
300
|
return parsed;
|
|
298
301
|
});
|
|
299
302
|
}
|
|
300
|
-
/**
|
|
301
|
-
* @see getEnv, with the additional constraint that the environment variable must be a valid JSON object.
|
|
302
|
-
* Note: the default value is not used instead of an invalid JSON object. An invalid JSON object therefore throws an error.
|
|
303
|
-
*
|
|
304
|
-
* @param schema The schema to use to validate the environment variable value
|
|
305
|
-
*/
|
|
306
303
|
function getEnvJson(key, defaultValue, schema) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
304
|
+
return getEnv(key, defaultValue, (s) => {
|
|
305
|
+
const parsed = (() => {
|
|
306
|
+
try {
|
|
307
|
+
return JSON.parse(s);
|
|
308
|
+
}
|
|
309
|
+
catch {
|
|
310
|
+
throw new GetenvHandledError(`Invalid JSON.`);
|
|
311
|
+
}
|
|
312
|
+
})();
|
|
313
|
+
if (schema === undefined)
|
|
314
|
+
return parsed;
|
|
315
|
+
return value_1.Value.Parse(schema, parsed);
|
|
316
|
+
});
|
|
311
317
|
}
|
|
312
318
|
const [currentTsMs, setCurrentTsMs] = whispr_1.Whispr.create(Date.now());
|
|
313
319
|
let currentTsMsInterval = null;
|
package/package.json
CHANGED