vest-utils 0.1.0-dev-c4ba9b → 0.1.0-dev-405df5

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.
@@ -5,32 +5,26 @@
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
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
- };
8
+ return (...args) => !fn(...args);
15
9
  }
16
10
 
17
11
  function isNumeric(value) {
18
- var str = String(value);
19
- var num = Number(value);
20
- var result = !isNaN(parseFloat(str)) && !isNaN(Number(value)) && isFinite(num);
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
- var isNotNumeric = bindNot(isNumeric);
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
- var numberNotEquals = bindNot(numberEquals);
22
+ const numberNotEquals = bindNot(numberEquals);
29
23
 
30
24
  function lengthEquals(value, arg1) {
31
25
  return numberEquals(value.length, arg1);
32
26
  }
33
- var lengthNotEquals = bindNot(lengthEquals);
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
- if (maxSize === void 0) { maxSize = 1; }
48
- var cacheStorage = [];
49
- var cache = function (deps, cacheAction) {
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
- var result = cacheAction();
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 = function (deps) {
62
- var index = findIndex(deps);
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 = function (deps) {
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(function (_a) {
73
- var cachedDeps = _a[0];
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
- var isNotNull = bindNot(isNull);
71
+ const isNotNull = bindNot(isNull);
84
72
 
85
73
  function isUndefined(value) {
86
74
  return value === undefined;
87
75
  }
88
- var isNotUndefined = bindNot(isUndefined);
76
+ const isNotUndefined = bindNot(isUndefined);
89
77
 
90
78
  function isNullish(value) {
91
79
  return isNull(value) || isUndefined(value);
92
80
  }
93
- var isNotNullish = bindNot(isNullish);
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
- var args = [];
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
- var isNotArray = bindNot(isArray);
106
+ const isNotArray = bindNot(isArray);
123
107
 
124
108
  function last(values) {
125
- var valuesArray = asArray(values);
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
- var res = [];
134
- for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
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
- var output = cb(v);
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
- var current = getCurrent(array, path);
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(function (acc, value) {
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
- var current = array;
166
- for (var _i = 0, _a = path.slice(0, -1); _i < _a.length; _i++) {
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(function (fn) { return fn(); });
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(function () {
213
+ setTimeout(() => {
232
214
  throw new Error(message);
233
215
  }, 0);
234
216
  }
235
217
 
236
218
  function createBus() {
237
- var listeners = {};
219
+ const listeners = {};
238
220
  return {
239
- emit: function (event, data) {
240
- listener(event).forEach(function (handler) {
221
+ emit(event, data) {
222
+ listener(event).forEach(handler => {
241
223
  handler(data);
242
224
  });
243
225
  },
244
- on: function (event, handler) {
226
+ on(event, handler) {
245
227
  listeners[event] = listener(event).concat(handler);
246
228
  return {
247
- off: function () {
248
- listeners[event] = listener(event).filter(function (h) { return h !== handler; });
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
- var seq = (function (n) { return function () {
267
- return "".concat(n++);
268
- }; })(0);
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
- var broke = false;
272
- var breakoutValue = null;
273
- for (var i = 0; i < array.length; i++) {
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
- var isNotEmpty = bindNot(isEmpty);
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 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=String(n),r=Number(n),e=!isNaN(parseFloat(t))&&!isNaN(Number(n))&&isFinite(r);return Boolean(e)}var e=t(r);function u(n,t){return r(n)&&r(t)&&Number(n)===Number(t)}var i=t(u);function o(n,t){return u(n.length,t)}var f=t(o);function c(n,t){return r(n)&&r(t)&&Number(n)>Number(t)}function a(n,t){return c(n.length,t)}function l(n){return null===n}var s=t(l);function v(n){return void 0===n}var h=t(v);function d(n){return l(n)||v(n)}var g=t(d);function p(n){return[].concat(n)}function N(n){return"function"==typeof n}function y(n){for(var t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return N(n)?n.apply(void 0,t):n}function b(n,t){var r;return null!==(r=y(n))&&void 0!==r?r:y(t)}function m(n){return Boolean(Array.isArray(n))}var E=t(m);function O(n){var t=p(n);return t[t.length-1]}function _(n,t){for(var r=n,e=0,u=t.slice(0,-1);e<u.length;e++){var i=u[e];r[i]=b(r[i],[]),r=r[i]}return r}var j=Object.freeze({__proto__:null,transform:function n(t,r){for(var e=[],u=0,i=t;u<i.length;u++){var o=i[u];if(m(o))e.push(n(o,r));else{var f=r(o);s(f)&&e.push(f)}}return e},valueAtPath:function(n,t){return _(n,t)[O(t)]},setValueAtPath:function(n,t,r){return _(n,t)[O(t)]=r,n},flatten:function n(t){return p(t).reduce((function(t,r){return m(r)?t.concat(n(r)):p(t).concat(r)}),[])},getCurrent:_});function w(n,t){return Object.prototype.hasOwnProperty.call(n,t)}var A=Object.assign;var P,T=Object.freeze({__proto__:null,createBus:function(){var n={};return{emit:function(n,r){t(n).forEach((function(n){n(r)}))},on:function(r,e){return n[r]=t(r).concat(e),{off:function(){n[r]=t(r).filter((function(n){return n!==e}))}}}};function t(t){return n[t]||[]}}}),S=(P=0,function(){return"".concat(P++)});function q(n){return!n||(w(n,"length")?o(n,0):"object"==typeof n&&o(Object.keys(n),0))}var F=t(q);n.StringObject=function(n){return new String(y(n))},n.asArray=p,n.assign=A,n.bindNot=t,n.bus=T,n.cache=function(n){void 0===n&&(n=1);var t=[],r=function(e,u){var i=r.get(e);if(i)return i[1];var o=u();return t.unshift([e.concat(),o]),a(t,n)&&(t.length=n),o};return r.invalidate=function(n){var r=e(n);r>-1&&t.splice(r,1)},r.get=function(n){return t[e(n)]||null},r;function e(n){return t.findIndex((function(t){var r=t[0];return o(n,r.length)&&n.every((function(n,t){return n===r[t]}))}))}},n.callEach=function(n){return n.forEach((function(n){return n()}))},n.defaultTo=b,n.deferThrow=function(n){setTimeout((function(){throw new Error(n)}),0)},n.either=function(n,t){return!!n!=!!t},n.greaterThan=c,n.hasOwnProperty=w,n.invariant=function(n,t){if(!n)throw t instanceof String?t.valueOf():new Error(t?y(t):t)},n.isArray=m,n.isBoolean=function(n){return!!n===n},n.isEmpty=q,n.isFunction=N,n.isNotArray=E,n.isNotEmpty=F,n.isNotNull=s,n.isNotNullish=g,n.isNotNumeric=e,n.isNotUndefined=h,n.isNull=l,n.isNullish=d,n.isNumeric=r,n.isPositive=function(n){return c(n,0)},n.isPromise=function(n){return n&&N(n.then)},n.isStringValue=function(n){return String(n)===n},n.isUndefined=v,n.last=O,n.lengthEquals=o,n.lengthNotEquals=f,n.longerThan=a,n.mapFirst=function(n,t){for(var r=!1,e=null,u=0;u<n.length;u++)if(t(n[u],i,u),r)return e;function i(n,t){n&&(r=!0,e=t)}},n.nestedArray=j,n.numberEquals=u,n.numberNotEquals=i,n.optionalFunctionValue=y,n.seq=S,Object.defineProperty(n,"__esModule",{value:!0})}));
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-c4ba9b",
2
+ "version": "0.1.0-dev-405df5",
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 { random, datatype } from 'faker';
1
+ import { faker } from '@faker-js/faker';
2
2
 
3
3
  import { greaterThan } from 'greaterThan';
4
4
 
5
- describe('Tests greaterThan rule', () => {
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(`${arg0}`, `${arg0 - 1}`)).toBe(true);
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(lengthEquals(length, 0)).toBe(false);
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 { random, datatype } from 'faker';
1
+ import { faker } from '@faker-js/faker';
2
2
 
3
3
  import { numberEquals } from 'numberEquals';
4
4
 
5
- describe('Tests numberEquals rule', () => {
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(arg0, arg0)).toBe(true);
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(maxSize = 1): {
8
- <T>(deps: unknown[], cacheAction: (...args: unknown[]) => T): T;
9
- get(deps: unknown[]): any;
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[], any]> = [];
14
+ const cacheStorage: Array<[unknown[], T]> = [];
13
15
 
14
- const cache = <T>(
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[], any] | null =>
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
@@ -3,3 +3,5 @@ export default function deferThrow(message?: string): void {
3
3
  throw new Error(message);
4
4
  }, 0);
5
5
  }
6
+
7
+ export type TDeferThrow = typeof deferThrow;
package/src/seq.ts CHANGED
@@ -2,9 +2,13 @@
2
2
  * @returns a unique numeric id.
3
3
  */
4
4
 
5
- const seq: () => string = (
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
+ }