vest-utils 0.0.3-rc → 0.0.4-dev-afe5de
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/dist/cjs/vest-utils.development.js +64 -24
- package/dist/cjs/vest-utils.production.js +1 -1
- package/dist/es/vest-utils.development.js +45 -24
- package/dist/es/vest-utils.production.js +1 -1
- package/dist/umd/vest-utils.development.js +64 -24
- package/dist/umd/vest-utils.production.js +1 -1
- package/package.json +1 -1
- package/src/__tests__/bus.test.ts +10 -10
- package/src/__tests__/greaterThan.test.ts +59 -0
- package/src/__tests__/isArray.test.ts +15 -0
- package/src/__tests__/isNull.test.ts +25 -0
- package/src/__tests__/isNumeric.test.ts +26 -0
- package/src/__tests__/isUndefined.test.ts +26 -0
- package/src/__tests__/lengthEquals.test.ts +56 -0
- package/src/__tests__/longerThan.test.ts +56 -0
- package/src/__tests__/numberEquals.test.ts +59 -0
- package/src/greaterThan.ts +8 -0
- package/src/isArrayValue.ts +11 -0
- package/src/isEmpty.ts +17 -0
- package/src/isNull.ts +7 -0
- package/src/isNullish.ts +1 -2
- package/src/isNumeric.ts +11 -0
- package/src/isPositive.ts +5 -0
- package/src/isUndefined.ts +7 -0
- package/src/lengthEquals.ts +11 -0
- package/src/longerThan.ts +8 -0
- package/src/nestedArray.ts +2 -3
- package/src/numberEquals.ts +11 -0
- package/src/vest-utils.ts +12 -2
- package/types/vest-utils.d.ts +25 -5
- package/tsconfig.json +0 -6
|
@@ -2,20 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
function bindNot(fn) {
|
|
6
|
+
return function () {
|
|
7
|
+
var args = [];
|
|
8
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
9
|
+
args[_i] = arguments[_i];
|
|
10
|
+
}
|
|
11
|
+
return !fn.apply(void 0, args);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
5
15
|
function isNumeric(value) {
|
|
6
16
|
var str = String(value);
|
|
7
17
|
var num = Number(value);
|
|
8
18
|
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
9
19
|
return Boolean(result);
|
|
10
20
|
}
|
|
21
|
+
var isNotNumeric = bindNot(isNumeric);
|
|
11
22
|
|
|
12
23
|
function numberEquals(value, eq) {
|
|
13
24
|
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
14
25
|
}
|
|
26
|
+
var numberNotEquals = bindNot(numberEquals);
|
|
15
27
|
|
|
16
28
|
function lengthEquals(value, arg1) {
|
|
17
29
|
return numberEquals(value.length, arg1);
|
|
18
30
|
}
|
|
31
|
+
var lengthNotEquals = bindNot(lengthEquals);
|
|
19
32
|
|
|
20
33
|
function greaterThan(value, gt) {
|
|
21
34
|
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
@@ -70,25 +83,13 @@ var isNotNull = bindNot(isNull);
|
|
|
70
83
|
function isUndefined(value) {
|
|
71
84
|
return value === undefined;
|
|
72
85
|
}
|
|
86
|
+
var isNotUndefined = bindNot(isUndefined);
|
|
73
87
|
|
|
74
88
|
function isNullish(value) {
|
|
75
89
|
return isNull(value) || isUndefined(value);
|
|
76
90
|
}
|
|
77
91
|
var isNotNullish = bindNot(isNullish);
|
|
78
92
|
|
|
79
|
-
var isNullish$1 = /*#__PURE__*/Object.freeze({
|
|
80
|
-
__proto__: null,
|
|
81
|
-
isNullish: isNullish,
|
|
82
|
-
isNotNullish: isNotNullish
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// The module is named "isArrayValue" since it
|
|
86
|
-
// is conflicting with a nested npm dependency.
|
|
87
|
-
// We may need to revisit this in the future.
|
|
88
|
-
function isArray(value) {
|
|
89
|
-
return Boolean(Array.isArray(value));
|
|
90
|
-
}
|
|
91
|
-
|
|
92
93
|
function asArray(possibleArg) {
|
|
93
94
|
return [].concat(possibleArg);
|
|
94
95
|
}
|
|
@@ -110,6 +111,14 @@ function defaultTo(value, defaultValue) {
|
|
|
110
111
|
return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
|
|
111
112
|
}
|
|
112
113
|
|
|
114
|
+
// The module is named "isArrayValue" since it
|
|
115
|
+
// is conflicting with a nested npm dependency.
|
|
116
|
+
// We may need to revisit this in the future.
|
|
117
|
+
function isArray(value) {
|
|
118
|
+
return Boolean(Array.isArray(value));
|
|
119
|
+
}
|
|
120
|
+
var isNotArray = bindNot(isArray);
|
|
121
|
+
|
|
113
122
|
function last(values) {
|
|
114
123
|
var valuesArray = asArray(values);
|
|
115
124
|
return valuesArray[valuesArray.length - 1];
|
|
@@ -199,6 +208,10 @@ message) {
|
|
|
199
208
|
? message.valueOf()
|
|
200
209
|
: new Error(message ? optionalFunctionValue(message) : message);
|
|
201
210
|
}
|
|
211
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
212
|
+
function StringObject(value) {
|
|
213
|
+
return new String(optionalFunctionValue(value));
|
|
214
|
+
}
|
|
202
215
|
|
|
203
216
|
function isStringValue(v) {
|
|
204
217
|
return String(v) === v;
|
|
@@ -211,16 +224,6 @@ function partition(array, predicate) {
|
|
|
211
224
|
}, [[], []]);
|
|
212
225
|
}
|
|
213
226
|
|
|
214
|
-
function bindNot(fn) {
|
|
215
|
-
return function () {
|
|
216
|
-
var args = [];
|
|
217
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
218
|
-
args[_i] = arguments[_i];
|
|
219
|
-
}
|
|
220
|
-
return !fn.apply(void 0, args);
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
227
|
function either(a, b) {
|
|
225
228
|
return !!a !== !!b;
|
|
226
229
|
}
|
|
@@ -286,6 +289,25 @@ function mapFirst(array, callback) {
|
|
|
286
289
|
}
|
|
287
290
|
}
|
|
288
291
|
|
|
292
|
+
function isEmpty(value) {
|
|
293
|
+
if (!value) {
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
else if (hasOwnProperty(value, 'length')) {
|
|
297
|
+
return lengthEquals(value, 0);
|
|
298
|
+
}
|
|
299
|
+
else if (typeof value === 'object') {
|
|
300
|
+
return lengthEquals(Object.keys(value), 0);
|
|
301
|
+
}
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
var isNotEmpty = bindNot(isEmpty);
|
|
305
|
+
|
|
306
|
+
function isPositive(value) {
|
|
307
|
+
return greaterThan(value, 0);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
exports.StringObject = StringObject;
|
|
289
311
|
exports.asArray = asArray;
|
|
290
312
|
exports.assign = assign;
|
|
291
313
|
exports.bindNot = bindNot;
|
|
@@ -296,15 +318,33 @@ exports.defaultTo = defaultTo;
|
|
|
296
318
|
exports.deferThrow = deferThrow;
|
|
297
319
|
exports.either = either;
|
|
298
320
|
exports.genId = genId;
|
|
321
|
+
exports.greaterThan = greaterThan;
|
|
299
322
|
exports.hasOwnProperty = hasOwnProperty;
|
|
300
323
|
exports.invariant = invariant;
|
|
324
|
+
exports.isArray = isArray;
|
|
301
325
|
exports.isBoolean = isBoolean;
|
|
326
|
+
exports.isEmpty = isEmpty;
|
|
302
327
|
exports.isFunction = isFunction;
|
|
303
|
-
exports.
|
|
328
|
+
exports.isNotArray = isNotArray;
|
|
329
|
+
exports.isNotEmpty = isNotEmpty;
|
|
330
|
+
exports.isNotNull = isNotNull;
|
|
331
|
+
exports.isNotNullish = isNotNullish;
|
|
332
|
+
exports.isNotNumeric = isNotNumeric;
|
|
333
|
+
exports.isNotUndefined = isNotUndefined;
|
|
334
|
+
exports.isNull = isNull;
|
|
335
|
+
exports.isNullish = isNullish;
|
|
336
|
+
exports.isNumeric = isNumeric;
|
|
337
|
+
exports.isPositive = isPositive;
|
|
304
338
|
exports.isPromise = isPromise;
|
|
305
339
|
exports.isStringValue = isStringValue;
|
|
340
|
+
exports.isUndefined = isUndefined;
|
|
306
341
|
exports.last = last;
|
|
342
|
+
exports.lengthEquals = lengthEquals;
|
|
343
|
+
exports.lengthNotEquals = lengthNotEquals;
|
|
344
|
+
exports.longerThan = longerThan;
|
|
307
345
|
exports.mapFirst = mapFirst;
|
|
308
346
|
exports.nestedArray = nestedArray;
|
|
347
|
+
exports.numberEquals = numberEquals;
|
|
348
|
+
exports.numberNotEquals = numberNotEquals;
|
|
309
349
|
exports.optionalFunctionValue = optionalFunctionValue;
|
|
310
350
|
exports.partition = partition;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";function
|
|
1
|
+
"use strict";function t(t){return function(){for(var r=[],n=0;n<arguments.length;n++)r[n]=arguments[n];return!t.apply(void 0,r)}}function r(t){var r=Number(t);return!(isNaN(parseFloat(String(t)))||isNaN(Number(t))||!isFinite(r))}Object.defineProperty(exports,"__esModule",{value:!0});var n=t(r);function e(t,n){return r(t)&&r(n)&&Number(t)===Number(n)}var o=t(e);function u(t,r){return e(t.length,r)}var i=t(u);function s(t,n){return r(t)&&r(n)&&Number(t)>Number(n)}function c(t,r){return s(t.length,r)}function a(t){return null===t}var f=t(a);function p(t){return void 0===t}var l=t(p);function x(t){return a(t)||p(t)}var v=t(x);function h(t){return[].concat(t)}function g(t){return"function"==typeof t}function N(t){for(var r=[],n=1;n<arguments.length;n++)r[n-1]=arguments[n];return g(t)?t.apply(void 0,r):t}function d(t,r){var n;return null!==(n=N(t))&&void 0!==n?n:N(r)}function b(t){return!!Array.isArray(t)}var y=t(b);function m(t){return(t=h(t))[t.length-1]}function E(t,r){var n=0;for(r=r.slice(0,-1);n<r.length;n++){var e=r[n];t[e]=d(t[e],[]),t=t[e]}return t}var O=Object.freeze({__proto__:null,transform:function t(r,n){for(var e=[],o=0;o<r.length;o++){var u=r[o];b(u)?e.push(t(u,n)):(u=n(u),f(u)&&e.push(u))}return e},valueAtPath:function(t,r){return E(t,r)[m(r)]},setValueAtPath:function(t,r,n){return E(t,r)[m(r)]=n,t},flatten:function t(r){return h(r).reduce((function(r,n){return b(n)?r.concat(t(n)):h(r).concat(n)}),[])},getCurrent:E});function _(t,r){return Object.prototype.hasOwnProperty.call(t,r)}var j=Object.assign;var A,P=Object.freeze({__proto__:null,createBus:function(){var t={};return{emit:function(r,n){(t[r]||[]).forEach((function(t){t(n)}))},on:function(r,n){return t[r]=(t[r]||[]).concat(n),{off:function(){t[r]=(t[r]||[]).filter((function(t){return t!==n}))}}}}}}),w=(A=0,function(){return"".concat(A++)});function S(t){return!t||(_(t,"length")?u(t,0):"object"==typeof t&&u(Object.keys(t),0))}var F=t(S);exports.StringObject=function(t){return new String(N(t))},exports.asArray=h,exports.assign=j,exports.bindNot=t,exports.bus=P,exports.cache=function(t){function r(t){return n.findIndex((function(r){var n=r[0];return u(t,n.length)&&t.every((function(t,r){return t===n[r]}))}))}void 0===t&&(t=1);var n=[],e=function(r,o){var u=e.get(r);return u?u[1]:(o=o(),n.unshift([r.concat(),o]),c(n,t)&&(n.length=t),o)};return e.invalidate=function(t){-1<(t=r(t))&&n.splice(t,1)},e.get=function(t){return n[r(t)]||null},e},exports.callEach=function(t){return t.forEach((function(t){return t()}))},exports.defaultTo=d,exports.deferThrow=function(t){setTimeout((function(){throw Error(t)}),0)},exports.either=function(t,r){return!!t!=!!r},exports.genId=w,exports.greaterThan=s,exports.hasOwnProperty=_,exports.invariant=function(t,r){if(!t)throw r instanceof String?r.valueOf():Error(r?N(r):r)},exports.isArray=b,exports.isBoolean=function(t){return!!t===t},exports.isEmpty=S,exports.isFunction=g,exports.isNotArray=y,exports.isNotEmpty=F,exports.isNotNull=f,exports.isNotNullish=v,exports.isNotNumeric=n,exports.isNotUndefined=l,exports.isNull=a,exports.isNullish=x,exports.isNumeric=r,exports.isPositive=function(t){return s(t,0)},exports.isPromise=function(t){return t&&g(t.then)},exports.isStringValue=function(t){return String(t)===t},exports.isUndefined=p,exports.last=m,exports.lengthEquals=u,exports.lengthNotEquals=i,exports.longerThan=c,exports.mapFirst=function(t,r){function n(t,r){t&&(e=!0,o=r)}for(var e=!1,o=null,u=0;u<t.length;u++)if(r(t[u],n,u),e)return o},exports.nestedArray=O,exports.numberEquals=e,exports.numberNotEquals=o,exports.optionalFunctionValue=N,exports.partition=function(t,r){return t.reduce((function(n,e,o){return n[r(e,o,t)?0:1].push(e),n}),[[],[]])};
|
|
@@ -1,17 +1,30 @@
|
|
|
1
|
+
function bindNot(fn) {
|
|
2
|
+
return function () {
|
|
3
|
+
var args = [];
|
|
4
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
5
|
+
args[_i] = arguments[_i];
|
|
6
|
+
}
|
|
7
|
+
return !fn.apply(void 0, args);
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
1
11
|
function isNumeric(value) {
|
|
2
12
|
var str = String(value);
|
|
3
13
|
var num = Number(value);
|
|
4
14
|
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
5
15
|
return Boolean(result);
|
|
6
16
|
}
|
|
17
|
+
var isNotNumeric = bindNot(isNumeric);
|
|
7
18
|
|
|
8
19
|
function numberEquals(value, eq) {
|
|
9
20
|
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
10
21
|
}
|
|
22
|
+
var numberNotEquals = bindNot(numberEquals);
|
|
11
23
|
|
|
12
24
|
function lengthEquals(value, arg1) {
|
|
13
25
|
return numberEquals(value.length, arg1);
|
|
14
26
|
}
|
|
27
|
+
var lengthNotEquals = bindNot(lengthEquals);
|
|
15
28
|
|
|
16
29
|
function greaterThan(value, gt) {
|
|
17
30
|
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
@@ -66,25 +79,13 @@ var isNotNull = bindNot(isNull);
|
|
|
66
79
|
function isUndefined(value) {
|
|
67
80
|
return value === undefined;
|
|
68
81
|
}
|
|
82
|
+
var isNotUndefined = bindNot(isUndefined);
|
|
69
83
|
|
|
70
84
|
function isNullish(value) {
|
|
71
85
|
return isNull(value) || isUndefined(value);
|
|
72
86
|
}
|
|
73
87
|
var isNotNullish = bindNot(isNullish);
|
|
74
88
|
|
|
75
|
-
var isNullish$1 = /*#__PURE__*/Object.freeze({
|
|
76
|
-
__proto__: null,
|
|
77
|
-
isNullish: isNullish,
|
|
78
|
-
isNotNullish: isNotNullish
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
// The module is named "isArrayValue" since it
|
|
82
|
-
// is conflicting with a nested npm dependency.
|
|
83
|
-
// We may need to revisit this in the future.
|
|
84
|
-
function isArray(value) {
|
|
85
|
-
return Boolean(Array.isArray(value));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
89
|
function asArray(possibleArg) {
|
|
89
90
|
return [].concat(possibleArg);
|
|
90
91
|
}
|
|
@@ -106,6 +107,14 @@ function defaultTo(value, defaultValue) {
|
|
|
106
107
|
return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
|
|
107
108
|
}
|
|
108
109
|
|
|
110
|
+
// The module is named "isArrayValue" since it
|
|
111
|
+
// is conflicting with a nested npm dependency.
|
|
112
|
+
// We may need to revisit this in the future.
|
|
113
|
+
function isArray(value) {
|
|
114
|
+
return Boolean(Array.isArray(value));
|
|
115
|
+
}
|
|
116
|
+
var isNotArray = bindNot(isArray);
|
|
117
|
+
|
|
109
118
|
function last(values) {
|
|
110
119
|
var valuesArray = asArray(values);
|
|
111
120
|
return valuesArray[valuesArray.length - 1];
|
|
@@ -195,6 +204,10 @@ message) {
|
|
|
195
204
|
? message.valueOf()
|
|
196
205
|
: new Error(message ? optionalFunctionValue(message) : message);
|
|
197
206
|
}
|
|
207
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
208
|
+
function StringObject(value) {
|
|
209
|
+
return new String(optionalFunctionValue(value));
|
|
210
|
+
}
|
|
198
211
|
|
|
199
212
|
function isStringValue(v) {
|
|
200
213
|
return String(v) === v;
|
|
@@ -207,16 +220,6 @@ function partition(array, predicate) {
|
|
|
207
220
|
}, [[], []]);
|
|
208
221
|
}
|
|
209
222
|
|
|
210
|
-
function bindNot(fn) {
|
|
211
|
-
return function () {
|
|
212
|
-
var args = [];
|
|
213
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
214
|
-
args[_i] = arguments[_i];
|
|
215
|
-
}
|
|
216
|
-
return !fn.apply(void 0, args);
|
|
217
|
-
};
|
|
218
|
-
}
|
|
219
|
-
|
|
220
223
|
function either(a, b) {
|
|
221
224
|
return !!a !== !!b;
|
|
222
225
|
}
|
|
@@ -282,4 +285,22 @@ function mapFirst(array, callback) {
|
|
|
282
285
|
}
|
|
283
286
|
}
|
|
284
287
|
|
|
285
|
-
|
|
288
|
+
function isEmpty(value) {
|
|
289
|
+
if (!value) {
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
else if (hasOwnProperty(value, 'length')) {
|
|
293
|
+
return lengthEquals(value, 0);
|
|
294
|
+
}
|
|
295
|
+
else if (typeof value === 'object') {
|
|
296
|
+
return lengthEquals(Object.keys(value), 0);
|
|
297
|
+
}
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
var isNotEmpty = bindNot(isEmpty);
|
|
301
|
+
|
|
302
|
+
function isPositive(value) {
|
|
303
|
+
return greaterThan(value, 0);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export { StringObject, asArray, assign, bindNot, bus, createCache as cache, callEach, defaultTo, deferThrow, either, genId, greaterThan, hasOwnProperty, invariant, isArray, isBoolean, isEmpty, isFunction, isNotArray, isNotEmpty, isNotNull, isNotNullish, isNotNumeric, isNotUndefined, isNull, isNullish, isNumeric, isPositive, isPromise, isStringValue, isUndefined, last, lengthEquals, lengthNotEquals, longerThan, mapFirst, nestedArray, numberEquals, numberNotEquals, optionalFunctionValue, partition };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function n(n){var r=Number(n);return!(isNaN(parseFloat(String(n)))||isNaN(Number(n))||!isFinite(
|
|
1
|
+
function n(n){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return!n.apply(void 0,t)}}function t(n){var t=Number(n);return!(isNaN(parseFloat(String(n)))||isNaN(Number(n))||!isFinite(t))}var r=n(t);function u(n,r){return t(n)&&t(r)&&Number(n)===Number(r)}var e=n(u);function o(n,t){return u(n.length,t)}var i=n(o);function a(n,r){return t(n)&&t(r)&&Number(n)>Number(r)}function c(n,t){return a(n.length,t)}function f(n){return null===n}var s=n(f);function l(n){return void 0===n}var v=n(l);function h(n){return null===n||l(n)}var g=n(h);function N(n){return[].concat(n)}function p(n){return"function"==typeof n}function d(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return p(n)?n.apply(void 0,t):n}function b(n,t){var r;return null!==(r=d(n))&&void 0!==r?r:d(t)}function y(n){return!!Array.isArray(n)}var m=n(y);function E(n){return(n=N(n))[n.length-1]}function A(n,t){var r=0;for(t=t.slice(0,-1);r<t.length;r++){var u=t[r];n[u]=b(n[u],[]),n=n[u]}return n}var _=Object.freeze({__proto__:null,transform:function n(t,r){for(var u=[],e=0;e<t.length;e++){var o=t[e];y(o)?u.push(n(o,r)):(o=r(o),s(o)&&u.push(o))}return u},valueAtPath:function(n,t){return A(n,t)[E(t)]},setValueAtPath:function(n,t,r){return A(n,t)[E(t)]=r,n},flatten:function n(t){return N(t).reduce((function(t,r){return y(r)?t.concat(n(r)):N(t).concat(r)}),[])},getCurrent:A});function O(n,t){return Object.prototype.hasOwnProperty.call(n,t)}var j,q=Object.assign,w=Object.freeze({__proto__:null,createBus:function(){var n={};return{emit:function(t,r){(n[t]||[]).forEach((function(n){n(r)}))},on:function(t,r){return n[t]=(n[t]||[]).concat(r),{off:function(){n[t]=(n[t]||[]).filter((function(n){return n!==r}))}}}}}}),F=(j=0,function(){return"".concat(j++)});function S(n){return!n||(O(n,"length")?o(n,0):"object"==typeof n&&o(Object.keys(n),0))}var T=n(S);function P(n){return new String(d(n))}function x(n){function t(r,e){var o=t.get(r);return o?o[1]:(e=e(),u.unshift([r.concat(),e]),c(u,n)&&(u.length=n),e)}function r(n){return u.findIndex((function(t){var r=t[0];return o(n,r.length)&&n.every((function(n,t){return n===r[t]}))}))}void 0===n&&(n=1);var u=[];return t.invalidate=function(n){-1<(n=r(n))&&u.splice(n,1)},t.get=function(n){return u[r(n)]||null},t}function z(n){return n.forEach((function(n){return n()}))}function I(n){setTimeout((function(){throw Error(n)}),0)}function U(n,t){return!!n!=!!t}function V(n,t){if(!n)throw t instanceof String?t.valueOf():Error(t?d(t):t)}function k(n){return!!n===n}function B(n){return a(n,0)}function C(n){return n&&p(n.then)}function D(n){return String(n)===n}function G(n,t){function r(n,t){n&&(u=!0,e=t)}for(var u=!1,e=null,o=0;o<n.length;o++)if(t(n[o],r,o),u)return e}function H(n,t){return n.reduce((function(r,u,e){return r[t(u,e,n)?0:1].push(u),r}),[[],[]])}export{P as StringObject,N as asArray,q as assign,n as bindNot,w as bus,x as cache,z as callEach,b as defaultTo,I as deferThrow,U as either,F as genId,a as greaterThan,O as hasOwnProperty,V as invariant,y as isArray,k as isBoolean,S as isEmpty,p as isFunction,m as isNotArray,T as isNotEmpty,s as isNotNull,g as isNotNullish,r as isNotNumeric,v as isNotUndefined,f as isNull,h as isNullish,t as isNumeric,B as isPositive,C as isPromise,D as isStringValue,l as isUndefined,E as last,o as lengthEquals,i as lengthNotEquals,c as longerThan,G as mapFirst,_ as nestedArray,u as numberEquals,e as numberNotEquals,d as optionalFunctionValue,H as partition};
|
|
@@ -4,20 +4,33 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['vest-utils'] = {}));
|
|
5
5
|
}(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
|
+
function bindNot(fn) {
|
|
8
|
+
return function () {
|
|
9
|
+
var args = [];
|
|
10
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
11
|
+
args[_i] = arguments[_i];
|
|
12
|
+
}
|
|
13
|
+
return !fn.apply(void 0, args);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
function isNumeric(value) {
|
|
8
18
|
var str = String(value);
|
|
9
19
|
var num = Number(value);
|
|
10
20
|
var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
11
21
|
return Boolean(result);
|
|
12
22
|
}
|
|
23
|
+
var isNotNumeric = bindNot(isNumeric);
|
|
13
24
|
|
|
14
25
|
function numberEquals(value, eq) {
|
|
15
26
|
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
16
27
|
}
|
|
28
|
+
var numberNotEquals = bindNot(numberEquals);
|
|
17
29
|
|
|
18
30
|
function lengthEquals(value, arg1) {
|
|
19
31
|
return numberEquals(value.length, arg1);
|
|
20
32
|
}
|
|
33
|
+
var lengthNotEquals = bindNot(lengthEquals);
|
|
21
34
|
|
|
22
35
|
function greaterThan(value, gt) {
|
|
23
36
|
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
@@ -72,25 +85,13 @@
|
|
|
72
85
|
function isUndefined(value) {
|
|
73
86
|
return value === undefined;
|
|
74
87
|
}
|
|
88
|
+
var isNotUndefined = bindNot(isUndefined);
|
|
75
89
|
|
|
76
90
|
function isNullish(value) {
|
|
77
91
|
return isNull(value) || isUndefined(value);
|
|
78
92
|
}
|
|
79
93
|
var isNotNullish = bindNot(isNullish);
|
|
80
94
|
|
|
81
|
-
var isNullish$1 = /*#__PURE__*/Object.freeze({
|
|
82
|
-
__proto__: null,
|
|
83
|
-
isNullish: isNullish,
|
|
84
|
-
isNotNullish: isNotNullish
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// The module is named "isArrayValue" since it
|
|
88
|
-
// is conflicting with a nested npm dependency.
|
|
89
|
-
// We may need to revisit this in the future.
|
|
90
|
-
function isArray(value) {
|
|
91
|
-
return Boolean(Array.isArray(value));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
95
|
function asArray(possibleArg) {
|
|
95
96
|
return [].concat(possibleArg);
|
|
96
97
|
}
|
|
@@ -112,6 +113,14 @@
|
|
|
112
113
|
return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
|
|
113
114
|
}
|
|
114
115
|
|
|
116
|
+
// The module is named "isArrayValue" since it
|
|
117
|
+
// is conflicting with a nested npm dependency.
|
|
118
|
+
// We may need to revisit this in the future.
|
|
119
|
+
function isArray(value) {
|
|
120
|
+
return Boolean(Array.isArray(value));
|
|
121
|
+
}
|
|
122
|
+
var isNotArray = bindNot(isArray);
|
|
123
|
+
|
|
115
124
|
function last(values) {
|
|
116
125
|
var valuesArray = asArray(values);
|
|
117
126
|
return valuesArray[valuesArray.length - 1];
|
|
@@ -201,6 +210,10 @@
|
|
|
201
210
|
? message.valueOf()
|
|
202
211
|
: new Error(message ? optionalFunctionValue(message) : message);
|
|
203
212
|
}
|
|
213
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
214
|
+
function StringObject(value) {
|
|
215
|
+
return new String(optionalFunctionValue(value));
|
|
216
|
+
}
|
|
204
217
|
|
|
205
218
|
function isStringValue(v) {
|
|
206
219
|
return String(v) === v;
|
|
@@ -213,16 +226,6 @@
|
|
|
213
226
|
}, [[], []]);
|
|
214
227
|
}
|
|
215
228
|
|
|
216
|
-
function bindNot(fn) {
|
|
217
|
-
return function () {
|
|
218
|
-
var args = [];
|
|
219
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
220
|
-
args[_i] = arguments[_i];
|
|
221
|
-
}
|
|
222
|
-
return !fn.apply(void 0, args);
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
229
|
function either(a, b) {
|
|
227
230
|
return !!a !== !!b;
|
|
228
231
|
}
|
|
@@ -288,6 +291,25 @@
|
|
|
288
291
|
}
|
|
289
292
|
}
|
|
290
293
|
|
|
294
|
+
function isEmpty(value) {
|
|
295
|
+
if (!value) {
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
else if (hasOwnProperty(value, 'length')) {
|
|
299
|
+
return lengthEquals(value, 0);
|
|
300
|
+
}
|
|
301
|
+
else if (typeof value === 'object') {
|
|
302
|
+
return lengthEquals(Object.keys(value), 0);
|
|
303
|
+
}
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
var isNotEmpty = bindNot(isEmpty);
|
|
307
|
+
|
|
308
|
+
function isPositive(value) {
|
|
309
|
+
return greaterThan(value, 0);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
exports.StringObject = StringObject;
|
|
291
313
|
exports.asArray = asArray;
|
|
292
314
|
exports.assign = assign;
|
|
293
315
|
exports.bindNot = bindNot;
|
|
@@ -298,16 +320,34 @@
|
|
|
298
320
|
exports.deferThrow = deferThrow;
|
|
299
321
|
exports.either = either;
|
|
300
322
|
exports.genId = genId;
|
|
323
|
+
exports.greaterThan = greaterThan;
|
|
301
324
|
exports.hasOwnProperty = hasOwnProperty;
|
|
302
325
|
exports.invariant = invariant;
|
|
326
|
+
exports.isArray = isArray;
|
|
303
327
|
exports.isBoolean = isBoolean;
|
|
328
|
+
exports.isEmpty = isEmpty;
|
|
304
329
|
exports.isFunction = isFunction;
|
|
305
|
-
exports.
|
|
330
|
+
exports.isNotArray = isNotArray;
|
|
331
|
+
exports.isNotEmpty = isNotEmpty;
|
|
332
|
+
exports.isNotNull = isNotNull;
|
|
333
|
+
exports.isNotNullish = isNotNullish;
|
|
334
|
+
exports.isNotNumeric = isNotNumeric;
|
|
335
|
+
exports.isNotUndefined = isNotUndefined;
|
|
336
|
+
exports.isNull = isNull;
|
|
337
|
+
exports.isNullish = isNullish;
|
|
338
|
+
exports.isNumeric = isNumeric;
|
|
339
|
+
exports.isPositive = isPositive;
|
|
306
340
|
exports.isPromise = isPromise;
|
|
307
341
|
exports.isStringValue = isStringValue;
|
|
342
|
+
exports.isUndefined = isUndefined;
|
|
308
343
|
exports.last = last;
|
|
344
|
+
exports.lengthEquals = lengthEquals;
|
|
345
|
+
exports.lengthNotEquals = lengthNotEquals;
|
|
346
|
+
exports.longerThan = longerThan;
|
|
309
347
|
exports.mapFirst = mapFirst;
|
|
310
348
|
exports.nestedArray = nestedArray;
|
|
349
|
+
exports.numberEquals = numberEquals;
|
|
350
|
+
exports.numberNotEquals = numberNotEquals;
|
|
311
351
|
exports.optionalFunctionValue = optionalFunctionValue;
|
|
312
352
|
exports.partition = partition;
|
|
313
353
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self)["vest-utils"]={})}(this,(function(n){function t(n){var t=Number(n);return!(isNaN(parseFloat(String(n)))||isNaN(Number(n))||!isFinite(t))}function r(n){return null
|
|
1
|
+
"use strict";!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((n="undefined"!=typeof globalThis?globalThis:n||self)["vest-utils"]={})}(this,(function(n){function t(n){return function(){for(var t=[],r=0;r<arguments.length;r++)t[r]=arguments[r];return!n.apply(void 0,t)}}function r(n){var t=Number(n);return!(isNaN(parseFloat(String(n)))||isNaN(Number(n))||!isFinite(t))}function e(n,t){return r(n)&&r(t)&&Number(n)===Number(t)}function u(n,t){return e(n.length,t)}function i(n,t){return r(n)&&r(t)&&Number(n)>Number(t)}function o(n,t){return i(n.length,t)}function c(n){return null===n}function f(n){return void 0===n}function a(n){return null===n||f(n)}function l(n){return[].concat(n)}function s(n){return"function"==typeof n}function h(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return s(n)?n.apply(void 0,t):n}function d(n,t){var r;return null!==(r=h(n))&&void 0!==r?r:h(t)}function p(n){return!!Array.isArray(n)}function g(n){return(n=l(n))[n.length-1]}function v(n,t){var r=0;for(t=t.slice(0,-1);r<t.length;r++){var e=t[r];n[e]=d(n[e],[]),n=n[e]}return n}function N(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function y(n){return!n||(N(n,"length")?u(n,0):"object"==typeof n&&u(Object.keys(n),0))}var b,m=t(r),E=t(e),O=t(u),_=t(c),j=t(f),A=t(a),P=t(p),T=Object.freeze({__proto__:null,transform:function n(t,r){for(var e=[],u=0;u<t.length;u++){var i=t[u];p(i)?e.push(n(i,r)):(i=r(i),_(i)&&e.push(i))}return e},valueAtPath:function(n,t){return v(n,t)[g(t)]},setValueAtPath:function(n,t,r){return v(n,t)[g(t)]=r,n},flatten:function n(t){return l(t).reduce((function(t,r){return p(r)?t.concat(n(r)):l(t).concat(r)}),[])},getCurrent:v}),w=Object.assign,S=Object.freeze({__proto__:null,createBus:function(){var n={};return{emit:function(t,r){(n[t]||[]).forEach((function(n){n(r)}))},on:function(t,r){return n[t]=(n[t]||[]).concat(r),{off:function(){n[t]=(n[t]||[]).filter((function(n){return n!==r}))}}}}}}),F=(b=0,function(){return"".concat(b++)}),q=t(y);n.StringObject=function(n){return new String(h(n))},n.asArray=l,n.assign=w,n.bindNot=t,n.bus=S,n.cache=function(n){function t(n){return r.findIndex((function(t){var r=t[0];return u(n,r.length)&&n.every((function(n,t){return n===r[t]}))}))}void 0===n&&(n=1);var r=[],e=function(t,u){var i=e.get(t);return i?i[1]:(u=u(),r.unshift([t.concat(),u]),o(r,n)&&(r.length=n),u)};return e.invalidate=function(n){-1<(n=t(n))&&r.splice(n,1)},e.get=function(n){return r[t(n)]||null},e},n.callEach=function(n){return n.forEach((function(n){return n()}))},n.defaultTo=d,n.deferThrow=function(n){setTimeout((function(){throw Error(n)}),0)},n.either=function(n,t){return!!n!=!!t},n.genId=F,n.greaterThan=i,n.hasOwnProperty=N,n.invariant=function(n,t){if(!n)throw t instanceof String?t.valueOf():Error(t?h(t):t)},n.isArray=p,n.isBoolean=function(n){return!!n===n},n.isEmpty=y,n.isFunction=s,n.isNotArray=P,n.isNotEmpty=q,n.isNotNull=_,n.isNotNullish=A,n.isNotNumeric=m,n.isNotUndefined=j,n.isNull=c,n.isNullish=a,n.isNumeric=r,n.isPositive=function(n){return i(n,0)},n.isPromise=function(n){return n&&s(n.then)},n.isStringValue=function(n){return String(n)===n},n.isUndefined=f,n.last=g,n.lengthEquals=u,n.lengthNotEquals=O,n.longerThan=o,n.mapFirst=function(n,t){function r(n,t){n&&(e=!0,u=t)}for(var e=!1,u=null,i=0;i<n.length;i++)if(t(n[i],r,i),e)return u},n.nestedArray=T,n.numberEquals=e,n.numberNotEquals=E,n.optionalFunctionValue=h,n.partition=function(n,t){return n.reduce((function(r,e,u){return r[t(e,u,n)?0:1].push(e),r}),[[],[]])},Object.defineProperty(n,"__esModule",{value:!0})}));
|
package/package.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createBus } from 'bus';
|
|
2
2
|
|
|
3
3
|
describe('bus', () => {
|
|
4
4
|
it('should be a function', () => {
|
|
5
|
-
expect(
|
|
5
|
+
expect(createBus).toBeInstanceOf(Function);
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
it('should return a bus', () => {
|
|
9
|
-
const bus =
|
|
9
|
+
const bus = createBus();
|
|
10
10
|
expect(bus).toBeInstanceOf(Object);
|
|
11
11
|
expect(bus.emit).toBeInstanceOf(Function);
|
|
12
12
|
expect(bus.on).toBeInstanceOf(Function);
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
it('should emit events', () => {
|
|
16
|
-
const bus =
|
|
16
|
+
const bus = createBus();
|
|
17
17
|
const spy = jest.fn();
|
|
18
18
|
bus.on('test', spy);
|
|
19
19
|
bus.emit('test');
|
|
@@ -21,7 +21,7 @@ describe('bus', () => {
|
|
|
21
21
|
});
|
|
22
22
|
|
|
23
23
|
it('should emit events with data', () => {
|
|
24
|
-
const bus =
|
|
24
|
+
const bus = createBus();
|
|
25
25
|
const spy = jest.fn();
|
|
26
26
|
bus.on('test', spy);
|
|
27
27
|
bus.emit('test', 'testData');
|
|
@@ -29,7 +29,7 @@ describe('bus', () => {
|
|
|
29
29
|
});
|
|
30
30
|
|
|
31
31
|
it('should emit events with multiple listeners', () => {
|
|
32
|
-
const bus =
|
|
32
|
+
const bus = createBus();
|
|
33
33
|
const spy1 = jest.fn();
|
|
34
34
|
const spy2 = jest.fn();
|
|
35
35
|
bus.on('test', spy1);
|
|
@@ -40,7 +40,7 @@ describe('bus', () => {
|
|
|
40
40
|
});
|
|
41
41
|
|
|
42
42
|
it('should emit events with multiple listeners and data', () => {
|
|
43
|
-
const bus =
|
|
43
|
+
const bus = createBus();
|
|
44
44
|
const spy1 = jest.fn();
|
|
45
45
|
const spy2 = jest.fn();
|
|
46
46
|
bus.on('test', spy1);
|
|
@@ -51,7 +51,7 @@ describe('bus', () => {
|
|
|
51
51
|
});
|
|
52
52
|
|
|
53
53
|
test('on returns an object with an `off` function', () => {
|
|
54
|
-
const bus =
|
|
54
|
+
const bus = createBus();
|
|
55
55
|
const spy = jest.fn();
|
|
56
56
|
const off = bus.on('test', spy);
|
|
57
57
|
expect(off).toBeInstanceOf(Object);
|
|
@@ -59,7 +59,7 @@ describe('bus', () => {
|
|
|
59
59
|
});
|
|
60
60
|
|
|
61
61
|
test('off should remove a listener', () => {
|
|
62
|
-
const bus =
|
|
62
|
+
const bus = createBus();
|
|
63
63
|
const spy = jest.fn();
|
|
64
64
|
const off = bus.on('test', spy);
|
|
65
65
|
off.off();
|
|
@@ -68,7 +68,7 @@ describe('bus', () => {
|
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
test('off should only remove specific handler', () => {
|
|
71
|
-
const bus =
|
|
71
|
+
const bus = createBus();
|
|
72
72
|
const spy1 = jest.fn();
|
|
73
73
|
const spy2 = jest.fn();
|
|
74
74
|
const off = bus.on('test', spy1);
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { random, datatype } from 'faker';
|
|
2
|
+
|
|
3
|
+
import { greaterThan } from 'greaterThan';
|
|
4
|
+
|
|
5
|
+
describe('Tests greaterThan rule', () => {
|
|
6
|
+
let arg0;
|
|
7
|
+
|
|
8
|
+
describe('Arguments are numbers', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
arg0 = datatype.number();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('When first argument is larger', () => {
|
|
14
|
+
it('Should return true', () => {
|
|
15
|
+
expect(greaterThan(arg0, arg0 - 1)).toBe(true);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('When first argument is smaller', () => {
|
|
20
|
+
it('Should return true', () => {
|
|
21
|
+
expect(greaterThan(arg0, arg0 + 1)).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('When values are equal', () => {
|
|
26
|
+
it('Should return false', () => {
|
|
27
|
+
expect(greaterThan(arg0, arg0)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('Arguments are numeric strings', () => {
|
|
33
|
+
describe('When first argument is larger', () => {
|
|
34
|
+
it('Should return true', () => {
|
|
35
|
+
expect(greaterThan(`${arg0}`, `${arg0 - 1}`)).toBe(true);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('When first argument is smaller', () => {
|
|
40
|
+
it('Should return true', () => {
|
|
41
|
+
expect(greaterThan(`${arg0}`, `${arg0 + 1}`)).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('When values are equal', () => {
|
|
46
|
+
it('Should return false', () => {
|
|
47
|
+
expect(greaterThan(arg0, arg0)).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('Arguments are non numeric', () => {
|
|
53
|
+
[random.word(), `${datatype.number()}`.split(''), {}].forEach(element => {
|
|
54
|
+
it('Should return false', () => {
|
|
55
|
+
expect(greaterThan(element, 0)).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isArray } from 'isArrayValue';
|
|
2
|
+
|
|
3
|
+
describe('Tests isArray rule', () => {
|
|
4
|
+
it('Should return true for an empty array', () => {
|
|
5
|
+
expect(isArray([])).toBe(true);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('Should return true for an array with elements', () => {
|
|
9
|
+
expect(isArray([1, 2, 3])).toBe(true);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('Should return false a string', () => {
|
|
13
|
+
expect(isArray('1')).toBe(false);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isNull } from 'isNull';
|
|
2
|
+
|
|
3
|
+
describe('Tests isNull rule', () => {
|
|
4
|
+
it('Should return true for `null` value', () => {
|
|
5
|
+
expect(isNull(null)).toBe(true);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it.each([
|
|
9
|
+
undefined,
|
|
10
|
+
NaN,
|
|
11
|
+
false,
|
|
12
|
+
true,
|
|
13
|
+
Object,
|
|
14
|
+
Array(0),
|
|
15
|
+
'',
|
|
16
|
+
' ',
|
|
17
|
+
0,
|
|
18
|
+
1,
|
|
19
|
+
'0',
|
|
20
|
+
'1',
|
|
21
|
+
Function.prototype,
|
|
22
|
+
])('Should return false for %s value', v => {
|
|
23
|
+
expect(isNull(v)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isNumeric } from 'isNumeric';
|
|
2
|
+
|
|
3
|
+
const NUMERICS = ['-10', '0', 0xff, '0xFF', '8e5', '3.1415', +10, '0144'];
|
|
4
|
+
|
|
5
|
+
const NON_NUMERICS = [
|
|
6
|
+
'-0x42',
|
|
7
|
+
'7.2acdgs',
|
|
8
|
+
'',
|
|
9
|
+
{},
|
|
10
|
+
NaN,
|
|
11
|
+
null,
|
|
12
|
+
true,
|
|
13
|
+
Infinity,
|
|
14
|
+
undefined,
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
describe('Tests isNumeric rule', () => {
|
|
18
|
+
it('Should return true for numeric values', () => {
|
|
19
|
+
NUMERICS.forEach(value => expect(isNumeric(value)).toBe(true));
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('Should return false for non numeric values', () => {
|
|
23
|
+
// @ts-expect-error - testing bad usage
|
|
24
|
+
NON_NUMERICS.forEach(value => expect(isNumeric(value)).toBe(false));
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { isUndefined } from 'isUndefined';
|
|
2
|
+
|
|
3
|
+
describe('Tests isUndefined rule', () => {
|
|
4
|
+
it('Should return true for `undefined` value', () => {
|
|
5
|
+
expect(isUndefined(undefined)).toBe(true);
|
|
6
|
+
expect(isUndefined()).toBe(true);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it.each([
|
|
10
|
+
null,
|
|
11
|
+
NaN,
|
|
12
|
+
false,
|
|
13
|
+
true,
|
|
14
|
+
Object,
|
|
15
|
+
Array(0),
|
|
16
|
+
'',
|
|
17
|
+
' ',
|
|
18
|
+
0,
|
|
19
|
+
1,
|
|
20
|
+
'0',
|
|
21
|
+
'1',
|
|
22
|
+
() => undefined,
|
|
23
|
+
])('Should return false for %s value', v => {
|
|
24
|
+
expect(isUndefined(v)).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import faker from 'faker';
|
|
2
|
+
|
|
3
|
+
import { lengthEquals } from 'lengthEquals';
|
|
4
|
+
|
|
5
|
+
describe('Tests lengthEquals rule', () => {
|
|
6
|
+
const length = faker.datatype.number();
|
|
7
|
+
const word = faker.random.word();
|
|
8
|
+
const boolean = faker.datatype.boolean();
|
|
9
|
+
|
|
10
|
+
describe('First argument is array or string', () => {
|
|
11
|
+
describe('When first argument is equal to a given value', () => {
|
|
12
|
+
it('Should return true for an array equal to length', () => {
|
|
13
|
+
expect(lengthEquals(new Array(length), length)).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('Should return true for a string equal to word length', () => {
|
|
17
|
+
expect(lengthEquals(word, word.length)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('When first argument is shorter', () => {
|
|
22
|
+
it('Should return false for an array shorter than length', () => {
|
|
23
|
+
expect(lengthEquals(new Array(length), length + 1)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('Should return false for a string shorter than word length', () => {
|
|
27
|
+
expect(lengthEquals(word, word.length + 1)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('When first argument is longer', () => {
|
|
32
|
+
it('Should return false for an array longer than length', () => {
|
|
33
|
+
expect(lengthEquals(new Array(length), length - 1)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('Should return false for a string longer than word length', () => {
|
|
37
|
+
expect(lengthEquals(word, word.length - 1)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("First argument isn't array or string", () => {
|
|
43
|
+
it('Should throw error', () => {
|
|
44
|
+
// @ts-expect-error - testing wrong input
|
|
45
|
+
expect(() => lengthEquals(undefined, 0)).toThrow(TypeError);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('Should return false for number argument', () => {
|
|
49
|
+
expect(lengthEquals(length, 0)).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('Should return false for boolean argument', () => {
|
|
53
|
+
expect(lengthEquals(boolean, 0)).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import faker from 'faker';
|
|
2
|
+
|
|
3
|
+
import { longerThan } from 'longerThan';
|
|
4
|
+
|
|
5
|
+
describe('Tests longerThan rule', () => {
|
|
6
|
+
const length = faker.datatype.number();
|
|
7
|
+
const word = faker.random.word();
|
|
8
|
+
const boolean = faker.datatype.boolean();
|
|
9
|
+
|
|
10
|
+
describe('First argument is array or string', () => {
|
|
11
|
+
describe('When first argument is longer', () => {
|
|
12
|
+
it('Should return true for an array longer than length', () => {
|
|
13
|
+
expect(longerThan(new Array(length), length - 1)).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('Should return true for a string longer than word length', () => {
|
|
17
|
+
expect(longerThan(word, word.length - 1)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('When first argument is shorter', () => {
|
|
22
|
+
it('Should return false for an array shorter than length', () => {
|
|
23
|
+
expect(longerThan(new Array(length), length + 1)).toBe(false);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it('Should return false for a string shorter than word length', () => {
|
|
27
|
+
expect(longerThan(word, word.length + 1)).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('When first argument is equal to a given value', () => {
|
|
32
|
+
it('Should return false for an array equal to length', () => {
|
|
33
|
+
expect(longerThan(new Array(length), length)).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('Should return false for a string equal to word length', () => {
|
|
37
|
+
expect(longerThan(word, word.length)).toBe(false);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("First argument isn't array or string", () => {
|
|
43
|
+
it('Should throw error', () => {
|
|
44
|
+
// @ts-expect-error - testing wrong input
|
|
45
|
+
expect(() => longerThan(undefined, 0)).toThrow(TypeError);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('Should return false for number argument', () => {
|
|
49
|
+
expect(longerThan(length, 0)).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('Should return false for boolean argument', () => {
|
|
53
|
+
expect(longerThan(boolean, 0)).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { random, datatype } from 'faker';
|
|
2
|
+
|
|
3
|
+
import { numberEquals } from 'numberEquals';
|
|
4
|
+
|
|
5
|
+
describe('Tests numberEquals rule', () => {
|
|
6
|
+
let arg0;
|
|
7
|
+
|
|
8
|
+
describe('Arguments are numbers', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
arg0 = datatype.number();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
describe('When first argument is larger', () => {
|
|
14
|
+
it('Should return false', () => {
|
|
15
|
+
expect(numberEquals(arg0, arg0 - 1)).toBe(false);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe('When first argument is smaller', () => {
|
|
20
|
+
it('Should return false', () => {
|
|
21
|
+
expect(numberEquals(arg0, arg0 + 1)).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe('When values are equal', () => {
|
|
26
|
+
it('Should return true', () => {
|
|
27
|
+
expect(numberEquals(arg0, arg0)).toBe(true);
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('Arguments are numeric strings', () => {
|
|
33
|
+
describe('When first argument is larger', () => {
|
|
34
|
+
it('Should return false', () => {
|
|
35
|
+
expect(numberEquals(`${arg0}`, `${arg0 - 1}`)).toBe(false);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('When first argument is smaller', () => {
|
|
40
|
+
it('Should return false', () => {
|
|
41
|
+
expect(numberEquals(`${arg0}`, `${arg0 + 1}`)).toBe(false);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('When values are equal', () => {
|
|
46
|
+
it('Should return true', () => {
|
|
47
|
+
expect(numberEquals(arg0, arg0)).toBe(true);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('Arguments are non numeric', () => {
|
|
53
|
+
[random.word(), `${datatype.number()}`.split(''), {}].forEach(element => {
|
|
54
|
+
it('Should return false', () => {
|
|
55
|
+
expect(numberEquals(element, 0)).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import bindNot from 'bindNot';
|
|
2
|
+
|
|
3
|
+
// The module is named "isArrayValue" since it
|
|
4
|
+
// is conflicting with a nested npm dependency.
|
|
5
|
+
// We may need to revisit this in the future.
|
|
6
|
+
|
|
7
|
+
export function isArray(value: unknown): value is Array<unknown> {
|
|
8
|
+
return Boolean(Array.isArray(value));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const isNotArray = bindNot(isArray);
|
package/src/isEmpty.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import bindNot from 'bindNot';
|
|
2
|
+
import hasOwnProperty from 'hasOwnProperty';
|
|
3
|
+
import { lengthEquals } from 'lengthEquals';
|
|
4
|
+
|
|
5
|
+
export function isEmpty(value: unknown): boolean {
|
|
6
|
+
if (!value) {
|
|
7
|
+
return true;
|
|
8
|
+
} else if (hasOwnProperty(value, 'length')) {
|
|
9
|
+
return lengthEquals(value as string | unknown[], 0);
|
|
10
|
+
} else if (typeof value === 'object') {
|
|
11
|
+
return lengthEquals(Object.keys(value as Record<string, unknown>), 0);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const isNotEmpty = bindNot(isEmpty);
|
package/src/isNull.ts
ADDED
package/src/isNullish.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import bindNot from 'bindNot';
|
|
1
2
|
import { isNull } from 'isNull';
|
|
2
3
|
import { isUndefined } from 'isUndefined';
|
|
3
4
|
|
|
4
|
-
import { bindNot } from 'vest-utils';
|
|
5
|
-
|
|
6
5
|
export function isNullish(value: any): value is null | undefined {
|
|
7
6
|
return isNull(value) || isUndefined(value);
|
|
8
7
|
}
|
package/src/isNumeric.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import bindNot from 'bindNot';
|
|
2
|
+
|
|
3
|
+
export function isNumeric(value: string | number): boolean {
|
|
4
|
+
const str = String(value);
|
|
5
|
+
const num = Number(value);
|
|
6
|
+
const result =
|
|
7
|
+
!isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
8
|
+
return Boolean(result);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const isNotNumeric = bindNot(isNumeric);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import bindNot from 'bindNot';
|
|
2
|
+
import { numberEquals } from 'numberEquals';
|
|
3
|
+
|
|
4
|
+
export function lengthEquals(
|
|
5
|
+
value: string | unknown[],
|
|
6
|
+
arg1: string | number
|
|
7
|
+
): boolean {
|
|
8
|
+
return numberEquals(value.length, arg1);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const lengthNotEquals = bindNot(lengthEquals);
|
package/src/nestedArray.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { isArray } from 'isArrayValue';
|
|
2
|
-
import { isNotNull } from 'isNull';
|
|
3
|
-
|
|
4
1
|
import asArray from 'asArray';
|
|
5
2
|
import defaultTo from 'defaultTo';
|
|
3
|
+
import { isArray } from 'isArrayValue';
|
|
4
|
+
import { isNotNull } from 'isNull';
|
|
6
5
|
import last from 'last';
|
|
7
6
|
|
|
8
7
|
export type NestedArray<T> = Array<NestedArray<T> | T>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import bindNot from 'bindNot';
|
|
2
|
+
import { isNumeric } from 'isNumeric';
|
|
3
|
+
|
|
4
|
+
export function numberEquals(
|
|
5
|
+
value: string | number,
|
|
6
|
+
eq: string | number
|
|
7
|
+
): boolean {
|
|
8
|
+
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export const numberNotEquals = bindNot(numberEquals);
|
package/src/vest-utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { default as cache } from 'cache';
|
|
2
|
-
export
|
|
2
|
+
export { isNullish, isNotNullish } from 'isNullish';
|
|
3
3
|
export * as nestedArray from 'nestedArray';
|
|
4
4
|
export { default as asArray } from 'asArray';
|
|
5
5
|
export { default as callEach } from 'callEach';
|
|
@@ -8,7 +8,7 @@ export { default as isPromise } from 'isPromise';
|
|
|
8
8
|
export { default as optionalFunctionValue } from 'optionalFunctionValue';
|
|
9
9
|
export { default as assign } from 'assign';
|
|
10
10
|
export { default as defaultTo } from 'defaultTo';
|
|
11
|
-
export { default as invariant } from 'invariant';
|
|
11
|
+
export { default as invariant, StringObject } from 'invariant';
|
|
12
12
|
export { default as isStringValue } from 'isStringValue';
|
|
13
13
|
export { default as partition } from 'partition';
|
|
14
14
|
export { default as bindNot } from 'bindNot';
|
|
@@ -20,3 +20,13 @@ export * as bus from 'bus';
|
|
|
20
20
|
export { default as genId } from 'genId';
|
|
21
21
|
export { default as isFunction } from 'isFunction';
|
|
22
22
|
export { default as mapFirst } from 'mapFirst';
|
|
23
|
+
export { greaterThan } from 'greaterThan';
|
|
24
|
+
export { longerThan } from 'longerThan';
|
|
25
|
+
export { isNumeric, isNotNumeric } from 'isNumeric';
|
|
26
|
+
export { lengthEquals, lengthNotEquals } from 'lengthEquals';
|
|
27
|
+
export { numberEquals, numberNotEquals } from 'numberEquals';
|
|
28
|
+
export { isNull, isNotNull } from 'isNull';
|
|
29
|
+
export { isUndefined, isNotUndefined } from 'isUndefined';
|
|
30
|
+
export { isArray, isNotArray } from 'isArrayValue';
|
|
31
|
+
export { isEmpty, isNotEmpty } from 'isEmpty';
|
|
32
|
+
export { isPositive } from 'isPositive';
|
package/types/vest-utils.d.ts
CHANGED
|
@@ -6,10 +6,8 @@ declare function createCache(maxSize?: number): {
|
|
|
6
6
|
get(deps: unknown[]): any;
|
|
7
7
|
invalidate(item: any): void;
|
|
8
8
|
};
|
|
9
|
-
declare
|
|
10
|
-
|
|
11
|
-
const isNotNullish: (value: any) => boolean;
|
|
12
|
-
}
|
|
9
|
+
declare function isNullish(value: any): value is null | undefined;
|
|
10
|
+
declare const isNotNullish: (value: any) => boolean;
|
|
13
11
|
declare namespace nestedArray {
|
|
14
12
|
type NestedArray<T> = Array<NestedArray<T> | T>;
|
|
15
13
|
// This is kind of a map/filter in one function.
|
|
@@ -41,6 +39,8 @@ type CB = (...args: any[]) => void;
|
|
|
41
39
|
declare function invariant(condition: any,
|
|
42
40
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
43
41
|
message?: String | Stringable): asserts condition;
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
43
|
+
declare function StringObject(value?: Stringable): String;
|
|
44
44
|
declare function isStringValue(v: unknown): v is string;
|
|
45
45
|
declare function partition<T>(array: T[], predicate: (value: T, index: number, array: T[]) => boolean): [
|
|
46
46
|
T[],
|
|
@@ -73,4 +73,24 @@ declare namespace bus {
|
|
|
73
73
|
declare const genId: () => string;
|
|
74
74
|
declare function isFunction(value: unknown): value is (...args: unknown[]) => unknown;
|
|
75
75
|
declare function mapFirst<T>(array: T[], callback: (item: T, breakout: (conditional: boolean, value: unknown) => void, index: number) => unknown): any;
|
|
76
|
-
|
|
76
|
+
declare function greaterThan(value: number | string, gt: number | string): boolean;
|
|
77
|
+
declare function longerThan(value: string | unknown[], arg1: string | number): boolean;
|
|
78
|
+
declare function isNumeric(value: string | number): boolean;
|
|
79
|
+
declare const isNotNumeric: (value: string | number) => boolean;
|
|
80
|
+
declare function lengthEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
81
|
+
declare const lengthNotEquals: (value: string | unknown[], arg1: string | number) => boolean;
|
|
82
|
+
declare function numberEquals(value: string | number, eq: string | number): boolean;
|
|
83
|
+
declare const numberNotEquals: (value: string | number, eq: string | number) => boolean;
|
|
84
|
+
declare function isNull(value: unknown): value is null;
|
|
85
|
+
declare const isNotNull: (value: unknown) => boolean;
|
|
86
|
+
declare function isUndefined(value?: unknown): value is undefined;
|
|
87
|
+
declare const isNotUndefined: (value?: unknown) => boolean;
|
|
88
|
+
// The module is named "isArrayValue" since it
|
|
89
|
+
// is conflicting with a nested npm dependency.
|
|
90
|
+
// We may need to revisit this in the future.
|
|
91
|
+
declare function isArray(value: unknown): value is Array<unknown>;
|
|
92
|
+
declare const isNotArray: (value: unknown) => boolean;
|
|
93
|
+
declare function isEmpty(value: unknown): boolean;
|
|
94
|
+
declare const isNotEmpty: (value: unknown) => boolean;
|
|
95
|
+
declare function isPositive(value: number | string): boolean;
|
|
96
|
+
export { createCache as cache, isNullish, isNotNullish, nestedArray, asArray, callEach, hasOwnProperty, isPromise, optionalFunctionValue, _default as assign, defaultTo, invariant, StringObject, isStringValue, partition, bindNot, either, isBoolean, last, deferThrow, bus, genId, isFunction, mapFirst, greaterThan, longerThan, isNumeric, isNotNumeric, lengthEquals, lengthNotEquals, numberEquals, numberNotEquals, isNull, isNotNull, isUndefined, isNotUndefined, isArray, isNotArray, isEmpty, isNotEmpty, isPositive };
|