binhend 2.1.16 → 2.1.17

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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.1.16",
3
+ "version": "2.1.17",
4
4
  "description": "",
5
5
  "main": "index.js",
6
+ "types": "src/types/index.d.ts",
6
7
  "author": "Nguyen Duc Binh",
7
8
  "license": "UNLICENSED",
8
9
  "bin": {
@@ -0,0 +1,10 @@
1
+
2
+ /**
3
+ * @typedef {Object<string, any>} ObjectType
4
+ */
5
+
6
+ /**
7
+ * @typedef {(Date|string|number)} DateType
8
+ */
9
+
10
+ export {};
@@ -0,0 +1,2 @@
1
+
2
+ export * from './common.d.ts';
@@ -7,6 +7,10 @@ function isObject(input) {
7
7
  return typeOf(input) === 'Object';
8
8
  }
9
9
 
10
+ function mustObject(input, defaultValue = {}) {
11
+ return isObject(input) ? input : isObject(defaultValue) ? defaultValue : {};
12
+ }
13
+
10
14
  function isEmptyObject(input) {
11
15
  return isObject(input) && Object.keys(input).length === 0;
12
16
  }
@@ -17,8 +21,8 @@ function isEmptyArray(input) {
17
21
  return isArray(input) && input.length === 0;
18
22
  }
19
23
 
20
- function mustArray(input, defaultOutput = []) {
21
- return isArray(input) ? input : isArray(defaultOutput) ? defaultOutput : [defaultOutput];
24
+ function mustArray(input, defaultValue = []) {
25
+ return isArray(input) ? input : isArray(defaultValue) ? defaultValue : [];
22
26
  }
23
27
 
24
28
  function isFunction(input) {
@@ -86,6 +90,7 @@ module.exports = {
86
90
  typeOf,
87
91
  isObject,
88
92
  isEmptyObject,
93
+ mustObject,
89
94
  isArray,
90
95
  isEmptyArray,
91
96
  mustArray,
@@ -71,6 +71,18 @@ function Number(input, options = {}) {
71
71
  return Validator(input, types.isNumber, { ...options, type: 'number' });
72
72
  }
73
73
 
74
+ /**
75
+ * Validate an input being an integer
76
+ *
77
+ * @param {*} input - Input value needs to be validated.
78
+ * @param {Options} options - Additional options for validation.
79
+ * @returns {number} - The validated input
80
+ * @throws {HttpError} - If validation fails
81
+ */
82
+ function Integer(input, options = {}) {
83
+ return Validator(input, global.Number.isInteger, { ...options, type: 'integer' });
84
+ }
85
+
74
86
  /**
75
87
  * Validate an input being boolean
76
88
  *
@@ -136,13 +148,38 @@ function Date(input, options = {}) {
136
148
  *
137
149
  * @param {*} input - Input value needs to be validated.
138
150
  * @param {Options} options - Additional options for validation.
139
- * @returns {*} - The validated input
151
+ * @returns {Date|string|number} - The validated input
140
152
  * @throws {HttpError} - If validation fails
141
153
  */
142
154
  function DateLike(input, options = {}) {
143
155
  return Validator(input, types.isDateLike, { ...options, type: 'date-like' });
144
156
  }
145
157
 
158
+ /**
159
+ * Validate an input being one of the specified enum values
160
+ *
161
+ * @param {*} input - Input value needs to be validated.
162
+ * @param {Array|Object} enumValues - Array of allowed values or object whose keys are allowed values
163
+ * @param {Options} options - Additional options for validation.
164
+ * @returns {*} - The validated input
165
+ * @throws {HttpError} - If validation fails
166
+ */
167
+ function Enum(input, enumValues, options = {}) {
168
+ var validArray = types.isArray(enumValues),
169
+ validObject = types.isObject(enumValues);
170
+
171
+ if (!(validArray || validObject)) {
172
+ throwError('Enum validator requires an array or object of allowed values');
173
+ }
174
+
175
+ var method = validArray ? 'includes' : 'hasOwnProperty';
176
+
177
+ return Validator(
178
+ input, (input) => enumValues[method](input),
179
+ { ...options, message: options.message || `${input} is not defined in enum` }
180
+ );
181
+ }
182
+
146
183
  /**
147
184
  * Validate an input being defined (not undefined)
148
185
  *
@@ -219,12 +256,14 @@ function Validator(input, validate, options = {}) {
219
256
  module.exports = {
220
257
  String,
221
258
  Number,
259
+ Integer,
222
260
  Boolean,
223
261
  Array,
224
262
  Object,
225
263
  Function,
226
264
  Date,
227
265
  DateLike,
266
+ Enum,
228
267
  Defined,
229
268
  NotNull,
230
269
  NotNullish,