@catbee/utils 0.0.6 → 0.0.7
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/README.md +51 -1106
- package/build/esm/config.d.ts +5 -13
- package/build/esm/config.js +0 -12
- package/build/esm/config.js.map +1 -1
- package/build/esm/servers/server.builder.d.ts +1 -118
- package/build/esm/servers/server.builder.js +0 -20
- package/build/esm/servers/server.builder.js.map +1 -1
- package/build/esm/servers/server.d.ts +2 -2
- package/build/esm/servers/server.js +3 -13
- package/build/esm/servers/server.js.map +1 -1
- package/build/esm/types/api-response.d.ts +28 -0
- package/build/esm/types/api-response.js +10 -1
- package/build/esm/types/api-response.js.map +1 -1
- package/build/esm/types/server.d.ts +0 -17
- package/build/esm/types/server.js.map +1 -1
- package/build/esm/utils/async.utils.d.ts +91 -0
- package/build/esm/utils/async.utils.js +244 -0
- package/build/esm/utils/async.utils.js.map +1 -1
- package/build/esm/utils/context-store.utils.d.ts +3 -0
- package/build/esm/utils/context-store.utils.js +9 -6
- package/build/esm/utils/context-store.utils.js.map +1 -1
- package/build/esm/utils/date.utils.d.ts +159 -0
- package/build/esm/utils/date.utils.js +389 -0
- package/build/esm/utils/date.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +3 -9
- package/build/esm/utils/logger.utils.js +3 -9
- package/build/esm/utils/logger.utils.js.map +1 -1
- package/build/esm/utils/middleware.utils.js +2 -1
- package/build/esm/utils/middleware.utils.js.map +1 -1
- package/build/esm/utils/performance.utils.d.ts +136 -0
- package/build/esm/utils/performance.utils.js +376 -0
- package/build/esm/utils/performance.utils.js.map +1 -0
- package/build/esm/utils/request.utils.d.ts +9 -0
- package/build/esm/utils/request.utils.js +32 -5
- package/build/esm/utils/request.utils.js.map +1 -1
- package/build/esm/utils/stream.utils.d.ts +88 -0
- package/build/esm/utils/stream.utils.js +284 -0
- package/build/esm/utils/stream.utils.js.map +1 -0
- package/build/esm/utils/type.utils.d.ts +90 -0
- package/build/esm/utils/type.utils.js +190 -0
- package/build/esm/utils/type.utils.js.map +1 -0
- package/build/esnext/config.d.ts +5 -13
- package/build/esnext/config.js +0 -12
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/servers/server.builder.d.ts +1 -118
- package/build/esnext/servers/server.builder.js +0 -20
- package/build/esnext/servers/server.builder.js.map +1 -1
- package/build/esnext/servers/server.d.ts +2 -2
- package/build/esnext/servers/server.js +3 -13
- package/build/esnext/servers/server.js.map +1 -1
- package/build/esnext/types/api-response.d.ts +28 -0
- package/build/esnext/types/api-response.js +10 -1
- package/build/esnext/types/api-response.js.map +1 -1
- package/build/esnext/types/server.d.ts +0 -17
- package/build/esnext/types/server.js.map +1 -1
- package/build/esnext/utils/async.utils.d.ts +91 -0
- package/build/esnext/utils/async.utils.js +190 -0
- package/build/esnext/utils/async.utils.js.map +1 -1
- package/build/esnext/utils/context-store.utils.d.ts +3 -0
- package/build/esnext/utils/context-store.utils.js +9 -6
- package/build/esnext/utils/context-store.utils.js.map +1 -1
- package/build/esnext/utils/date.utils.d.ts +159 -0
- package/build/esnext/utils/date.utils.js +384 -0
- package/build/esnext/utils/date.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +3 -9
- package/build/esnext/utils/logger.utils.js +3 -9
- package/build/esnext/utils/logger.utils.js.map +1 -1
- package/build/esnext/utils/middleware.utils.js +2 -1
- package/build/esnext/utils/middleware.utils.js.map +1 -1
- package/build/esnext/utils/performance.utils.d.ts +136 -0
- package/build/esnext/utils/performance.utils.js +271 -0
- package/build/esnext/utils/performance.utils.js.map +1 -0
- package/build/esnext/utils/request.utils.d.ts +9 -0
- package/build/esnext/utils/request.utils.js +23 -5
- package/build/esnext/utils/request.utils.js.map +1 -1
- package/build/esnext/utils/stream.utils.d.ts +88 -0
- package/build/esnext/utils/stream.utils.js +210 -0
- package/build/esnext/utils/stream.utils.js.map +1 -0
- package/build/esnext/utils/type.utils.d.ts +90 -0
- package/build/esnext/utils/type.utils.js +187 -0
- package/build/esnext/utils/type.utils.js.map +1 -0
- package/build/src/config.d.ts +5 -13
- package/build/src/config.js +0 -12
- package/build/src/config.js.map +1 -1
- package/build/src/servers/server.builder.d.ts +1 -118
- package/build/src/servers/server.builder.js +0 -20
- package/build/src/servers/server.builder.js.map +1 -1
- package/build/src/servers/server.d.ts +2 -2
- package/build/src/servers/server.js +2 -12
- package/build/src/servers/server.js.map +1 -1
- package/build/src/types/api-response.d.ts +28 -0
- package/build/src/types/api-response.js +11 -0
- package/build/src/types/api-response.js.map +1 -1
- package/build/src/types/server.d.ts +0 -17
- package/build/src/types/server.js.map +1 -1
- package/build/src/utils/async.utils.d.ts +91 -0
- package/build/src/utils/async.utils.js +194 -0
- package/build/src/utils/async.utils.js.map +1 -1
- package/build/src/utils/context-store.utils.d.ts +3 -0
- package/build/src/utils/context-store.utils.js +9 -6
- package/build/src/utils/context-store.utils.js.map +1 -1
- package/build/src/utils/date.utils.d.ts +159 -0
- package/build/src/utils/date.utils.js +396 -0
- package/build/src/utils/date.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +3 -9
- package/build/src/utils/logger.utils.js +4 -10
- package/build/src/utils/logger.utils.js.map +1 -1
- package/build/src/utils/middleware.utils.js +2 -1
- package/build/src/utils/middleware.utils.js.map +1 -1
- package/build/src/utils/performance.utils.d.ts +136 -0
- package/build/src/utils/performance.utils.js +278 -0
- package/build/src/utils/performance.utils.js.map +1 -0
- package/build/src/utils/request.utils.d.ts +9 -0
- package/build/src/utils/request.utils.js +25 -5
- package/build/src/utils/request.utils.js.map +1 -1
- package/build/src/utils/stream.utils.d.ts +88 -0
- package/build/src/utils/stream.utils.js +218 -0
- package/build/src/utils/stream.utils.js.map +1 -0
- package/build/src/utils/type.utils.d.ts +90 -0
- package/build/src/utils/type.utils.js +196 -0
- package/build/src/utils/type.utils.js.map +1 -0
- package/package.json +6 -2
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* The MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2025 Catbee Technologies
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.isPrimitiveType = isPrimitiveType;
|
|
27
|
+
exports.getTypeOf = getTypeOf;
|
|
28
|
+
exports.isArrayOf = isArrayOf;
|
|
29
|
+
exports.toStr = toStr;
|
|
30
|
+
exports.toNum = toNum;
|
|
31
|
+
exports.toBool = toBool;
|
|
32
|
+
exports.ensureType = ensureType;
|
|
33
|
+
/**
|
|
34
|
+
* Check if a value is of a specific primitive type.
|
|
35
|
+
*
|
|
36
|
+
* @param value - Value to check
|
|
37
|
+
* @param type - Type to check against
|
|
38
|
+
* @returns Whether the value is of the specified type
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* isPrimitiveType('hello', 'string'); // true
|
|
43
|
+
* isPrimitiveType(42, 'number'); // true
|
|
44
|
+
* isPrimitiveType(true, 'boolean'); // true
|
|
45
|
+
* isPrimitiveType(null, 'null'); // true
|
|
46
|
+
* isPrimitiveType(undefined, 'undefined'); // true
|
|
47
|
+
* isPrimitiveType({}, 'object'); // true
|
|
48
|
+
* isPrimitiveType([], 'array'); // true
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
function isPrimitiveType(value, type) {
|
|
52
|
+
if (type === 'array') {
|
|
53
|
+
return Array.isArray(value);
|
|
54
|
+
}
|
|
55
|
+
if (type === 'null') {
|
|
56
|
+
return value === null;
|
|
57
|
+
}
|
|
58
|
+
if (type === 'undefined') {
|
|
59
|
+
return value === undefined;
|
|
60
|
+
}
|
|
61
|
+
if (type === 'object') {
|
|
62
|
+
// Only plain objects, not arrays or null
|
|
63
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
64
|
+
}
|
|
65
|
+
return typeof value === type;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Get the primitive type of a value as a string.
|
|
69
|
+
*
|
|
70
|
+
* @param value - Value to get the type of
|
|
71
|
+
* @returns String representing the type
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* getTypeOf('hello'); // 'string'
|
|
76
|
+
* getTypeOf(42); // 'number'
|
|
77
|
+
* getTypeOf([]); // 'array'
|
|
78
|
+
* getTypeOf(null); // 'null'
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
function getTypeOf(value) {
|
|
82
|
+
if (value === null)
|
|
83
|
+
return 'null';
|
|
84
|
+
if (value === undefined)
|
|
85
|
+
return 'undefined';
|
|
86
|
+
if (Array.isArray(value))
|
|
87
|
+
return 'array';
|
|
88
|
+
return typeof value;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Type guard for checking if a value is an array of a specific type.
|
|
92
|
+
*
|
|
93
|
+
* @param value - Value to check
|
|
94
|
+
* @param itemTypeGuard - Function that checks if items are of the expected type
|
|
95
|
+
* @returns True if the value is an array with items of the expected type
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* isArrayOf([1, 2, 3], (item): item is number => typeof item === 'number'); // true
|
|
100
|
+
* isArrayOf(['a', 'b', 'c'], (item): item is string => typeof item === 'string'); // true
|
|
101
|
+
* isArrayOf([1, '2', 3], (item): item is number => typeof item === 'number'); // false
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
function isArrayOf(value, itemTypeGuard) {
|
|
105
|
+
if (!Array.isArray(value))
|
|
106
|
+
return false;
|
|
107
|
+
return value.every(item => itemTypeGuard(item));
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Convert a value to a string.
|
|
111
|
+
*
|
|
112
|
+
* @param value - Value to convert
|
|
113
|
+
* @param defaultValue - Default value if conversion fails
|
|
114
|
+
* @returns String representation of the value
|
|
115
|
+
*/
|
|
116
|
+
function toStr(value, defaultValue = '') {
|
|
117
|
+
if (value === null || value === undefined)
|
|
118
|
+
return defaultValue;
|
|
119
|
+
try {
|
|
120
|
+
if (typeof value === 'object') {
|
|
121
|
+
return JSON.stringify(value);
|
|
122
|
+
}
|
|
123
|
+
return String(value);
|
|
124
|
+
}
|
|
125
|
+
catch (_a) {
|
|
126
|
+
return defaultValue;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Convert a value to a number.
|
|
131
|
+
*
|
|
132
|
+
* @param value - Value to convert
|
|
133
|
+
* @param defaultValue - Default value if conversion fails
|
|
134
|
+
* @returns Numeric representation of the value
|
|
135
|
+
*/
|
|
136
|
+
function toNum(value, defaultValue = 0) {
|
|
137
|
+
if (value === null || value === undefined)
|
|
138
|
+
return defaultValue;
|
|
139
|
+
if (typeof value === 'number')
|
|
140
|
+
return value;
|
|
141
|
+
try {
|
|
142
|
+
const num = Number(value);
|
|
143
|
+
return isNaN(num) ? defaultValue : num;
|
|
144
|
+
}
|
|
145
|
+
catch (_a) {
|
|
146
|
+
return defaultValue;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Convert a value to a boolean.
|
|
151
|
+
*
|
|
152
|
+
* @param value - Value to convert
|
|
153
|
+
* @param defaultValue - Default value if conversion fails
|
|
154
|
+
* @returns Boolean representation of the value
|
|
155
|
+
*/
|
|
156
|
+
function toBool(value, defaultValue = false) {
|
|
157
|
+
if (value === null || value === undefined)
|
|
158
|
+
return defaultValue;
|
|
159
|
+
if (typeof value === 'boolean')
|
|
160
|
+
return value;
|
|
161
|
+
if (typeof value === 'string') {
|
|
162
|
+
const lowercased = value.toLowerCase();
|
|
163
|
+
if (lowercased === 'true' || lowercased === 'yes' || lowercased === 'y' || lowercased === '1') {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
if (lowercased === 'false' || lowercased === 'no' || lowercased === 'n' || lowercased === '0') {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (typeof value === 'number') {
|
|
171
|
+
return value !== 0;
|
|
172
|
+
}
|
|
173
|
+
return defaultValue;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Ensure a value matches the expected type, or provide a default.
|
|
177
|
+
*
|
|
178
|
+
* @param value - Value to check
|
|
179
|
+
* @param expectedType - Expected primitive type
|
|
180
|
+
* @param defaultValue - Default value to use if type doesn't match
|
|
181
|
+
* @returns The value if it matches the type, otherwise the default
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```typescript
|
|
185
|
+
* ensureType(42, 'number', 0); // 42
|
|
186
|
+
* ensureType('42', 'number', 0); // 0
|
|
187
|
+
* ensureType(undefined, 'string', 'default'); // 'default'
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
function ensureType(value, expectedType, defaultValue) {
|
|
191
|
+
if (getTypeOf(value) === expectedType) {
|
|
192
|
+
return value;
|
|
193
|
+
}
|
|
194
|
+
return defaultValue;
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=type.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.utils.js","sourceRoot":"","sources":["../../../src/utils/type.utils.ts"],"names":[],"mappings":"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","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Check if a value is of a specific primitive type.\n *\n * @param value - Value to check\n * @param type - Type to check against\n * @returns Whether the value is of the specified type\n *\n * @example\n * ```typescript\n * isPrimitiveType('hello', 'string'); // true\n * isPrimitiveType(42, 'number'); // true\n * isPrimitiveType(true, 'boolean'); // true\n * isPrimitiveType(null, 'null'); // true\n * isPrimitiveType(undefined, 'undefined'); // true\n * isPrimitiveType({}, 'object'); // true\n * isPrimitiveType([], 'array'); // true\n * ```\n */\nexport function isPrimitiveType(\n value: unknown,\n type: 'string' | 'number' | 'boolean' | 'symbol' | 'bigint' | 'function' | 'object' | 'array' | 'null' | 'undefined'\n): boolean {\n if (type === 'array') {\n return Array.isArray(value);\n }\n\n if (type === 'null') {\n return value === null;\n }\n\n if (type === 'undefined') {\n return value === undefined;\n }\n\n if (type === 'object') {\n // Only plain objects, not arrays or null\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n }\n\n return typeof value === type;\n}\n\n/**\n * Get the primitive type of a value as a string.\n *\n * @param value - Value to get the type of\n * @returns String representing the type\n *\n * @example\n * ```typescript\n * getTypeOf('hello'); // 'string'\n * getTypeOf(42); // 'number'\n * getTypeOf([]); // 'array'\n * getTypeOf(null); // 'null'\n * ```\n */\nexport function getTypeOf(value: unknown): string {\n if (value === null) return 'null';\n if (value === undefined) return 'undefined';\n if (Array.isArray(value)) return 'array';\n return typeof value;\n}\n\n/**\n * Type guard for checking if a value is an array of a specific type.\n *\n * @param value - Value to check\n * @param itemTypeGuard - Function that checks if items are of the expected type\n * @returns True if the value is an array with items of the expected type\n *\n * @example\n * ```typescript\n * isArrayOf([1, 2, 3], (item): item is number => typeof item === 'number'); // true\n * isArrayOf(['a', 'b', 'c'], (item): item is string => typeof item === 'string'); // true\n * isArrayOf([1, '2', 3], (item): item is number => typeof item === 'number'); // false\n * ```\n */\nexport function isArrayOf<T>(value: unknown, itemTypeGuard: (item: unknown) => item is T): value is T[] {\n if (!Array.isArray(value)) return false;\n return value.every(item => itemTypeGuard(item));\n}\n\n/**\n * Convert a value to a string.\n *\n * @param value - Value to convert\n * @param defaultValue - Default value if conversion fails\n * @returns String representation of the value\n */\nexport function toStr(value: unknown, defaultValue: string = ''): string {\n if (value === null || value === undefined) return defaultValue;\n\n try {\n if (typeof value === 'object') {\n return JSON.stringify(value);\n }\n return String(value);\n } catch {\n return defaultValue;\n }\n}\n\n/**\n * Convert a value to a number.\n *\n * @param value - Value to convert\n * @param defaultValue - Default value if conversion fails\n * @returns Numeric representation of the value\n */\nexport function toNum(value: unknown, defaultValue: number = 0): number {\n if (value === null || value === undefined) return defaultValue;\n\n if (typeof value === 'number') return value;\n\n try {\n const num = Number(value);\n return isNaN(num) ? defaultValue : num;\n } catch {\n return defaultValue;\n }\n}\n\n/**\n * Convert a value to a boolean.\n *\n * @param value - Value to convert\n * @param defaultValue - Default value if conversion fails\n * @returns Boolean representation of the value\n */\nexport function toBool(value: unknown, defaultValue: boolean = false): boolean {\n if (value === null || value === undefined) return defaultValue;\n\n if (typeof value === 'boolean') return value;\n\n if (typeof value === 'string') {\n const lowercased = value.toLowerCase();\n if (lowercased === 'true' || lowercased === 'yes' || lowercased === 'y' || lowercased === '1') {\n return true;\n }\n if (lowercased === 'false' || lowercased === 'no' || lowercased === 'n' || lowercased === '0') {\n return false;\n }\n }\n\n if (typeof value === 'number') {\n return value !== 0;\n }\n\n return defaultValue;\n}\n\n/**\n * Ensure a value matches the expected type, or provide a default.\n *\n * @param value - Value to check\n * @param expectedType - Expected primitive type\n * @param defaultValue - Default value to use if type doesn't match\n * @returns The value if it matches the type, otherwise the default\n *\n * @example\n * ```typescript\n * ensureType(42, 'number', 0); // 42\n * ensureType('42', 'number', 0); // 0\n * ensureType(undefined, 'string', 'default'); // 'default'\n * ```\n */\nexport function ensureType<T>(value: unknown, expectedType: string, defaultValue: T): T {\n if (getTypeOf(value) === expectedType) {\n return value as unknown as T;\n }\n return defaultValue;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@catbee/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.",
|
|
5
5
|
"main": "build/src/index.js",
|
|
6
6
|
"module": "build/esm/index.js",
|
|
@@ -29,7 +29,11 @@
|
|
|
29
29
|
"coverage": "npm run test:coverage",
|
|
30
30
|
"test:debug": "cross-env LOGGER_LEVEL=info jest",
|
|
31
31
|
"add-license": "npx license-check-and-add add -f file-banner-lic.json && npm run lint:fix",
|
|
32
|
-
"check-license": "npx license-check-and-add check -f file-banner-lic.json"
|
|
32
|
+
"check-license": "npx license-check-and-add check -f file-banner-lic.json",
|
|
33
|
+
"docs:install": "cd docs && npm install",
|
|
34
|
+
"docs:start": "cd docs && npm run start",
|
|
35
|
+
"docs:build": "cd docs && npm run build",
|
|
36
|
+
"docs:serve": "npm run docs:build && cd docs && npm run serve"
|
|
33
37
|
},
|
|
34
38
|
"keywords": [
|
|
35
39
|
"utils",
|