vest-utils 0.1.0-dev-c4ba9b → 0.1.0-dev-d315d9
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 +68 -66
- package/dist/cjs/vest-utils.production.js +1 -1
- package/dist/es/vest-utils.development.js +67 -67
- package/dist/es/vest-utils.production.js +1 -1
- package/dist/umd/vest-utils.development.js +68 -66
- package/dist/umd/vest-utils.production.js +1 -1
- package/package.json +2 -2
- package/src/__tests__/cache.test.ts +2 -2
- package/src/__tests__/greaterThan.test.ts +13 -4
- package/src/__tests__/lengthEquals.test.ts +4 -2
- package/src/__tests__/longerThan.test.ts +3 -1
- package/src/__tests__/numberEquals.test.ts +13 -5
- package/src/__tests__/seq.test.ts +17 -1
- package/src/__tests__/tinyState.test.ts +67 -0
- package/src/bus.ts +6 -4
- package/src/cache.ts +14 -6
- package/src/deferThrow.ts +2 -0
- package/src/seq.ts +8 -4
- package/src/tinyState.ts +23 -0
- package/src/vest-utils.ts +5 -2
- package/tsconfig.json +2 -1
- package/types/vest-utils.d.ts +48 -15
- package/types/vest-utils.d.ts.map +1 -1
|
@@ -5,32 +5,26 @@
|
|
|
5
5
|
})(this, (function (exports) { 'use strict';
|
|
6
6
|
|
|
7
7
|
function bindNot(fn) {
|
|
8
|
-
return
|
|
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
|
-
};
|
|
8
|
+
return (...args) => !fn(...args);
|
|
15
9
|
}
|
|
16
10
|
|
|
17
11
|
function isNumeric(value) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
12
|
+
const str = String(value);
|
|
13
|
+
const num = Number(value);
|
|
14
|
+
const result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
|
|
21
15
|
return Boolean(result);
|
|
22
16
|
}
|
|
23
|
-
|
|
17
|
+
const isNotNumeric = bindNot(isNumeric);
|
|
24
18
|
|
|
25
19
|
function numberEquals(value, eq) {
|
|
26
20
|
return isNumeric(value) && isNumeric(eq) && Number(value) === Number(eq);
|
|
27
21
|
}
|
|
28
|
-
|
|
22
|
+
const numberNotEquals = bindNot(numberEquals);
|
|
29
23
|
|
|
30
24
|
function lengthEquals(value, arg1) {
|
|
31
25
|
return numberEquals(value.length, arg1);
|
|
32
26
|
}
|
|
33
|
-
|
|
27
|
+
const lengthNotEquals = bindNot(lengthEquals);
|
|
34
28
|
|
|
35
29
|
function greaterThan(value, gt) {
|
|
36
30
|
return isNumeric(value) && isNumeric(gt) && Number(value) > Number(gt);
|
|
@@ -43,54 +37,48 @@
|
|
|
43
37
|
/**
|
|
44
38
|
* Creates a cache function
|
|
45
39
|
*/
|
|
46
|
-
function createCache(maxSize) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
var cacheHit = cache.get(deps);
|
|
40
|
+
function createCache(maxSize = 1) {
|
|
41
|
+
const cacheStorage = [];
|
|
42
|
+
const cache = (deps, cacheAction) => {
|
|
43
|
+
const cacheHit = cache.get(deps);
|
|
51
44
|
// cache hit is not null
|
|
52
45
|
if (cacheHit)
|
|
53
46
|
return cacheHit[1];
|
|
54
|
-
|
|
47
|
+
const result = cacheAction();
|
|
55
48
|
cacheStorage.unshift([deps.concat(), result]);
|
|
56
49
|
if (longerThan(cacheStorage, maxSize))
|
|
57
50
|
cacheStorage.length = maxSize;
|
|
58
51
|
return result;
|
|
59
52
|
};
|
|
60
53
|
// invalidate an item in the cache by its dependencies
|
|
61
|
-
cache.invalidate =
|
|
62
|
-
|
|
54
|
+
cache.invalidate = (deps) => {
|
|
55
|
+
const index = findIndex(deps);
|
|
63
56
|
if (index > -1)
|
|
64
57
|
cacheStorage.splice(index, 1);
|
|
65
58
|
};
|
|
66
59
|
// Retrieves an item from the cache.
|
|
67
|
-
cache.get =
|
|
68
|
-
return cacheStorage[findIndex(deps)] || null;
|
|
69
|
-
};
|
|
60
|
+
cache.get = (deps) => cacheStorage[findIndex(deps)] || null;
|
|
70
61
|
return cache;
|
|
71
62
|
function findIndex(deps) {
|
|
72
|
-
return cacheStorage.findIndex(
|
|
73
|
-
|
|
74
|
-
return lengthEquals(deps, cachedDeps.length) &&
|
|
75
|
-
deps.every(function (dep, i) { return dep === cachedDeps[i]; });
|
|
76
|
-
});
|
|
63
|
+
return cacheStorage.findIndex(([cachedDeps]) => lengthEquals(deps, cachedDeps.length) &&
|
|
64
|
+
deps.every((dep, i) => dep === cachedDeps[i]));
|
|
77
65
|
}
|
|
78
66
|
}
|
|
79
67
|
|
|
80
68
|
function isNull(value) {
|
|
81
69
|
return value === null;
|
|
82
70
|
}
|
|
83
|
-
|
|
71
|
+
const isNotNull = bindNot(isNull);
|
|
84
72
|
|
|
85
73
|
function isUndefined(value) {
|
|
86
74
|
return value === undefined;
|
|
87
75
|
}
|
|
88
|
-
|
|
76
|
+
const isNotUndefined = bindNot(isUndefined);
|
|
89
77
|
|
|
90
78
|
function isNullish(value) {
|
|
91
79
|
return isNull(value) || isUndefined(value);
|
|
92
80
|
}
|
|
93
|
-
|
|
81
|
+
const isNotNullish = bindNot(isNullish);
|
|
94
82
|
|
|
95
83
|
function asArray(possibleArg) {
|
|
96
84
|
return [].concat(possibleArg);
|
|
@@ -100,12 +88,8 @@
|
|
|
100
88
|
return typeof value === 'function';
|
|
101
89
|
}
|
|
102
90
|
|
|
103
|
-
function optionalFunctionValue(value) {
|
|
104
|
-
|
|
105
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
106
|
-
args[_i - 1] = arguments[_i];
|
|
107
|
-
}
|
|
108
|
-
return isFunction(value) ? value.apply(void 0, args) : value;
|
|
91
|
+
function optionalFunctionValue(value, ...args) {
|
|
92
|
+
return isFunction(value) ? value(...args) : value;
|
|
109
93
|
}
|
|
110
94
|
|
|
111
95
|
function defaultTo(value, defaultValue) {
|
|
@@ -119,10 +103,10 @@
|
|
|
119
103
|
function isArray(value) {
|
|
120
104
|
return Boolean(Array.isArray(value));
|
|
121
105
|
}
|
|
122
|
-
|
|
106
|
+
const isNotArray = bindNot(isArray);
|
|
123
107
|
|
|
124
108
|
function last(values) {
|
|
125
|
-
|
|
109
|
+
const valuesArray = asArray(values);
|
|
126
110
|
return valuesArray[valuesArray.length - 1];
|
|
127
111
|
}
|
|
128
112
|
|
|
@@ -130,14 +114,13 @@
|
|
|
130
114
|
// Normally, behaves like a nested-array map,
|
|
131
115
|
// but returning `null` will drop the element from the array
|
|
132
116
|
function transform(array, cb) {
|
|
133
|
-
|
|
134
|
-
for (
|
|
135
|
-
var v = array_1[_i];
|
|
117
|
+
const res = [];
|
|
118
|
+
for (const v of array) {
|
|
136
119
|
if (isArray(v)) {
|
|
137
120
|
res.push(transform(v, cb));
|
|
138
121
|
}
|
|
139
122
|
else {
|
|
140
|
-
|
|
123
|
+
const output = cb(v);
|
|
141
124
|
if (isNotNull(output)) {
|
|
142
125
|
res.push(output);
|
|
143
126
|
}
|
|
@@ -149,12 +132,12 @@
|
|
|
149
132
|
return getCurrent(array, path)[last(path)];
|
|
150
133
|
}
|
|
151
134
|
function setValueAtPath(array, path, value) {
|
|
152
|
-
|
|
135
|
+
const current = getCurrent(array, path);
|
|
153
136
|
current[last(path)] = value;
|
|
154
137
|
return array;
|
|
155
138
|
}
|
|
156
139
|
function flatten(values) {
|
|
157
|
-
return asArray(values).reduce(
|
|
140
|
+
return asArray(values).reduce((acc, value) => {
|
|
158
141
|
if (isArray(value)) {
|
|
159
142
|
return acc.concat(flatten(value));
|
|
160
143
|
}
|
|
@@ -162,9 +145,8 @@
|
|
|
162
145
|
}, []);
|
|
163
146
|
}
|
|
164
147
|
function getCurrent(array, path) {
|
|
165
|
-
|
|
166
|
-
for (
|
|
167
|
-
var p = _a[_i];
|
|
148
|
+
let current = array;
|
|
149
|
+
for (const p of path.slice(0, -1)) {
|
|
168
150
|
current[p] = defaultTo(current[p], []);
|
|
169
151
|
current = current[p];
|
|
170
152
|
}
|
|
@@ -181,7 +163,7 @@
|
|
|
181
163
|
});
|
|
182
164
|
|
|
183
165
|
function callEach(arr) {
|
|
184
|
-
return arr.forEach(
|
|
166
|
+
return arr.forEach(fn => fn());
|
|
185
167
|
}
|
|
186
168
|
|
|
187
169
|
/**
|
|
@@ -228,27 +210,27 @@
|
|
|
228
210
|
}
|
|
229
211
|
|
|
230
212
|
function deferThrow(message) {
|
|
231
|
-
setTimeout(
|
|
213
|
+
setTimeout(() => {
|
|
232
214
|
throw new Error(message);
|
|
233
215
|
}, 0);
|
|
234
216
|
}
|
|
235
217
|
|
|
236
218
|
function createBus() {
|
|
237
|
-
|
|
219
|
+
const listeners = {};
|
|
238
220
|
return {
|
|
239
|
-
emit
|
|
240
|
-
listener(event).forEach(
|
|
221
|
+
emit(event, data) {
|
|
222
|
+
listener(event).forEach(handler => {
|
|
241
223
|
handler(data);
|
|
242
224
|
});
|
|
243
225
|
},
|
|
244
|
-
on
|
|
226
|
+
on(event, handler) {
|
|
245
227
|
listeners[event] = listener(event).concat(handler);
|
|
246
228
|
return {
|
|
247
|
-
off
|
|
248
|
-
listeners[event] = listener(event).filter(
|
|
249
|
-
}
|
|
229
|
+
off() {
|
|
230
|
+
listeners[event] = listener(event).filter(h => h !== handler);
|
|
231
|
+
},
|
|
250
232
|
};
|
|
251
|
-
}
|
|
233
|
+
},
|
|
252
234
|
};
|
|
253
235
|
function listener(event) {
|
|
254
236
|
return listeners[event] || [];
|
|
@@ -263,14 +245,15 @@
|
|
|
263
245
|
/**
|
|
264
246
|
* @returns a unique numeric id.
|
|
265
247
|
*/
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
248
|
+
const seq = genSeq();
|
|
249
|
+
function genSeq(namespace) {
|
|
250
|
+
return ((n) => () => `${namespace ? namespace + '_' : ''}${n++}`)(0);
|
|
251
|
+
}
|
|
269
252
|
|
|
270
253
|
function mapFirst(array, callback) {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
for (
|
|
254
|
+
let broke = false;
|
|
255
|
+
let breakoutValue = null;
|
|
256
|
+
for (let i = 0; i < array.length; i++) {
|
|
274
257
|
callback(array[i], breakout, i);
|
|
275
258
|
if (broke) {
|
|
276
259
|
return breakoutValue;
|
|
@@ -296,12 +279,29 @@
|
|
|
296
279
|
}
|
|
297
280
|
return false;
|
|
298
281
|
}
|
|
299
|
-
|
|
282
|
+
const isNotEmpty = bindNot(isEmpty);
|
|
300
283
|
|
|
301
284
|
function isPositive(value) {
|
|
302
285
|
return greaterThan(value, 0);
|
|
303
286
|
}
|
|
304
287
|
|
|
288
|
+
function createTinyState(initialValue) {
|
|
289
|
+
let value;
|
|
290
|
+
resetValue();
|
|
291
|
+
return () => [value, setValue, resetValue];
|
|
292
|
+
function setValue(nextValue) {
|
|
293
|
+
value = optionalFunctionValue(nextValue, value);
|
|
294
|
+
}
|
|
295
|
+
function resetValue() {
|
|
296
|
+
setValue(optionalFunctionValue(initialValue));
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
var tinyState = /*#__PURE__*/Object.freeze({
|
|
301
|
+
__proto__: null,
|
|
302
|
+
createTinyState: createTinyState
|
|
303
|
+
});
|
|
304
|
+
|
|
305
305
|
exports.StringObject = StringObject;
|
|
306
306
|
exports.asArray = asArray;
|
|
307
307
|
exports.assign = assign;
|
|
@@ -312,6 +312,7 @@
|
|
|
312
312
|
exports.defaultTo = defaultTo;
|
|
313
313
|
exports.deferThrow = deferThrow;
|
|
314
314
|
exports.either = either;
|
|
315
|
+
exports.genSeq = genSeq;
|
|
315
316
|
exports.greaterThan = greaterThan;
|
|
316
317
|
exports.hasOwnProperty = hasOwnProperty;
|
|
317
318
|
exports.invariant = invariant;
|
|
@@ -342,6 +343,7 @@
|
|
|
342
343
|
exports.numberNotEquals = numberNotEquals;
|
|
343
344
|
exports.optionalFunctionValue = optionalFunctionValue;
|
|
344
345
|
exports.seq = seq;
|
|
346
|
+
exports.tinyState = tinyState;
|
|
345
347
|
|
|
346
348
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
347
349
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!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){"use strict";function t(n){return
|
|
1
|
+
!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){"use strict";function t(n){return(...t)=>!n(...t)}function e(n){const t=String(n),e=Number(n),r=!isNaN(parseFloat(t))&&!isNaN(Number(n))&&isFinite(e);return Boolean(r)}const r=t(e);function o(n,t){return e(n)&&e(t)&&Number(n)===Number(t)}const u=t(o);function i(n,t){return o(n.length,t)}const c=t(i);function s(n,t){return e(n)&&e(t)&&Number(n)>Number(t)}function f(n,t){return s(n.length,t)}function l(n){return null===n}const a=t(l);function h(n){return void 0===n}const d=t(h);function g(n){return l(n)||h(n)}const p=t(g);function N(n){return[].concat(n)}function b(n){return"function"==typeof n}function y(n,...t){return b(n)?n(...t):n}function m(n,t){var e;return null!==(e=y(n))&&void 0!==e?e:y(t)}function v(n){return Boolean(Array.isArray(n))}const _=t(v);function E(n){const t=N(n);return t[t.length-1]}function O(n,t){let e=n;for(const n of t.slice(0,-1))e[n]=m(e[n],[]),e=e[n];return e}var j=Object.freeze({__proto__:null,transform:function n(t,e){const r=[];for(const o of t)if(v(o))r.push(n(o,e));else{const n=e(o);a(n)&&r.push(n)}return r},valueAtPath:function(n,t){return O(n,t)[E(t)]},setValueAtPath:function(n,t,e){return O(n,t)[E(t)]=e,n},flatten:function n(t){return N(t).reduce(((t,e)=>v(e)?t.concat(n(e)):N(t).concat(e)),[])},getCurrent:O});function S(n,t){return Object.prototype.hasOwnProperty.call(n,t)}var w=Object.assign;var A=Object.freeze({__proto__:null,createBus:function(){const n={};return{emit(n,e){t(n).forEach((n=>{n(e)}))},on:(e,r)=>(n[e]=t(e).concat(r),{off(){n[e]=t(e).filter((n=>n!==r))}})};function t(t){return n[t]||[]}}});const T=P();function P(n){return t=0,()=>`${n?n+"_":""}${t++}`;var t}function q(n){return!n||(S(n,"length")?i(n,0):"object"==typeof n&&i(Object.keys(n),0))}const F=t(q);var x=Object.freeze({__proto__:null,createTinyState:function(n){let t;return r(),()=>[t,e,r];function e(n){t=y(n,t)}function r(){e(y(n))}}});n.StringObject=function(n){return new String(y(n))},n.asArray=N,n.assign=w,n.bindNot=t,n.bus=A,n.cache=function(n=1){const t=[],e=(r,o)=>{const u=e.get(r);if(u)return u[1];const i=o();return t.unshift([r.concat(),i]),f(t,n)&&(t.length=n),i};return e.invalidate=n=>{const e=r(n);e>-1&&t.splice(e,1)},e.get=n=>t[r(n)]||null,e;function r(n){return t.findIndex((([t])=>i(n,t.length)&&n.every(((n,e)=>n===t[e]))))}},n.callEach=function(n){return n.forEach((n=>n()))},n.defaultTo=m,n.deferThrow=function(n){setTimeout((()=>{throw new Error(n)}),0)},n.either=function(n,t){return!!n!=!!t},n.genSeq=P,n.greaterThan=s,n.hasOwnProperty=S,n.invariant=function(n,t){if(!n)throw t instanceof String?t.valueOf():new Error(t?y(t):t)},n.isArray=v,n.isBoolean=function(n){return!!n===n},n.isEmpty=q,n.isFunction=b,n.isNotArray=_,n.isNotEmpty=F,n.isNotNull=a,n.isNotNullish=p,n.isNotNumeric=r,n.isNotUndefined=d,n.isNull=l,n.isNullish=g,n.isNumeric=e,n.isPositive=function(n){return s(n,0)},n.isPromise=function(n){return n&&b(n.then)},n.isStringValue=function(n){return String(n)===n},n.isUndefined=h,n.last=E,n.lengthEquals=i,n.lengthNotEquals=c,n.longerThan=f,n.mapFirst=function(n,t){let e=!1,r=null;for(let u=0;u<n.length;u++)if(t(n[u],o,u),e)return r;function o(n,t){n&&(e=!0,r=t)}},n.nestedArray=j,n.numberEquals=o,n.numberNotEquals=u,n.optionalFunctionValue=y,n.seq=T,n.tinyState=x,Object.defineProperty(n,"__esModule",{value:!0})}));
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.1.0-dev-
|
|
2
|
+
"version": "0.1.0-dev-d315d9",
|
|
3
3
|
"name": "vest-utils",
|
|
4
4
|
"author": "ealush",
|
|
5
5
|
"scripts": {
|
|
@@ -42,6 +42,6 @@
|
|
|
42
42
|
"default": "./dist/cjs/vest-utils.production.js"
|
|
43
43
|
},
|
|
44
44
|
"./package.json": "./package.json",
|
|
45
|
-
"
|
|
45
|
+
"./*": "./*"
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -95,8 +95,8 @@ describe('lib: cache', () => {
|
|
|
95
95
|
describe('On cache hit', () => {
|
|
96
96
|
it('Should return cached key and item from cache storage', () => {
|
|
97
97
|
const res = c([1, 2, 3], Math.random);
|
|
98
|
-
expect(c.get([1, 2, 3])[0]).toEqual([1, 2, 3]);
|
|
99
|
-
expect(c.get([1, 2, 3])[1]).toEqual(res);
|
|
98
|
+
expect(c.get([1, 2, 3])?.[0]).toEqual([1, 2, 3]);
|
|
99
|
+
expect(c.get([1, 2, 3])?.[1]).toEqual(res);
|
|
100
100
|
});
|
|
101
101
|
});
|
|
102
102
|
});
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
3
|
import { greaterThan } from 'greaterThan';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
let arg0;
|
|
5
|
+
const { random, datatype } = faker;
|
|
7
6
|
|
|
7
|
+
describe('Tests greaterThan rule', () => {
|
|
8
8
|
describe('Arguments are numbers', () => {
|
|
9
|
+
let arg0: number;
|
|
10
|
+
|
|
9
11
|
beforeEach(() => {
|
|
10
12
|
arg0 = datatype.number();
|
|
11
13
|
});
|
|
@@ -30,9 +32,15 @@ describe('Tests greaterThan rule', () => {
|
|
|
30
32
|
});
|
|
31
33
|
|
|
32
34
|
describe('Arguments are numeric strings', () => {
|
|
35
|
+
let arg0: string;
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
arg0 = datatype.number().toString();
|
|
39
|
+
});
|
|
40
|
+
|
|
33
41
|
describe('When first argument is larger', () => {
|
|
34
42
|
it('Should return true', () => {
|
|
35
|
-
expect(greaterThan(
|
|
43
|
+
expect(greaterThan('100', '99')).toBe(true);
|
|
36
44
|
});
|
|
37
45
|
});
|
|
38
46
|
|
|
@@ -52,6 +60,7 @@ describe('Tests greaterThan rule', () => {
|
|
|
52
60
|
describe('Arguments are non numeric', () => {
|
|
53
61
|
[random.word(), `${datatype.number()}`.split(''), {}].forEach(element => {
|
|
54
62
|
it('Should return false', () => {
|
|
63
|
+
// @ts-expect-error - testing invalid input
|
|
55
64
|
expect(greaterThan(element, 0)).toBe(false);
|
|
56
65
|
});
|
|
57
66
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import faker from 'faker';
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
3
|
import { lengthEquals } from 'lengthEquals';
|
|
4
4
|
|
|
@@ -46,10 +46,12 @@ describe('Tests lengthEquals rule', () => {
|
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
it('Should return false for number argument', () => {
|
|
49
|
-
expect
|
|
49
|
+
// @ts-expect-error - testing wrong input
|
|
50
|
+
expect(lengthEquals(100, 0)).toBe(false);
|
|
50
51
|
});
|
|
51
52
|
|
|
52
53
|
it('Should return false for boolean argument', () => {
|
|
54
|
+
// @ts-expect-error - testing wrong input
|
|
53
55
|
expect(lengthEquals(boolean, 0)).toBe(false);
|
|
54
56
|
});
|
|
55
57
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import faker from 'faker';
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
3
|
import { longerThan } from 'longerThan';
|
|
4
4
|
|
|
@@ -46,10 +46,12 @@ describe('Tests longerThan rule', () => {
|
|
|
46
46
|
});
|
|
47
47
|
|
|
48
48
|
it('Should return false for number argument', () => {
|
|
49
|
+
// @ts-expect-error - testing wrong input
|
|
49
50
|
expect(longerThan(length, 0)).toBe(false);
|
|
50
51
|
});
|
|
51
52
|
|
|
52
53
|
it('Should return false for boolean argument', () => {
|
|
54
|
+
// @ts-expect-error - testing wrong input
|
|
53
55
|
expect(longerThan(boolean, 0)).toBe(false);
|
|
54
56
|
});
|
|
55
57
|
});
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { faker } from '@faker-js/faker';
|
|
2
2
|
|
|
3
3
|
import { numberEquals } from 'numberEquals';
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
let arg0;
|
|
5
|
+
const { random, datatype } = faker;
|
|
7
6
|
|
|
7
|
+
describe('Tests numberEquals rule', () => {
|
|
8
8
|
describe('Arguments are numbers', () => {
|
|
9
|
+
let arg0: number;
|
|
9
10
|
beforeEach(() => {
|
|
10
11
|
arg0 = datatype.number();
|
|
11
12
|
});
|
|
@@ -30,9 +31,15 @@ describe('Tests numberEquals rule', () => {
|
|
|
30
31
|
});
|
|
31
32
|
|
|
32
33
|
describe('Arguments are numeric strings', () => {
|
|
34
|
+
let arg0: string;
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
arg0 = datatype.number().toString();
|
|
38
|
+
});
|
|
39
|
+
|
|
33
40
|
describe('When first argument is larger', () => {
|
|
34
41
|
it('Should return false', () => {
|
|
35
|
-
expect(numberEquals(`${arg0}`, `${arg0 - 1}`)).toBe(false);
|
|
42
|
+
expect(numberEquals(`${arg0}`, `${Number(arg0) - 1}`)).toBe(false);
|
|
36
43
|
});
|
|
37
44
|
});
|
|
38
45
|
|
|
@@ -44,7 +51,7 @@ describe('Tests numberEquals rule', () => {
|
|
|
44
51
|
|
|
45
52
|
describe('When values are equal', () => {
|
|
46
53
|
it('Should return true', () => {
|
|
47
|
-
expect(numberEquals(
|
|
54
|
+
expect(numberEquals('100', '100')).toBe(true);
|
|
48
55
|
});
|
|
49
56
|
});
|
|
50
57
|
});
|
|
@@ -52,6 +59,7 @@ describe('Tests numberEquals rule', () => {
|
|
|
52
59
|
describe('Arguments are non numeric', () => {
|
|
53
60
|
[random.word(), `${datatype.number()}`.split(''), {}].forEach(element => {
|
|
54
61
|
it('Should return false', () => {
|
|
62
|
+
// @ts-expect-error - testing invalid input
|
|
55
63
|
expect(numberEquals(element, 0)).toBe(false);
|
|
56
64
|
});
|
|
57
65
|
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import seq from 'seq';
|
|
1
|
+
import seq, { genSeq } from 'seq';
|
|
2
2
|
|
|
3
3
|
describe('lib:seq', () => {
|
|
4
4
|
it('Should return a new id on each run', () => {
|
|
@@ -9,4 +9,20 @@ describe('lib:seq', () => {
|
|
|
9
9
|
return existing;
|
|
10
10
|
}, {});
|
|
11
11
|
});
|
|
12
|
+
|
|
13
|
+
describe('genSeq', () => {
|
|
14
|
+
it('Creates a namespaced sequence', () => {
|
|
15
|
+
const seq = genSeq('test');
|
|
16
|
+
expect(seq()).toBe('test_0');
|
|
17
|
+
expect(seq()).toBe('test_1');
|
|
18
|
+
expect(seq()).toBe('test_2');
|
|
19
|
+
|
|
20
|
+
const seq2 = genSeq('test2');
|
|
21
|
+
expect(seq2()).toBe('test2_0');
|
|
22
|
+
expect(seq2()).toBe('test2_1');
|
|
23
|
+
expect(seq2()).toBe('test2_2');
|
|
24
|
+
|
|
25
|
+
expect(seq()).toBe('test_3');
|
|
26
|
+
});
|
|
27
|
+
});
|
|
12
28
|
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createTinyState } from 'tinyState';
|
|
2
|
+
|
|
3
|
+
describe('tinyTest', () => {
|
|
4
|
+
it('Should be a function', () => {
|
|
5
|
+
expect(typeof createTinyState).toBe('function');
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
it('Should return a function', () => {
|
|
9
|
+
expect(typeof createTinyState(1)).toBe('function');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('Should return a function that returns an array', () => {
|
|
13
|
+
expect(Array.isArray(createTinyState(1)())).toBe(true);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it('Should return a function that returns an array with three items', () => {
|
|
17
|
+
expect(createTinyState(1)()).toHaveLength(3);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('Should return a function that returns an array with three items, the first being the initial value', () => {
|
|
21
|
+
expect(createTinyState('initial_value')()[0]).toBe('initial_value');
|
|
22
|
+
|
|
23
|
+
const initialValue = {};
|
|
24
|
+
expect(createTinyState(initialValue)()[0]).toBe(initialValue);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('Should return a function that returns an array with three items, the second being a function', () => {
|
|
28
|
+
expect(typeof createTinyState(1)()[1]).toBe('function');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('Should return a function that returns an array with three items, the third being a function', () => {
|
|
32
|
+
expect(typeof createTinyState(1)()[2]).toBe('function');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('Updates the value when the second item is called', () => {
|
|
36
|
+
const testState = createTinyState('initial_value');
|
|
37
|
+
|
|
38
|
+
{
|
|
39
|
+
const [, setValue] = testState();
|
|
40
|
+
setValue('new_value');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
{
|
|
44
|
+
const [value] = testState();
|
|
45
|
+
expect(value).toBe('new_value');
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('resets the value when the third item is called', () => {
|
|
50
|
+
const testState = createTinyState('initial_value');
|
|
51
|
+
|
|
52
|
+
{
|
|
53
|
+
const [, setValue] = testState();
|
|
54
|
+
setValue('new_value');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
{
|
|
58
|
+
const [, , resetValue] = testState();
|
|
59
|
+
resetValue();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
{
|
|
63
|
+
const [value] = testState();
|
|
64
|
+
expect(value).toBe('initial_value');
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
package/src/bus.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import type { CB } from 'utilityTypes';
|
|
2
2
|
|
|
3
|
-
export function createBus(): {
|
|
4
|
-
on: (event: string, handler: CB) => OnReturn;
|
|
5
|
-
emit: (event: string, ...args: any[]) => void;
|
|
6
|
-
} {
|
|
3
|
+
export function createBus(): BusType {
|
|
7
4
|
const listeners: Record<string, CB[]> = {};
|
|
8
5
|
|
|
9
6
|
return {
|
|
@@ -30,3 +27,8 @@ export function createBus(): {
|
|
|
30
27
|
}
|
|
31
28
|
|
|
32
29
|
type OnReturn = { off: () => void };
|
|
30
|
+
|
|
31
|
+
export type BusType = {
|
|
32
|
+
on: (event: string, handler: CB) => OnReturn;
|
|
33
|
+
emit: (event: string, ...args: any[]) => void;
|
|
34
|
+
};
|
package/src/cache.ts
CHANGED
|
@@ -4,14 +4,16 @@ import { longerThan } from 'longerThan';
|
|
|
4
4
|
/**
|
|
5
5
|
* Creates a cache function
|
|
6
6
|
*/
|
|
7
|
-
export default function createCache
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
export default function createCache<T = unknown>(
|
|
8
|
+
maxSize = 1
|
|
9
|
+
): {
|
|
10
|
+
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T;
|
|
11
|
+
get(deps: unknown[]): [unknown[], T] | null;
|
|
10
12
|
invalidate(item: any): void;
|
|
11
13
|
} {
|
|
12
|
-
const cacheStorage: Array<[unknown[],
|
|
14
|
+
const cacheStorage: Array<[unknown[], T]> = [];
|
|
13
15
|
|
|
14
|
-
const cache =
|
|
16
|
+
const cache = (
|
|
15
17
|
deps: unknown[],
|
|
16
18
|
cacheAction: (...args: unknown[]) => T
|
|
17
19
|
): T => {
|
|
@@ -34,7 +36,7 @@ export default function createCache(maxSize = 1): {
|
|
|
34
36
|
};
|
|
35
37
|
|
|
36
38
|
// Retrieves an item from the cache.
|
|
37
|
-
cache.get = (deps: unknown[]): [unknown[],
|
|
39
|
+
cache.get = (deps: unknown[]): [unknown[], T] | null =>
|
|
38
40
|
cacheStorage[findIndex(deps)] || null;
|
|
39
41
|
|
|
40
42
|
return cache;
|
|
@@ -47,3 +49,9 @@ export default function createCache(maxSize = 1): {
|
|
|
47
49
|
);
|
|
48
50
|
}
|
|
49
51
|
}
|
|
52
|
+
|
|
53
|
+
export type CacheApi<T = unknown> = {
|
|
54
|
+
(deps: unknown[], cacheAction: (...args: unknown[]) => T): T;
|
|
55
|
+
get(deps: unknown[]): [unknown[], T] | null;
|
|
56
|
+
invalidate(item: any): void;
|
|
57
|
+
};
|
package/src/deferThrow.ts
CHANGED
package/src/seq.ts
CHANGED
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
* @returns a unique numeric id.
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
const seq
|
|
6
|
-
(n: number) => (): string =>
|
|
7
|
-
`${n++}`
|
|
8
|
-
)(0);
|
|
5
|
+
const seq = genSeq();
|
|
9
6
|
|
|
10
7
|
export default seq;
|
|
8
|
+
|
|
9
|
+
export function genSeq(namespace?: string): () => string {
|
|
10
|
+
return (
|
|
11
|
+
(n: number) => () =>
|
|
12
|
+
`${namespace ? namespace + '_' : ''}${n++}`
|
|
13
|
+
)(0);
|
|
14
|
+
}
|