@oscarpalmer/atoms 0.187.2 → 0.188.1
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/function/retry.mjs +1 -0
- package/dist/index.d.mts +19 -12
- package/dist/index.mjs +61 -95
- package/dist/logger.d.mts +12 -12
- package/dist/logger.mjs +37 -79
- package/dist/string/index.d.mts +7 -0
- package/dist/string/index.mjs +21 -13
- package/package.json +2 -2
- package/src/logger.ts +52 -44
- package/src/string/index.ts +44 -13
package/dist/function/retry.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -5137,6 +5137,13 @@ declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): st
|
|
|
5137
5137
|
* @returns Dedented string
|
|
5138
5138
|
*/
|
|
5139
5139
|
declare function dedent(value: string): string;
|
|
5140
|
+
/**
|
|
5141
|
+
* Get a new UUID string _(version 4)_ for use in _HTML_
|
|
5142
|
+
*
|
|
5143
|
+
* @param html Use _HTML_-friendly format
|
|
5144
|
+
* @returns UUID string
|
|
5145
|
+
*/
|
|
5146
|
+
declare function getUuid(html: true): string;
|
|
5140
5147
|
/**
|
|
5141
5148
|
* Get a new UUID string _(version 4)_
|
|
5142
5149
|
*
|
|
@@ -6589,12 +6596,7 @@ declare function isNumerical(value: unknown): value is number | `${number}`;
|
|
|
6589
6596
|
declare function isObject(value: unknown): value is object;
|
|
6590
6597
|
//#endregion
|
|
6591
6598
|
//#region src/logger.d.ts
|
|
6592
|
-
|
|
6593
|
-
* A logger that can be used to log messages to the console
|
|
6594
|
-
*
|
|
6595
|
-
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
6596
|
-
*/
|
|
6597
|
-
declare class Logger {
|
|
6599
|
+
type LoggerInstance = {
|
|
6598
6600
|
/**
|
|
6599
6601
|
* Log any number of values at the "debug" log level
|
|
6600
6602
|
*/
|
|
@@ -6639,14 +6641,14 @@ declare class Logger {
|
|
|
6639
6641
|
* Start a timed logger with a label
|
|
6640
6642
|
*
|
|
6641
6643
|
* @param label Label for the logger
|
|
6642
|
-
* @returns
|
|
6644
|
+
* @returns _TimedLogger_ instance
|
|
6643
6645
|
*/
|
|
6644
|
-
time(label: string):
|
|
6645
|
-
}
|
|
6646
|
+
time(label: string): TimedLogger;
|
|
6647
|
+
};
|
|
6646
6648
|
/**
|
|
6647
6649
|
* A named timer that can be used to log durations to the console
|
|
6648
6650
|
*/
|
|
6649
|
-
declare class
|
|
6651
|
+
declare class TimedLogger {
|
|
6650
6652
|
#private;
|
|
6651
6653
|
/**
|
|
6652
6654
|
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
@@ -6664,7 +6666,12 @@ declare class Timed {
|
|
|
6664
6666
|
get stop(): () => void;
|
|
6665
6667
|
constructor(label: string);
|
|
6666
6668
|
}
|
|
6667
|
-
|
|
6669
|
+
/**
|
|
6670
|
+
* A logger that can be used to log messages to the console
|
|
6671
|
+
*
|
|
6672
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
6673
|
+
*/
|
|
6674
|
+
declare const Logger: LoggerInstance;
|
|
6668
6675
|
//#endregion
|
|
6669
6676
|
//#region src/internal/math/aggregate.d.ts
|
|
6670
6677
|
/**
|
|
@@ -8179,4 +8186,4 @@ declare class SizedSet<Value = unknown> extends Set<Value> {
|
|
|
8179
8186
|
get(value: Value, update?: boolean): Value | undefined;
|
|
8180
8187
|
}
|
|
8181
8188
|
//#endregion
|
|
8182
|
-
export { AnyResult, ArrayComparison, ArrayComparisonSorter, ArrayKeySorter, ArrayOrPlainObject, ArrayValueSorter, AssertProperty, Asserter, AssignOptions, Assigner, AsyncCancelableCallback, AttemptFlow, AttemptFlowPromise, type Beacon, type BeaconOptions, BuiltIns, CancelableCallback, CancelablePromise, type Color, Constructor, DiffOptions, DiffResult, DiffValue, EqualOptions, Err, EventPosition, type Events, ExtendedErr, ExtendedResult, Flow, FlowPromise, Frozen, FulfilledPromise, FuzzyConfiguration, FuzzyOptions, FuzzyResult, FuzzySearchOptions, GenericAsyncCallback, GenericCallback, type HSLAColor, type HSLColor, type Herald, Key$1 as Key, type KeyedQueue, KeyedValue,
|
|
8189
|
+
export { AnyResult, ArrayComparison, ArrayComparisonSorter, ArrayKeySorter, ArrayOrPlainObject, ArrayValueSorter, AssertProperty, Asserter, AssignOptions, Assigner, AsyncCancelableCallback, AttemptFlow, AttemptFlowPromise, type Beacon, type BeaconOptions, BuiltIns, CancelableCallback, CancelablePromise, type Color, Constructor, DiffOptions, DiffResult, DiffValue, EqualOptions, Err, EventPosition, type Events, ExtendedErr, ExtendedResult, Flow, FlowPromise, Frozen, FulfilledPromise, FuzzyConfiguration, FuzzyOptions, FuzzyResult, FuzzySearchOptions, GenericAsyncCallback, GenericCallback, type HSLAColor, type HSLColor, type Herald, Key$1 as Key, type KeyedQueue, KeyedValue, Logger, type Memoized, type MemoizedOptions, MergeOptions, Merger, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NormalizeOptions, Normalizer, NumericalKeys, NumericalValues, type Observable, type Observer, Ok, OnceAsyncCallback, OnceCallback, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PlainObject, Primitive, PromiseData, PromiseHandlers, PromiseOptions, PromiseParameters, PromiseStrategy, PromiseTimeoutError, PromisesItems, PromisesOptions, PromisesResult, PromisesUnwrapped, PromisesValue, PromisesValues, type Queue, type QueueError, type QueueOptions, type Queued, type QueuedResult, type RGBAColor, type RGBColor, RejectedPromise, RequiredKeys, Result, ResultMatch, RetryError, RetryOptions, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, type Shaken, Simplify, SizedMap, SizedSet, type Smushed, SortDirection, Sorter, type Subscription, TemplateOptions, type TimedLogger, ToString, Transformer, TypedArray, UnionToIntersection, type Unsmushed, Unsubscriber, UnwrapValue, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFrozen, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isSorted, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, lowerCase, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
package/dist/index.mjs
CHANGED
|
@@ -3059,19 +3059,24 @@ function dedent(value, ...values) {
|
|
|
3059
3059
|
*
|
|
3060
3060
|
* @returns UUID string
|
|
3061
3061
|
*/
|
|
3062
|
-
function getUuid() {
|
|
3063
|
-
const
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
hex.
|
|
3070
|
-
hex.substring(
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3062
|
+
function getUuid(html) {
|
|
3063
|
+
const forHTML = html === true;
|
|
3064
|
+
while (true) {
|
|
3065
|
+
const bytes = new Uint8Array(16);
|
|
3066
|
+
crypto.getRandomValues(bytes);
|
|
3067
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
3068
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
3069
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, ZERO)).join("");
|
|
3070
|
+
const first = hex.substring(0, 8);
|
|
3071
|
+
if (forHTML && NUMERICAL_PREFIX_PATTERN.test(first)) continue;
|
|
3072
|
+
return [
|
|
3073
|
+
first,
|
|
3074
|
+
hex.substring(8, 12),
|
|
3075
|
+
hex.substring(12, 16),
|
|
3076
|
+
hex.substring(16, 20),
|
|
3077
|
+
hex.substring(20, 32)
|
|
3078
|
+
].join(forHTML ? DELIMITER_UUID_HTML : DELIMITER_UUID_DEFAULT);
|
|
3079
|
+
}
|
|
3075
3080
|
}
|
|
3076
3081
|
/**
|
|
3077
3082
|
* Parse a JSON string into its proper value _(or `undefined` if it fails)_
|
|
@@ -3113,6 +3118,9 @@ function truncate(value, length, suffix) {
|
|
|
3113
3118
|
const truncatedLength = length - actualSuffixLength;
|
|
3114
3119
|
return `${value.slice(0, truncatedLength)}${actualSuffix}`;
|
|
3115
3120
|
}
|
|
3121
|
+
const DELIMITER_UUID_DEFAULT = "-";
|
|
3122
|
+
const DELIMITER_UUID_HTML = "_";
|
|
3123
|
+
const NUMERICAL_PREFIX_PATTERN = /^\d/;
|
|
3116
3124
|
const ZERO = "0";
|
|
3117
3125
|
//#endregion
|
|
3118
3126
|
//#region src/string/normalize.ts
|
|
@@ -4859,85 +4867,9 @@ function getColor(value) {
|
|
|
4859
4867
|
//#endregion
|
|
4860
4868
|
//#region src/logger.ts
|
|
4861
4869
|
/**
|
|
4862
|
-
* A logger that can be used to log messages to the console
|
|
4863
|
-
*
|
|
4864
|
-
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
4865
|
-
*/
|
|
4866
|
-
var Logger = class {
|
|
4867
|
-
/**
|
|
4868
|
-
* Log any number of values at the "debug" log level
|
|
4869
|
-
*/
|
|
4870
|
-
get debug() {
|
|
4871
|
-
return enabled ? console.debug : noop;
|
|
4872
|
-
}
|
|
4873
|
-
/**
|
|
4874
|
-
* Log the value and shows all its properties
|
|
4875
|
-
*/
|
|
4876
|
-
get dir() {
|
|
4877
|
-
return enabled ? console.dir : noop;
|
|
4878
|
-
}
|
|
4879
|
-
/**
|
|
4880
|
-
* Is logging to the console enabled? _(defaults to `true`)_
|
|
4881
|
-
*/
|
|
4882
|
-
get enabled() {
|
|
4883
|
-
return enabled;
|
|
4884
|
-
}
|
|
4885
|
-
/**
|
|
4886
|
-
* Enable or disable logging to the console
|
|
4887
|
-
*/
|
|
4888
|
-
set enabled(value) {
|
|
4889
|
-
enabled = typeof value === "boolean" ? value : enabled;
|
|
4890
|
-
}
|
|
4891
|
-
/**
|
|
4892
|
-
* Log any number of values at the "error" log level
|
|
4893
|
-
*/
|
|
4894
|
-
get error() {
|
|
4895
|
-
return enabled ? console.error : noop;
|
|
4896
|
-
}
|
|
4897
|
-
/**
|
|
4898
|
-
* Log any number of values at the "info" log level
|
|
4899
|
-
*/
|
|
4900
|
-
get info() {
|
|
4901
|
-
return enabled ? console.info : noop;
|
|
4902
|
-
}
|
|
4903
|
-
/**
|
|
4904
|
-
* Log any number of values at the "log" log level
|
|
4905
|
-
*/
|
|
4906
|
-
get log() {
|
|
4907
|
-
return enabled ? console.log : noop;
|
|
4908
|
-
}
|
|
4909
|
-
/**
|
|
4910
|
-
* Log data as a table, with optional properties to use as columns
|
|
4911
|
-
*/
|
|
4912
|
-
get table() {
|
|
4913
|
-
return enabled ? console.table : noop;
|
|
4914
|
-
}
|
|
4915
|
-
/**
|
|
4916
|
-
* Log any number of values together with a trace from where it was called
|
|
4917
|
-
*/
|
|
4918
|
-
get trace() {
|
|
4919
|
-
return enabled ? console.trace : noop;
|
|
4920
|
-
}
|
|
4921
|
-
/**
|
|
4922
|
-
* Log any number of values at the "warn" log level
|
|
4923
|
-
*/
|
|
4924
|
-
get warn() {
|
|
4925
|
-
return enabled ? console.warn : noop;
|
|
4926
|
-
}
|
|
4927
|
-
/**
|
|
4928
|
-
* Start a timed logger with a label
|
|
4929
|
-
*
|
|
4930
|
-
* @param label Label for the logger
|
|
4931
|
-
* @returns _Timed_ instance
|
|
4932
|
-
*/
|
|
4933
|
-
time(label) {
|
|
4934
|
-
return new Timed(label);
|
|
4935
|
-
}
|
|
4936
|
-
};
|
|
4937
|
-
/**
|
|
4938
4870
|
* A named timer that can be used to log durations to the console
|
|
4939
4871
|
*/
|
|
4940
|
-
var
|
|
4872
|
+
var TimedLogger = class {
|
|
4941
4873
|
#logger;
|
|
4942
4874
|
#stopper;
|
|
4943
4875
|
#state;
|
|
@@ -4979,7 +4911,41 @@ var Timed = class {
|
|
|
4979
4911
|
return stopper;
|
|
4980
4912
|
}
|
|
4981
4913
|
};
|
|
4982
|
-
const
|
|
4914
|
+
const methods = [
|
|
4915
|
+
"debug",
|
|
4916
|
+
"dir",
|
|
4917
|
+
"error",
|
|
4918
|
+
"info",
|
|
4919
|
+
"log",
|
|
4920
|
+
"table",
|
|
4921
|
+
"trace",
|
|
4922
|
+
"warn"
|
|
4923
|
+
];
|
|
4924
|
+
/**
|
|
4925
|
+
* A logger that can be used to log messages to the console
|
|
4926
|
+
*
|
|
4927
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
4928
|
+
*/
|
|
4929
|
+
const Logger = (() => {
|
|
4930
|
+
const instance = {};
|
|
4931
|
+
Object.defineProperties(instance, {
|
|
4932
|
+
enabled: {
|
|
4933
|
+
get() {
|
|
4934
|
+
return enabled;
|
|
4935
|
+
},
|
|
4936
|
+
set(value) {
|
|
4937
|
+
enabled = typeof value === "boolean" ? value : enabled;
|
|
4938
|
+
}
|
|
4939
|
+
},
|
|
4940
|
+
time: { value(label) {
|
|
4941
|
+
return new TimedLogger(label);
|
|
4942
|
+
} }
|
|
4943
|
+
});
|
|
4944
|
+
for (const method of methods) Object.defineProperty(instance, method, { get() {
|
|
4945
|
+
return enabled ? console[method].bind(console) : noop;
|
|
4946
|
+
} });
|
|
4947
|
+
return Object.freeze(instance);
|
|
4948
|
+
})();
|
|
4983
4949
|
let enabled = true;
|
|
4984
4950
|
//#endregion
|
|
4985
4951
|
//#region src/math.ts
|
|
@@ -5187,7 +5153,7 @@ function settlePromise(aborter, settler, value, signal) {
|
|
|
5187
5153
|
}
|
|
5188
5154
|
async function toResult(value) {
|
|
5189
5155
|
const actual = typeof value === "function" ? value() : value;
|
|
5190
|
-
if (!(actual instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
|
|
5156
|
+
if (!(actual instanceof Promise)) return Promise.reject(/* @__PURE__ */ new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
|
|
5191
5157
|
return actual.then((result) => ok(result)).catch((reason) => error(reason));
|
|
5192
5158
|
}
|
|
5193
5159
|
//#endregion
|
|
@@ -5235,7 +5201,7 @@ async function getTimedPromise(promise, time, signal) {
|
|
|
5235
5201
|
});
|
|
5236
5202
|
}
|
|
5237
5203
|
async function timed(promise, options) {
|
|
5238
|
-
if (!(promise instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
|
|
5204
|
+
if (!(promise instanceof Promise)) return Promise.reject(/* @__PURE__ */ new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
|
|
5239
5205
|
const { signal, time } = getPromiseOptions(options);
|
|
5240
5206
|
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
5241
5207
|
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
@@ -5244,7 +5210,7 @@ async function timed(promise, options) {
|
|
|
5244
5210
|
//#region src/promise/index.ts
|
|
5245
5211
|
async function attemptPromise(value, options) {
|
|
5246
5212
|
const isFunction = typeof value === "function";
|
|
5247
|
-
if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
|
|
5213
|
+
if (!isFunction && !(value instanceof Promise)) return Promise.reject(/* @__PURE__ */ new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
|
|
5248
5214
|
const { signal, time } = getPromiseOptions(options);
|
|
5249
5215
|
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
5250
5216
|
function abort() {
|
|
@@ -5992,4 +5958,4 @@ var SizedSet = class extends Set {
|
|
|
5992
5958
|
}
|
|
5993
5959
|
};
|
|
5994
5960
|
//#endregion
|
|
5995
|
-
export { CancelablePromise, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFrozen, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isSorted, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault,
|
|
5961
|
+
export { CancelablePromise, Logger, PROMISE_ABORT_EVENT, PROMISE_ABORT_OPTIONS, PROMISE_ERROR_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_RESULT, PROMISE_MESSAGE_EXPECTATION_TIMED, PROMISE_MESSAGE_TIMEOUT, PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError, RetryError, SORT_DIRECTION_ASCENDING, SORT_DIRECTION_DESCENDING, SizedMap, SizedSet, assert, assertCondition, assertDefined, assertInstanceOf, assertIs, assertProperty, assign, asyncAttempt, asyncDebounce, asyncFlow, asyncMatchResult, asyncOnce, asyncPipe, asyncThrottle, attempt, attemptAsyncFlow, attemptAsyncPipe, attemptFlow, attemptPipe, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, copy, count, debounce, deburr, dedent, delay, deregisterCloner, deregisterComparator, deregisterEqualizer, diff, difference, drop, endsWith, endsWithArray, equal, error, exclude, exists, filter, find, findLast, first, firstOrDefault, flatFreeze, flatten, floor, flow, freeze, fromQuery, toPromise as fromResult, toPromise, fuzzy, fuzzyMatch, getArray, getArrayComparison, getColor, getError, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloatingNumber as getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getSortedIndex, getString, getTimedPromise, getUuid, getValue, groupArraysBy, groupBy, handleResult, hasValue, hasValueResult, herald, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, inMap, inSet, includes, includesArray, indexOf, indexOfArray, initializeAssigner, initializeEqualizer, initializeMerger, initializeNormalizer, initializeSorter, initializeTemplater, initializeTransformer, insert, interpolate, intersection, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFrozen, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonArrayOrPlainObject, isNonConstructor, isNonEmpty, isNonInstanceOf, isNonKey, isNonNullable, isNonNullableOrEmpty, isNonNullableOrWhitespace, isNonNumber, isNonNumerical, isNonObject, isNonPlainObject, isNonPrimitive, isNonTypedArray, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isSorted, isTypedArray, join, kebabCase, keyedQueue, last, lastIndexOf, lastOrDefault, lowerCase, matchResult, max, median, memoize, merge, min, move, moveIndices, moveToIndex, noop, normalize, ok, omit, once, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, registerCloner, registerComparator, registerEqualizer, resultPromises, retry, reverse, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, settlePromise, shake, shuffle, single, slice, smush, snakeCase, sort, splice, startsWith, startsWithArray, sum, swap, take, template, throttle, timed, times, titleCase, toMap, toMapArrays, toQuery, toRecord, toRecordArrays, toResult, toSet, toggle, transform, trim, truncate, tryDecode, tryEncode, union, unique, unsmush, unwrap, update, upperCase, words };
|
package/dist/logger.d.mts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
//#region src/logger.d.ts
|
|
2
|
-
|
|
3
|
-
* A logger that can be used to log messages to the console
|
|
4
|
-
*
|
|
5
|
-
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
6
|
-
*/
|
|
7
|
-
declare class Logger {
|
|
2
|
+
type LoggerInstance = {
|
|
8
3
|
/**
|
|
9
4
|
* Log any number of values at the "debug" log level
|
|
10
5
|
*/
|
|
@@ -49,14 +44,14 @@ declare class Logger {
|
|
|
49
44
|
* Start a timed logger with a label
|
|
50
45
|
*
|
|
51
46
|
* @param label Label for the logger
|
|
52
|
-
* @returns
|
|
47
|
+
* @returns _TimedLogger_ instance
|
|
53
48
|
*/
|
|
54
|
-
time(label: string):
|
|
55
|
-
}
|
|
49
|
+
time(label: string): TimedLogger;
|
|
50
|
+
};
|
|
56
51
|
/**
|
|
57
52
|
* A named timer that can be used to log durations to the console
|
|
58
53
|
*/
|
|
59
|
-
declare class
|
|
54
|
+
declare class TimedLogger {
|
|
60
55
|
#private;
|
|
61
56
|
/**
|
|
62
57
|
* Is the timer active? _(i.e. has it been started and not stopped, and is logging enabled?)_
|
|
@@ -74,6 +69,11 @@ declare class Timed {
|
|
|
74
69
|
get stop(): () => void;
|
|
75
70
|
constructor(label: string);
|
|
76
71
|
}
|
|
77
|
-
|
|
72
|
+
/**
|
|
73
|
+
* A logger that can be used to log messages to the console
|
|
74
|
+
*
|
|
75
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
76
|
+
*/
|
|
77
|
+
declare const Logger: LoggerInstance;
|
|
78
78
|
//#endregion
|
|
79
|
-
export {
|
|
79
|
+
export { Logger, type TimedLogger };
|
package/dist/logger.mjs
CHANGED
|
@@ -1,85 +1,9 @@
|
|
|
1
1
|
import { noop } from "./internal/function/misc.mjs";
|
|
2
2
|
//#region src/logger.ts
|
|
3
3
|
/**
|
|
4
|
-
* A logger that can be used to log messages to the console
|
|
5
|
-
*
|
|
6
|
-
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
7
|
-
*/
|
|
8
|
-
var Logger = class {
|
|
9
|
-
/**
|
|
10
|
-
* Log any number of values at the "debug" log level
|
|
11
|
-
*/
|
|
12
|
-
get debug() {
|
|
13
|
-
return enabled ? console.debug : noop;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Log the value and shows all its properties
|
|
17
|
-
*/
|
|
18
|
-
get dir() {
|
|
19
|
-
return enabled ? console.dir : noop;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Is logging to the console enabled? _(defaults to `true`)_
|
|
23
|
-
*/
|
|
24
|
-
get enabled() {
|
|
25
|
-
return enabled;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Enable or disable logging to the console
|
|
29
|
-
*/
|
|
30
|
-
set enabled(value) {
|
|
31
|
-
enabled = typeof value === "boolean" ? value : enabled;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Log any number of values at the "error" log level
|
|
35
|
-
*/
|
|
36
|
-
get error() {
|
|
37
|
-
return enabled ? console.error : noop;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Log any number of values at the "info" log level
|
|
41
|
-
*/
|
|
42
|
-
get info() {
|
|
43
|
-
return enabled ? console.info : noop;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Log any number of values at the "log" log level
|
|
47
|
-
*/
|
|
48
|
-
get log() {
|
|
49
|
-
return enabled ? console.log : noop;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Log data as a table, with optional properties to use as columns
|
|
53
|
-
*/
|
|
54
|
-
get table() {
|
|
55
|
-
return enabled ? console.table : noop;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Log any number of values together with a trace from where it was called
|
|
59
|
-
*/
|
|
60
|
-
get trace() {
|
|
61
|
-
return enabled ? console.trace : noop;
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Log any number of values at the "warn" log level
|
|
65
|
-
*/
|
|
66
|
-
get warn() {
|
|
67
|
-
return enabled ? console.warn : noop;
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Start a timed logger with a label
|
|
71
|
-
*
|
|
72
|
-
* @param label Label for the logger
|
|
73
|
-
* @returns _Timed_ instance
|
|
74
|
-
*/
|
|
75
|
-
time(label) {
|
|
76
|
-
return new Timed(label);
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
/**
|
|
80
4
|
* A named timer that can be used to log durations to the console
|
|
81
5
|
*/
|
|
82
|
-
var
|
|
6
|
+
var TimedLogger = class {
|
|
83
7
|
#logger;
|
|
84
8
|
#stopper;
|
|
85
9
|
#state;
|
|
@@ -121,7 +45,41 @@ var Timed = class {
|
|
|
121
45
|
return stopper;
|
|
122
46
|
}
|
|
123
47
|
};
|
|
124
|
-
const
|
|
48
|
+
const methods = [
|
|
49
|
+
"debug",
|
|
50
|
+
"dir",
|
|
51
|
+
"error",
|
|
52
|
+
"info",
|
|
53
|
+
"log",
|
|
54
|
+
"table",
|
|
55
|
+
"trace",
|
|
56
|
+
"warn"
|
|
57
|
+
];
|
|
58
|
+
/**
|
|
59
|
+
* A logger that can be used to log messages to the console
|
|
60
|
+
*
|
|
61
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
62
|
+
*/
|
|
63
|
+
const Logger = (() => {
|
|
64
|
+
const instance = {};
|
|
65
|
+
Object.defineProperties(instance, {
|
|
66
|
+
enabled: {
|
|
67
|
+
get() {
|
|
68
|
+
return enabled;
|
|
69
|
+
},
|
|
70
|
+
set(value) {
|
|
71
|
+
enabled = typeof value === "boolean" ? value : enabled;
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
time: { value(label) {
|
|
75
|
+
return new TimedLogger(label);
|
|
76
|
+
} }
|
|
77
|
+
});
|
|
78
|
+
for (const method of methods) Object.defineProperty(instance, method, { get() {
|
|
79
|
+
return enabled ? console[method].bind(console) : noop;
|
|
80
|
+
} });
|
|
81
|
+
return Object.freeze(instance);
|
|
82
|
+
})();
|
|
125
83
|
let enabled = true;
|
|
126
84
|
//#endregion
|
|
127
|
-
export {
|
|
85
|
+
export { Logger };
|
package/dist/string/index.d.mts
CHANGED
|
@@ -14,6 +14,13 @@ declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): st
|
|
|
14
14
|
* @returns Dedented string
|
|
15
15
|
*/
|
|
16
16
|
declare function dedent(value: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* Get a new UUID string _(version 4)_ for use in _HTML_
|
|
19
|
+
*
|
|
20
|
+
* @param html Use _HTML_-friendly format
|
|
21
|
+
* @returns UUID string
|
|
22
|
+
*/
|
|
23
|
+
declare function getUuid(html: true): string;
|
|
17
24
|
/**
|
|
18
25
|
* Get a new UUID string _(version 4)_
|
|
19
26
|
*
|
package/dist/string/index.mjs
CHANGED
|
@@ -30,19 +30,24 @@ function dedent(value, ...values) {
|
|
|
30
30
|
*
|
|
31
31
|
* @returns UUID string
|
|
32
32
|
*/
|
|
33
|
-
function getUuid() {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
hex.
|
|
41
|
-
hex.substring(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
function getUuid(html) {
|
|
34
|
+
const forHTML = html === true;
|
|
35
|
+
while (true) {
|
|
36
|
+
const bytes = new Uint8Array(16);
|
|
37
|
+
crypto.getRandomValues(bytes);
|
|
38
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
39
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
40
|
+
const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, ZERO)).join("");
|
|
41
|
+
const first = hex.substring(0, 8);
|
|
42
|
+
if (forHTML && NUMERICAL_PREFIX_PATTERN.test(first)) continue;
|
|
43
|
+
return [
|
|
44
|
+
first,
|
|
45
|
+
hex.substring(8, 12),
|
|
46
|
+
hex.substring(12, 16),
|
|
47
|
+
hex.substring(16, 20),
|
|
48
|
+
hex.substring(20, 32)
|
|
49
|
+
].join(forHTML ? DELIMITER_UUID_HTML : DELIMITER_UUID_DEFAULT);
|
|
50
|
+
}
|
|
46
51
|
}
|
|
47
52
|
/**
|
|
48
53
|
* Parse a JSON string into its proper value _(or `undefined` if it fails)_
|
|
@@ -84,6 +89,9 @@ function truncate(value, length, suffix) {
|
|
|
84
89
|
const truncatedLength = length - actualSuffixLength;
|
|
85
90
|
return `${value.slice(0, truncatedLength)}${actualSuffix}`;
|
|
86
91
|
}
|
|
92
|
+
const DELIMITER_UUID_DEFAULT = "-";
|
|
93
|
+
const DELIMITER_UUID_HTML = "_";
|
|
94
|
+
const NUMERICAL_PREFIX_PATTERN = /^\d/;
|
|
87
95
|
const ZERO = "0";
|
|
88
96
|
//#endregion
|
|
89
97
|
export { dedent, getString, getUuid, join, parse, trim, truncate, words };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oscarpalmer/atoms",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.188.1",
|
|
4
4
|
"description": "Atomic utilities for making your JavaScript better.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"helper",
|
|
@@ -257,7 +257,7 @@
|
|
|
257
257
|
"watch": "npx vite build --watch"
|
|
258
258
|
},
|
|
259
259
|
"devDependencies": {
|
|
260
|
-
"@oxlint/plugins": "^1.
|
|
260
|
+
"@oxlint/plugins": "^1.69",
|
|
261
261
|
"@types/node": "^25.9",
|
|
262
262
|
"@vitest/coverage-istanbul": "^4.1",
|
|
263
263
|
"eslint": "^10.4",
|
package/src/logger.ts
CHANGED
|
@@ -2,97 +2,70 @@ import {noop} from './internal/function/misc';
|
|
|
2
2
|
|
|
3
3
|
// #region Types
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
* A logger that can be used to log messages to the console
|
|
7
|
-
*
|
|
8
|
-
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
9
|
-
*/
|
|
10
|
-
class Logger {
|
|
5
|
+
type LoggerInstance = {
|
|
11
6
|
/**
|
|
12
7
|
* Log any number of values at the "debug" log level
|
|
13
8
|
*/
|
|
14
|
-
get debug(): typeof console.debug
|
|
15
|
-
return enabled ? console.debug : noop;
|
|
16
|
-
}
|
|
9
|
+
get debug(): typeof console.debug;
|
|
17
10
|
|
|
18
11
|
/**
|
|
19
12
|
* Log the value and shows all its properties
|
|
20
13
|
*/
|
|
21
|
-
get dir(): typeof console.dir
|
|
22
|
-
return enabled ? console.dir : noop;
|
|
23
|
-
}
|
|
14
|
+
get dir(): typeof console.dir;
|
|
24
15
|
|
|
25
16
|
/**
|
|
26
17
|
* Is logging to the console enabled? _(defaults to `true`)_
|
|
27
18
|
*/
|
|
28
|
-
get enabled(): boolean
|
|
29
|
-
return enabled;
|
|
30
|
-
}
|
|
19
|
+
get enabled(): boolean;
|
|
31
20
|
|
|
32
21
|
/**
|
|
33
22
|
* Enable or disable logging to the console
|
|
34
23
|
*/
|
|
35
|
-
set enabled(value: boolean)
|
|
36
|
-
enabled = typeof value === 'boolean' ? value : enabled;
|
|
37
|
-
}
|
|
24
|
+
set enabled(value: boolean);
|
|
38
25
|
|
|
39
26
|
/**
|
|
40
27
|
* Log any number of values at the "error" log level
|
|
41
28
|
*/
|
|
42
|
-
get error(): typeof console.error
|
|
43
|
-
return enabled ? console.error : noop;
|
|
44
|
-
}
|
|
29
|
+
get error(): typeof console.error;
|
|
45
30
|
|
|
46
31
|
/**
|
|
47
32
|
* Log any number of values at the "info" log level
|
|
48
33
|
*/
|
|
49
|
-
get info(): typeof console.info
|
|
50
|
-
return enabled ? console.info : noop;
|
|
51
|
-
}
|
|
34
|
+
get info(): typeof console.info;
|
|
52
35
|
|
|
53
36
|
/**
|
|
54
37
|
* Log any number of values at the "log" log level
|
|
55
38
|
*/
|
|
56
|
-
get log(): typeof console.log
|
|
57
|
-
return enabled ? console.log : noop;
|
|
58
|
-
}
|
|
39
|
+
get log(): typeof console.log;
|
|
59
40
|
|
|
60
41
|
/**
|
|
61
42
|
* Log data as a table, with optional properties to use as columns
|
|
62
43
|
*/
|
|
63
|
-
get table(): typeof console.table
|
|
64
|
-
return enabled ? console.table : noop;
|
|
65
|
-
}
|
|
44
|
+
get table(): typeof console.table;
|
|
66
45
|
|
|
67
46
|
/**
|
|
68
47
|
* Log any number of values together with a trace from where it was called
|
|
69
48
|
*/
|
|
70
|
-
get trace(): typeof console.trace
|
|
71
|
-
return enabled ? console.trace : noop;
|
|
72
|
-
}
|
|
49
|
+
get trace(): typeof console.trace;
|
|
73
50
|
|
|
74
51
|
/**
|
|
75
52
|
* Log any number of values at the "warn" log level
|
|
76
53
|
*/
|
|
77
|
-
get warn(): typeof console.warn
|
|
78
|
-
return enabled ? console.warn : noop;
|
|
79
|
-
}
|
|
54
|
+
get warn(): typeof console.warn;
|
|
80
55
|
|
|
81
56
|
/**
|
|
82
57
|
* Start a timed logger with a label
|
|
83
58
|
*
|
|
84
59
|
* @param label Label for the logger
|
|
85
|
-
* @returns
|
|
60
|
+
* @returns _TimedLogger_ instance
|
|
86
61
|
*/
|
|
87
|
-
time(label: string):
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
}
|
|
62
|
+
time(label: string): TimedLogger;
|
|
63
|
+
};
|
|
91
64
|
|
|
92
65
|
/**
|
|
93
66
|
* A named timer that can be used to log durations to the console
|
|
94
67
|
*/
|
|
95
|
-
class
|
|
68
|
+
class TimedLogger {
|
|
96
69
|
#logger: typeof console.timeLog | undefined;
|
|
97
70
|
#stopper: typeof console.timeEnd | undefined;
|
|
98
71
|
|
|
@@ -158,7 +131,42 @@ type TimeState = {
|
|
|
158
131
|
|
|
159
132
|
// #region Variables
|
|
160
133
|
|
|
161
|
-
const
|
|
134
|
+
const methods = ['debug', 'dir', 'error', 'info', 'log', 'table', 'trace', 'warn'] as const;
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* A logger that can be used to log messages to the console
|
|
138
|
+
*
|
|
139
|
+
* _(Logging can be enabled or disabled by setting the `enabled` property)_
|
|
140
|
+
*/
|
|
141
|
+
const Logger = (() => {
|
|
142
|
+
const instance = {};
|
|
143
|
+
|
|
144
|
+
Object.defineProperties(instance, {
|
|
145
|
+
enabled: {
|
|
146
|
+
get() {
|
|
147
|
+
return enabled;
|
|
148
|
+
},
|
|
149
|
+
set(value: boolean) {
|
|
150
|
+
enabled = typeof value === 'boolean' ? value : enabled;
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
time: {
|
|
154
|
+
value(label: string) {
|
|
155
|
+
return new TimedLogger(label);
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
for (const method of methods) {
|
|
161
|
+
Object.defineProperty(instance, method, {
|
|
162
|
+
get() {
|
|
163
|
+
return enabled ? console[method].bind(console) : noop;
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return Object.freeze(instance);
|
|
169
|
+
})() as LoggerInstance;
|
|
162
170
|
|
|
163
171
|
let enabled = true;
|
|
164
172
|
|
|
@@ -166,6 +174,6 @@ let enabled = true;
|
|
|
166
174
|
|
|
167
175
|
// #region Exports
|
|
168
176
|
|
|
169
|
-
export {
|
|
177
|
+
export {Logger, type TimedLogger};
|
|
170
178
|
|
|
171
179
|
// #endregion
|
package/src/string/index.ts
CHANGED
|
@@ -69,29 +69,54 @@ export function dedent(value: string | TemplateStringsArray, ...values: unknown[
|
|
|
69
69
|
return result.trim();
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* Get a new UUID string _(version 4)_ for use in _HTML_
|
|
74
|
+
*
|
|
75
|
+
* @param html Use _HTML_-friendly format
|
|
76
|
+
* @returns UUID string
|
|
77
|
+
*/
|
|
78
|
+
export function getUuid(html: true): string;
|
|
79
|
+
|
|
72
80
|
/**
|
|
73
81
|
* Get a new UUID string _(version 4)_
|
|
74
82
|
*
|
|
75
83
|
* @returns UUID string
|
|
76
84
|
*/
|
|
77
|
-
export function getUuid(): string
|
|
78
|
-
const bytes = new Uint8Array(16);
|
|
85
|
+
export function getUuid(): string;
|
|
79
86
|
|
|
80
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Get a new UUID string _(version 4)_
|
|
89
|
+
*
|
|
90
|
+
* @returns UUID string
|
|
91
|
+
*/
|
|
92
|
+
export function getUuid(html?: unknown): string {
|
|
93
|
+
const forHTML = html === true;
|
|
81
94
|
|
|
82
|
-
|
|
95
|
+
while (true) {
|
|
96
|
+
const bytes = new Uint8Array(16);
|
|
83
97
|
|
|
84
|
-
|
|
98
|
+
crypto.getRandomValues(bytes);
|
|
85
99
|
|
|
86
|
-
|
|
100
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40;
|
|
87
101
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
hex.
|
|
91
|
-
|
|
92
|
-
hex.substring(
|
|
93
|
-
|
|
94
|
-
|
|
102
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80;
|
|
103
|
+
|
|
104
|
+
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, ZERO)).join('');
|
|
105
|
+
|
|
106
|
+
const first = hex.substring(0, 8);
|
|
107
|
+
|
|
108
|
+
if (forHTML && NUMERICAL_PREFIX_PATTERN.test(first)) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return [
|
|
113
|
+
first,
|
|
114
|
+
hex.substring(8, 12),
|
|
115
|
+
hex.substring(12, 16),
|
|
116
|
+
hex.substring(16, 20),
|
|
117
|
+
hex.substring(20, 32),
|
|
118
|
+
].join(forHTML ? DELIMITER_UUID_HTML : DELIMITER_UUID_DEFAULT);
|
|
119
|
+
}
|
|
95
120
|
}
|
|
96
121
|
|
|
97
122
|
/**
|
|
@@ -155,6 +180,12 @@ export function truncate(value: string, length: number, suffix?: string): string
|
|
|
155
180
|
|
|
156
181
|
// #region Variables
|
|
157
182
|
|
|
183
|
+
const DELIMITER_UUID_DEFAULT = '-';
|
|
184
|
+
|
|
185
|
+
const DELIMITER_UUID_HTML = '_';
|
|
186
|
+
|
|
187
|
+
const NUMERICAL_PREFIX_PATTERN = /^\d/;
|
|
188
|
+
|
|
158
189
|
const ZERO = '0';
|
|
159
190
|
|
|
160
191
|
// #endregion
|