@catbee/utils 1.1.0 → 2.0.0-next.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/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
package/string/index.mjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
var __defProp = Object.defineProperty;
|
|
26
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
27
|
+
|
|
28
|
+
// src/string/string.utils.ts
|
|
29
|
+
function capitalize(str) {
|
|
30
|
+
if (typeof str !== "string") throw new TypeError("Expected a string");
|
|
31
|
+
return str.length === 0 ? "" : str.charAt(0).toUpperCase() + str.slice(1);
|
|
32
|
+
}
|
|
33
|
+
__name(capitalize, "capitalize");
|
|
34
|
+
function toKebabCase(str) {
|
|
35
|
+
if (typeof str !== "string") throw new TypeError("Expected a string");
|
|
36
|
+
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/\s+/g, "-").replace(/_+/g, "-").toLowerCase();
|
|
37
|
+
}
|
|
38
|
+
__name(toKebabCase, "toKebabCase");
|
|
39
|
+
function toCamelCase(str) {
|
|
40
|
+
if (typeof str !== "string") throw new TypeError("Expected a string");
|
|
41
|
+
return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
|
|
42
|
+
}
|
|
43
|
+
__name(toCamelCase, "toCamelCase");
|
|
44
|
+
function slugify(str) {
|
|
45
|
+
return str.toLowerCase().replace(/[^\w\s-]/g, "").replace(/\s+/g, "-").replace(/-+/g, "-").replace(/^-+|-+$/g, "");
|
|
46
|
+
}
|
|
47
|
+
__name(slugify, "slugify");
|
|
48
|
+
function truncate(str, len) {
|
|
49
|
+
return str.length > len ? str.slice(0, len) + "..." : str;
|
|
50
|
+
}
|
|
51
|
+
__name(truncate, "truncate");
|
|
52
|
+
function toPascalCase(str) {
|
|
53
|
+
return capitalize(toCamelCase(str));
|
|
54
|
+
}
|
|
55
|
+
__name(toPascalCase, "toPascalCase");
|
|
56
|
+
function toSnakeCase(str) {
|
|
57
|
+
return str.trim().replace(/([a-z])([A-Z])/g, "$1_$2").replace(/\s+/g, "_").replace(/-+/g, "_").toLowerCase();
|
|
58
|
+
}
|
|
59
|
+
__name(toSnakeCase, "toSnakeCase");
|
|
60
|
+
function mask(str, visibleStart = 0, visibleEnd = 0, maskChar = "*") {
|
|
61
|
+
if (!str || str.length === 0) return "";
|
|
62
|
+
if (visibleStart >= str.length) return str;
|
|
63
|
+
const start = str.slice(0, visibleStart);
|
|
64
|
+
const end = visibleEnd > 0 ? str.slice(-visibleEnd) : "";
|
|
65
|
+
const masked = maskChar.repeat(Math.max(0, str.length - visibleStart - visibleEnd));
|
|
66
|
+
return start + masked + end;
|
|
67
|
+
}
|
|
68
|
+
__name(mask, "mask");
|
|
69
|
+
function stripHtml(str) {
|
|
70
|
+
return str.replace(/<[^>]*>/g, "");
|
|
71
|
+
}
|
|
72
|
+
__name(stripHtml, "stripHtml");
|
|
73
|
+
function equalsIgnoreCase(a, b) {
|
|
74
|
+
return a.toLowerCase() === b.toLowerCase();
|
|
75
|
+
}
|
|
76
|
+
__name(equalsIgnoreCase, "equalsIgnoreCase");
|
|
77
|
+
function reverse(str) {
|
|
78
|
+
return str.split("").reverse().join("");
|
|
79
|
+
}
|
|
80
|
+
__name(reverse, "reverse");
|
|
81
|
+
function countOccurrences(str, substring, caseSensitive = true) {
|
|
82
|
+
if (!caseSensitive) {
|
|
83
|
+
str = str.toLowerCase();
|
|
84
|
+
substring = substring.toLowerCase();
|
|
85
|
+
}
|
|
86
|
+
return str.split(substring).length - 1;
|
|
87
|
+
}
|
|
88
|
+
__name(countOccurrences, "countOccurrences");
|
|
89
|
+
function toTitleCase(str) {
|
|
90
|
+
if (typeof str !== "string") throw new TypeError("Expected a string");
|
|
91
|
+
return str.split(/(\s+)/).map((part) => part.trim().length === 0 ? part : part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
|
|
92
|
+
}
|
|
93
|
+
__name(toTitleCase, "toTitleCase");
|
|
94
|
+
|
|
95
|
+
export { capitalize, countOccurrences, equalsIgnoreCase, mask, reverse, slugify, stripHtml, toCamelCase, toKebabCase, toPascalCase, toSnakeCase, toTitleCase, truncate };
|
package/type/index.cjs
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
+
|
|
30
|
+
// src/type/type.utils.ts
|
|
31
|
+
function isPrimitiveType(value, type) {
|
|
32
|
+
if (type === "array") {
|
|
33
|
+
return Array.isArray(value);
|
|
34
|
+
}
|
|
35
|
+
if (type === "null") {
|
|
36
|
+
return value === null;
|
|
37
|
+
}
|
|
38
|
+
if (type === "undefined") {
|
|
39
|
+
return value === void 0;
|
|
40
|
+
}
|
|
41
|
+
if (type === "object") {
|
|
42
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
43
|
+
}
|
|
44
|
+
return typeof value === type;
|
|
45
|
+
}
|
|
46
|
+
__name(isPrimitiveType, "isPrimitiveType");
|
|
47
|
+
function getTypeOf(value) {
|
|
48
|
+
if (value === null) return "null";
|
|
49
|
+
if (value === void 0) return "undefined";
|
|
50
|
+
if (Array.isArray(value)) return "array";
|
|
51
|
+
return typeof value;
|
|
52
|
+
}
|
|
53
|
+
__name(getTypeOf, "getTypeOf");
|
|
54
|
+
function isArrayOf(value, itemTypeGuard) {
|
|
55
|
+
if (!Array.isArray(value)) return false;
|
|
56
|
+
return value.every((item) => itemTypeGuard(item));
|
|
57
|
+
}
|
|
58
|
+
__name(isArrayOf, "isArrayOf");
|
|
59
|
+
function toStr(value, defaultValue = "") {
|
|
60
|
+
if (value === null || value === void 0) return defaultValue;
|
|
61
|
+
try {
|
|
62
|
+
if (typeof value === "object") {
|
|
63
|
+
return JSON.stringify(value);
|
|
64
|
+
}
|
|
65
|
+
return String(value);
|
|
66
|
+
} catch {
|
|
67
|
+
return defaultValue;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
__name(toStr, "toStr");
|
|
71
|
+
function toNum(value, defaultValue = 0) {
|
|
72
|
+
if (value === null || value === void 0) return defaultValue;
|
|
73
|
+
if (typeof value === "number") return value;
|
|
74
|
+
try {
|
|
75
|
+
const num = Number(value);
|
|
76
|
+
return Number.isNaN(num) ? defaultValue : num;
|
|
77
|
+
} catch {
|
|
78
|
+
return defaultValue;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
__name(toNum, "toNum");
|
|
82
|
+
function toBool(value, defaultValue = false) {
|
|
83
|
+
if (value === null || value === void 0) return defaultValue;
|
|
84
|
+
if (typeof value === "boolean") return value;
|
|
85
|
+
if (typeof value === "string") {
|
|
86
|
+
const lowercased = value.toLowerCase();
|
|
87
|
+
if (lowercased === "true" || lowercased === "yes" || lowercased === "y" || lowercased === "1") {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
if (lowercased === "false" || lowercased === "no" || lowercased === "n" || lowercased === "0") {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (typeof value === "number") {
|
|
95
|
+
return value !== 0;
|
|
96
|
+
}
|
|
97
|
+
return defaultValue;
|
|
98
|
+
}
|
|
99
|
+
__name(toBool, "toBool");
|
|
100
|
+
function ensureType(value, expectedType, defaultValue) {
|
|
101
|
+
if (getTypeOf(value) === expectedType) {
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
return defaultValue;
|
|
105
|
+
}
|
|
106
|
+
__name(ensureType, "ensureType");
|
|
107
|
+
function isDefined(value) {
|
|
108
|
+
return value !== null && value !== void 0;
|
|
109
|
+
}
|
|
110
|
+
__name(isDefined, "isDefined");
|
|
111
|
+
function isEmpty(value) {
|
|
112
|
+
if (value == null) return true;
|
|
113
|
+
if (typeof value === "string") return value.trim().length === 0;
|
|
114
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
115
|
+
if (value instanceof Map || value instanceof Set) return value.size === 0;
|
|
116
|
+
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
__name(isEmpty, "isEmpty");
|
|
120
|
+
|
|
121
|
+
exports.ensureType = ensureType;
|
|
122
|
+
exports.getTypeOf = getTypeOf;
|
|
123
|
+
exports.isArrayOf = isArrayOf;
|
|
124
|
+
exports.isDefined = isDefined;
|
|
125
|
+
exports.isEmpty = isEmpty;
|
|
126
|
+
exports.isPrimitiveType = isPrimitiveType;
|
|
127
|
+
exports.toBool = toBool;
|
|
128
|
+
exports.toNum = toNum;
|
|
129
|
+
exports.toStr = toStr;
|
package/type/index.d.ts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Check if a value is of a specific primitive type.
|
|
27
|
+
*
|
|
28
|
+
* @param value - Value to check
|
|
29
|
+
* @param type - Type to check against
|
|
30
|
+
* @returns Whether the value is of the specified type
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* isPrimitiveType('hello', 'string'); // true
|
|
35
|
+
* isPrimitiveType(42, 'number'); // true
|
|
36
|
+
* isPrimitiveType(true, 'boolean'); // true
|
|
37
|
+
* isPrimitiveType(null, 'null'); // true
|
|
38
|
+
* isPrimitiveType(undefined, 'undefined'); // true
|
|
39
|
+
* isPrimitiveType({}, 'object'); // true
|
|
40
|
+
* isPrimitiveType([], 'array'); // true
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function isPrimitiveType(value: unknown, type: 'string' | 'number' | 'boolean' | 'symbol' | 'bigint' | 'function' | 'object' | 'array' | 'null' | 'undefined'): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Get the primitive type of a value as a string.
|
|
46
|
+
*
|
|
47
|
+
* @param value - Value to get the type of
|
|
48
|
+
* @returns String representing the type
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* getTypeOf('hello'); // 'string'
|
|
53
|
+
* getTypeOf(42); // 'number'
|
|
54
|
+
* getTypeOf([]); // 'array'
|
|
55
|
+
* getTypeOf(null); // 'null'
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare function getTypeOf(value: unknown): string;
|
|
59
|
+
/**
|
|
60
|
+
* Type guard for checking if a value is an array of a specific type.
|
|
61
|
+
*
|
|
62
|
+
* @param value - Value to check
|
|
63
|
+
* @param itemTypeGuard - Function that checks if items are of the expected type
|
|
64
|
+
* @returns True if the value is an array with items of the expected type
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* isArrayOf([1, 2, 3], (item): item is number => typeof item === 'number'); // true
|
|
69
|
+
* isArrayOf(['a', 'b', 'c'], (item): item is string => typeof item === 'string'); // true
|
|
70
|
+
* isArrayOf([1, '2', 3], (item): item is number => typeof item === 'number'); // false
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
declare function isArrayOf<T>(value: unknown, itemTypeGuard: (item: unknown) => item is T): value is T[];
|
|
74
|
+
/**
|
|
75
|
+
* Convert a value to a string.
|
|
76
|
+
*
|
|
77
|
+
* @param value - Value to convert
|
|
78
|
+
* @param defaultValue - Default value if conversion fails
|
|
79
|
+
* @returns String representation of the value
|
|
80
|
+
*/
|
|
81
|
+
declare function toStr(value: unknown, defaultValue?: string): string;
|
|
82
|
+
/**
|
|
83
|
+
* Convert a value to a number.
|
|
84
|
+
*
|
|
85
|
+
* @param value - Value to convert
|
|
86
|
+
* @param defaultValue - Default value if conversion fails
|
|
87
|
+
* @returns Numeric representation of the value
|
|
88
|
+
*/
|
|
89
|
+
declare function toNum(value: unknown, defaultValue?: number): number;
|
|
90
|
+
/**
|
|
91
|
+
* Convert a value to a boolean.
|
|
92
|
+
*
|
|
93
|
+
* @param value - Value to convert
|
|
94
|
+
* @param defaultValue - Default value if conversion fails
|
|
95
|
+
* @returns Boolean representation of the value
|
|
96
|
+
*/
|
|
97
|
+
declare function toBool(value: unknown, defaultValue?: boolean): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Ensure a value matches the expected type, or provide a default.
|
|
100
|
+
*
|
|
101
|
+
* @param value - Value to check
|
|
102
|
+
* @param expectedType - Expected primitive type
|
|
103
|
+
* @param defaultValue - Default value to use if type doesn't match
|
|
104
|
+
* @returns The value if it matches the type, otherwise the default
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```typescript
|
|
108
|
+
* ensureType(42, 'number', 0); // 42
|
|
109
|
+
* ensureType('42', 'number', 0); // 0
|
|
110
|
+
* ensureType(undefined, 'string', 'default'); // 'default'
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function ensureType<T>(value: unknown, expectedType: string, defaultValue: T): T;
|
|
114
|
+
/**
|
|
115
|
+
* Check whether a value is neither null nor undefined.
|
|
116
|
+
* Useful in filter chains and guards.
|
|
117
|
+
*
|
|
118
|
+
* @param value - Value to check
|
|
119
|
+
* @returns True when value !== null && value !== undefined
|
|
120
|
+
*/
|
|
121
|
+
declare function isDefined<T>(value: T | null | undefined): value is T;
|
|
122
|
+
/**
|
|
123
|
+
* Check whether a value is empty.
|
|
124
|
+
* Supports strings, arrays, maps, sets and plain objects.
|
|
125
|
+
*
|
|
126
|
+
* @param value - Value to inspect
|
|
127
|
+
* @returns True when value is considered empty
|
|
128
|
+
*/
|
|
129
|
+
declare function isEmpty(value: any): boolean;
|
|
130
|
+
|
|
131
|
+
export { ensureType, getTypeOf, isArrayOf, isDefined, isEmpty, isPrimitiveType, toBool, toNum, toStr };
|
package/type/index.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
var __defProp = Object.defineProperty;
|
|
26
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
27
|
+
|
|
28
|
+
// src/type/type.utils.ts
|
|
29
|
+
function isPrimitiveType(value, type) {
|
|
30
|
+
if (type === "array") {
|
|
31
|
+
return Array.isArray(value);
|
|
32
|
+
}
|
|
33
|
+
if (type === "null") {
|
|
34
|
+
return value === null;
|
|
35
|
+
}
|
|
36
|
+
if (type === "undefined") {
|
|
37
|
+
return value === void 0;
|
|
38
|
+
}
|
|
39
|
+
if (type === "object") {
|
|
40
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
41
|
+
}
|
|
42
|
+
return typeof value === type;
|
|
43
|
+
}
|
|
44
|
+
__name(isPrimitiveType, "isPrimitiveType");
|
|
45
|
+
function getTypeOf(value) {
|
|
46
|
+
if (value === null) return "null";
|
|
47
|
+
if (value === void 0) return "undefined";
|
|
48
|
+
if (Array.isArray(value)) return "array";
|
|
49
|
+
return typeof value;
|
|
50
|
+
}
|
|
51
|
+
__name(getTypeOf, "getTypeOf");
|
|
52
|
+
function isArrayOf(value, itemTypeGuard) {
|
|
53
|
+
if (!Array.isArray(value)) return false;
|
|
54
|
+
return value.every((item) => itemTypeGuard(item));
|
|
55
|
+
}
|
|
56
|
+
__name(isArrayOf, "isArrayOf");
|
|
57
|
+
function toStr(value, defaultValue = "") {
|
|
58
|
+
if (value === null || value === void 0) return defaultValue;
|
|
59
|
+
try {
|
|
60
|
+
if (typeof value === "object") {
|
|
61
|
+
return JSON.stringify(value);
|
|
62
|
+
}
|
|
63
|
+
return String(value);
|
|
64
|
+
} catch {
|
|
65
|
+
return defaultValue;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
__name(toStr, "toStr");
|
|
69
|
+
function toNum(value, defaultValue = 0) {
|
|
70
|
+
if (value === null || value === void 0) return defaultValue;
|
|
71
|
+
if (typeof value === "number") return value;
|
|
72
|
+
try {
|
|
73
|
+
const num = Number(value);
|
|
74
|
+
return Number.isNaN(num) ? defaultValue : num;
|
|
75
|
+
} catch {
|
|
76
|
+
return defaultValue;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
__name(toNum, "toNum");
|
|
80
|
+
function toBool(value, defaultValue = false) {
|
|
81
|
+
if (value === null || value === void 0) return defaultValue;
|
|
82
|
+
if (typeof value === "boolean") return value;
|
|
83
|
+
if (typeof value === "string") {
|
|
84
|
+
const lowercased = value.toLowerCase();
|
|
85
|
+
if (lowercased === "true" || lowercased === "yes" || lowercased === "y" || lowercased === "1") {
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
if (lowercased === "false" || lowercased === "no" || lowercased === "n" || lowercased === "0") {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (typeof value === "number") {
|
|
93
|
+
return value !== 0;
|
|
94
|
+
}
|
|
95
|
+
return defaultValue;
|
|
96
|
+
}
|
|
97
|
+
__name(toBool, "toBool");
|
|
98
|
+
function ensureType(value, expectedType, defaultValue) {
|
|
99
|
+
if (getTypeOf(value) === expectedType) {
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
return defaultValue;
|
|
103
|
+
}
|
|
104
|
+
__name(ensureType, "ensureType");
|
|
105
|
+
function isDefined(value) {
|
|
106
|
+
return value !== null && value !== void 0;
|
|
107
|
+
}
|
|
108
|
+
__name(isDefined, "isDefined");
|
|
109
|
+
function isEmpty(value) {
|
|
110
|
+
if (value == null) return true;
|
|
111
|
+
if (typeof value === "string") return value.trim().length === 0;
|
|
112
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
113
|
+
if (value instanceof Map || value instanceof Set) return value.size === 0;
|
|
114
|
+
if (typeof value === "object") return Object.keys(value).length === 0;
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
__name(isEmpty, "isEmpty");
|
|
118
|
+
|
|
119
|
+
export { ensureType, getTypeOf, isArrayOf, isDefined, isEmpty, isPrimitiveType, toBool, toNum, toStr };
|
package/types/index.cjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
// src/types/api-response.ts
|
|
28
|
+
var SortDirection = /* @__PURE__ */ (function(SortDirection2) {
|
|
29
|
+
SortDirection2["ASC"] = "asc";
|
|
30
|
+
SortDirection2["DESC"] = "desc";
|
|
31
|
+
return SortDirection2;
|
|
32
|
+
})({});
|
|
33
|
+
|
|
34
|
+
exports.SortDirection = SortDirection;
|