@powfix/core-js 0.20.5 → 0.21.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/browser/cjs/index.browser.js +3 -0
- package/dist/browser/cjs/shared/utils/index.js +1 -1
- package/dist/browser/esm/index.browser.mjs +1 -0
- package/dist/browser/esm/shared/utils/index.mjs +1 -1
- package/dist/browser/types/index.browser.d.ts +1 -0
- package/dist/browser/types/shared/utils/UUID.d.ts +1 -1
- package/dist/browser/types/shared/utils/index.d.ts +1 -0
- package/dist/cjs/index.js +17 -0
- package/dist/cjs/shared/constants/COORDINATE.js +10 -0
- package/dist/cjs/shared/constants/CRUD.js +22 -0
- package/dist/cjs/shared/constants/DATE.js +26 -0
- package/dist/cjs/shared/constants/DISTANCE.js +18 -0
- package/dist/cjs/shared/constants/DURATION.js +21 -0
- package/dist/cjs/shared/constants/NOT_NULL.js +4 -0
- package/dist/cjs/shared/constants/index.js +22 -0
- package/dist/cjs/shared/index.js +23 -0
- package/dist/cjs/shared/interfaces/Coordinate.js +2 -0
- package/dist/cjs/shared/interfaces/Point2.js +2 -0
- package/dist/cjs/shared/interfaces/Point3.js +2 -0
- package/dist/cjs/shared/interfaces/StorageProvider.js +2 -0
- package/dist/cjs/shared/interfaces/index.js +20 -0
- package/dist/cjs/shared/managers/SessionManager.js +134 -0
- package/dist/cjs/shared/managers/SessionManager.type.js +2 -0
- package/dist/cjs/shared/managers/index.js +18 -0
- package/dist/cjs/shared/scripts/base64Polyfill.js +18 -0
- package/dist/cjs/shared/scripts/index.js +17 -0
- package/dist/cjs/shared/services/index.js +17 -0
- package/dist/cjs/shared/services/time/TimeService.js +180 -0
- package/dist/cjs/shared/services/time/TimeService.type.js +2 -0
- package/dist/cjs/shared/services/time/index.js +18 -0
- package/dist/cjs/shared/types/IntRage.js +2 -0
- package/dist/cjs/shared/types/PartialExcept.js +2 -0
- package/dist/cjs/shared/types/index.js +18 -0
- package/dist/cjs/shared/utils/ArrayUtils.js +30 -0
- package/dist/cjs/shared/utils/AxiosUtils.js +53 -0
- package/dist/cjs/shared/utils/BooleanUtils.js +8 -0
- package/dist/cjs/shared/utils/Calc.js +27 -0
- package/dist/cjs/shared/utils/CoordinateUtils.js +42 -0
- package/dist/cjs/shared/utils/DateUtils.js +310 -0
- package/dist/cjs/shared/utils/Enum.js +36 -0
- package/dist/cjs/shared/utils/Enum.types.js +2 -0
- package/dist/cjs/shared/utils/I18nUtils.js +13 -0
- package/dist/cjs/shared/utils/JuminNumberUtils.js +50 -0
- package/dist/cjs/shared/utils/NumberUtils.js +25 -0
- package/dist/cjs/shared/utils/ObjectIdUtils.js +30 -0
- package/dist/cjs/shared/utils/Point3Utils.js +12 -0
- package/dist/cjs/shared/utils/RandomUtils.js +64 -0
- package/dist/cjs/shared/utils/Sequencer.js +148 -0
- package/dist/cjs/shared/utils/StringUtils.js +37 -0
- package/dist/cjs/shared/utils/UUID.js +320 -0
- package/dist/cjs/shared/utils/UUID.types.js +2 -0
- package/dist/cjs/shared/utils/Uint8ArrayUtils.js +13 -0
- package/dist/cjs/shared/utils/UuidUtils.js +77 -0
- package/dist/cjs/shared/utils/Validator.js +118 -0
- package/dist/cjs/shared/utils/global/atob.js +9 -0
- package/dist/cjs/shared/utils/global/base64.js +25 -0
- package/dist/cjs/shared/utils/global/between.js +7 -0
- package/dist/cjs/shared/utils/global/btoa.js +9 -0
- package/dist/cjs/shared/utils/global/castArray.js +6 -0
- package/dist/cjs/shared/utils/global/circularDistance.js +14 -0
- package/dist/cjs/shared/utils/global/fallbackIfMatch.js +9 -0
- package/dist/cjs/shared/utils/global/fallbackIfNull.js +7 -0
- package/dist/cjs/shared/utils/global/fallbackIfNullish.js +9 -0
- package/dist/cjs/shared/utils/global/fallbackIfUndefined.js +7 -0
- package/dist/cjs/shared/utils/global/firstNonNullish.js +13 -0
- package/dist/cjs/shared/utils/global/flat.js +117 -0
- package/dist/cjs/shared/utils/global/index.js +31 -0
- package/dist/cjs/shared/utils/global/processFirstNonNullish.js +11 -0
- package/dist/cjs/shared/utils/global/pureEnum.js +23 -0
- package/dist/cjs/shared/utils/global/sleep.js +5 -0
- package/dist/cjs/shared/utils/index.js +39 -0
- package/dist/cjs/shared/utils/try-catch/TryCatch.js +78 -0
- package/dist/cjs/shared/utils/try-catch/index.js +17 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/shared/constants/COORDINATE.mjs +6 -0
- package/dist/esm/shared/constants/CRUD.mjs +19 -0
- package/dist/esm/shared/constants/DATE.mjs +22 -0
- package/dist/esm/shared/constants/DISTANCE.mjs +14 -0
- package/dist/esm/shared/constants/DURATION.mjs +17 -0
- package/dist/esm/shared/constants/NOT_NULL.mjs +1 -0
- package/dist/esm/shared/constants/index.mjs +6 -0
- package/dist/esm/shared/index.mjs +7 -0
- package/dist/esm/shared/interfaces/Coordinate.mjs +1 -0
- package/dist/esm/shared/interfaces/Point2.mjs +1 -0
- package/dist/esm/shared/interfaces/Point3.mjs +1 -0
- package/dist/esm/shared/interfaces/StorageProvider.mjs +1 -0
- package/dist/esm/shared/interfaces/index.mjs +4 -0
- package/dist/esm/shared/managers/SessionManager.mjs +127 -0
- package/dist/esm/shared/managers/SessionManager.type.mjs +1 -0
- package/dist/esm/shared/managers/index.mjs +2 -0
- package/dist/esm/shared/scripts/base64Polyfill.mjs +11 -0
- package/dist/esm/shared/scripts/index.mjs +1 -0
- package/dist/esm/shared/services/index.mjs +1 -0
- package/dist/esm/shared/services/time/TimeService.mjs +173 -0
- package/dist/esm/shared/services/time/TimeService.type.mjs +1 -0
- package/dist/esm/shared/services/time/index.mjs +2 -0
- package/dist/esm/shared/types/IntRage.mjs +1 -0
- package/dist/esm/shared/types/PartialExcept.mjs +1 -0
- package/dist/esm/shared/types/index.mjs +2 -0
- package/dist/esm/shared/utils/ArrayUtils.mjs +26 -0
- package/dist/esm/shared/utils/AxiosUtils.mjs +49 -0
- package/dist/esm/shared/utils/BooleanUtils.mjs +5 -0
- package/dist/esm/shared/utils/Calc.mjs +23 -0
- package/dist/esm/shared/utils/CoordinateUtils.mjs +38 -0
- package/dist/esm/shared/utils/DateUtils.mjs +303 -0
- package/dist/esm/shared/utils/Enum.mjs +32 -0
- package/dist/esm/shared/utils/Enum.types.mjs +1 -0
- package/dist/esm/shared/utils/I18nUtils.mjs +6 -0
- package/dist/esm/shared/utils/JuminNumberUtils.mjs +46 -0
- package/dist/esm/shared/utils/NumberUtils.mjs +21 -0
- package/dist/esm/shared/utils/ObjectIdUtils.mjs +26 -0
- package/dist/esm/shared/utils/Point3Utils.mjs +8 -0
- package/dist/esm/shared/utils/RandomUtils.mjs +60 -0
- package/dist/esm/shared/utils/Sequencer.mjs +141 -0
- package/dist/esm/shared/utils/StringUtils.mjs +33 -0
- package/dist/esm/shared/utils/UUID.mjs +316 -0
- package/dist/esm/shared/utils/UUID.types.mjs +1 -0
- package/dist/esm/shared/utils/Uint8ArrayUtils.mjs +9 -0
- package/dist/esm/shared/utils/UuidUtils.mjs +40 -0
- package/dist/esm/shared/utils/Validator.mjs +114 -0
- package/dist/esm/shared/utils/global/atob.mjs +3 -0
- package/dist/esm/shared/utils/global/base64.mjs +21 -0
- package/dist/esm/shared/utils/global/between.mjs +3 -0
- package/dist/esm/shared/utils/global/btoa.mjs +3 -0
- package/dist/esm/shared/utils/global/castArray.mjs +3 -0
- package/dist/esm/shared/utils/global/circularDistance.mjs +11 -0
- package/dist/esm/shared/utils/global/fallbackIfMatch.mjs +6 -0
- package/dist/esm/shared/utils/global/fallbackIfNull.mjs +4 -0
- package/dist/esm/shared/utils/global/fallbackIfNullish.mjs +6 -0
- package/dist/esm/shared/utils/global/fallbackIfUndefined.mjs +4 -0
- package/dist/esm/shared/utils/global/firstNonNullish.mjs +10 -0
- package/dist/esm/shared/utils/global/flat.mjs +113 -0
- package/dist/esm/shared/utils/global/index.mjs +15 -0
- package/dist/esm/shared/utils/global/processFirstNonNullish.mjs +8 -0
- package/dist/esm/shared/utils/global/pureEnum.mjs +18 -0
- package/dist/esm/shared/utils/global/sleep.mjs +1 -0
- package/dist/esm/shared/utils/index.mjs +23 -0
- package/dist/esm/shared/utils/try-catch/TryCatch.mjs +74 -0
- package/dist/esm/shared/utils/try-catch/index.mjs +1 -0
- package/dist/node/cjs/index.nodejs.js +3 -0
- package/dist/node/cjs/shared/utils/index.js +1 -1
- package/dist/node/esm/index.nodejs.mjs +1 -0
- package/dist/node/esm/shared/utils/index.mjs +1 -1
- package/dist/node/types/index.nodejs.d.ts +1 -0
- package/dist/node/types/shared/utils/UUID.d.ts +1 -1
- package/dist/node/types/shared/utils/index.d.ts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/shared/constants/COORDINATE.d.ts +7 -0
- package/dist/types/shared/constants/CRUD.d.ts +9 -0
- package/dist/types/shared/constants/DATE.d.ts +13 -0
- package/dist/types/shared/constants/DISTANCE.d.ts +13 -0
- package/dist/types/shared/constants/DURATION.d.ts +16 -0
- package/dist/types/shared/constants/NOT_NULL.d.ts +1 -0
- package/dist/types/shared/constants/index.d.ts +6 -0
- package/dist/types/shared/index.d.ts +7 -0
- package/dist/types/shared/interfaces/Coordinate.d.ts +8 -0
- package/dist/types/shared/interfaces/Point2.d.ts +4 -0
- package/dist/types/shared/interfaces/Point3.d.ts +4 -0
- package/dist/types/shared/interfaces/StorageProvider.d.ts +7 -0
- package/dist/types/shared/interfaces/index.d.ts +4 -0
- package/dist/types/shared/managers/SessionManager.d.ts +15 -0
- package/dist/types/shared/managers/SessionManager.type.d.ts +9 -0
- package/dist/types/shared/managers/index.d.ts +2 -0
- package/dist/types/shared/scripts/base64Polyfill.d.ts +1 -0
- package/dist/types/shared/scripts/index.d.ts +1 -0
- package/dist/types/shared/services/index.d.ts +1 -0
- package/dist/types/shared/services/time/TimeService.d.ts +31 -0
- package/dist/types/shared/services/time/TimeService.type.d.ts +21 -0
- package/dist/types/shared/services/time/index.d.ts +2 -0
- package/dist/types/shared/types/IntRage.d.ts +3 -0
- package/dist/types/shared/types/PartialExcept.d.ts +1 -0
- package/dist/types/shared/types/index.d.ts +2 -0
- package/dist/types/shared/utils/ArrayUtils.d.ts +12 -0
- package/dist/types/shared/utils/AxiosUtils.d.ts +5 -0
- package/dist/types/shared/utils/BooleanUtils.d.ts +1 -0
- package/dist/types/shared/utils/Calc.d.ts +4 -0
- package/dist/types/shared/utils/CoordinateUtils.d.ts +8 -0
- package/dist/types/shared/utils/DateUtils.d.ts +24 -0
- package/dist/types/shared/utils/Enum.d.ts +16 -0
- package/dist/types/shared/utils/Enum.types.d.ts +6 -0
- package/dist/types/shared/utils/I18nUtils.d.ts +4 -0
- package/dist/types/shared/utils/JuminNumberUtils.d.ts +4 -0
- package/dist/types/shared/utils/NumberUtils.d.ts +4 -0
- package/dist/types/shared/utils/ObjectIdUtils.d.ts +4 -0
- package/dist/types/shared/utils/Point3Utils.d.ts +4 -0
- package/dist/types/shared/utils/RandomUtils.d.ts +8 -0
- package/dist/types/shared/utils/Sequencer.d.ts +38 -0
- package/dist/types/shared/utils/StringUtils.d.ts +5 -0
- package/dist/types/shared/utils/UUID.d.ts +147 -0
- package/dist/types/shared/utils/UUID.types.d.ts +2 -0
- package/dist/types/shared/utils/Uint8ArrayUtils.d.ts +4 -0
- package/dist/types/shared/utils/UuidUtils.d.ts +14 -0
- package/dist/types/shared/utils/Validator.d.ts +48 -0
- package/dist/types/shared/utils/global/atob.d.ts +2 -0
- package/dist/types/shared/utils/global/base64.d.ts +2 -0
- package/dist/types/shared/utils/global/between.d.ts +1 -0
- package/dist/types/shared/utils/global/btoa.d.ts +2 -0
- package/dist/types/shared/utils/global/castArray.d.ts +1 -0
- package/dist/types/shared/utils/global/circularDistance.d.ts +1 -0
- package/dist/types/shared/utils/global/fallbackIfMatch.d.ts +1 -0
- package/dist/types/shared/utils/global/fallbackIfNull.d.ts +1 -0
- package/dist/types/shared/utils/global/fallbackIfNullish.d.ts +1 -0
- package/dist/types/shared/utils/global/fallbackIfUndefined.d.ts +1 -0
- package/dist/types/shared/utils/global/firstNonNullish.d.ts +1 -0
- package/dist/types/shared/utils/global/flat.d.ts +14 -0
- package/dist/types/shared/utils/global/index.d.ts +15 -0
- package/dist/types/shared/utils/global/processFirstNonNullish.d.ts +1 -0
- package/dist/types/shared/utils/global/pureEnum.d.ts +17 -0
- package/dist/types/shared/utils/global/sleep.d.ts +1 -0
- package/dist/types/shared/utils/index.d.ts +23 -0
- package/dist/types/shared/utils/try-catch/TryCatch.d.ts +24 -0
- package/dist/types/shared/utils/try-catch/index.d.ts +1 -0
- package/package.json +10 -4
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export class StringUtils {
|
|
2
|
+
static numberWithCommas(x) {
|
|
3
|
+
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
4
|
+
}
|
|
5
|
+
static getByte(s) {
|
|
6
|
+
const getByteLength = (decimal) => {
|
|
7
|
+
const LINE_FEED = 10;
|
|
8
|
+
return (decimal >> 7) || (LINE_FEED === decimal) ? 2 : 1;
|
|
9
|
+
};
|
|
10
|
+
return s
|
|
11
|
+
.split('')
|
|
12
|
+
.map((s) => s.charCodeAt(0))
|
|
13
|
+
.reduce((prev, unicodeDecimalValue) => prev + getByteLength(unicodeDecimalValue), 0);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
StringUtils.levenshteinDistance = (str1 = '', str2 = '') => {
|
|
17
|
+
const track = Array(str2.length + 1).fill(null).map(() => Array(str1.length + 1).fill(null));
|
|
18
|
+
for (let i = 0; i <= str1.length; i += 1) {
|
|
19
|
+
track[0][i] = i;
|
|
20
|
+
}
|
|
21
|
+
for (let j = 0; j <= str2.length; j += 1) {
|
|
22
|
+
track[j][0] = j;
|
|
23
|
+
}
|
|
24
|
+
for (let j = 1; j <= str2.length; j += 1) {
|
|
25
|
+
for (let i = 1; i <= str1.length; i += 1) {
|
|
26
|
+
const indicator = str1[i - 1] === str2[j - 1] ? 0 : 1;
|
|
27
|
+
track[j][i] = Math.min(track[j][i - 1] + 1, // deletion
|
|
28
|
+
track[j - 1][i] + 1, // insertion
|
|
29
|
+
track[j - 1][i - 1] + indicator);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return track[str2.length][str1.length];
|
|
33
|
+
};
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { Uint8ArrayUtils } from "../index.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* Represents a UUID (Universally Unique Identifier) and provides various utility
|
|
4
|
+
* methods for validation, conversion, comparison and creation.
|
|
5
|
+
*/
|
|
6
|
+
export class UUID {
|
|
7
|
+
/* --------------------------------------------------------------------
|
|
8
|
+
* Validation helpers
|
|
9
|
+
* -------------------------------------------------------------------- */
|
|
10
|
+
/**
|
|
11
|
+
* Checks if the supplied value is a valid hex representation of a UUID.
|
|
12
|
+
* @param hex - The string to validate.
|
|
13
|
+
* @returns true if the string matches {@link REGEX_HEX}.
|
|
14
|
+
*/
|
|
15
|
+
static isValidHex(hex) {
|
|
16
|
+
if (typeof hex !== 'string') {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
return UUID.REGEX_HEX.test(hex);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Checks if the supplied value is a valid RFC 4122 UUID string.
|
|
23
|
+
* @param str - The string to validate.
|
|
24
|
+
* @returns true if the string matches {@link REGEX_RFC4122}.
|
|
25
|
+
*/
|
|
26
|
+
static isValidString(str) {
|
|
27
|
+
if (typeof str !== 'string') {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
return UUID.REGEX_RFC4122.test(str);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Checks whether an ArrayBufferView contains exactly {@link BYTE_LENGTH} bytes.
|
|
34
|
+
* @param bytes - The view to check.
|
|
35
|
+
* @returns true if the byte length is correct.
|
|
36
|
+
*/
|
|
37
|
+
static isValidBytes(bytes) {
|
|
38
|
+
return bytes.byteLength === UUID.BYTE_LENGTH;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Generic validation that accepts either a string (hex or RFC 4122 format)
|
|
42
|
+
* or an ArrayBufferView containing the raw bytes.
|
|
43
|
+
* @param input - The value to validate.
|
|
44
|
+
* @returns true if the input is a valid representation of a UUID.
|
|
45
|
+
*/
|
|
46
|
+
static isValid(input) {
|
|
47
|
+
if (typeof input === 'string') {
|
|
48
|
+
const length = input.length;
|
|
49
|
+
switch (length) {
|
|
50
|
+
case UUID.STR_LENGTH:
|
|
51
|
+
// RFC 4122 uuid(string)
|
|
52
|
+
// 9e472052-a654-4693-9a8b-3ce57ada3d6c
|
|
53
|
+
return UUID.isValidString(input);
|
|
54
|
+
case UUID.HEX_STR_LENGTH:
|
|
55
|
+
// RFC 4122 uuid(string) without hyphens
|
|
56
|
+
// 9e472052a65446939a8b3ce57ada3d6c
|
|
57
|
+
return UUID.isValidHex(input);
|
|
58
|
+
default:
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else if (input instanceof UUID) {
|
|
63
|
+
return UUID.isValidBytes(input.bytes);
|
|
64
|
+
}
|
|
65
|
+
else if (ArrayBuffer.isView(input)) {
|
|
66
|
+
return UUID.isValidBytes(input);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
static version(input) {
|
|
73
|
+
return (this.parse(input)[6] >> 4) & 0x0F;
|
|
74
|
+
}
|
|
75
|
+
/* --------------------------------------------------------------------
|
|
76
|
+
* Parsing / formatting helpers
|
|
77
|
+
* -------------------------------------------------------------------- */
|
|
78
|
+
/**
|
|
79
|
+
* Inserts hyphens into a 32‑character hex string to produce an RFC 4122 string.
|
|
80
|
+
* @param hex - The plain hexadecimal UUID.
|
|
81
|
+
* @returns The formatted RFC 4122 string.
|
|
82
|
+
*/
|
|
83
|
+
static formatHex(hex) {
|
|
84
|
+
if (hex.length !== UUID.HEX_STR_LENGTH) {
|
|
85
|
+
throw new Error(`hex length should be ${UUID.HEX_STR_LENGTH}`);
|
|
86
|
+
}
|
|
87
|
+
return hex.slice(0, 8) + '-' +
|
|
88
|
+
hex.slice(8, 12) + '-' +
|
|
89
|
+
hex.slice(12, 16) + '-' +
|
|
90
|
+
hex.slice(16, 20) + '-' +
|
|
91
|
+
hex.slice(20);
|
|
92
|
+
}
|
|
93
|
+
/** Removes all hyphens from a UUID string. */
|
|
94
|
+
static stripHyphens(str) {
|
|
95
|
+
return str.replace(/-/g, "");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Converts a plain hexadecimal UUID into a Uint8Array.
|
|
99
|
+
* @param hex - The hex string to parse.
|
|
100
|
+
* @returns A Uint8Array containing the raw bytes.
|
|
101
|
+
*/
|
|
102
|
+
static parseHex(hex) {
|
|
103
|
+
if (hex.length !== UUID.HEX_STR_LENGTH) {
|
|
104
|
+
throw new Error(`Invalid hex string, length should be ${UUID.HEX_STR_LENGTH}`);
|
|
105
|
+
}
|
|
106
|
+
return Uint8ArrayUtils.fromHex(hex);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Parses an RFC 4122 string into a Uint8Array.
|
|
110
|
+
* @param str - The formatted UUID string.
|
|
111
|
+
* @returns A Uint8Array containing the raw bytes.
|
|
112
|
+
*/
|
|
113
|
+
static parseString(str) {
|
|
114
|
+
if (str.length !== UUID.STR_LENGTH) {
|
|
115
|
+
throw new Error(`Invalid UUID string, invalid character length should be ${UUID.STR_LENGTH}`);
|
|
116
|
+
}
|
|
117
|
+
if (!UUID.isValid(str)) {
|
|
118
|
+
throw new Error('Invalid UUID string, should be RFC 4122 format');
|
|
119
|
+
}
|
|
120
|
+
const hex = UUID.stripHyphens(str);
|
|
121
|
+
if (hex.length !== UUID.HEX_STR_LENGTH) {
|
|
122
|
+
throw new Error('Invalid UUID string, invalid character length after strip hyphens');
|
|
123
|
+
}
|
|
124
|
+
return Uint8ArrayUtils.fromHex(hex);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Parses an ArrayBufferView into a Uint8Array ensuring the correct byte length.
|
|
128
|
+
* @param bytes - The view to parse.
|
|
129
|
+
* @returns A Uint8Array containing the raw bytes.
|
|
130
|
+
*/
|
|
131
|
+
static parseBytes(bytes) {
|
|
132
|
+
const view = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
133
|
+
if (view.length !== UUID.BYTE_LENGTH) {
|
|
134
|
+
throw new Error(`Expected ${UUID.BYTE_LENGTH} bytes`);
|
|
135
|
+
}
|
|
136
|
+
return new Uint8Array(view);
|
|
137
|
+
}
|
|
138
|
+
/* --------------------------------------------------------------------
|
|
139
|
+
* Factory methods
|
|
140
|
+
* -------------------------------------------------------------------- */
|
|
141
|
+
/**
|
|
142
|
+
* Creates a UUID instance from a plain hexadecimal string.
|
|
143
|
+
* @param hex - The hex representation of the UUID.
|
|
144
|
+
* @returns A new {@link UUID} object.
|
|
145
|
+
*/
|
|
146
|
+
static fromHex(hex) {
|
|
147
|
+
return new this(UUID.parseHex(hex));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates a UUID instance from an RFC 4122 formatted string.
|
|
151
|
+
* @param str - The UUID string with hyphens.
|
|
152
|
+
* @returns A new {@link UUID} object.
|
|
153
|
+
*/
|
|
154
|
+
static fromString(str) {
|
|
155
|
+
return new this(UUID.parseString(str));
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Creates a UUID instance from raw bytes.
|
|
159
|
+
* @param bytes - An ArrayBufferView containing 16 bytes.
|
|
160
|
+
* @returns A new {@link UUID} object.
|
|
161
|
+
*/
|
|
162
|
+
static fromBytes(bytes) {
|
|
163
|
+
return new this(UUID.parseBytes(bytes));
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Parses any supported input (string or bytes) and returns the raw byte array.
|
|
167
|
+
* @param input - The value to parse.
|
|
168
|
+
* @returns A Uint8Array of length {@link BYTE_LENGTH}.
|
|
169
|
+
*/
|
|
170
|
+
static parse(input) {
|
|
171
|
+
if (typeof input === 'string') {
|
|
172
|
+
const length = input.length;
|
|
173
|
+
switch (length) {
|
|
174
|
+
case UUID.STR_LENGTH:
|
|
175
|
+
// RFC 4122 uuid(string)
|
|
176
|
+
return UUID.parseString(input);
|
|
177
|
+
case UUID.HEX_STR_LENGTH:
|
|
178
|
+
// RFC 4122 uuid(string) without hyphens
|
|
179
|
+
return UUID.parseHex(input);
|
|
180
|
+
default:
|
|
181
|
+
throw new Error(`Invalid input string, length should be ${UUID.STR_LENGTH} or ${UUID.HEX_STR_LENGTH}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else if (input instanceof UUID) {
|
|
185
|
+
return input.toBytes();
|
|
186
|
+
}
|
|
187
|
+
else if (ArrayBuffer.isView(input)) {
|
|
188
|
+
return UUID.parseBytes(input);
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
throw new Error("Invalid input, Expected string or ArrayBufferView");
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Creates a UUID from any supported input type.
|
|
196
|
+
* @param input - The value to parse.
|
|
197
|
+
* @returns A new {@link UUID} object.
|
|
198
|
+
*/
|
|
199
|
+
static from(input) {
|
|
200
|
+
return this.fromBytes(UUID.parse(input));
|
|
201
|
+
}
|
|
202
|
+
/** Returns the nil (all zero) UUID. */
|
|
203
|
+
static nil() {
|
|
204
|
+
return this.fromBytes(new Uint8Array(UUID.BYTE_LENGTH));
|
|
205
|
+
}
|
|
206
|
+
/** Returns a UUID consisting of all 0xFF bytes. */
|
|
207
|
+
static max() {
|
|
208
|
+
return this.fromBytes((new Uint8Array(UUID.BYTE_LENGTH)).fill(0xFF));
|
|
209
|
+
}
|
|
210
|
+
/* --------------------------------------------------------------------
|
|
211
|
+
* Equality / comparison
|
|
212
|
+
* -------------------------------------------------------------------- */
|
|
213
|
+
/**
|
|
214
|
+
* Compares multiple UUIDs for strict equality.
|
|
215
|
+
* @param inputs - The UUIDs to compare (at least two required).
|
|
216
|
+
* @returns true if all provided UUIDs are identical.
|
|
217
|
+
*/
|
|
218
|
+
static equals(...inputs) {
|
|
219
|
+
const n = inputs.length;
|
|
220
|
+
if (n < 2) {
|
|
221
|
+
throw new Error('At least two UUIDs required for comparison');
|
|
222
|
+
}
|
|
223
|
+
const ref = this.parse(inputs[0]);
|
|
224
|
+
for (let i = 1; i < n; ++i) {
|
|
225
|
+
const b = this.parse(inputs[i]);
|
|
226
|
+
for (let j = 0; j < UUID.BYTE_LENGTH; ++j) {
|
|
227
|
+
if (ref[j] !== b[j])
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Lexicographically compares two UUIDs.
|
|
235
|
+
* @param uuid1 - The first UUID.
|
|
236
|
+
* @param uuid2 - The second UUID.
|
|
237
|
+
* @returns -1 if uuid1 < uuid2, 1 if uuid1 > uuid2, 0 otherwise.
|
|
238
|
+
*/
|
|
239
|
+
static compare(uuid1, uuid2) {
|
|
240
|
+
const a = this.parse(uuid1);
|
|
241
|
+
const b = this.parse(uuid2);
|
|
242
|
+
for (let i = 0; i < UUID.BYTE_LENGTH; i++) {
|
|
243
|
+
if (a[i] !== b[i])
|
|
244
|
+
return a[i] < b[i] ? -1 : 1;
|
|
245
|
+
}
|
|
246
|
+
return 0;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Constructs a new {@link UUID} instance from any supported input type.
|
|
250
|
+
* @param input - The value to parse (string or ArrayBufferView).
|
|
251
|
+
*/
|
|
252
|
+
constructor(input) {
|
|
253
|
+
this.bytes = UUID.parse(input);
|
|
254
|
+
}
|
|
255
|
+
/** Instance wrapper for {@link equals}. */
|
|
256
|
+
equals(...inputs) {
|
|
257
|
+
return UUID.equals(this, ...inputs);
|
|
258
|
+
}
|
|
259
|
+
/** Instance wrapper for {@link compare}. */
|
|
260
|
+
compare(other) {
|
|
261
|
+
return UUID.compare(this, other);
|
|
262
|
+
}
|
|
263
|
+
version() {
|
|
264
|
+
return UUID.version(this);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Returns the RFC 4122 string representation of this UUID.
|
|
268
|
+
* @returns The formatted UUID string.
|
|
269
|
+
*/
|
|
270
|
+
toString() {
|
|
271
|
+
if (this._str != null) {
|
|
272
|
+
return this._str;
|
|
273
|
+
}
|
|
274
|
+
const hex = this.toHex();
|
|
275
|
+
this._str = UUID.formatHex(hex);
|
|
276
|
+
return this._str;
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Returns the plain hexadecimal representation of this UUID.
|
|
280
|
+
* @returns A 32‑character hex string.
|
|
281
|
+
*/
|
|
282
|
+
toHex() {
|
|
283
|
+
if (this._hex != null) {
|
|
284
|
+
return this._hex;
|
|
285
|
+
}
|
|
286
|
+
this._hex = Uint8ArrayUtils.toHex(this.bytes);
|
|
287
|
+
return this._hex;
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Returns a copy of the raw byte array.
|
|
291
|
+
* @returns A new Uint8Array containing the UUID bytes.
|
|
292
|
+
*/
|
|
293
|
+
toBytes() {
|
|
294
|
+
return new Uint8Array(this.bytes);
|
|
295
|
+
}
|
|
296
|
+
/** JSON serialization helper – returns the RFC 4122 string. */
|
|
297
|
+
toJSON() {
|
|
298
|
+
return this.toString();
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
/* --------------------------------------------------------------------
|
|
302
|
+
* Regular expressions used to validate UUIDs in different string forms
|
|
303
|
+
* -------------------------------------------------------------------- */
|
|
304
|
+
/** Matches a 32‑character hexadecimal representation of a UUID without hyphens. */
|
|
305
|
+
UUID.REGEX_HEX = /^[0-9a-fA-F]{8}[0-9a-fA-F]{4}[1-5][0-9a-fA-F]{3}[89abAB][0-9a-fA-F]{3}[0-9a-fA-F]{12}$/;
|
|
306
|
+
/** Matches the RFC 4122 canonical UUID format with hyphens. */
|
|
307
|
+
UUID.REGEX_RFC4122 = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
|
|
308
|
+
/* --------------------------------------------------------------------
|
|
309
|
+
* Constant lengths used throughout the class
|
|
310
|
+
* -------------------------------------------------------------------- */
|
|
311
|
+
/** Number of bytes that represent a UUID. */
|
|
312
|
+
UUID.BYTE_LENGTH = 16;
|
|
313
|
+
/** Total length of an RFC 4122 UUID string (including hyphens). */
|
|
314
|
+
UUID.STR_LENGTH = 36;
|
|
315
|
+
/** Length of a UUID represented as a plain hexadecimal string. */
|
|
316
|
+
UUID.HEX_STR_LENGTH = 32;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export class Uint8ArrayUtils {
|
|
2
|
+
static fromHex(hex) {
|
|
3
|
+
var _a, _b;
|
|
4
|
+
return Uint8Array.from((_b = (_a = hex.match(/.{1,2}/g)) === null || _a === void 0 ? void 0 : _a.map((byte) => parseInt(byte, 16))) !== null && _b !== void 0 ? _b : []);
|
|
5
|
+
}
|
|
6
|
+
static toHex(bytes) {
|
|
7
|
+
return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import * as uuid from "uuid";
|
|
2
|
+
import { Uint8ArrayUtils } from "./Uint8ArrayUtils.mjs";
|
|
3
|
+
export class UuidUtils {
|
|
4
|
+
static v4() {
|
|
5
|
+
return uuid.v4();
|
|
6
|
+
}
|
|
7
|
+
static format(uuid) {
|
|
8
|
+
if (uuid.length === 32) {
|
|
9
|
+
// Without dash: ca23c587d7f84c76be59f53bbc9f91f8
|
|
10
|
+
return `${uuid.substring(0, 8)}-${uuid.substring(8, 12)}-${uuid.substring(12, 16)}-${uuid.substring(16, 20)}-${uuid.substring(20, 32)}`.toLowerCase();
|
|
11
|
+
}
|
|
12
|
+
else if (uuid.length === 36) {
|
|
13
|
+
// With dash: ca23c587-d7f8-4c76-be59-f53bbc9f91f8
|
|
14
|
+
return uuid.toLowerCase();
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
// Unexpected uuid
|
|
18
|
+
console.warn('Unexpected uuid length', uuid);
|
|
19
|
+
return uuid;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
static toString(binary) {
|
|
23
|
+
if (!binary)
|
|
24
|
+
return null;
|
|
25
|
+
return UuidUtils.format(Uint8ArrayUtils.toHex(binary));
|
|
26
|
+
}
|
|
27
|
+
static toBuffer(uuid) {
|
|
28
|
+
if (!uuid) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return Buffer.from(uuid.replace(/-/g, ''), 'hex');
|
|
32
|
+
}
|
|
33
|
+
static isValidUUID(uuid) {
|
|
34
|
+
if (!uuid)
|
|
35
|
+
return false;
|
|
36
|
+
if (typeof uuid !== 'string')
|
|
37
|
+
return false;
|
|
38
|
+
return RegExp(/^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$/i).test(uuid);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
export class Validator {
|
|
2
|
+
static validate(value, options) {
|
|
3
|
+
value = value || '';
|
|
4
|
+
const validations = [];
|
|
5
|
+
const errors = [];
|
|
6
|
+
const { length } = value;
|
|
7
|
+
const spaceCount = (value.match(/\s/g) || []).length;
|
|
8
|
+
const numberCount = (value.match(/\d/g) || []).length;
|
|
9
|
+
const alphabetCount = (value.match(/[A-Za-z]/g) || []).length;
|
|
10
|
+
const alphabetLowerCaseCount = (value.match(/[a-z]/g) || []).length;
|
|
11
|
+
const alphabetUpperCaseCount = (value.match(/[A-Z]/g) || []).length;
|
|
12
|
+
const specialCount = (value.match(/[~`!@#$%^&*()\-+={[}\]|\\:;"'<,>.?/]/g) || []).length;
|
|
13
|
+
const startsWithNumber = /^\d/.test(value);
|
|
14
|
+
const startsWithAlphabet = /^[A-Za-z]/.test(value);
|
|
15
|
+
const startsWithSpecialCharacter = /^[~`!@#$%^&*()\-+={[}\]|\\:;"'<,>.?/]/.test(value);
|
|
16
|
+
// 최소 길이
|
|
17
|
+
if (options.minLength !== undefined && options.minLength > -1) {
|
|
18
|
+
validations.push(Validator.VALIDATION.MIN_LENGTH);
|
|
19
|
+
if (length < options.minLength) {
|
|
20
|
+
errors.push(Validator.VALIDATION.MIN_LENGTH);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// 최대 길이
|
|
24
|
+
if (options.maxLength !== undefined && options.maxLength > -1) {
|
|
25
|
+
validations.push(Validator.VALIDATION.MAX_LENGTH);
|
|
26
|
+
if (length > options.maxLength) {
|
|
27
|
+
errors.push(Validator.VALIDATION.MAX_LENGTH);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const validateMin = (option, value, validation) => {
|
|
31
|
+
if (option !== undefined && option > -1) {
|
|
32
|
+
validations.push(validation);
|
|
33
|
+
if (value < option) {
|
|
34
|
+
errors.push(validation);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const validateMax = (option, value, validation) => {
|
|
39
|
+
if (option !== undefined && option > -1) {
|
|
40
|
+
validations.push(validation);
|
|
41
|
+
if (value > option) {
|
|
42
|
+
errors.push(validation);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
// 공백 개수
|
|
47
|
+
validateMin(options.spaceMinCount, spaceCount, Validator.VALIDATION.SPACE_MIN_COUNT);
|
|
48
|
+
validateMax(options.spaceMaxCount, spaceCount, Validator.VALIDATION.SPACE_MAX_COUNT);
|
|
49
|
+
// 숫자 개수
|
|
50
|
+
validateMin(options.numberMinCount, numberCount, Validator.VALIDATION.NUMBER_MIN_COUNT);
|
|
51
|
+
validateMax(options.numberMaxCount, numberCount, Validator.VALIDATION.NUMBER_MAX_COUNT);
|
|
52
|
+
// 알파벳 개수
|
|
53
|
+
validateMin(options.alphabetMinCount, alphabetCount, Validator.VALIDATION.ALPHABET_MIN_COUNT);
|
|
54
|
+
validateMax(options.alphabetMaxCount, alphabetCount, Validator.VALIDATION.ALPHABET_MAX_COUNT);
|
|
55
|
+
// 알파벳(소문자) 개수
|
|
56
|
+
validateMin(options.alphabetLowerCaseMinCount, alphabetLowerCaseCount, Validator.VALIDATION.ALPHABET_LOWER_CASE_MIN_COUNT);
|
|
57
|
+
validateMax(options.alphabetLowerCaseMaxCount, alphabetLowerCaseCount, Validator.VALIDATION.ALPHABET_LOWER_CASE_MAX_COUNT);
|
|
58
|
+
// 알파벳(대문자) 개수
|
|
59
|
+
validateMin(options.alphabetUpperCaseMinCount, alphabetUpperCaseCount, Validator.VALIDATION.ALPHABET_UPPER_CASE_MIN_COUNT);
|
|
60
|
+
validateMax(options.alphabetUpperCaseMaxCount, alphabetUpperCaseCount, Validator.VALIDATION.ALPHABET_UPPER_CASE_MAX_COUNT);
|
|
61
|
+
// 특수문자 개수
|
|
62
|
+
validateMin(options.specialCharacterMinCount, specialCount, Validator.VALIDATION.SPECIAL_CHARACTER_MIN_COUNT);
|
|
63
|
+
validateMax(options.specialCharacterMaxCount, specialCount, Validator.VALIDATION.SPECIAL_CHARACTER_MAX_COUNT);
|
|
64
|
+
// 숫자로 시작
|
|
65
|
+
if (options.startsWithNumber) {
|
|
66
|
+
validations.push(Validator.VALIDATION.STARTS_WITH_NUMBER);
|
|
67
|
+
if (!startsWithNumber) {
|
|
68
|
+
errors.push(Validator.VALIDATION.STARTS_WITH_NUMBER);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// 영문으로 시작
|
|
72
|
+
if (options.startsWithAlphabet) {
|
|
73
|
+
validations.push(Validator.VALIDATION.STARTS_WITH_ALPHABET);
|
|
74
|
+
if (!startsWithAlphabet) {
|
|
75
|
+
errors.push(Validator.VALIDATION.STARTS_WITH_ALPHABET);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// 특수문자로 시작
|
|
79
|
+
if (options.startsWithSpecialCharacter) {
|
|
80
|
+
validations.push(Validator.VALIDATION.STARTS_WITH_SPECIAL_CHARACTER);
|
|
81
|
+
if (!startsWithSpecialCharacter) {
|
|
82
|
+
errors.push(Validator.VALIDATION.STARTS_WITH_SPECIAL_CHARACTER);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
result: errors.length === 0,
|
|
87
|
+
validations,
|
|
88
|
+
passes: validations.filter(e => !errors.includes(e)),
|
|
89
|
+
errors,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
(function (Validator) {
|
|
94
|
+
let VALIDATION;
|
|
95
|
+
(function (VALIDATION) {
|
|
96
|
+
VALIDATION["MIN_LENGTH"] = "MIN_LENGTH";
|
|
97
|
+
VALIDATION["MAX_LENGTH"] = "MAX_LENGTH";
|
|
98
|
+
VALIDATION["SPACE_MIN_COUNT"] = "SPACE_MIN_COUNT";
|
|
99
|
+
VALIDATION["SPACE_MAX_COUNT"] = "SPACE_MAX_COUNT";
|
|
100
|
+
VALIDATION["NUMBER_MIN_COUNT"] = "NUMBER_MIN_COUNT";
|
|
101
|
+
VALIDATION["NUMBER_MAX_COUNT"] = "NUMBER_MAX_COUNT";
|
|
102
|
+
VALIDATION["ALPHABET_MIN_COUNT"] = "ALPHABET_MIN_COUNT";
|
|
103
|
+
VALIDATION["ALPHABET_MAX_COUNT"] = "ALPHABET_MAX_COUNT";
|
|
104
|
+
VALIDATION["ALPHABET_LOWER_CASE_MIN_COUNT"] = "ALPHABET_LOWER_CASE_MIN_COUNT";
|
|
105
|
+
VALIDATION["ALPHABET_LOWER_CASE_MAX_COUNT"] = "ALPHABET_LOWER_CASE_MAX_COUNT";
|
|
106
|
+
VALIDATION["ALPHABET_UPPER_CASE_MIN_COUNT"] = "ALPHABET_UPPER_CASE_MIN_COUNT";
|
|
107
|
+
VALIDATION["ALPHABET_UPPER_CASE_MAX_COUNT"] = "ALPHABET_UPPER_CASE_MAX_COUNT";
|
|
108
|
+
VALIDATION["SPECIAL_CHARACTER_MIN_COUNT"] = "SPECIAL_CHARACTER_MIN_COUNT";
|
|
109
|
+
VALIDATION["SPECIAL_CHARACTER_MAX_COUNT"] = "SPECIAL_CHARACTER_MAX_COUNT";
|
|
110
|
+
VALIDATION["STARTS_WITH_ALPHABET"] = "STARTS_WITH_ALPHABET";
|
|
111
|
+
VALIDATION["STARTS_WITH_NUMBER"] = "STARTS_WITH_NUMBER";
|
|
112
|
+
VALIDATION["STARTS_WITH_SPECIAL_CHARACTER"] = "STARTS_WITH_SPECIAL_CHARACTER";
|
|
113
|
+
})(VALIDATION = Validator.VALIDATION || (Validator.VALIDATION = {}));
|
|
114
|
+
})(Validator || (Validator = {}));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { btoa } from "./btoa.mjs";
|
|
2
|
+
import { atob } from "./atob.mjs";
|
|
3
|
+
export function encodeBase64(str) {
|
|
4
|
+
const encoder = new TextEncoder();
|
|
5
|
+
const bytes = encoder.encode(str);
|
|
6
|
+
let binary = '';
|
|
7
|
+
for (const byte of bytes) {
|
|
8
|
+
binary += String.fromCharCode(byte);
|
|
9
|
+
}
|
|
10
|
+
return btoa(binary);
|
|
11
|
+
}
|
|
12
|
+
export function decodeBase64(base64) {
|
|
13
|
+
const binary = atob(base64);
|
|
14
|
+
const binaryLength = binary.length;
|
|
15
|
+
const bytes = new Uint8Array(binaryLength);
|
|
16
|
+
for (let i = 0; i < binaryLength; ++i) {
|
|
17
|
+
bytes[i] = binary.charCodeAt(i);
|
|
18
|
+
}
|
|
19
|
+
const decoder = new TextDecoder('utf-8');
|
|
20
|
+
return decoder.decode(bytes);
|
|
21
|
+
}
|