@cyberskill/shared 3.2.0 → 3.3.0
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/config/commitlint/index.js +8 -9
- package/dist/config/commitlint/index.js.map +1 -1
- package/dist/config/config.type.js +8 -5
- package/dist/config/config.type.js.map +1 -1
- package/dist/config/config.util.js +33 -42
- package/dist/config/config.util.js.map +1 -1
- package/dist/config/env/env.constant.js +6 -5
- package/dist/config/env/env.constant.js.map +1 -1
- package/dist/config/env/env.util.js +26 -27
- package/dist/config/env/env.util.js.map +1 -1
- package/dist/config/env/index.js +2 -7
- package/dist/config/eslint/index.js +23 -19
- package/dist/config/eslint/index.js.map +1 -1
- package/dist/config/graphql-codegen/graphql-codegen.util.js +30 -38
- package/dist/config/graphql-codegen/graphql-codegen.util.js.map +1 -1
- package/dist/config/graphql-codegen/index.js +2 -5
- package/dist/config/index.js +3 -7
- package/dist/config/lint-staged/index.js +8 -7
- package/dist/config/lint-staged/index.js.map +1 -1
- package/dist/config/storybook/index.js +2 -6
- package/dist/config/storybook/storybook.main.js +19 -18
- package/dist/config/storybook/storybook.main.js.map +1 -1
- package/dist/config/storybook/storybook.preview.js +31 -37
- package/dist/config/storybook/storybook.preview.js.map +1 -1
- package/dist/config/vitest/index.js +3 -7
- package/dist/config/vitest/vitest.e2e.js +14 -17
- package/dist/config/vitest/vitest.e2e.js.map +1 -1
- package/dist/config/vitest/vitest.unit.js +31 -32
- package/dist/config/vitest/vitest.unit.js.map +1 -1
- package/dist/constant/common.js +6 -5
- package/dist/constant/common.js.map +1 -1
- package/dist/constant/index.js +3 -7
- package/dist/constant/response-status.d.ts +186 -186
- package/dist/constant/response-status.js +254 -253
- package/dist/constant/response-status.js.map +1 -1
- package/dist/node/apollo-server/apollo-server.util.js +27 -29
- package/dist/node/apollo-server/apollo-server.util.js.map +1 -1
- package/dist/node/apollo-server/index.js +2 -7
- package/dist/node/cli/index.js +112 -113
- package/dist/node/cli/index.js.map +1 -1
- package/dist/node/command/command.type.js +8 -5
- package/dist/node/command/command.type.js.map +1 -1
- package/dist/node/command/command.util.js +136 -149
- package/dist/node/command/command.util.js.map +1 -1
- package/dist/node/command/index.js +3 -12
- package/dist/node/express/express.type.d.ts +11 -0
- package/dist/node/express/express.type.js +2 -0
- package/dist/node/express/express.util.js +74 -65
- package/dist/node/express/express.util.js.map +1 -1
- package/dist/node/express/index.js +3 -14
- package/dist/node/fs/fs.util.js +27 -51
- package/dist/node/fs/fs.util.js.map +1 -1
- package/dist/node/fs/index.js +2 -19
- package/dist/node/log/index.js +2 -8
- package/dist/node/log/log.type.js +8 -5
- package/dist/node/log/log.type.js.map +1 -1
- package/dist/node/log/log.util.js +51 -68
- package/dist/node/log/log.util.js.map +1 -1
- package/dist/node/mongo/index.js +8 -26
- package/dist/node/mongo/mongo.constant.js +6 -6
- package/dist/node/mongo/mongo.constant.js.map +1 -1
- package/dist/node/mongo/mongo.controller.mongoose.d.ts +6 -1
- package/dist/node/mongo/mongo.controller.mongoose.js +333 -467
- package/dist/node/mongo/mongo.controller.mongoose.js.map +1 -1
- package/dist/node/mongo/mongo.controller.native.js +153 -212
- package/dist/node/mongo/mongo.controller.native.js.map +1 -1
- package/dist/node/mongo/mongo.dynamic-populate.js +164 -192
- package/dist/node/mongo/mongo.dynamic-populate.js.map +1 -1
- package/dist/node/mongo/mongo.populate.js +229 -262
- package/dist/node/mongo/mongo.populate.js.map +1 -1
- package/dist/node/mongo/mongo.type.js +8 -17
- package/dist/node/mongo/mongo.type.js.map +1 -1
- package/dist/node/mongo/mongo.util.js +134 -293
- package/dist/node/mongo/mongo.util.js.map +1 -1
- package/dist/node/package/index.js +3 -11
- package/dist/node/package/package.type.js +8 -5
- package/dist/node/package/package.type.js.map +1 -1
- package/dist/node/package/package.util.js +216 -225
- package/dist/node/package/package.util.js.map +1 -1
- package/dist/node/path/index.js +3 -58
- package/dist/node/path/path.constant.js +211 -298
- package/dist/node/path/path.constant.js.map +1 -1
- package/dist/node/path/path.util.js +13 -16
- package/dist/node/path/path.util.js.map +1 -1
- package/dist/node/storage/index.js +3 -10
- package/dist/node/storage/storage.constant.js +6 -8
- package/dist/node/storage/storage.constant.js.map +1 -1
- package/dist/node/storage/storage.util.js +127 -178
- package/dist/node/storage/storage.util.js.map +1 -1
- package/dist/node/upload/index.js +4 -16
- package/dist/node/upload/upload.constant.js +58 -32
- package/dist/node/upload/upload.constant.js.map +1 -1
- package/dist/node/upload/upload.type.js +8 -5
- package/dist/node/upload/upload.type.js.map +1 -1
- package/dist/node/upload/upload.util.js +125 -137
- package/dist/node/upload/upload.util.js.map +1 -1
- package/dist/node/ws/index.js +2 -6
- package/dist/node/ws/ws.type.d.ts +7 -8
- package/dist/node/ws/ws.util.js +46 -44
- package/dist/node/ws/ws.util.js.map +1 -1
- package/dist/node_modules/.pnpm/vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.0_@types_nod_53aa4254f295b3c40bb8f17b6ab226b5/node_modules/vitest/dist/config.js +8 -0
- package/dist/node_modules/.pnpm/vitest@4.1.0_@types_node@25.5.0_jsdom@29.0.0_@noble_hashes@1.8.0__vite@8.0.0_@types_nod_53aa4254f295b3c40bb8f17b6ab226b5/node_modules/vitest/dist/config.js.map +1 -0
- package/dist/react/apollo-client/apollo-client.component.js +16 -18
- package/dist/react/apollo-client/apollo-client.component.js.map +1 -1
- package/dist/react/apollo-client/apollo-client.constant.js +6 -5
- package/dist/react/apollo-client/apollo-client.constant.js.map +1 -1
- package/dist/react/apollo-client/apollo-client.context.js +10 -13
- package/dist/react/apollo-client/apollo-client.context.js.map +1 -1
- package/dist/react/apollo-client/apollo-client.module.scss.js +8 -7
- package/dist/react/apollo-client/apollo-client.module.scss.js.map +1 -1
- package/dist/react/apollo-client/apollo-client.util.js +57 -75
- package/dist/react/apollo-client/apollo-client.util.js.map +1 -1
- package/dist/react/apollo-client/index.js +7 -30
- package/dist/react/apollo-client/links/index.js +2 -5
- package/dist/react/apollo-client/links/upload.js +63 -107
- package/dist/react/apollo-client/links/upload.js.map +1 -1
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.component.js +14 -16
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.component.js.map +1 -1
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.rsc.js +8 -7
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.rsc.js.map +1 -1
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.util.js +14 -14
- package/dist/react/apollo-client-nextjs/apollo-client-nextjs.util.js.map +1 -1
- package/dist/react/apollo-client-nextjs/index.js +3 -7
- package/dist/react/apollo-error/apollo-error.component.js +57 -69
- package/dist/react/apollo-error/apollo-error.component.js.map +1 -1
- package/dist/react/apollo-error/apollo-error.context.js +7 -6
- package/dist/react/apollo-error/apollo-error.context.js.map +1 -1
- package/dist/react/apollo-error/apollo-error.hook.js +10 -10
- package/dist/react/apollo-error/apollo-error.hook.js.map +1 -1
- package/dist/react/apollo-error/apollo-error.module.scss.js +15 -11
- package/dist/react/apollo-error/apollo-error.module.scss.js.map +1 -1
- package/dist/react/apollo-error/apollo-error.provider.js +31 -25
- package/dist/react/apollo-error/apollo-error.provider.js.map +1 -1
- package/dist/react/apollo-error/apollo-error.util.js +14 -16
- package/dist/react/apollo-error/apollo-error.util.js.map +1 -1
- package/dist/react/apollo-error/index.js +5 -15
- package/dist/react/i18next/i18next.hook.js +7 -6
- package/dist/react/i18next/i18next.hook.js.map +1 -1
- package/dist/react/i18next/i18next.util.js +9 -8
- package/dist/react/i18next/i18next.util.js.map +1 -1
- package/dist/react/i18next/index.js +3 -7
- package/dist/react/loading/index.js +3 -9
- package/dist/react/loading/loading.component.js +47 -48
- package/dist/react/loading/loading.component.js.map +1 -1
- package/dist/react/loading/loading.context.js +7 -6
- package/dist/react/loading/loading.context.js.map +1 -1
- package/dist/react/loading/loading.hook.js +11 -11
- package/dist/react/loading/loading.hook.js.map +1 -1
- package/dist/react/loading/loading.module.scss.js +18 -15
- package/dist/react/loading/loading.module.scss.js.map +1 -1
- package/dist/react/loading/loading.provider.js +26 -18
- package/dist/react/loading/loading.provider.js.map +1 -1
- package/dist/react/log/index.js +2 -6
- package/dist/react/log/log.util.d.ts +8 -0
- package/dist/react/log/log.util.js +33 -29
- package/dist/react/log/log.util.js.map +1 -1
- package/dist/react/next-intl/index.js +6 -14
- package/dist/react/next-intl/next-intl.constant.js +16 -15
- package/dist/react/next-intl/next-intl.constant.js.map +1 -1
- package/dist/react/next-intl/next-intl.context.js +7 -6
- package/dist/react/next-intl/next-intl.context.js.map +1 -1
- package/dist/react/next-intl/next-intl.hoc.js +20 -23
- package/dist/react/next-intl/next-intl.hoc.js.map +1 -1
- package/dist/react/next-intl/next-intl.hook.js +13 -14
- package/dist/react/next-intl/next-intl.hook.js.map +1 -1
- package/dist/react/next-intl/next-intl.provider.js +28 -23
- package/dist/react/next-intl/next-intl.provider.js.map +1 -1
- package/dist/react/storage/index.js +3 -7
- package/dist/react/storage/storage.hook.js +61 -58
- package/dist/react/storage/storage.hook.js.map +1 -1
- package/dist/react/storage/storage.util.js +36 -68
- package/dist/react/storage/storage.util.js.map +1 -1
- package/dist/react/toast/index.js +2 -6
- package/dist/react/userback/index.js +2 -5
- package/dist/react/userback/userback.component.js +27 -27
- package/dist/react/userback/userback.component.js.map +1 -1
- package/dist/style.css +2 -1
- package/dist/typescript/common.type.js +8 -5
- package/dist/typescript/common.type.js.map +1 -1
- package/dist/typescript/index.js +2 -5
- package/dist/util/common/common.util.js +119 -48
- package/dist/util/common/common.util.js.map +1 -1
- package/dist/util/common/index.js +2 -9
- package/dist/util/index.js +6 -27
- package/dist/util/object/index.d.ts +3 -0
- package/dist/util/object/index.js +2 -10
- package/dist/util/object/object.util.js +94 -128
- package/dist/util/object/object.util.js.map +1 -1
- package/dist/util/serializer/index.js +2 -5
- package/dist/util/serializer/serializer.util.js +73 -78
- package/dist/util/serializer/serializer.util.js.map +1 -1
- package/dist/util/string/index.d.ts +3 -0
- package/dist/util/string/index.js +2 -10
- package/dist/util/string/string.util.js +59 -71
- package/dist/util/string/string.util.js.map +1 -1
- package/dist/util/validate/index.d.ts +3 -0
- package/dist/util/validate/index.js +2 -5
- package/dist/util/validate/validate.util.js +13 -39
- package/dist/util/validate/validate.util.js.map +1 -1
- package/package.json +29 -27
- package/dist/config/env/index.js.map +0 -1
- package/dist/config/graphql-codegen/index.js.map +0 -1
- package/dist/config/index.js.map +0 -1
- package/dist/config/storybook/index.js.map +0 -1
- package/dist/config/vitest/index.js.map +0 -1
- package/dist/constant/index.js.map +0 -1
- package/dist/node/apollo-server/index.js.map +0 -1
- package/dist/node/command/index.js.map +0 -1
- package/dist/node/express/index.js.map +0 -1
- package/dist/node/fs/index.js.map +0 -1
- package/dist/node/log/index.js.map +0 -1
- package/dist/node/mongo/index.js.map +0 -1
- package/dist/node/package/index.js.map +0 -1
- package/dist/node/path/index.js.map +0 -1
- package/dist/node/storage/index.js.map +0 -1
- package/dist/node/upload/index.js.map +0 -1
- package/dist/node/ws/index.js.map +0 -1
- package/dist/node_modules/.pnpm/vitest@4.0.18_@types_node@25.3.5_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2/node_modules/vitest/dist/config.js +0 -7
- package/dist/node_modules/.pnpm/vitest@4.0.18_@types_node@25.3.5_jiti@2.6.1_jsdom@28.1.0_@noble_hashes@1.8.0__sass@1.97.3_tsx@4.21.0_yaml@2.8.2/node_modules/vitest/dist/config.js.map +0 -1
- package/dist/react/apollo-client/index.js.map +0 -1
- package/dist/react/apollo-client/links/index.js.map +0 -1
- package/dist/react/apollo-client-nextjs/index.js.map +0 -1
- package/dist/react/apollo-error/index.js.map +0 -1
- package/dist/react/i18next/index.js.map +0 -1
- package/dist/react/loading/index.js.map +0 -1
- package/dist/react/log/index.js.map +0 -1
- package/dist/react/next-intl/index.js.map +0 -1
- package/dist/react/storage/index.js.map +0 -1
- package/dist/react/toast/index.js.map +0 -1
- package/dist/react/userback/index.js.map +0 -1
- package/dist/typescript/index.js.map +0 -1
- package/dist/util/common/index.js.map +0 -1
- package/dist/util/index.js.map +0 -1
- package/dist/util/object/index.js.map +0 -1
- package/dist/util/serializer/index.js.map +0 -1
- package/dist/util/string/index.js.map +0 -1
- package/dist/util/validate/index.js.map +0 -1
|
@@ -1,79 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return i.deserialize(t.value);
|
|
71
|
-
}
|
|
72
|
-
return t;
|
|
73
|
-
});
|
|
74
|
-
}
|
|
1
|
+
//#region src/util/serializer/serializer.util.ts
|
|
2
|
+
var e = {
|
|
3
|
+
Date: {
|
|
4
|
+
is: (e) => e instanceof Date,
|
|
5
|
+
serialize: (e) => ({
|
|
6
|
+
__type: "Date",
|
|
7
|
+
value: e.toISOString()
|
|
8
|
+
}),
|
|
9
|
+
deserialize: (e) => new Date(e)
|
|
10
|
+
},
|
|
11
|
+
Map: {
|
|
12
|
+
is: (e) => e instanceof Map,
|
|
13
|
+
serialize: (e) => ({
|
|
14
|
+
__type: "Map",
|
|
15
|
+
value: [...e.entries()]
|
|
16
|
+
}),
|
|
17
|
+
deserialize: (e) => new Map(e)
|
|
18
|
+
},
|
|
19
|
+
Set: {
|
|
20
|
+
is: (e) => e instanceof Set,
|
|
21
|
+
serialize: (e) => ({
|
|
22
|
+
__type: "Set",
|
|
23
|
+
value: [...e]
|
|
24
|
+
}),
|
|
25
|
+
deserialize: (e) => new Set(e)
|
|
26
|
+
},
|
|
27
|
+
RegExp: {
|
|
28
|
+
is: (e) => e instanceof RegExp,
|
|
29
|
+
serialize: (e) => ({
|
|
30
|
+
__type: "RegExp",
|
|
31
|
+
value: {
|
|
32
|
+
source: e.source,
|
|
33
|
+
flags: e.flags
|
|
34
|
+
}
|
|
35
|
+
}),
|
|
36
|
+
deserialize: (e) => {
|
|
37
|
+
let { source: t, flags: n } = e;
|
|
38
|
+
return new RegExp(t, n);
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
BigInt: {
|
|
42
|
+
is: (e) => typeof e == "bigint",
|
|
43
|
+
serialize: (e) => ({
|
|
44
|
+
__type: "BigInt",
|
|
45
|
+
value: e.toString()
|
|
46
|
+
}),
|
|
47
|
+
deserialize: (e) => BigInt(e)
|
|
48
|
+
}
|
|
49
|
+
}, t = {
|
|
50
|
+
serialize(t) {
|
|
51
|
+
return JSON.stringify(t, function(t, n) {
|
|
52
|
+
let r = this[t];
|
|
53
|
+
if (r instanceof Date) return e.Date.serialize(r);
|
|
54
|
+
for (let t of Object.keys(e)) {
|
|
55
|
+
let r = e[t];
|
|
56
|
+
if (r.is(n)) return r.serialize(n);
|
|
57
|
+
}
|
|
58
|
+
return n;
|
|
59
|
+
});
|
|
60
|
+
},
|
|
61
|
+
deserialize(t) {
|
|
62
|
+
return JSON.parse(t, (t, n) => {
|
|
63
|
+
if (n && typeof n == "object" && "__type" in n && typeof n.__type == "string") {
|
|
64
|
+
let t = e[n.__type];
|
|
65
|
+
if (t) return t.deserialize(n.value);
|
|
66
|
+
}
|
|
67
|
+
return n;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
75
70
|
};
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
//# sourceMappingURL=serializer.util.js.map
|
|
71
|
+
//#endregion
|
|
72
|
+
export { t as serializer };
|
|
73
|
+
|
|
74
|
+
//# sourceMappingURL=serializer.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serializer.util.js","sources":["../../../src/util/serializer/serializer.util.ts"],"sourcesContent":["import type {\n I_Serializer,\n I_SerializerTypeWrapper,\n I_SerializerValueMap,\n T_SerializerKnownTypes,\n} from './serializer.type.js';\n\n/**\n * Interface for handling serialization and deserialization of specific types.\n * Each handler provides methods to check if a value is of the specific type,\n * serialize it to a format that can be JSON stringified, and deserialize it back.\n */\ninterface I_Handler<T extends T_SerializerKnownTypes> {\n /** Checks if a value is of the specific type */\n is: (value: unknown) => value is I_SerializerValueMap[T];\n /** Serializes a value of the specific type to a format that can be JSON stringified */\n serialize: (value: I_SerializerValueMap[T]) => I_SerializerTypeWrapper<T>;\n /** Deserializes a value back to the specific type */\n deserialize: (value: unknown) => I_SerializerValueMap[T];\n}\n\n/**\n * Type handlers for different JavaScript types that cannot be directly serialized to JSON.\n * Each handler provides methods to serialize and deserialize specific types like Date, Map, Set, etc.\n */\nconst typeHandlers: {\n [K in T_SerializerKnownTypes]: I_Handler<K>;\n} = {\n Date: {\n is: (v): v is Date => v instanceof Date,\n serialize: v => ({ __type: 'Date', value: v.toISOString() }),\n deserialize: v => new Date(v as string),\n },\n Map: {\n is: (v): v is Map<unknown, unknown> => v instanceof Map,\n serialize: v => ({ __type: 'Map', value: [...v.entries()] }),\n deserialize: v => new Map(v as [unknown, unknown][]),\n },\n Set: {\n is: (v): v is Set<unknown> => v instanceof Set,\n serialize: v => ({ __type: 'Set', value: [...v] }),\n deserialize: v => new Set(v as unknown[]),\n },\n RegExp: {\n is: (v): v is RegExp => v instanceof RegExp,\n serialize: v => ({\n __type: 'RegExp',\n value: { source: v.source, flags: v.flags },\n }),\n deserialize: (v) => {\n const { source, flags } = v as { source: string; flags: string };\n return new RegExp(source, flags);\n },\n },\n BigInt: {\n is: (v): v is bigint => typeof v === 'bigint',\n serialize: v => ({ __type: 'BigInt', value: v.toString() }),\n deserialize: v => BigInt(v as string),\n },\n};\n\n/**\n * A serializer that can handle complex JavaScript types that cannot be directly JSON stringified.\n * This serializer extends JSON.stringify and JSON.parse to handle types like Date, Map, Set, RegExp, and BigInt.\n *\n * The serializer works by:\n * 1. During serialization: Wrapping special types with type information before JSON stringification\n * 2. During deserialization: Detecting wrapped types and reconstructing them to their original form\n */\nexport const serializer: I_Serializer<unknown> = {\n /**\n * Serializes a value to a JSON string.\n * If the value is of a known type (Date, Map, Set, RegExp, BigInt),\n * it will be serialized using the corresponding handler.\n * Otherwise, it will be serialized as is.\n *\n * @param value - The value to serialize to a JSON string.\n * @returns The serialized JSON string that can be safely stored or transmitted.\n */\n serialize(value) {\n return JSON.stringify(value, function (_key, val) {\n // eslint-disable-next-line ts/no-this-alias\n const context = this;\n const originalValue = context[_key];\n\n if (originalValue instanceof Date) {\n return typeHandlers.Date.serialize(originalValue);\n }\n\n for (const type of Object.keys(typeHandlers) as T_SerializerKnownTypes[]) {\n const handler = typeHandlers[type];\n\n if (handler.is(val)) {\n return (handler as I_Handler<typeof type>).serialize(val);\n }\n }\n\n return val;\n });\n },\n /**\n * Deserializes a JSON string to its original value.\n * If the value is of a known type (Date, Map, Set, RegExp, BigInt),\n * it will be deserialized using the corresponding handler.\n * Otherwise, it will be deserialized as is.\n *\n * @param json - The JSON string to deserialize back to its original form.\n * @returns The deserialized value with all special types reconstructed.\n */\n deserialize(json) {\n return JSON.parse(json, (_key, val) => {\n if (\n val\n && typeof val === 'object'\n && '__type' in val\n && typeof val.__type === 'string'\n ) {\n const type = val.__type as T_SerializerKnownTypes;\n const handler = typeHandlers[type];\n\n if (handler) {\n return handler.deserialize(val.value);\n }\n }\n return val;\n });\n },\n};\n"],"
|
|
1
|
+
{"version":3,"file":"serializer.util.js","names":[],"sources":["../../../src/util/serializer/serializer.util.ts"],"sourcesContent":["import type {\n I_Serializer,\n I_SerializerTypeWrapper,\n I_SerializerValueMap,\n T_SerializerKnownTypes,\n} from './serializer.type.js';\n\n/**\n * Interface for handling serialization and deserialization of specific types.\n * Each handler provides methods to check if a value is of the specific type,\n * serialize it to a format that can be JSON stringified, and deserialize it back.\n */\ninterface I_Handler<T extends T_SerializerKnownTypes> {\n /** Checks if a value is of the specific type */\n is: (value: unknown) => value is I_SerializerValueMap[T];\n /** Serializes a value of the specific type to a format that can be JSON stringified */\n serialize: (value: I_SerializerValueMap[T]) => I_SerializerTypeWrapper<T>;\n /** Deserializes a value back to the specific type */\n deserialize: (value: unknown) => I_SerializerValueMap[T];\n}\n\n/**\n * Type handlers for different JavaScript types that cannot be directly serialized to JSON.\n * Each handler provides methods to serialize and deserialize specific types like Date, Map, Set, etc.\n */\nconst typeHandlers: {\n [K in T_SerializerKnownTypes]: I_Handler<K>;\n} = {\n Date: {\n is: (v): v is Date => v instanceof Date,\n serialize: v => ({ __type: 'Date', value: v.toISOString() }),\n deserialize: v => new Date(v as string),\n },\n Map: {\n is: (v): v is Map<unknown, unknown> => v instanceof Map,\n serialize: v => ({ __type: 'Map', value: [...v.entries()] }),\n deserialize: v => new Map(v as [unknown, unknown][]),\n },\n Set: {\n is: (v): v is Set<unknown> => v instanceof Set,\n serialize: v => ({ __type: 'Set', value: [...v] }),\n deserialize: v => new Set(v as unknown[]),\n },\n RegExp: {\n is: (v): v is RegExp => v instanceof RegExp,\n serialize: v => ({\n __type: 'RegExp',\n value: { source: v.source, flags: v.flags },\n }),\n deserialize: (v) => {\n const { source, flags } = v as { source: string; flags: string };\n return new RegExp(source, flags);\n },\n },\n BigInt: {\n is: (v): v is bigint => typeof v === 'bigint',\n serialize: v => ({ __type: 'BigInt', value: v.toString() }),\n deserialize: v => BigInt(v as string),\n },\n};\n\n/**\n * A serializer that can handle complex JavaScript types that cannot be directly JSON stringified.\n * This serializer extends JSON.stringify and JSON.parse to handle types like Date, Map, Set, RegExp, and BigInt.\n *\n * The serializer works by:\n * 1. During serialization: Wrapping special types with type information before JSON stringification\n * 2. During deserialization: Detecting wrapped types and reconstructing them to their original form\n */\nexport const serializer: I_Serializer<unknown> = {\n /**\n * Serializes a value to a JSON string.\n * If the value is of a known type (Date, Map, Set, RegExp, BigInt),\n * it will be serialized using the corresponding handler.\n * Otherwise, it will be serialized as is.\n *\n * @param value - The value to serialize to a JSON string.\n * @returns The serialized JSON string that can be safely stored or transmitted.\n */\n serialize(value) {\n return JSON.stringify(value, function (_key, val) {\n // eslint-disable-next-line ts/no-this-alias\n const context = this;\n const originalValue = context[_key];\n\n if (originalValue instanceof Date) {\n return typeHandlers.Date.serialize(originalValue);\n }\n\n for (const type of Object.keys(typeHandlers) as T_SerializerKnownTypes[]) {\n const handler = typeHandlers[type];\n\n if (handler.is(val)) {\n return (handler as I_Handler<typeof type>).serialize(val);\n }\n }\n\n return val;\n });\n },\n /**\n * Deserializes a JSON string to its original value.\n * If the value is of a known type (Date, Map, Set, RegExp, BigInt),\n * it will be deserialized using the corresponding handler.\n * Otherwise, it will be deserialized as is.\n *\n * @param json - The JSON string to deserialize back to its original form.\n * @returns The deserialized value with all special types reconstructed.\n */\n deserialize(json) {\n return JSON.parse(json, (_key, val) => {\n if (\n val\n && typeof val === 'object'\n && '__type' in val\n && typeof val.__type === 'string'\n ) {\n const type = val.__type as T_SerializerKnownTypes;\n const handler = typeHandlers[type];\n\n if (handler) {\n return handler.deserialize(val.value);\n }\n }\n return val;\n });\n },\n};\n"],"mappings":";AAyBA,IAAM,IAEF;CACA,MAAM;EACF,KAAK,MAAiB,aAAa;EACnC,YAAW,OAAM;GAAE,QAAQ;GAAQ,OAAO,EAAE,aAAa;GAAE;EAC3D,cAAa,MAAK,IAAI,KAAK,EAAY;EAC1C;CACD,KAAK;EACD,KAAK,MAAkC,aAAa;EACpD,YAAW,OAAM;GAAE,QAAQ;GAAO,OAAO,CAAC,GAAG,EAAE,SAAS,CAAC;GAAE;EAC3D,cAAa,MAAK,IAAI,IAAI,EAA0B;EACvD;CACD,KAAK;EACD,KAAK,MAAyB,aAAa;EAC3C,YAAW,OAAM;GAAE,QAAQ;GAAO,OAAO,CAAC,GAAG,EAAE;GAAE;EACjD,cAAa,MAAK,IAAI,IAAI,EAAe;EAC5C;CACD,QAAQ;EACJ,KAAK,MAAmB,aAAa;EACrC,YAAW,OAAM;GACb,QAAQ;GACR,OAAO;IAAE,QAAQ,EAAE;IAAQ,OAAO,EAAE;IAAO;GAC9C;EACD,cAAc,MAAM;GAChB,IAAM,EAAE,WAAQ,aAAU;AAC1B,UAAO,IAAI,OAAO,GAAQ,EAAM;;EAEvC;CACD,QAAQ;EACJ,KAAK,MAAmB,OAAO,KAAM;EACrC,YAAW,OAAM;GAAE,QAAQ;GAAU,OAAO,EAAE,UAAU;GAAE;EAC1D,cAAa,MAAK,OAAO,EAAY;EACxC;CACJ,EAUY,IAAoC;CAU7C,UAAU,GAAO;AACb,SAAO,KAAK,UAAU,GAAO,SAAU,GAAM,GAAK;GAG9C,IAAM,IADU,KACc;AAE9B,OAAI,aAAyB,KACzB,QAAO,EAAa,KAAK,UAAU,EAAc;AAGrD,QAAK,IAAM,KAAQ,OAAO,KAAK,EAAa,EAA8B;IACtE,IAAM,IAAU,EAAa;AAE7B,QAAI,EAAQ,GAAG,EAAI,CACf,QAAQ,EAAmC,UAAU,EAAI;;AAIjE,UAAO;IACT;;CAWN,YAAY,GAAM;AACd,SAAO,KAAK,MAAM,IAAO,GAAM,MAAQ;AACnC,OACI,KACG,OAAO,KAAQ,YACf,YAAY,KACZ,OAAO,EAAI,UAAW,UAC3B;IAEE,IAAM,IAAU,EADH,EAAI;AAGjB,QAAI,EACA,QAAO,EAAQ,YAAY,EAAI,MAAM;;AAG7C,UAAO;IACT;;CAET"}
|
|
@@ -1,10 +1,2 @@
|
|
|
1
|
-
import { generateRandomPassword as
|
|
2
|
-
export {
|
|
3
|
-
t as generateRandomPassword,
|
|
4
|
-
n as generateRandomString,
|
|
5
|
-
a as generateShortId,
|
|
6
|
-
g as generateSlug,
|
|
7
|
-
o as getFileName,
|
|
8
|
-
d as substringBetween
|
|
9
|
-
};
|
|
10
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
import { generateRandomPassword as e, generateRandomString as t, generateShortId as n, generateSlug as r, getFileName as i, substringBetween as a } from "./string.util.js";
|
|
2
|
+
export { e as generateRandomPassword, t as generateRandomString, n as generateShortId, r as generateSlug, i as getFileName, a as substringBetween };
|
|
@@ -1,71 +1,59 @@
|
|
|
1
|
-
import { removeAccent as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const o = r + n.length, s = e.indexOf(t, o);
|
|
61
|
-
return s === -1 ? "" : e.slice(o, s);
|
|
62
|
-
}
|
|
63
|
-
export {
|
|
64
|
-
A as generateRandomPassword,
|
|
65
|
-
b as generateRandomString,
|
|
66
|
-
_ as generateShortId,
|
|
67
|
-
E as generateSlug,
|
|
68
|
-
N as getFileName,
|
|
69
|
-
S as substringBetween
|
|
70
|
-
};
|
|
71
|
-
//# sourceMappingURL=string.util.js.map
|
|
1
|
+
import { removeAccent as e } from "../common/common.util.js";
|
|
2
|
+
//#region src/util/string/string.util.ts
|
|
3
|
+
var t = /[^a-z0-9\s-]/gi, n = /[\s-]+/g, r = /^-+|-+$/g, i = /-/g, a = /[?#]/;
|
|
4
|
+
function o(i, a) {
|
|
5
|
+
let o = i.trim();
|
|
6
|
+
return o = e(o), a?.lower !== !1 && (o = o.toLowerCase()), o = o.replace(t, " "), o = o.replace(n, "-"), o = o.replace(r, ""), o;
|
|
7
|
+
}
|
|
8
|
+
function s(e, t) {
|
|
9
|
+
let n = (e) => o(e ?? "", t);
|
|
10
|
+
if (typeof e == "object" && e) {
|
|
11
|
+
let t = {};
|
|
12
|
+
for (let [r, i] of Object.entries(e)) t[r] = n(i);
|
|
13
|
+
return t;
|
|
14
|
+
}
|
|
15
|
+
return n(e);
|
|
16
|
+
}
|
|
17
|
+
function c(e, t = 4) {
|
|
18
|
+
let n = 2166136261;
|
|
19
|
+
for (let t = 0; t < e.length; t++) n ^= e.charCodeAt(t), n = Math.imul(n, 16777619);
|
|
20
|
+
let r = (n >>> 0).toString(16).padStart(8, "0");
|
|
21
|
+
return t > 8 ? e.replace(i, "").slice(0, t) : r.slice(0, t);
|
|
22
|
+
}
|
|
23
|
+
function l(e, t) {
|
|
24
|
+
let n = Math.floor(2 ** 32 / t.length) * t.length, r = [], i = 16384;
|
|
25
|
+
for (; r.length < e;) {
|
|
26
|
+
let a = e - r.length, o = new Uint32Array(a > i ? i : a);
|
|
27
|
+
crypto.getRandomValues(o);
|
|
28
|
+
for (let i of o) {
|
|
29
|
+
if (r.length >= e) break;
|
|
30
|
+
i < n && r.push(t[i % t.length]);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return r.join("");
|
|
34
|
+
}
|
|
35
|
+
function u(e = 8) {
|
|
36
|
+
if (!Number.isSafeInteger(e) || e < 0) throw RangeError("length must be a non-negative safe integer");
|
|
37
|
+
return l(e, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+[]{}|;:,.<>?");
|
|
38
|
+
}
|
|
39
|
+
function d(e = 8, t = "abcdefghijklmnopqrstuvwxyz0123456789") {
|
|
40
|
+
if (!Number.isSafeInteger(e) || e < 0) throw RangeError("length must be a non-negative safe integer");
|
|
41
|
+
if (t.length === 0 || t.length > 2 ** 32) throw RangeError("charset.length must be between 1 and 2^32");
|
|
42
|
+
return l(e, t);
|
|
43
|
+
}
|
|
44
|
+
function f(e = "", t = !1) {
|
|
45
|
+
let n = e.split(a)[0] || "", r = n.substring(n.lastIndexOf("/") + 1);
|
|
46
|
+
if (t) return r;
|
|
47
|
+
let i = r.lastIndexOf(".");
|
|
48
|
+
return i > 0 ? r.slice(0, i) : r;
|
|
49
|
+
}
|
|
50
|
+
function p(e, t, n) {
|
|
51
|
+
let r = e.indexOf(t);
|
|
52
|
+
if (r === -1) return "";
|
|
53
|
+
let i = r + t.length, a = e.indexOf(n, i);
|
|
54
|
+
return a === -1 ? "" : e.slice(i, a);
|
|
55
|
+
}
|
|
56
|
+
//#endregion
|
|
57
|
+
export { u as generateRandomPassword, d as generateRandomString, c as generateShortId, s as generateSlug, f as getFileName, p as substringBetween };
|
|
58
|
+
|
|
59
|
+
//# sourceMappingURL=string.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"string.util.js","sources":["../../../src/util/string/string.util.ts"],"sourcesContent":["import type { T_Object } from '#typescript/index.js';\n\nimport type { I_SlugifyOptions } from './string.type.js';\n\nimport { removeAccent } from '../common/common.util.js';\n\nconst RE_NON_ALNUM = /[^a-z0-9\\s-]/gi;\nconst RE_MULTI_SPACE_DASH = /[\\s-]+/g;\nconst RE_LEADING_TRAILING_DASH = /^-+|-+$/g;\nconst RE_HYPHEN = /-/g;\nconst RE_QUERY_FRAGMENT = /[?#]/;\n\n/**\n * Generates a slug from a string.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens.\n *\n * @param input - The string to be slugified.\n * @param options - Options for slugification.\n * @returns The slugified string.\n */\nfunction slugify(input: string, options?: I_SlugifyOptions): string {\n let slug = input.trim();\n\n // 1. Remove accents\n slug = removeAccent(slug);\n\n // 2. To lower case if requested (default true)\n if (options?.lower !== false) {\n slug = slug.toLowerCase();\n }\n\n // 3. Replace invalid characters with space (keeping alphanumeric, hyphens, and spaces)\n slug = slug.replace(RE_NON_ALNUM, ' ');\n\n // 4. Replace multiple spaces or hyphens with a single hyphen\n slug = slug.replace(RE_MULTI_SPACE_DASH, '-');\n\n // 5. Remove leading/trailing hyphens\n slug = slug.replace(RE_LEADING_TRAILING_DASH, '');\n\n return slug;\n}\n\n/**\n * Generates a slug from a string or an object containing strings.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens. This function can handle both single strings\n * and objects with string values.\n *\n * @param input - The string or object to be slugified.\n * @param options - Options for slugification including replacement character, case sensitivity, locale, etc.\n * @returns The slugified string or object with the same structure as the input.\n */\nexport function generateSlug<T = string>(\n input: T,\n options?: I_SlugifyOptions,\n): T {\n const slugifyWithOptions = (value: string) =>\n slugify(value ?? '', options);\n\n if (typeof input === 'object' && input !== null) {\n const result: T_Object = {};\n\n for (const [key, value] of Object.entries(input)) {\n result[key] = slugifyWithOptions(value as string);\n }\n\n return result as T;\n }\n\n return slugifyWithOptions(input as string) as T;\n}\n\n/**\n * Generates a short ID from a UUID.\n * The ID is a substring of the UUID, providing a shorter identifier.\n * Note: This is NOT cryptographically secure and collisions are possible,\n * but suitable for display purposes where uniqueness is handled elsewhere.\n *\n * @param uuid - The UUID to be converted to a short ID.\n * @param length - The desired length of the short ID (default: 4 characters).\n * @returns A short ID string of the specified length derived from the UUID.\n */\nexport function generateShortId(uuid: string, length = 4): string {\n // Simple hash function (FNV-1a variant) to generate a hex string from the UUID\n let hash = 0x811C9DC5;\n for (let i = 0; i < uuid.length; i++) {\n hash ^= uuid.charCodeAt(i);\n hash = Math.imul(hash, 0x01000193);\n }\n // Convert to unsigned 32-bit integer hex string\n const hex = (hash >>> 0).toString(16).padStart(8, '0');\n\n // If we need more than 8 chars, we can just append part of the original UUID (stripped of dashes)\n // or use a different strategy. For short IDs (usually < 8), the hash is fine.\n // If length > 8, we fallback to just slicing the clean UUID.\n if (length > 8) {\n return uuid.replace(RE_HYPHEN, '').slice(0, length);\n }\n\n return hex.slice(0, length);\n}\n\n/**\n * Internal helper that fills `length` characters from `charset` using\n * rejection-sampling over `crypto.getRandomValues` to avoid modulo bias.\n * Random values are requested in bounded chunks to keep allocations small.\n *\n * @param length - Number of characters to generate.\n * @param charset - The pool of characters to draw from.\n * @returns A randomly generated string of the requested length.\n */\nfunction generateRandomFromCharset(length: number, charset: string): string {\n const limit = Math.floor(2 ** 32 / charset.length) * charset.length;\n const result: string[] = [];\n const MAX_UINT32_VALUES_PER_CALL = 16384;\n\n while (result.length < length) {\n const remaining = length - result.length;\n const chunkSize = remaining > MAX_UINT32_VALUES_PER_CALL ? MAX_UINT32_VALUES_PER_CALL : remaining;\n const values = new Uint32Array(chunkSize);\n crypto.getRandomValues(values);\n\n for (const value of values) {\n if (result.length >= length) {\n break;\n }\n if (value < limit) {\n result.push(charset[value % charset.length] as string);\n }\n }\n }\n\n return result.join('');\n}\n\n/**\n * Generates a random password of a given length.\n * The password contains a mix of letters (both cases), numbers, and special characters\n * to ensure complexity and security.\n *\n * @param length - The desired length of the password (default: 8 characters).\n * @returns A randomly generated password string with the specified length.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n */\nexport function generateRandomPassword(length = 8): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+[]{}|;:,.<>?';\n\n return generateRandomFromCharset(length, charset);\n}\n\n/**\n * Generates a random string of a given length using a secure random number generator.\n * This function is a cryptographically secure alternative to Math.random().toString(36).\n *\n * @param length - The desired length of the string (default: 8 characters).\n * @param charset - The characters to use (default: lowercase alphanumeric).\n * @returns A randomly generated string.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n * @throws {RangeError} If `charset` is empty or exceeds 2^32 characters.\n */\nexport function generateRandomString(\n length = 8,\n charset = 'abcdefghijklmnopqrstuvwxyz0123456789',\n): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n if (charset.length === 0 || charset.length > 2 ** 32) {\n throw new RangeError('charset.length must be between 1 and 2^32');\n }\n\n return generateRandomFromCharset(length, charset);\n}\n\n/**\n * Get the file name from a URL.\n * This function extracts the file name from a URL, optionally including or excluding\n * the file extension. It handles URLs with query parameters and fragments.\n *\n * @param url - The URL to extract the file name from (default: empty string).\n * @param getExtension - Whether to include the file extension in the result (default: false).\n * @returns The file name extracted from the URL, with or without the extension.\n */\nexport function getFileName(url = '', getExtension = false): string {\n const withoutQuery = url.split(RE_QUERY_FRAGMENT)[0] || '';\n const fileName = withoutQuery.substring(withoutQuery.lastIndexOf('/') + 1);\n\n if (getExtension) {\n return fileName;\n }\n\n const dotIndex = fileName.lastIndexOf('.');\n\n return dotIndex > 0 ? fileName.slice(0, dotIndex) : fileName;\n}\n\n/**\n * Extracts a substring between two strings.\n * This function finds the first occurrence of the starting string, then finds the first\n * occurrence of the ending string after the starting string, and returns the content between them.\n *\n * @param s - The original string to search within.\n * @param a - The starting string that marks the beginning of the desired substring.\n * @param b - The ending string that marks the end of the desired substring.\n * @returns The substring between the two specified strings, or an empty string if either string is not found.\n */\nexport function substringBetween(s: string, a: string, b: string): string {\n const start = s.indexOf(a);\n\n if (start === -1) {\n return '';\n }\n\n const from = start + a.length;\n const end = s.indexOf(b, from);\n\n if (end === -1) {\n return '';\n }\n\n return s.slice(from, end);\n}\n"],"names":["RE_NON_ALNUM","RE_MULTI_SPACE_DASH","RE_LEADING_TRAILING_DASH","RE_HYPHEN","RE_QUERY_FRAGMENT","slugify","input","options","slug","removeAccent","generateSlug","slugifyWithOptions","value","result","key","generateShortId","uuid","length","hash","i","hex","generateRandomFromCharset","charset","limit","MAX_UINT32_VALUES_PER_CALL","remaining","chunkSize","values","generateRandomPassword","generateRandomString","getFileName","url","getExtension","withoutQuery","fileName","dotIndex","substringBetween","s","a","b","start","from","end"],"mappings":";AAMA,MAAMA,IAAe,kBACfC,IAAsB,WACtBC,IAA2B,YAC3BC,IAAY,MACZC,IAAoB;AAW1B,SAASC,EAAQC,GAAeC,GAAoC;AAChE,MAAIC,IAAOF,EAAM,KAAA;AAGjB,SAAAE,IAAOC,EAAaD,CAAI,GAGpBD,GAAS,UAAU,OACnBC,IAAOA,EAAK,YAAA,IAIhBA,IAAOA,EAAK,QAAQR,GAAc,GAAG,GAGrCQ,IAAOA,EAAK,QAAQP,GAAqB,GAAG,GAG5CO,IAAOA,EAAK,QAAQN,GAA0B,EAAE,GAEzCM;AACX;AAYO,SAASE,EACZJ,GACAC,GACC;AACD,QAAMI,IAAqB,CAACC,MACxBP,EAAQO,KAAS,IAAIL,CAAO;AAEhC,MAAI,OAAOD,KAAU,YAAYA,MAAU,MAAM;AAC7C,UAAMO,IAAmB,CAAA;AAEzB,eAAW,CAACC,GAAKF,CAAK,KAAK,OAAO,QAAQN,CAAK;AAC3C,MAAAO,EAAOC,CAAG,IAAIH,EAAmBC,CAAe;AAGpD,WAAOC;AAAA,EACX;AAEA,SAAOF,EAAmBL,CAAe;AAC7C;AAYO,SAASS,EAAgBC,GAAcC,IAAS,GAAW;AAE9D,MAAIC,IAAO;AACX,WAASC,IAAI,GAAGA,IAAIH,EAAK,QAAQG;AAC7B,IAAAD,KAAQF,EAAK,WAAWG,CAAC,GACzBD,IAAO,KAAK,KAAKA,GAAM,QAAU;AAGrC,QAAME,KAAOF,MAAS,GAAG,SAAS,EAAE,EAAE,SAAS,GAAG,GAAG;AAKrD,SAAID,IAAS,IACFD,EAAK,QAAQb,GAAW,EAAE,EAAE,MAAM,GAAGc,CAAM,IAG/CG,EAAI,MAAM,GAAGH,CAAM;AAC9B;AAWA,SAASI,EAA0BJ,GAAgBK,GAAyB;AACxE,QAAMC,IAAQ,KAAK,MAAM,aAAUD,EAAQ,MAAM,IAAIA,EAAQ,QACvDT,IAAmB,CAAA,GACnBW,IAA6B;AAEnC,SAAOX,EAAO,SAASI,KAAQ;AAC3B,UAAMQ,IAAYR,IAASJ,EAAO,QAC5Ba,IAAYD,IAAYD,IAA6BA,IAA6BC,GAClFE,IAAS,IAAI,YAAYD,CAAS;AACxC,WAAO,gBAAgBC,CAAM;AAE7B,eAAWf,KAASe,GAAQ;AACxB,UAAId,EAAO,UAAUI;AACjB;AAEJ,MAAIL,IAAQW,KACRV,EAAO,KAAKS,EAAQV,IAAQU,EAAQ,MAAM,CAAW;AAAA,IAE7D;AAAA,EACJ;AAEA,SAAOT,EAAO,KAAK,EAAE;AACzB;AAWO,SAASe,EAAuBX,IAAS,GAAW;AACvD,MAAI,CAAC,OAAO,cAAcA,CAAM,KAAKA,IAAS;AAC1C,UAAM,IAAI,WAAW,4CAA4C;AAKrE,SAAOI,EAA0BJ,GAFjB,wFAEgC;AACpD;AAYO,SAASY,EACZZ,IAAS,GACTK,IAAU,wCACJ;AACN,MAAI,CAAC,OAAO,cAAcL,CAAM,KAAKA,IAAS;AAC1C,UAAM,IAAI,WAAW,4CAA4C;AAGrE,MAAIK,EAAQ,WAAW,KAAKA,EAAQ,SAAS,KAAK;AAC9C,UAAM,IAAI,WAAW,2CAA2C;AAGpE,SAAOD,EAA0BJ,GAAQK,CAAO;AACpD;AAWO,SAASQ,EAAYC,IAAM,IAAIC,IAAe,IAAe;AAChE,QAAMC,IAAeF,EAAI,MAAM3B,CAAiB,EAAE,CAAC,KAAK,IAClD8B,IAAWD,EAAa,UAAUA,EAAa,YAAY,GAAG,IAAI,CAAC;AAEzE,MAAID;AACA,WAAOE;AAGX,QAAMC,IAAWD,EAAS,YAAY,GAAG;AAEzC,SAAOC,IAAW,IAAID,EAAS,MAAM,GAAGC,CAAQ,IAAID;AACxD;AAYO,SAASE,EAAiBC,GAAWC,GAAWC,GAAmB;AACtE,QAAMC,IAAQH,EAAE,QAAQC,CAAC;AAEzB,MAAIE,MAAU;AACV,WAAO;AAGX,QAAMC,IAAOD,IAAQF,EAAE,QACjBI,IAAML,EAAE,QAAQE,GAAGE,CAAI;AAE7B,SAAIC,MAAQ,KACD,KAGJL,EAAE,MAAMI,GAAMC,CAAG;AAC5B;"}
|
|
1
|
+
{"version":3,"file":"string.util.js","names":[],"sources":["../../../src/util/string/string.util.ts"],"sourcesContent":["import type { T_Object } from '#typescript/index.js';\n\nimport type { I_SlugifyOptions } from './string.type.js';\n\nimport { removeAccent } from '../common/common.util.js';\n\nconst RE_NON_ALNUM = /[^a-z0-9\\s-]/gi;\nconst RE_MULTI_SPACE_DASH = /[\\s-]+/g;\nconst RE_LEADING_TRAILING_DASH = /^-+|-+$/g;\nconst RE_HYPHEN = /-/g;\nconst RE_QUERY_FRAGMENT = /[?#]/;\n\n/**\n * Generates a slug from a string.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens.\n *\n * @param input - The string to be slugified.\n * @param options - Options for slugification.\n * @returns The slugified string.\n */\nfunction slugify(input: string, options?: I_SlugifyOptions): string {\n let slug = input.trim();\n\n // 1. Remove accents\n slug = removeAccent(slug);\n\n // 2. To lower case if requested (default true)\n if (options?.lower !== false) {\n slug = slug.toLowerCase();\n }\n\n // 3. Replace invalid characters with space (keeping alphanumeric, hyphens, and spaces)\n slug = slug.replace(RE_NON_ALNUM, ' ');\n\n // 4. Replace multiple spaces or hyphens with a single hyphen\n slug = slug.replace(RE_MULTI_SPACE_DASH, '-');\n\n // 5. Remove leading/trailing hyphens\n slug = slug.replace(RE_LEADING_TRAILING_DASH, '');\n\n return slug;\n}\n\n/**\n * Generates a slug from a string or an object containing strings.\n * The slug is a URL-friendly version of the string, removing special characters\n * and converting spaces to hyphens. This function can handle both single strings\n * and objects with string values.\n *\n * @param input - The string or object to be slugified.\n * @param options - Options for slugification including replacement character, case sensitivity, locale, etc.\n * @returns The slugified string or object with the same structure as the input.\n */\nexport function generateSlug<T = string>(\n input: T,\n options?: I_SlugifyOptions,\n): T {\n const slugifyWithOptions = (value: string) =>\n slugify(value ?? '', options);\n\n if (typeof input === 'object' && input !== null) {\n const result: T_Object = {};\n\n for (const [key, value] of Object.entries(input)) {\n result[key] = slugifyWithOptions(value as string);\n }\n\n return result as T;\n }\n\n return slugifyWithOptions(input as string) as T;\n}\n\n/**\n * Generates a short ID from a UUID.\n * The ID is a substring of the UUID, providing a shorter identifier.\n * Note: This is NOT cryptographically secure and collisions are possible,\n * but suitable for display purposes where uniqueness is handled elsewhere.\n *\n * @param uuid - The UUID to be converted to a short ID.\n * @param length - The desired length of the short ID (default: 4 characters).\n * @returns A short ID string of the specified length derived from the UUID.\n */\nexport function generateShortId(uuid: string, length = 4): string {\n // Simple hash function (FNV-1a variant) to generate a hex string from the UUID\n let hash = 0x811C9DC5;\n for (let i = 0; i < uuid.length; i++) {\n hash ^= uuid.charCodeAt(i);\n hash = Math.imul(hash, 0x01000193);\n }\n // Convert to unsigned 32-bit integer hex string\n const hex = (hash >>> 0).toString(16).padStart(8, '0');\n\n // If we need more than 8 chars, we can just append part of the original UUID (stripped of dashes)\n // or use a different strategy. For short IDs (usually < 8), the hash is fine.\n // If length > 8, we fallback to just slicing the clean UUID.\n if (length > 8) {\n return uuid.replace(RE_HYPHEN, '').slice(0, length);\n }\n\n return hex.slice(0, length);\n}\n\n/**\n * Internal helper that fills `length` characters from `charset` using\n * rejection-sampling over `crypto.getRandomValues` to avoid modulo bias.\n * Random values are requested in bounded chunks to keep allocations small.\n *\n * @param length - Number of characters to generate.\n * @param charset - The pool of characters to draw from.\n * @returns A randomly generated string of the requested length.\n */\nfunction generateRandomFromCharset(length: number, charset: string): string {\n const limit = Math.floor(2 ** 32 / charset.length) * charset.length;\n const result: string[] = [];\n const MAX_UINT32_VALUES_PER_CALL = 16384;\n\n while (result.length < length) {\n const remaining = length - result.length;\n const chunkSize = remaining > MAX_UINT32_VALUES_PER_CALL ? MAX_UINT32_VALUES_PER_CALL : remaining;\n const values = new Uint32Array(chunkSize);\n crypto.getRandomValues(values);\n\n for (const value of values) {\n if (result.length >= length) {\n break;\n }\n if (value < limit) {\n result.push(charset[value % charset.length] as string);\n }\n }\n }\n\n return result.join('');\n}\n\n/**\n * Generates a random password of a given length.\n * The password contains a mix of letters (both cases), numbers, and special characters\n * to ensure complexity and security.\n *\n * @param length - The desired length of the password (default: 8 characters).\n * @returns A randomly generated password string with the specified length.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n */\nexport function generateRandomPassword(length = 8): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+[]{}|;:,.<>?';\n\n return generateRandomFromCharset(length, charset);\n}\n\n/**\n * Generates a random string of a given length using a secure random number generator.\n * This function is a cryptographically secure alternative to Math.random().toString(36).\n *\n * @param length - The desired length of the string (default: 8 characters).\n * @param charset - The characters to use (default: lowercase alphanumeric).\n * @returns A randomly generated string.\n * @throws {RangeError} If `length` is not a non-negative safe integer.\n * @throws {RangeError} If `charset` is empty or exceeds 2^32 characters.\n */\nexport function generateRandomString(\n length = 8,\n charset = 'abcdefghijklmnopqrstuvwxyz0123456789',\n): string {\n if (!Number.isSafeInteger(length) || length < 0) {\n throw new RangeError('length must be a non-negative safe integer');\n }\n\n if (charset.length === 0 || charset.length > 2 ** 32) {\n throw new RangeError('charset.length must be between 1 and 2^32');\n }\n\n return generateRandomFromCharset(length, charset);\n}\n\n/**\n * Get the file name from a URL.\n * This function extracts the file name from a URL, optionally including or excluding\n * the file extension. It handles URLs with query parameters and fragments.\n *\n * @param url - The URL to extract the file name from (default: empty string).\n * @param getExtension - Whether to include the file extension in the result (default: false).\n * @returns The file name extracted from the URL, with or without the extension.\n */\nexport function getFileName(url = '', getExtension = false): string {\n const withoutQuery = url.split(RE_QUERY_FRAGMENT)[0] || '';\n const fileName = withoutQuery.substring(withoutQuery.lastIndexOf('/') + 1);\n\n if (getExtension) {\n return fileName;\n }\n\n const dotIndex = fileName.lastIndexOf('.');\n\n return dotIndex > 0 ? fileName.slice(0, dotIndex) : fileName;\n}\n\n/**\n * Extracts a substring between two strings.\n * This function finds the first occurrence of the starting string, then finds the first\n * occurrence of the ending string after the starting string, and returns the content between them.\n *\n * @param s - The original string to search within.\n * @param a - The starting string that marks the beginning of the desired substring.\n * @param b - The ending string that marks the end of the desired substring.\n * @returns The substring between the two specified strings, or an empty string if either string is not found.\n */\nexport function substringBetween(s: string, a: string, b: string): string {\n const start = s.indexOf(a);\n\n if (start === -1) {\n return '';\n }\n\n const from = start + a.length;\n const end = s.indexOf(b, from);\n\n if (end === -1) {\n return '';\n }\n\n return s.slice(from, end);\n}\n"],"mappings":";;AAMA,IAAM,IAAe,kBACf,IAAsB,WACtB,IAA2B,YAC3B,IAAY,MACZ,IAAoB;AAW1B,SAAS,EAAQ,GAAe,GAAoC;CAChE,IAAI,IAAO,EAAM,MAAM;AAmBvB,QAhBA,IAAO,EAAa,EAAK,EAGrB,GAAS,UAAU,OACnB,IAAO,EAAK,aAAa,GAI7B,IAAO,EAAK,QAAQ,GAAc,IAAI,EAGtC,IAAO,EAAK,QAAQ,GAAqB,IAAI,EAG7C,IAAO,EAAK,QAAQ,GAA0B,GAAG,EAE1C;;AAaX,SAAgB,EACZ,GACA,GACC;CACD,IAAM,KAAsB,MACxB,EAAQ,KAAS,IAAI,EAAQ;AAEjC,KAAI,OAAO,KAAU,YAAY,GAAgB;EAC7C,IAAM,IAAmB,EAAE;AAE3B,OAAK,IAAM,CAAC,GAAK,MAAU,OAAO,QAAQ,EAAM,CAC5C,GAAO,KAAO,EAAmB,EAAgB;AAGrD,SAAO;;AAGX,QAAO,EAAmB,EAAgB;;AAa9C,SAAgB,EAAgB,GAAc,IAAS,GAAW;CAE9D,IAAI,IAAO;AACX,MAAK,IAAI,IAAI,GAAG,IAAI,EAAK,QAAQ,IAE7B,CADA,KAAQ,EAAK,WAAW,EAAE,EAC1B,IAAO,KAAK,KAAK,GAAM,SAAW;CAGtC,IAAM,KAAO,MAAS,GAAG,SAAS,GAAG,CAAC,SAAS,GAAG,IAAI;AAStD,QAJI,IAAS,IACF,EAAK,QAAQ,GAAW,GAAG,CAAC,MAAM,GAAG,EAAO,GAGhD,EAAI,MAAM,GAAG,EAAO;;AAY/B,SAAS,EAA0B,GAAgB,GAAyB;CACxE,IAAM,IAAQ,KAAK,MAAM,KAAK,KAAK,EAAQ,OAAO,GAAG,EAAQ,QACvD,IAAmB,EAAE,EACrB,IAA6B;AAEnC,QAAO,EAAO,SAAS,IAAQ;EAC3B,IAAM,IAAY,IAAS,EAAO,QAE5B,IAAS,IAAI,YADD,IAAY,IAA6B,IAA6B,EAC/C;AACzC,SAAO,gBAAgB,EAAO;AAE9B,OAAK,IAAM,KAAS,GAAQ;AACxB,OAAI,EAAO,UAAU,EACjB;AAEJ,GAAI,IAAQ,KACR,EAAO,KAAK,EAAQ,IAAQ,EAAQ,QAAkB;;;AAKlE,QAAO,EAAO,KAAK,GAAG;;AAY1B,SAAgB,EAAuB,IAAS,GAAW;AACvD,KAAI,CAAC,OAAO,cAAc,EAAO,IAAI,IAAS,EAC1C,OAAU,WAAW,6CAA6C;AAKtE,QAAO,EAA0B,GAFjB,yFAEiC;;AAarD,SAAgB,EACZ,IAAS,GACT,IAAU,wCACJ;AACN,KAAI,CAAC,OAAO,cAAc,EAAO,IAAI,IAAS,EAC1C,OAAU,WAAW,6CAA6C;AAGtE,KAAI,EAAQ,WAAW,KAAK,EAAQ,SAAS,KAAK,GAC9C,OAAU,WAAW,4CAA4C;AAGrE,QAAO,EAA0B,GAAQ,EAAQ;;AAYrD,SAAgB,EAAY,IAAM,IAAI,IAAe,IAAe;CAChE,IAAM,IAAe,EAAI,MAAM,EAAkB,CAAC,MAAM,IAClD,IAAW,EAAa,UAAU,EAAa,YAAY,IAAI,GAAG,EAAE;AAE1E,KAAI,EACA,QAAO;CAGX,IAAM,IAAW,EAAS,YAAY,IAAI;AAE1C,QAAO,IAAW,IAAI,EAAS,MAAM,GAAG,EAAS,GAAG;;AAaxD,SAAgB,EAAiB,GAAW,GAAW,GAAmB;CACtE,IAAM,IAAQ,EAAE,QAAQ,EAAE;AAE1B,KAAI,MAAU,GACV,QAAO;CAGX,IAAM,IAAO,IAAQ,EAAE,QACjB,IAAM,EAAE,QAAQ,GAAG,EAAK;AAM9B,QAJI,MAAQ,KACD,KAGJ,EAAE,MAAM,GAAM,EAAI"}
|
|
@@ -1,40 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* - For ArrayBuffer views, it checks if the byte length is 0.
|
|
11
|
-
* - For Dates, it returns false.
|
|
12
|
-
* - For null and undefined, it returns true.
|
|
13
|
-
* - For all other types, it returns false.
|
|
14
|
-
*
|
|
15
|
-
* @param value - The value to check for emptiness.
|
|
16
|
-
* @returns True if the value is empty, false otherwise.
|
|
17
|
-
*/
|
|
18
|
-
isEmpty(d) {
|
|
19
|
-
return d == null ? !0 : typeof d == "string" ? d.trim().length === 0 : Array.isArray(d) ? d.length === 0 : typeof d == "object" ? d instanceof Date ? !1 : d instanceof Map || d instanceof Set ? d.size === 0 : d instanceof WeakMap || d instanceof WeakSet ? !0 : ArrayBuffer.isView(d) ? d.byteLength === 0 : Object.keys(d).length === 0 : !1;
|
|
20
|
-
},
|
|
21
|
-
/**
|
|
22
|
-
* Checks if a string is a valid IP address (IPv4 or IPv6).
|
|
23
|
-
* This function validates IP addresses according to standard formats:
|
|
24
|
-
* - IPv4: Four octets separated by dots, each between 0–255.
|
|
25
|
-
* - IPv6: Eight groups of four hex digits, possibly compressed with `::`.
|
|
26
|
-
*
|
|
27
|
-
* @param ip - The IP address string to validate.
|
|
28
|
-
* @returns True if the IP is valid IPv4 or IPv6, false otherwise.
|
|
29
|
-
*/
|
|
30
|
-
isValidIP(d) {
|
|
31
|
-
const f = d.split(".");
|
|
32
|
-
return f.length === 4 && f.every(
|
|
33
|
-
(t) => r.test(t) && Number(t) >= 0 && Number(t) <= 255
|
|
34
|
-
) ? !0 : n.test(d);
|
|
35
|
-
}
|
|
1
|
+
//#region src/util/validate/validate.util.ts
|
|
2
|
+
var e = /^\d+$/, t = /^(?:(?:[a-f\d]{1,4}:){7}(?:[a-f\d]{1,4}|:)|(?:[a-f\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-f\d]{1,4}|:)|(?:[a-f\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,2}|:)|(?:[a-f\d]{1,4}:){4}(?:(?::[a-f\d]{1,4})?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,3}|:)|(?:[a-f\d]{1,4}:){3}(?:(?::[a-f\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,4}|:)|(?:[a-f\d]{1,4}:){2}(?:(?::[a-f\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,5}|:)|[a-f\d]{1,4}:(?:(?::[a-f\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,6}|:)|:(?:(?::[a-f\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-f\d]{1,4}){1,7}|:))(?:%[0-9a-z]+)?$/i, n = {
|
|
3
|
+
isEmpty(e) {
|
|
4
|
+
return e == null ? !0 : typeof e == "string" ? e.trim().length === 0 : Array.isArray(e) ? e.length === 0 : typeof e == "object" ? e instanceof Date ? !1 : e instanceof Map || e instanceof Set ? e.size === 0 : e instanceof WeakMap || e instanceof WeakSet ? !0 : ArrayBuffer.isView(e) ? e.byteLength === 0 : Object.keys(e).length === 0 : !1;
|
|
5
|
+
},
|
|
6
|
+
isValidIP(n) {
|
|
7
|
+
let r = n.split(".");
|
|
8
|
+
return r.length === 4 && r.every((t) => e.test(t) && Number(t) >= 0 && Number(t) <= 255) ? !0 : t.test(n);
|
|
9
|
+
}
|
|
36
10
|
};
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
//# sourceMappingURL=validate.util.js.map
|
|
11
|
+
//#endregion
|
|
12
|
+
export { n as validate };
|
|
13
|
+
|
|
14
|
+
//# sourceMappingURL=validate.util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.util.js","sources":["../../../src/util/validate/validate.util.ts"],"sourcesContent":["/**\n * A collection of validation utility functions for common data validation tasks.\n * This object provides methods to validate various data types and formats.\n */\nconst RE_DIGITS_ONLY = /^\\d+$/;\nconst RE_IPV6 = /^(?:(?:[a-f\\d]{1,4}:){7}(?:[a-f\\d]{1,4}|:)|(?:[a-f\\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|:[a-f\\d]{1,4}|:)|(?:[a-f\\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,2}|:)|(?:[a-f\\d]{1,4}:){4}(?:(?::[a-f\\d]{1,4})?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,3}|:)|(?:[a-f\\d]{1,4}:){3}(?:(?::[a-f\\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,4}|:)|(?:[a-f\\d]{1,4}:){2}(?:(?::[a-f\\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,5}|:)|[a-f\\d]{1,4}:(?:(?::[a-f\\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,6}|:)|:(?:(?::[a-f\\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,7}|:))(?:%[0-9a-z]+)?$/i;\n\nexport const validate = {\n /**\n * Checks if a value is empty.\n * This function provides comprehensive empty checking for different data types:\n * - For strings, it checks if the string is empty or contains only whitespace.\n * - For arrays, it checks if the array has no elements.\n * - For objects, it checks if the object has no own properties.\n * - For Maps and Sets, it checks if they are empty.\n * - For WeakMaps and WeakSets, it returns true (as they are not enumerable).\n * - For ArrayBuffer views, it checks if the byte length is 0.\n * - For Dates, it returns false.\n * - For null and undefined, it returns true.\n * - For all other types, it returns false.\n *\n * @param value - The value to check for emptiness.\n * @returns True if the value is empty, false otherwise.\n */\n isEmpty(value: unknown): boolean {\n if (value === null || value === undefined) {\n return true;\n }\n\n if (typeof value === 'string') {\n return value.trim().length === 0;\n }\n\n if (Array.isArray(value)) {\n return value.length === 0;\n }\n\n if (typeof value === 'object') {\n if (value instanceof Date) {\n return false;\n }\n\n if (value instanceof Map || value instanceof Set) {\n return value.size === 0;\n }\n\n if (value instanceof WeakMap || value instanceof WeakSet) {\n return true;\n }\n\n if (ArrayBuffer.isView(value)) {\n return value.byteLength === 0;\n }\n\n return Object.keys(value).length === 0;\n }\n\n return false;\n },\n /**\n * Checks if a string is a valid IP address (IPv4 or IPv6).\n * This function validates IP addresses according to standard formats:\n * - IPv4: Four octets separated by dots, each between 0–255.\n * - IPv6: Eight groups of four hex digits, possibly compressed with `::`.\n *\n * @param ip - The IP address string to validate.\n * @returns True if the IP is valid IPv4 or IPv6, false otherwise.\n */\n isValidIP(ip: string): boolean {\n const ipv4Parts = ip.split('.');\n\n if (ipv4Parts.length === 4 && ipv4Parts.every(octet =>\n RE_DIGITS_ONLY.test(octet)\n && Number(octet) >= 0\n && Number(octet) <= 255,\n )) {\n return true;\n }\n\n return RE_IPV6.test(ip);\n },\n};\n"],"
|
|
1
|
+
{"version":3,"file":"validate.util.js","names":[],"sources":["../../../src/util/validate/validate.util.ts"],"sourcesContent":["/**\n * A collection of validation utility functions for common data validation tasks.\n * This object provides methods to validate various data types and formats.\n */\nconst RE_DIGITS_ONLY = /^\\d+$/;\nconst RE_IPV6 = /^(?:(?:[a-f\\d]{1,4}:){7}(?:[a-f\\d]{1,4}|:)|(?:[a-f\\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|:[a-f\\d]{1,4}|:)|(?:[a-f\\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,2}|:)|(?:[a-f\\d]{1,4}:){4}(?:(?::[a-f\\d]{1,4})?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,3}|:)|(?:[a-f\\d]{1,4}:){3}(?:(?::[a-f\\d]{1,4}){0,2}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,4}|:)|(?:[a-f\\d]{1,4}:){2}(?:(?::[a-f\\d]{1,4}){0,3}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,5}|:)|[a-f\\d]{1,4}:(?:(?::[a-f\\d]{1,4}){0,4}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,6}|:)|:(?:(?::[a-f\\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)(?:\\.(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)){3}|(?::[a-f\\d]{1,4}){1,7}|:))(?:%[0-9a-z]+)?$/i;\n\nexport const validate = {\n /**\n * Checks if a value is empty.\n * This function provides comprehensive empty checking for different data types:\n * - For strings, it checks if the string is empty or contains only whitespace.\n * - For arrays, it checks if the array has no elements.\n * - For objects, it checks if the object has no own properties.\n * - For Maps and Sets, it checks if they are empty.\n * - For WeakMaps and WeakSets, it returns true (as they are not enumerable).\n * - For ArrayBuffer views, it checks if the byte length is 0.\n * - For Dates, it returns false.\n * - For null and undefined, it returns true.\n * - For all other types, it returns false.\n *\n * @param value - The value to check for emptiness.\n * @returns True if the value is empty, false otherwise.\n */\n isEmpty(value: unknown): boolean {\n if (value === null || value === undefined) {\n return true;\n }\n\n if (typeof value === 'string') {\n return value.trim().length === 0;\n }\n\n if (Array.isArray(value)) {\n return value.length === 0;\n }\n\n if (typeof value === 'object') {\n if (value instanceof Date) {\n return false;\n }\n\n if (value instanceof Map || value instanceof Set) {\n return value.size === 0;\n }\n\n if (value instanceof WeakMap || value instanceof WeakSet) {\n return true;\n }\n\n if (ArrayBuffer.isView(value)) {\n return value.byteLength === 0;\n }\n\n return Object.keys(value).length === 0;\n }\n\n return false;\n },\n /**\n * Checks if a string is a valid IP address (IPv4 or IPv6).\n * This function validates IP addresses according to standard formats:\n * - IPv4: Four octets separated by dots, each between 0–255.\n * - IPv6: Eight groups of four hex digits, possibly compressed with `::`.\n *\n * @param ip - The IP address string to validate.\n * @returns True if the IP is valid IPv4 or IPv6, false otherwise.\n */\n isValidIP(ip: string): boolean {\n const ipv4Parts = ip.split('.');\n\n if (ipv4Parts.length === 4 && ipv4Parts.every(octet =>\n RE_DIGITS_ONLY.test(octet)\n && Number(octet) >= 0\n && Number(octet) <= 255,\n )) {\n return true;\n }\n\n return RE_IPV6.test(ip);\n },\n};\n"],"mappings":";AAIA,IAAM,IAAiB,SACjB,IAAU,gjCAEH,IAAW;CAiBpB,QAAQ,GAAyB;AAiC7B,SAhCI,KAAU,OACH,KAGP,OAAO,KAAU,WACV,EAAM,MAAM,CAAC,WAAW,IAG/B,MAAM,QAAQ,EAAM,GACb,EAAM,WAAW,IAGxB,OAAO,KAAU,WACb,aAAiB,OACV,KAGP,aAAiB,OAAO,aAAiB,MAClC,EAAM,SAAS,IAGtB,aAAiB,WAAW,aAAiB,UACtC,KAGP,YAAY,OAAO,EAAM,GAClB,EAAM,eAAe,IAGzB,OAAO,KAAK,EAAM,CAAC,WAAW,IAGlC;;CAWX,UAAU,GAAqB;EAC3B,IAAM,IAAY,EAAG,MAAM,IAAI;AAU/B,SARI,EAAU,WAAW,KAAK,EAAU,OAAM,MAC1C,EAAe,KAAK,EAAM,IACvB,OAAO,EAAM,IAAI,KACjB,OAAO,EAAM,IAAI,IACvB,GACU,KAGJ,EAAQ,KAAK,EAAG;;CAE9B"}
|