@creejs/commons-lang 2.1.23 → 2.1.25

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.
@@ -2,7 +2,40 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ /**
6
+ * @typedef {{
7
+ * type: string,
8
+ * message: string,
9
+ * _code: number,
10
+ * }} _ErrorLike
11
+ */
12
+
13
+ // module vars
14
+ const ErrorCode = {
15
+ NOT_FOUND: 404,
16
+ NOT_SUPPORTED: 505,
17
+ NOT_IMPL: 501
18
+ };
19
+ /**
20
+ * @class _Error
21
+ */
5
22
  class _Error extends Error {
23
+ static get Code () {
24
+ return ErrorCode
25
+ }
26
+
27
+ /**
28
+ * Checks if the given value is an _ErrorLike object
29
+ * @param {{[key:string]:any}} err - The value to checke
30
+ * @returns {boolean} True if the value is error-like
31
+ */
32
+ static isErrorLike (err) {
33
+ if (err == null) {
34
+ return false
35
+ }
36
+ return err._type === '_' && typeof err.code === 'number'
37
+ }
38
+
6
39
  /**
7
40
  * Creates and returns a 404 Not Found error instance with the given message.
8
41
  * @param {string} message - The error message to include.
@@ -42,6 +75,7 @@ class _Error extends Error {
42
75
  */
43
76
  constructor (message, code) {
44
77
  super(message);
78
+ this._type = '_'; // use this to identify if it's _Error
45
79
  this.code = code;
46
80
  }
47
81
  }
@@ -128,14 +162,14 @@ function constructorName (value) {
128
162
 
129
163
  /**
130
164
  * Assigns default values from source objects to target object for undefined properties.
131
- * @param {Object<string, any>} target - The target object to assign defaults to
132
- * @param {...Object<string, any>} sources - Source objects containing default values
133
- * @returns {Object<string, any>} The modified target object with defaults applied
134
- * @throws {TypeError} If target is null or undefined
165
+ * 1. No Deep-Copy, only first level properties are assigned.
166
+ * @param {{[key:string]:any}} target - The target object to assign defaults to
167
+ * @param {...{[key:string]:any}|undefined} sources - Source objects containing default values
168
+ * @returns {{[key:string]:any}} The modified target object with defaults applied
135
169
  */
136
170
  function defaults (target, ...sources) {
137
171
  if (target == null) {
138
- throw new TypeError('"target" must not be null or undefined')
172
+ throw new TypeError('"target" Should Not Nil')
139
173
  }
140
174
  for (const source of sources) {
141
175
  if (source == null) {
@@ -1115,11 +1149,13 @@ function isEmpty (str) {
1115
1149
  /**
1116
1150
  * Asserts that the given string is not empty.
1117
1151
  * @param {string} str - The string to check.
1152
+ * @param {string} [paramName]
1153
+ *
1118
1154
  * @throws {Error} Throws an error if the string is empty.
1119
1155
  */
1120
- function assertNotEmpty (str) {
1156
+ function assertNotEmpty (str, paramName) {
1121
1157
  if (isEmpty(str)) {
1122
- throw new Error(`Empty String: ${str}`)
1158
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}IsEmpty String: ${str}`)
1123
1159
  }
1124
1160
  }
1125
1161
 
@@ -1140,11 +1176,12 @@ function isBlank (str) {
1140
1176
  /**
1141
1177
  * Asserts that the given string is not blank.
1142
1178
  * @param {string} str - The string to check.
1179
+ * @param {string} [paramName]
1143
1180
  * @throws {Error} Throws an error if the string is blank.
1144
1181
  */
1145
- function assertNotBlank (str) {
1182
+ function assertNotBlank (str, paramName) {
1146
1183
  if (isBlank(str)) {
1147
- throw new Error(`Blank String: ${str}`)
1184
+ throw new Error(`${paramName ? '"' + paramName + '" ' : ''}Is Blank: ${str}`)
1148
1185
  }
1149
1186
  }
1150
1187