@creejs/commons-lang 1.0.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/README.md +2 -0
- package/index.js +2 -0
- package/lib/exec-utils.js +58 -0
- package/lib/index.js +26 -0
- package/lib/lang-utils.js +110 -0
- package/lib/promise-utils.js +317 -0
- package/lib/string-utils.js +428 -0
- package/lib/type-assert.js +253 -0
- package/lib/type-utils.js +290 -0
- package/package.json +31 -0
- package/types/exec-utils.d.ts +14 -0
- package/types/index.d.ts +7 -0
- package/types/lang-utils.d.ts +58 -0
- package/types/lang.d.ts +5 -0
- package/types/promise-utils.d.ts +94 -0
- package/types/string-utils.d.ts +152 -0
- package/types/type-assert.d.ts +91 -0
- package/types/type-asset.d.ts +84 -0
- package/types/type-utils.d.ts +158 -0
|
@@ -0,0 +1,428 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @module StringUtils
|
|
5
|
+
* @description Utility functions for string manipulation, validation, and transformation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// 3rd
|
|
9
|
+
|
|
10
|
+
// internal
|
|
11
|
+
|
|
12
|
+
// owned
|
|
13
|
+
const { assertString, assertNumber } = require('./type-assert')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Checks if a string is null, undefined, or length is 0.
|
|
17
|
+
* @param {string} str
|
|
18
|
+
* @returns {boolean}
|
|
19
|
+
* @throws {Error} If `str` is not null or undefined, and not a string.
|
|
20
|
+
*/
|
|
21
|
+
function isEmpty (str) {
|
|
22
|
+
if (str == null) {
|
|
23
|
+
return true
|
|
24
|
+
}
|
|
25
|
+
assertString(str)
|
|
26
|
+
return str.length === 0
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Asserts that the given string is not empty.
|
|
31
|
+
* @param {string} str - The string to check.
|
|
32
|
+
* @throws {Error} Throws an error if the string is empty.
|
|
33
|
+
*/
|
|
34
|
+
function assertNotEmpty (str) {
|
|
35
|
+
if (isEmpty(str)) {
|
|
36
|
+
throw new Error(`Empty String: ${str}`)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Checks if a string is null, undefined, or consists only of whitespace.
|
|
42
|
+
* @param {string} str - The string to check.
|
|
43
|
+
* @returns {boolean} True if the string is blank, false otherwise.
|
|
44
|
+
* @throws {Error} If `str` is not null or undefined, and not a string.
|
|
45
|
+
*/
|
|
46
|
+
function isBlank (str) {
|
|
47
|
+
if (str == null) {
|
|
48
|
+
return true
|
|
49
|
+
}
|
|
50
|
+
assertString(str)
|
|
51
|
+
return str.trim().length === 0
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Asserts that the given string is not blank.
|
|
56
|
+
* @param {string} str - The string to check.
|
|
57
|
+
* @throws {Error} Throws an error if the string is blank.
|
|
58
|
+
*/
|
|
59
|
+
function assertNotBlank (str) {
|
|
60
|
+
if (isBlank(str)) {
|
|
61
|
+
throw new Error(`Blank String: ${str}`)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Capitalizes the first character of a string.
|
|
67
|
+
* @param {string} str - The string to capitalize.
|
|
68
|
+
* @returns {string} The capitalized string or original if unchanged.
|
|
69
|
+
*/
|
|
70
|
+
function capitalize (str) {
|
|
71
|
+
assertString(str)
|
|
72
|
+
if (str.length === 0) {
|
|
73
|
+
return str
|
|
74
|
+
}
|
|
75
|
+
const char0 = str.charAt(0)
|
|
76
|
+
const upperChar = char0.toUpperCase()
|
|
77
|
+
return char0 === upperChar ? str : upperChar + str.slice(1)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Converts the first character of a string to lowercase.
|
|
82
|
+
* If the string is null or empty, returns it unchanged.
|
|
83
|
+
* @param {string} str - The input string to decapitalize.
|
|
84
|
+
* @returns {string} The decapitalized string or original if unchanged.
|
|
85
|
+
*/
|
|
86
|
+
function decapitalize (str) {
|
|
87
|
+
assertString(str)
|
|
88
|
+
if (str.length === 0) {
|
|
89
|
+
return str
|
|
90
|
+
}
|
|
91
|
+
const char0 = str.charAt(0)
|
|
92
|
+
const lowerChar = char0.toLowerCase()
|
|
93
|
+
return char0 === lowerChar ? str : lowerChar + str.slice(1)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Splits a string into chunks of fixed length, padding the last chunk if needed.
|
|
98
|
+
* 1. if str is empty, returns an empty array "[]"
|
|
99
|
+
* 2. if length is less than string length, returns an array with the string padded with "padding" as the only element
|
|
100
|
+
* 3. the last chunk is padded with "padding" if needed
|
|
101
|
+
* @param {string} str - The string to split.
|
|
102
|
+
* @param {number} length - The desired length of each chunk.
|
|
103
|
+
* @param {string} [padding=' '] - The padding character for the last chunk.
|
|
104
|
+
* @returns {string[]} An array of string chunks with fixed length.
|
|
105
|
+
* @throws {Error} If `str` is not a string or `length` is not a number.
|
|
106
|
+
*/
|
|
107
|
+
function splitWithFixedLength (str, length, padding = ' ') {
|
|
108
|
+
assertString(str)
|
|
109
|
+
assertNumber(length)
|
|
110
|
+
assertString(padding)
|
|
111
|
+
if (str.length === 0) {
|
|
112
|
+
return []
|
|
113
|
+
}
|
|
114
|
+
if (length <= 0) {
|
|
115
|
+
throw new Error('length muse >=0')
|
|
116
|
+
}
|
|
117
|
+
if (str.length < length) {
|
|
118
|
+
return [str.padEnd(length, padding)]
|
|
119
|
+
}
|
|
120
|
+
const chunks = []
|
|
121
|
+
for (let i = 0; i < str.length; i += length) {
|
|
122
|
+
const splitted = str.substring(i, i + length)
|
|
123
|
+
chunks.push(splitted.padEnd(length, padding))
|
|
124
|
+
}
|
|
125
|
+
return chunks
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Splits a string into chunks using the specified markers.
|
|
130
|
+
* 1. If no markers are provided, defaults to comma (',').
|
|
131
|
+
* 2. null/undefined values are ignored
|
|
132
|
+
* 3. empty array "[]" returned, if string does not include any markers
|
|
133
|
+
* @param {string} str - The string to split.
|
|
134
|
+
* @param {...string} markers - The markers to split the string by.
|
|
135
|
+
* @returns {Array<string>} An array of string chunks.
|
|
136
|
+
* @throws {Error} If the input is not a string.
|
|
137
|
+
*/
|
|
138
|
+
function split (str, ...markers) {
|
|
139
|
+
assertString(str)
|
|
140
|
+
const strLength = str.length
|
|
141
|
+
if (strLength === 0) {
|
|
142
|
+
return []
|
|
143
|
+
}
|
|
144
|
+
const workingMarkers = [...markers]
|
|
145
|
+
if (markers.length === 0) {
|
|
146
|
+
markers.push(',')
|
|
147
|
+
}
|
|
148
|
+
const markerPostionPairs = findMarkerPositionsRegex(str, ...workingMarkers)
|
|
149
|
+
if (markerPostionPairs.length === 0) {
|
|
150
|
+
return []
|
|
151
|
+
}
|
|
152
|
+
const chunks = []
|
|
153
|
+
let chunk = ''
|
|
154
|
+
let startIndex = 0
|
|
155
|
+
for (const { marker, index: endIndex } of markerPostionPairs) {
|
|
156
|
+
chunk = str.substring(startIndex, endIndex)
|
|
157
|
+
chunks.push(chunk)
|
|
158
|
+
startIndex = endIndex + marker.length
|
|
159
|
+
}
|
|
160
|
+
// add rested into chunks
|
|
161
|
+
chunk = str.substring(startIndex)
|
|
162
|
+
chunks.push(chunk)
|
|
163
|
+
return chunks
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Finds all positions of markers in a string.
|
|
168
|
+
* 1. use loop to iterate over markers
|
|
169
|
+
* 2. use sting.indexOf to find position of each marker
|
|
170
|
+
* @param {string} str - The input string to search in.
|
|
171
|
+
* @param {...string} markers - The markers to search for.
|
|
172
|
+
* @returns {Array<{marker: string, index: number}>} Array of objects containing marker and its index.
|
|
173
|
+
* @throws {Error} If `str` is not a string or no markers are provided.
|
|
174
|
+
*/
|
|
175
|
+
function findMarkerPositions (str, ...markers) {
|
|
176
|
+
assertString(str)
|
|
177
|
+
if (markers.length === 0) {
|
|
178
|
+
throw new Error('At least one marker must be provided')
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const positions = []
|
|
182
|
+
for (const marker of new Set(markers)) { // filter duplicated markers
|
|
183
|
+
if (isEmpty(marker)) { // 'abc'.indexOf('') === 0
|
|
184
|
+
continue
|
|
185
|
+
}
|
|
186
|
+
assertString(marker)
|
|
187
|
+
let index = str.indexOf(marker)
|
|
188
|
+
while (index !== -1) {
|
|
189
|
+
positions.push({ marker, index })
|
|
190
|
+
index = str.indexOf(marker, index + marker.length)
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Sort positions by index in ascending order
|
|
194
|
+
positions.sort((p1, p2) => p1.index - p2.index)
|
|
195
|
+
return positions
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Finds all positions of markers in a string.
|
|
200
|
+
* 1. Finds all positions of markers in a string using regular expressions.
|
|
201
|
+
* 2. Each marker is included as a separate capture group in a single regex.
|
|
202
|
+
* @param {string} str - The input string to search in.
|
|
203
|
+
* @param {...string} markers - The markers to search for.
|
|
204
|
+
* @returns {Array<{marker: string, index: number}>} Array of objects containing marker and its index.
|
|
205
|
+
* @throws {Error} If `str` is not a string or no markers are provided.
|
|
206
|
+
*/
|
|
207
|
+
function findMarkerPositionsRegex (str, ...markers) {
|
|
208
|
+
assertString(str)
|
|
209
|
+
if (markers.length === 0) {
|
|
210
|
+
throw new Error('At least one marker must be provided')
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// filter duplicated, empty markers
|
|
214
|
+
// Escape special regex characters in markers
|
|
215
|
+
const escapedMarkers = [...new Set(markers.filter(v => v != null))].map(marker => {
|
|
216
|
+
assertString(marker)
|
|
217
|
+
return marker.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
218
|
+
})
|
|
219
|
+
|
|
220
|
+
// Create regex pattern with each marker as a separate capture group
|
|
221
|
+
// Format: (marker1)|(marker2)|(marker3)...
|
|
222
|
+
const pattern = new RegExp(escapedMarkers.map(m => `(${m})`).join('|'), 'g')
|
|
223
|
+
|
|
224
|
+
const positions = []
|
|
225
|
+
let match = null
|
|
226
|
+
|
|
227
|
+
// Find all matches
|
|
228
|
+
while ((match = pattern.exec(str)) !== null) {
|
|
229
|
+
// Determine which marker was matched by checking which capture group has a value
|
|
230
|
+
for (let i = 1; i < match.length; i++) {
|
|
231
|
+
if (match[i]) {
|
|
232
|
+
positions.push({
|
|
233
|
+
marker: markers[i - 1], // Use the original marker, not the escaped version
|
|
234
|
+
index: match.index
|
|
235
|
+
})
|
|
236
|
+
break // Only one group will match for each exec call
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Avoid infinite loops with zero-width matches
|
|
241
|
+
if (match[0].length === 0) {
|
|
242
|
+
pattern.lastIndex++
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return positions
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Returns the substring before the first occurrence of a marker.
|
|
251
|
+
* @param {string} str - The input string to search in.
|
|
252
|
+
* @param {string} marker - The string to search for.
|
|
253
|
+
* @returns {string | undefined} The substring before the marker, or undefined if marker not found.
|
|
254
|
+
* @throws {Error} If either input is not a string.
|
|
255
|
+
*/
|
|
256
|
+
function substringBefore (str, marker) {
|
|
257
|
+
assertString(str)
|
|
258
|
+
assertString(marker)
|
|
259
|
+
if (str.length === 0 || marker.length === 0) {
|
|
260
|
+
return
|
|
261
|
+
}
|
|
262
|
+
const index = str.indexOf(marker)
|
|
263
|
+
if (index === -1) {
|
|
264
|
+
return undefined
|
|
265
|
+
}
|
|
266
|
+
return str.substring(0, index)
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Returns the substring before the last occurrence of a marker.
|
|
271
|
+
* @param {string} str
|
|
272
|
+
* @param {string} marker
|
|
273
|
+
* @returns {string | undefined}The substring before the last marker, or undefined if marker not found.
|
|
274
|
+
* @throws {Error} If either input is not a string.
|
|
275
|
+
*/
|
|
276
|
+
function substringBeforeLast (str, marker) {
|
|
277
|
+
assertString(str)
|
|
278
|
+
assertString(marker)
|
|
279
|
+
if (str.length === 0 || marker.length === 0) {
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
const index = str.lastIndexOf(marker)
|
|
283
|
+
if (index === -1) {
|
|
284
|
+
return
|
|
285
|
+
}
|
|
286
|
+
return str.substring(0, index)
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Returns the substring after the first occurrence of the specified marker.
|
|
291
|
+
* If the marker is not found, returns an empty string.
|
|
292
|
+
*
|
|
293
|
+
* @param {string} str - The string to search in
|
|
294
|
+
* @param {string} marker - The string to search for
|
|
295
|
+
* @returns {string | undefined} The substring after the marker, or undefined if not found
|
|
296
|
+
*/
|
|
297
|
+
function substringAfter (str, marker) {
|
|
298
|
+
assertString(str)
|
|
299
|
+
assertString(marker)
|
|
300
|
+
if (str.length === 0 || marker.length === 0) {
|
|
301
|
+
return
|
|
302
|
+
}
|
|
303
|
+
const index = str.indexOf(marker)
|
|
304
|
+
if (index === -1) {
|
|
305
|
+
return
|
|
306
|
+
}
|
|
307
|
+
return str.substring(index + marker.length)
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Returns the substring after the last occurrence of a marker in a string.
|
|
312
|
+
* @param {string} str - The input string to search in.
|
|
313
|
+
* @param {string} marker - The marker to search for.
|
|
314
|
+
* @returns {string|undefined} The substring after the last marker, or undefined if marker not found.
|
|
315
|
+
*/
|
|
316
|
+
function substringAfterLast (str, marker) {
|
|
317
|
+
assertString(str)
|
|
318
|
+
assertString(marker)
|
|
319
|
+
if (str.length === 0 || marker.length === 0) {
|
|
320
|
+
return
|
|
321
|
+
}
|
|
322
|
+
const index = str.lastIndexOf(marker)
|
|
323
|
+
if (index === -1) {
|
|
324
|
+
return
|
|
325
|
+
}
|
|
326
|
+
return str.substring(index + marker.length)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* Extracts the substring between the specified start and end markers in a string.
|
|
331
|
+
* 1. NOT Greedy, substring between the FIRST startMarker and FIRST endMarker
|
|
332
|
+
* 2. undefined returned, if Not Found neither startMarker nor endMarker
|
|
333
|
+
* @param {string} str - The input string to search within
|
|
334
|
+
* @param {string} startMarker - The starting marker string
|
|
335
|
+
* @param {string} endMarker - The ending marker string
|
|
336
|
+
* @returns {string|undefined} The substring between markers, or undefined if markers not found
|
|
337
|
+
* @throws {Error} If any input is not a string.
|
|
338
|
+
*/
|
|
339
|
+
function substringBetween (str, startMarker, endMarker) {
|
|
340
|
+
assertNotEmpty(str)
|
|
341
|
+
assertNotEmpty(startMarker)
|
|
342
|
+
assertNotEmpty(endMarker)
|
|
343
|
+
const startIndex = str.indexOf(startMarker)
|
|
344
|
+
if (startIndex === -1) {
|
|
345
|
+
return
|
|
346
|
+
}
|
|
347
|
+
const endIndex = str.indexOf(endMarker, startIndex + startMarker.length)
|
|
348
|
+
if (endIndex === -1) {
|
|
349
|
+
return
|
|
350
|
+
}
|
|
351
|
+
return str.substring(startIndex + startMarker.length, endIndex)
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Extracts the substring between the first occurrence of `startMarker` and the last occurrence of `endMarker` in `str`.
|
|
356
|
+
* 1. Greedy, substring between the FIRST startMarker and LAST endMarker
|
|
357
|
+
* 2. undefined returned, if Not Found neither startMarker nor endMarker
|
|
358
|
+
* @param {string} str - The input string to search.
|
|
359
|
+
* @param {string} startMarker - The starting marker.
|
|
360
|
+
* @param {string} endMarker - The ending marker.
|
|
361
|
+
* @returns {string|undefined} The substring between markers, or `undefined` if markers are not found.
|
|
362
|
+
* @throws {Error} If any input is not a string.
|
|
363
|
+
*/
|
|
364
|
+
function substringBetweenGreedy (str, startMarker, endMarker) {
|
|
365
|
+
assertNotEmpty(str)
|
|
366
|
+
assertNotEmpty(startMarker)
|
|
367
|
+
assertNotEmpty(endMarker)
|
|
368
|
+
const startIndex = str.indexOf(startMarker)
|
|
369
|
+
if (startIndex === -1) {
|
|
370
|
+
return
|
|
371
|
+
}
|
|
372
|
+
const endIndex = str.lastIndexOf(endMarker)
|
|
373
|
+
if (endIndex === -1 || endIndex <= startIndex) {
|
|
374
|
+
return
|
|
375
|
+
}
|
|
376
|
+
return str.substring(startIndex + startMarker.length, endIndex)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Extracts all substrings between specified start and end markers in a string.
|
|
381
|
+
* 1. NOT Greedy
|
|
382
|
+
* @param {string} str - The input string to search within
|
|
383
|
+
* @param {string} startMarker - The substring marking the start of extraction
|
|
384
|
+
* @param {string} endMarker - The substring marking the end of extraction
|
|
385
|
+
* @returns {string[]} Array of all found substrings between markers, empty array "[]" returned if not found
|
|
386
|
+
* @throws {Error} If any input is not a string
|
|
387
|
+
*/
|
|
388
|
+
function substringsBetween (str, startMarker, endMarker) {
|
|
389
|
+
assertNotEmpty(str)
|
|
390
|
+
assertNotEmpty(startMarker)
|
|
391
|
+
assertNotEmpty(endMarker)
|
|
392
|
+
const substrings = []
|
|
393
|
+
let start = 0
|
|
394
|
+
while (true) {
|
|
395
|
+
const index = str.indexOf(startMarker, start)
|
|
396
|
+
if (index === -1) {
|
|
397
|
+
break
|
|
398
|
+
}
|
|
399
|
+
const endIndex = str.indexOf(endMarker, index + startMarker.length)
|
|
400
|
+
if (endIndex === -1) {
|
|
401
|
+
break
|
|
402
|
+
}
|
|
403
|
+
substrings.push(str.substring(index + startMarker.length, endIndex))
|
|
404
|
+
start = endIndex + endMarker.length
|
|
405
|
+
}
|
|
406
|
+
return substrings
|
|
407
|
+
}
|
|
408
|
+
const StringUtils = {
|
|
409
|
+
isEmpty,
|
|
410
|
+
assertNotEmpty,
|
|
411
|
+
isBlank,
|
|
412
|
+
assertNotBlank,
|
|
413
|
+
capitalize,
|
|
414
|
+
decapitalize,
|
|
415
|
+
splitWithFixedLength,
|
|
416
|
+
split,
|
|
417
|
+
findMarkerPositions,
|
|
418
|
+
findMarkerPositionsRegex,
|
|
419
|
+
substringBefore,
|
|
420
|
+
substringBeforeLast,
|
|
421
|
+
substringAfter,
|
|
422
|
+
substringAfterLast,
|
|
423
|
+
substringBetween,
|
|
424
|
+
substringBetweenGreedy,
|
|
425
|
+
substringsBetween
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
module.exports = StringUtils
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
/**
|
|
3
|
+
* @module TypeAssert
|
|
4
|
+
* @description Type assertion utility functions for validating data types and throwing errors for invalid types.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// 3rd
|
|
8
|
+
|
|
9
|
+
// internal
|
|
10
|
+
|
|
11
|
+
// owned
|
|
12
|
+
const { isString, isNumber, isBoolean, isObject, isPlainObject, isPromise, isSymbol, isFunction, isInstance, isNil, isNull, isUndefined, isPositive, isNegative } = require('./type-utils')
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* if value is not Array, throw error
|
|
16
|
+
* @param {*} value
|
|
17
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
18
|
+
* @returns {void}
|
|
19
|
+
* @throws {Error}
|
|
20
|
+
*/
|
|
21
|
+
function assertArray (value, paramName) {
|
|
22
|
+
if (!Array.isArray(value)) {
|
|
23
|
+
throw new Error(`${paramName ? paramName + '' : ' '}Not Array: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* if value is not a string, throw error
|
|
28
|
+
* @param {*} value
|
|
29
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
30
|
+
* @returns {void}
|
|
31
|
+
* @throws {Error}
|
|
32
|
+
*/
|
|
33
|
+
function assertString (value, paramName) {
|
|
34
|
+
if (!isString(value)) {
|
|
35
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not String: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* if value is not a Number, throw error
|
|
40
|
+
* @param {*} value
|
|
41
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
42
|
+
* @returns {void}
|
|
43
|
+
* @throws {Error}
|
|
44
|
+
*/
|
|
45
|
+
function assertNumber (value, paramName) {
|
|
46
|
+
if (!isNumber(value)) {
|
|
47
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Number: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Asserts that a value is a positive number.
|
|
53
|
+
* @param {number} value - The value to check.
|
|
54
|
+
* @param {string} [paramName] - Optional name of the parameter for error message.
|
|
55
|
+
* @throws {Error} If the value is not a number or is less than or equal to zero.
|
|
56
|
+
*/
|
|
57
|
+
function assertPositive (value, paramName) {
|
|
58
|
+
if (!isPositive(value)) {
|
|
59
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Positive: ${value}`)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Asserts that a value is a Negative number.
|
|
65
|
+
* @param {number} value - The value to check.
|
|
66
|
+
* @param {string} [paramName] - Optional name of the parameter for error message.
|
|
67
|
+
* @throws {Error} If the value is not a number or is less than or equal to zero.
|
|
68
|
+
*/
|
|
69
|
+
function assertNegative (value, paramName) {
|
|
70
|
+
if (!isNegative(value)) {
|
|
71
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Negative: ${value}`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* if value is not a string, throw error
|
|
77
|
+
* @param {*} value
|
|
78
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
79
|
+
* @returns {void}
|
|
80
|
+
* @throws {Error}
|
|
81
|
+
*/
|
|
82
|
+
function assertBoolean (value, paramName) {
|
|
83
|
+
if (!isBoolean(value)) {
|
|
84
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Boolean: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* if value is not a Object, throw error
|
|
89
|
+
* @param {*} value
|
|
90
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
91
|
+
* @returns {void}
|
|
92
|
+
* @throws {Error}
|
|
93
|
+
*/
|
|
94
|
+
function assertObject (value, paramName) {
|
|
95
|
+
if (!isObject(value)) {
|
|
96
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Object: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* if value is not a PlainObject, throw error
|
|
101
|
+
* @param {*} value
|
|
102
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
103
|
+
* @returns {void}
|
|
104
|
+
* @throws {Error}
|
|
105
|
+
*/
|
|
106
|
+
function assertPlainObject (value, paramName) {
|
|
107
|
+
if (!isPlainObject(value)) {
|
|
108
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not PlainObject: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* if value is not a Symbol, throw error
|
|
113
|
+
* @param {*} value
|
|
114
|
+
* @param {string} [paramName] - The name of the parameter to check@param {string} [paramName] - The name of the parameter to check
|
|
115
|
+
* @returns {void}
|
|
116
|
+
* @throws {Error}
|
|
117
|
+
*/
|
|
118
|
+
function assertSymbol (value, paramName) {
|
|
119
|
+
if (!isSymbol(value)) {
|
|
120
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Symbol: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* if value is not a Function, throw error
|
|
125
|
+
* @param {*} value
|
|
126
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
127
|
+
* @returns {void}
|
|
128
|
+
* @throws {Error}
|
|
129
|
+
*/
|
|
130
|
+
function assertFunction (value, paramName) {
|
|
131
|
+
if (!isFunction(value)) {
|
|
132
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Function: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* if value is not a Class instance, throw error
|
|
137
|
+
* @param {*} value
|
|
138
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
139
|
+
* @returns {void}
|
|
140
|
+
* @throws {Error}
|
|
141
|
+
*/
|
|
142
|
+
function assertInstance (value, paramName) {
|
|
143
|
+
if (!isInstance(value)) {
|
|
144
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Class Instance: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* if value is not a string, throw error
|
|
149
|
+
* @param {*} value
|
|
150
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
151
|
+
* @returns {void}
|
|
152
|
+
* @throws {Error}
|
|
153
|
+
*/
|
|
154
|
+
function assertPromise (value, paramName) {
|
|
155
|
+
if (!isPromise(value)) {
|
|
156
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Promise: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* if value is not a Null or Undefined, throw error
|
|
161
|
+
* @param {*} value
|
|
162
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
163
|
+
* @returns {void}
|
|
164
|
+
* @throws {Error}
|
|
165
|
+
*/
|
|
166
|
+
function assertNil (value, paramName) {
|
|
167
|
+
if (!isNil(value)) {
|
|
168
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Neither Null nor Undefined: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Asserts that the given value is not nil.
|
|
174
|
+
* @param {*} value - The value to check
|
|
175
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
176
|
+
* @throws {Error} Throws an error if the value is nil
|
|
177
|
+
*/
|
|
178
|
+
function assertNotNil (value, paramName) {
|
|
179
|
+
if (isNil(value)) {
|
|
180
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Should Not Nil`)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* if value is not a Null, throw error
|
|
186
|
+
* @param {*} value
|
|
187
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
188
|
+
* @returns {void}
|
|
189
|
+
* @throws {Error}
|
|
190
|
+
*/
|
|
191
|
+
function assertNull (value, paramName) {
|
|
192
|
+
if (!isNull(value)) {
|
|
193
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Null: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Asserts that the given value is not null.
|
|
199
|
+
* @param {*} value - The value to check
|
|
200
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
201
|
+
* @throws {Error} Throws an error if the value is null
|
|
202
|
+
*/
|
|
203
|
+
function assertNotNull (value, paramName) {
|
|
204
|
+
if (isNull(value)) {
|
|
205
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Should Not Null`)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* if value is not a Undefined, throw error
|
|
210
|
+
* @param {*} value
|
|
211
|
+
* @param {string} [paramName] - The name of the parameter to check
|
|
212
|
+
* @returns {void}
|
|
213
|
+
* @throws {Error}
|
|
214
|
+
*/
|
|
215
|
+
function assertUndefined (value, paramName) {
|
|
216
|
+
if (!isUndefined(value)) {
|
|
217
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not Undefined: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Asserts that the given value is either a string or a symbol.
|
|
223
|
+
* @param {*} value - The value to check.
|
|
224
|
+
* @param {string} [paramName] - Optional parameter name for error message.
|
|
225
|
+
* @throws {Error} Throws an error if the value is not a string or symbol.
|
|
226
|
+
*/
|
|
227
|
+
function assertStringOrSymbol (value, paramName) {
|
|
228
|
+
if (!isString(value) && !isSymbol(value)) {
|
|
229
|
+
throw new Error(`${paramName ? '"' + paramName + '" ' : ' '}Not String or Symbol: type=${typeof value} value=${JSON.stringify(value)}`)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const TypeAssert = {
|
|
233
|
+
assertNumber,
|
|
234
|
+
assertPositive,
|
|
235
|
+
assertNegative,
|
|
236
|
+
assertBoolean,
|
|
237
|
+
assertObject,
|
|
238
|
+
assertPlainObject,
|
|
239
|
+
assertSymbol,
|
|
240
|
+
assertFunction,
|
|
241
|
+
assertInstance,
|
|
242
|
+
assertPromise,
|
|
243
|
+
assertNil,
|
|
244
|
+
assertNotNil,
|
|
245
|
+
assertNull,
|
|
246
|
+
assertNotNull,
|
|
247
|
+
assertUndefined,
|
|
248
|
+
assertString,
|
|
249
|
+
assertArray,
|
|
250
|
+
assertStringOrSymbol
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
module.exports = TypeAssert
|