vest-utils 0.1.1 → 1.0.0-dev-ec989a

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.
Files changed (61) hide show
  1. package/dist/cjs/vest-utils.development.js +132 -138
  2. package/dist/cjs/vest-utils.production.js +1 -1
  3. package/dist/es/vest-utils.development.js +129 -136
  4. package/dist/es/vest-utils.production.js +1 -1
  5. package/dist/umd/vest-utils.development.js +132 -138
  6. package/dist/umd/vest-utils.production.js +1 -1
  7. package/package.json +2 -2
  8. package/types/vest-utils.d.ts +49 -20
  9. package/types/vest-utils.d.ts.map +1 -1
  10. package/src/__tests__/bindNot.test.ts +0 -37
  11. package/src/__tests__/bus.test.ts +0 -81
  12. package/src/__tests__/cache.test.ts +0 -113
  13. package/src/__tests__/defaultTo.test.ts +0 -50
  14. package/src/__tests__/deferThrow.test.ts +0 -24
  15. package/src/__tests__/greaterThan.test.ts +0 -59
  16. package/src/__tests__/invariant.test.ts +0 -45
  17. package/src/__tests__/isArray.test.ts +0 -15
  18. package/src/__tests__/isNull.test.ts +0 -25
  19. package/src/__tests__/isNumeric.test.ts +0 -26
  20. package/src/__tests__/isUndefined.test.ts +0 -26
  21. package/src/__tests__/lengthEquals.test.ts +0 -56
  22. package/src/__tests__/longerThan.test.ts +0 -56
  23. package/src/__tests__/mapFirst.test.ts +0 -29
  24. package/src/__tests__/numberEquals.test.ts +0 -59
  25. package/src/__tests__/optionalFunctionValue.test.ts +0 -27
  26. package/src/__tests__/seq.test.ts +0 -28
  27. package/src/asArray.ts +0 -3
  28. package/src/assign.ts +0 -1
  29. package/src/bindNot.ts +0 -3
  30. package/src/bus.ts +0 -32
  31. package/src/cache.ts +0 -57
  32. package/src/callEach.ts +0 -5
  33. package/src/defaultTo.ts +0 -8
  34. package/src/deferThrow.ts +0 -5
  35. package/src/either.ts +0 -3
  36. package/src/globals.d.ts +0 -3
  37. package/src/greaterThan.ts +0 -8
  38. package/src/hasOwnProperty.ts +0 -9
  39. package/src/invariant.ts +0 -24
  40. package/src/isArrayValue.ts +0 -11
  41. package/src/isBooleanValue.ts +0 -3
  42. package/src/isEmpty.ts +0 -17
  43. package/src/isFunction.ts +0 -5
  44. package/src/isNull.ts +0 -7
  45. package/src/isNullish.ts +0 -9
  46. package/src/isNumeric.ts +0 -11
  47. package/src/isPositive.ts +0 -5
  48. package/src/isPromise.ts +0 -5
  49. package/src/isStringValue.ts +0 -3
  50. package/src/isUndefined.ts +0 -7
  51. package/src/last.ts +0 -7
  52. package/src/lengthEquals.ts +0 -11
  53. package/src/longerThan.ts +0 -8
  54. package/src/mapFirst.ts +0 -25
  55. package/src/nestedArray.ts +0 -69
  56. package/src/numberEquals.ts +0 -11
  57. package/src/optionalFunctionValue.ts +0 -8
  58. package/src/seq.ts +0 -14
  59. package/src/utilityTypes.ts +0 -9
  60. package/src/vest-utils.ts +0 -36
  61. package/tsconfig.json +0 -46
@@ -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,145 +37,55 @@
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);
97
85
  }
98
86
 
99
- function isFunction(value) {
100
- return typeof value === 'function';
101
- }
102
-
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;
109
- }
110
-
111
- function defaultTo(value, defaultValue) {
112
- var _a;
113
- return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
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
-
124
- function last(values) {
125
- var valuesArray = asArray(values);
126
- return valuesArray[valuesArray.length - 1];
127
- }
128
-
129
- // This is kind of a map/filter in one function.
130
- // Normally, behaves like a nested-array map,
131
- // but returning `null` will drop the element from the array
132
- 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];
136
- if (isArray(v)) {
137
- res.push(transform(v, cb));
138
- }
139
- else {
140
- var output = cb(v);
141
- if (isNotNull(output)) {
142
- res.push(output);
143
- }
144
- }
145
- }
146
- return res;
147
- }
148
- function valueAtPath(array, path) {
149
- return getCurrent(array, path)[last(path)];
150
- }
151
- function setValueAtPath(array, path, value) {
152
- var current = getCurrent(array, path);
153
- current[last(path)] = value;
154
- return array;
155
- }
156
- function flatten(values) {
157
- return asArray(values).reduce(function (acc, value) {
158
- if (isArray(value)) {
159
- return acc.concat(flatten(value));
160
- }
161
- return asArray(acc).concat(value);
162
- }, []);
163
- }
164
- 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];
168
- current[p] = defaultTo(current[p], []);
169
- current = current[p];
170
- }
171
- return current;
172
- }
173
-
174
- var nestedArray = /*#__PURE__*/Object.freeze({
175
- __proto__: null,
176
- transform: transform,
177
- valueAtPath: valueAtPath,
178
- setValueAtPath: setValueAtPath,
179
- flatten: flatten,
180
- getCurrent: getCurrent
181
- });
182
-
183
87
  function callEach(arr) {
184
- return arr.forEach(function (fn) { return fn(); });
88
+ return arr.forEach(fn => fn());
185
89
  }
186
90
 
187
91
  /**
@@ -191,12 +95,25 @@
191
95
  return Object.prototype.hasOwnProperty.call(obj, key);
192
96
  }
193
97
 
98
+ function isFunction(value) {
99
+ return typeof value === 'function';
100
+ }
101
+
194
102
  function isPromise(value) {
195
103
  return value && isFunction(value.then);
196
104
  }
197
105
 
106
+ function optionalFunctionValue(value, ...args) {
107
+ return isFunction(value) ? value(...args) : value;
108
+ }
109
+
198
110
  var assign = Object.assign;
199
111
 
112
+ function defaultTo(value, defaultValue) {
113
+ var _a;
114
+ return (_a = optionalFunctionValue(value)) !== null && _a !== void 0 ? _a : optionalFunctionValue(defaultValue);
115
+ }
116
+
200
117
  function invariant(condition,
201
118
  // eslint-disable-next-line @typescript-eslint/ban-types
202
119
  message) {
@@ -227,28 +144,33 @@
227
144
  return !!value === value;
228
145
  }
229
146
 
147
+ function last(values) {
148
+ const valuesArray = asArray(values);
149
+ return valuesArray[valuesArray.length - 1];
150
+ }
151
+
230
152
  function deferThrow(message) {
231
- setTimeout(function () {
153
+ setTimeout(() => {
232
154
  throw new Error(message);
233
155
  }, 0);
234
156
  }
235
157
 
236
158
  function createBus() {
237
- var listeners = {};
159
+ const listeners = {};
238
160
  return {
239
- emit: function (event, data) {
240
- listener(event).forEach(function (handler) {
161
+ emit(event, data) {
162
+ listener(event).forEach(handler => {
241
163
  handler(data);
242
164
  });
243
165
  },
244
- on: function (event, handler) {
166
+ on(event, handler) {
245
167
  listeners[event] = listener(event).concat(handler);
246
168
  return {
247
- off: function () {
248
- listeners[event] = listener(event).filter(function (h) { return h !== handler; });
249
- }
169
+ off() {
170
+ listeners[event] = listener(event).filter(h => h !== handler);
171
+ },
250
172
  };
251
- }
173
+ },
252
174
  };
253
175
  function listener(event) {
254
176
  return listeners[event] || [];
@@ -263,17 +185,15 @@
263
185
  /**
264
186
  * @returns a unique numeric id.
265
187
  */
266
- var seq = genSeq();
188
+ const seq = genSeq();
267
189
  function genSeq(namespace) {
268
- return (function (n) { return function () {
269
- return "".concat(namespace ? namespace + '_' : '').concat(n++);
270
- }; })(0);
190
+ return ((n) => () => `${namespace ? namespace + '_' : ''}${n++}`)(0);
271
191
  }
272
192
 
273
193
  function mapFirst(array, callback) {
274
- var broke = false;
275
- var breakoutValue = null;
276
- for (var i = 0; i < array.length; i++) {
194
+ let broke = false;
195
+ let breakoutValue = null;
196
+ for (let i = 0; i < array.length; i++) {
277
197
  callback(array[i], breakout, i);
278
198
  if (broke) {
279
199
  return breakoutValue;
@@ -287,6 +207,18 @@
287
207
  }
288
208
  }
289
209
 
210
+ function isObject(v) {
211
+ return typeof v === 'object' && !isNullish(v);
212
+ }
213
+
214
+ // The module is named "isArrayValue" since it
215
+ // is conflicting with a nested npm dependency.
216
+ // We may need to revisit this in the future.
217
+ function isArray(value) {
218
+ return Boolean(Array.isArray(value));
219
+ }
220
+ const isNotArray = bindNot(isArray);
221
+
290
222
  function isEmpty(value) {
291
223
  if (!value) {
292
224
  return true;
@@ -294,17 +226,79 @@
294
226
  else if (hasOwnProperty(value, 'length')) {
295
227
  return lengthEquals(value, 0);
296
228
  }
297
- else if (typeof value === 'object') {
229
+ else if (isObject(value)) {
298
230
  return lengthEquals(Object.keys(value), 0);
299
231
  }
300
232
  return false;
301
233
  }
302
- var isNotEmpty = bindNot(isEmpty);
234
+ const isNotEmpty = bindNot(isEmpty);
303
235
 
304
236
  function isPositive(value) {
305
237
  return greaterThan(value, 0);
306
238
  }
307
239
 
240
+ const regexp = /{(.*?)}/g;
241
+ function text(str, ...substitutions) {
242
+ const first = substitutions[0];
243
+ if (isObject(first)) {
244
+ return str.replace(regexp, (placeholder, key) => {
245
+ var _a;
246
+ return `${(_a = first[key]) !== null && _a !== void 0 ? _a : placeholder}`;
247
+ });
248
+ }
249
+ const subs = [...substitutions];
250
+ return str.replace(regexp, placeholder => {
251
+ return `${isEmpty(subs) ? placeholder : subs.shift()}`;
252
+ });
253
+ }
254
+
255
+ const STATE_WILD_CARD = '*';
256
+ function StateMachine(machine) {
257
+ let state = machine.initial;
258
+ const api = { getState, transition };
259
+ return api;
260
+ function getState() {
261
+ return state;
262
+ }
263
+ // eslint-disable-next-line complexity
264
+ function transition(action, payload) {
265
+ var _a, _b, _c;
266
+ const transitionTo = (_b = (_a = machine.states[state]) === null || _a === void 0 ? void 0 : _a[action]) !== null && _b !== void 0 ? _b :
267
+ // @ts-expect-error - This is a valid state
268
+ (_c = machine.states[STATE_WILD_CARD]) === null || _c === void 0 ? void 0 : _c[action];
269
+ let target = transitionTo;
270
+ if (Array.isArray(target)) {
271
+ const [, conditional] = target;
272
+ if (!conditional(payload)) {
273
+ return;
274
+ }
275
+ target = target[0];
276
+ }
277
+ if (!target || target === state) {
278
+ return;
279
+ }
280
+ state = target;
281
+ }
282
+ }
283
+
284
+ function createTinyState(initialValue) {
285
+ let value;
286
+ resetValue();
287
+ return () => [value, setValue, resetValue];
288
+ function setValue(nextValue) {
289
+ value = optionalFunctionValue(nextValue, value);
290
+ }
291
+ function resetValue() {
292
+ setValue(optionalFunctionValue(initialValue));
293
+ }
294
+ }
295
+
296
+ var tinyState = /*#__PURE__*/Object.freeze({
297
+ __proto__: null,
298
+ createTinyState: createTinyState
299
+ });
300
+
301
+ exports.StateMachine = StateMachine;
308
302
  exports.StringObject = StringObject;
309
303
  exports.asArray = asArray;
310
304
  exports.assign = assign;
@@ -332,6 +326,7 @@
332
326
  exports.isNull = isNull;
333
327
  exports.isNullish = isNullish;
334
328
  exports.isNumeric = isNumeric;
329
+ exports.isObject = isObject;
335
330
  exports.isPositive = isPositive;
336
331
  exports.isPromise = isPromise;
337
332
  exports.isStringValue = isStringValue;
@@ -341,12 +336,11 @@
341
336
  exports.lengthNotEquals = lengthNotEquals;
342
337
  exports.longerThan = longerThan;
343
338
  exports.mapFirst = mapFirst;
344
- exports.nestedArray = nestedArray;
345
339
  exports.numberEquals = numberEquals;
346
340
  exports.numberNotEquals = numberNotEquals;
347
341
  exports.optionalFunctionValue = optionalFunctionValue;
348
342
  exports.seq = seq;
349
-
350
- Object.defineProperty(exports, '__esModule', { value: true });
343
+ exports.text = text;
344
+ exports.tinyState = tinyState;
351
345
 
352
346
  }));
@@ -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 c=t(o);function f(n,t){return r(n)&&r(t)&&Number(n)>Number(t)}function a(n,t){return f(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 g(n){return l(n)||v(n)}var d=t(g);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 _(n){var t=p(n);return t[t.length-1]}function O(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 c=r(o);s(c)&&e.push(c)}}return e},valueAtPath:function(n,t){return O(n,t)[_(t)]},setValueAtPath:function(n,t,r){return O(n,t)[_(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:O});function w(n,t){return Object.prototype.hasOwnProperty.call(n,t)}var A=Object.assign;var P=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=T();function T(n){return t=0,function(){return"".concat(n?n+"_":"").concat(t++)};var t}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=P,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.genSeq=T,n.greaterThan=f,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=d,n.isNotNumeric=e,n.isNotUndefined=h,n.isNull=l,n.isNullish=g,n.isNumeric=r,n.isPositive=function(n){return f(n,0)},n.isPromise=function(n){return n&&N(n.then)},n.isStringValue=function(n){return String(n)===n},n.isUndefined=v,n.last=_,n.lengthEquals=o,n.lengthNotEquals=c,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 i(n,t){return e(n)&&e(t)&&Number(n)===Number(t)}const o=t(i);function u(n,t){return i(n.length,t)}const c=t(u);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 N=t(g);function p(n){return[].concat(n)}function y(n,t){return Object.prototype.hasOwnProperty.call(n,t)}function b(n){return"function"==typeof n}function v(n,...t){return b(n)?n(...t):n}var m=Object.assign;var E=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 S=O();function O(n){return t=0,()=>`${n?n+"_":""}${t++}`;var t}function j(n){return"object"==typeof n&&!g(n)}function _(n){return Boolean(Array.isArray(n))}const w=t(_);function T(n){return!n||(y(n,"length")?u(n,0):!!j(n)&&u(Object.keys(n),0))}const A=t(T);const q=/{(.*?)}/g;var x=Object.freeze({__proto__:null,createTinyState:function(n){let t;return r(),()=>[t,e,r];function e(n){t=v(n,t)}function r(){e(v(n))}}});n.StateMachine=function(n){let t=n.initial;return{getState:function(){return t},transition:function(e,r){var i,o,u;let c=null!==(o=null===(i=n.states[t])||void 0===i?void 0:i[e])&&void 0!==o?o:null===(u=n.states["*"])||void 0===u?void 0:u[e];if(Array.isArray(c)){const[,n]=c;if(!n(r))return;c=c[0]}if(!c||c===t)return;t=c}}},n.StringObject=function(n){return new String(v(n))},n.asArray=p,n.assign=m,n.bindNot=t,n.bus=E,n.cache=function(n=1){const t=[],e=(r,i)=>{const o=e.get(r);if(o)return o[1];const u=i();return t.unshift([r.concat(),u]),f(t,n)&&(t.length=n),u};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])=>u(n,t.length)&&n.every(((n,e)=>n===t[e]))))}},n.callEach=function(n){return n.forEach((n=>n()))},n.defaultTo=function(n,t){var e;return null!==(e=v(n))&&void 0!==e?e:v(t)},n.deferThrow=function(n){setTimeout((()=>{throw new Error(n)}),0)},n.either=function(n,t){return!!n!=!!t},n.genSeq=O,n.greaterThan=s,n.hasOwnProperty=y,n.invariant=function(n,t){if(!n)throw t instanceof String?t.valueOf():new Error(t?v(t):t)},n.isArray=_,n.isBoolean=function(n){return!!n===n},n.isEmpty=T,n.isFunction=b,n.isNotArray=w,n.isNotEmpty=A,n.isNotNull=a,n.isNotNullish=N,n.isNotNumeric=r,n.isNotUndefined=d,n.isNull=l,n.isNullish=g,n.isNumeric=e,n.isObject=j,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=function(n){const t=p(n);return t[t.length-1]},n.lengthEquals=u,n.lengthNotEquals=c,n.longerThan=f,n.mapFirst=function(n,t){let e=!1,r=null;for(let o=0;o<n.length;o++)if(t(n[o],i,o),e)return r;function i(n,t){n&&(e=!0,r=t)}},n.numberEquals=i,n.numberNotEquals=o,n.optionalFunctionValue=v,n.seq=S,n.text=function(n,...t){const e=t[0];if(j(e))return n.replace(q,((n,t)=>{var r;return`${null!==(r=e[t])&&void 0!==r?r:n}`}));const r=[...t];return n.replace(q,(n=>`${T(r)?n:r.shift()}`))},n.tinyState=x}));
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.1",
2
+ "version": "1.0.0-dev-ec989a",
3
3
  "name": "vest-utils",
4
4
  "author": "ealush",
5
5
  "scripts": {
@@ -44,4 +44,4 @@
44
44
  "./package.json": "./package.json",
45
45
  "./*": "./*"
46
46
  }
47
- }
47
+ }
@@ -17,20 +17,6 @@ type CacheApi<T = unknown> = {
17
17
  ] | null;
18
18
  invalidate(item: any): void;
19
19
  };
20
- declare function isNullish(value: any): value is null | undefined;
21
- declare const isNotNullish: (value: any) => boolean;
22
- declare namespace nestedArray {
23
- type NestedArray<T> = Array<NestedArray<T> | T>;
24
- // This is kind of a map/filter in one function.
25
- // Normally, behaves like a nested-array map,
26
- // but returning `null` will drop the element from the array
27
- function transform<T>(array: NestedArray<T>, cb: (value: T) => NestedArray<T> | T | null): NestedArray<T>;
28
- function valueAtPath<T>(array: NestedArray<T>, path: number[]): T | NestedArray<T>;
29
- function setValueAtPath<T>(array: NestedArray<T>, path: number[], value: NestedArray<T> | T): NestedArray<T>;
30
- function flatten<T>(values: NestedArray<T> | T): T[];
31
- function getCurrent<T>(array: NestedArray<T>, path: number[]): NestedArray<T>;
32
- }
33
- declare function asArray<T>(possibleArg: T | T[]): T[];
34
20
  type DropFirst<T extends unknown[]> = T extends [
35
21
  unknown,
36
22
  ...infer U
@@ -38,6 +24,21 @@ type DropFirst<T extends unknown[]> = T extends [
38
24
  type Stringable = string | ((...args: any[]) => string);
39
25
  type CB = (...args: any[]) => any;
40
26
  type ValueOf<T> = T[keyof T];
27
+ type OnReturn = {
28
+ off: () => void;
29
+ };
30
+ type BusType = {
31
+ on: (event: string, handler: CB) => OnReturn;
32
+ emit: (event: string, ...args: any[]) => void;
33
+ };
34
+ type TinyState<S> = () => [
35
+ value: S,
36
+ setValue: (next: S | ((prev: S) => S)) => void,
37
+ resetValue: () => void
38
+ ];
39
+ declare function isNullish(value: any): value is null | undefined;
40
+ declare const isNotNullish: (value: any) => boolean;
41
+ declare function asArray<T>(possibleArg: T | T[]): T[];
41
42
  declare function callEach(arr: CB[]): void;
42
43
  /**
43
44
  * A safe hasOwnProperty access
@@ -51,7 +52,7 @@ declare const _default: {
51
52
  <T_2 extends {}, U_2, V_1, W>(target: T_2, source1: U_2, source2: V_1, source3: W): T_2 & U_2 & V_1 & W;
52
53
  (target: object, ...sources: any[]): any;
53
54
  };
54
- declare function defaultTo<T>(value: T | ((...args: any[]) => T), defaultValue: T | (() => T)): T;
55
+ declare function defaultTo<T>(value: T | void | null | ((...args: any[]) => T | void | null), defaultValue: T | (() => T)): T;
55
56
  declare function invariant(condition: any,
56
57
  // eslint-disable-next-line @typescript-eslint/ban-types
57
58
  message?: String | Stringable): asserts condition;
@@ -71,13 +72,14 @@ declare namespace bus {
71
72
  type Stringable = string | ((...args: any[]) => string);
72
73
  type CB = (...args: any[]) => any;
73
74
  type ValueOf<T> = T[keyof T];
74
- function createBus(): {
75
- on: (event: string, handler: CB) => OnReturn;
76
- emit: (event: string, ...args: any[]) => void;
77
- };
75
+ function createBus(): BusType;
78
76
  type OnReturn = {
79
77
  off: () => void;
80
78
  };
79
+ type BusType = {
80
+ on: (event: string, handler: CB) => OnReturn;
81
+ emit: (event: string, ...args: any[]) => void;
82
+ };
81
83
  }
82
84
  /**
83
85
  * @returns a unique numeric id.
@@ -90,6 +92,7 @@ declare function greaterThan(value: number | string, gt: number | string): boole
90
92
  declare function longerThan(value: string | unknown[], arg1: string | number): boolean;
91
93
  declare function isNumeric(value: string | number): boolean;
92
94
  declare const isNotNumeric: (value: string | number) => boolean;
95
+ declare function isObject(v: any): v is Record<any, any>;
93
96
  declare function lengthEquals(value: string | unknown[], arg1: string | number): boolean;
94
97
  declare const lengthNotEquals: (value: string | unknown[], arg1: string | number) => boolean;
95
98
  declare function numberEquals(value: string | number, eq: string | number): boolean;
@@ -106,6 +109,32 @@ declare const isNotArray: (value: unknown) => boolean;
106
109
  declare function isEmpty(value: unknown): boolean;
107
110
  declare const isNotEmpty: (value: unknown) => boolean;
108
111
  declare function isPositive(value: number | string): boolean;
109
- export { createCache as cache, CacheApi, isNullish, isNotNullish, nestedArray, asArray, callEach, hasOwnProperty, isPromise, optionalFunctionValue, _default as assign, defaultTo, invariant, StringObject, isStringValue, bindNot, either, isBoolean, last, deferThrow, bus, seq, genSeq, isFunction, mapFirst, greaterThan, longerThan, isNumeric, isNotNumeric, lengthEquals, lengthNotEquals, numberEquals, numberNotEquals, isNull, isNotNull, isUndefined, isNotUndefined, isArray, isNotArray, isEmpty, isNotEmpty, isPositive };
112
+ declare function text(str: string, ...substitutions: Array<unknown>): string;
113
+ declare const STATE_WILD_CARD = "*";
114
+ type TStateWildCard = typeof STATE_WILD_CARD;
115
+ type TStateMachine<S extends string, A extends string> = {
116
+ initial: S;
117
+ states: Partial<{
118
+ [key in S & TStateWildCard]: {
119
+ [key in A]?: S | [
120
+ S,
121
+ (payload?: any) => boolean
122
+ ];
123
+ };
124
+ }>;
125
+ };
126
+ declare function StateMachine<S extends string, A extends string>(machine: TStateMachine<S, A>): {
127
+ getState: () => S;
128
+ transition: (action: A, payload?: any) => void;
129
+ };
130
+ declare namespace tinyState {
131
+ function createTinyState<S>(initialValue: S | (() => S)): TinyState<S>;
132
+ type TinyState<S> = () => [
133
+ value: S,
134
+ setValue: (next: S | ((prev: S) => S)) => void,
135
+ resetValue: () => void
136
+ ];
137
+ }
138
+ export { createCache as cache, CacheApi, BusType, TinyState, isNullish, isNotNullish, asArray, callEach, hasOwnProperty, isPromise, optionalFunctionValue, _default as assign, defaultTo, invariant, StringObject, isStringValue, bindNot, either, isBoolean, last, deferThrow, bus, seq, genSeq, isFunction, mapFirst, greaterThan, longerThan, isNumeric, isNotNumeric, isObject, lengthEquals, lengthNotEquals, numberEquals, numberNotEquals, isNull, isNotNull, isUndefined, isNotUndefined, isArray, isNotArray, isEmpty, isNotEmpty, isPositive, text, TStateMachine, StateMachine, tinyState };
110
139
  export type { DropFirst, Stringable, CB, ValueOf };
111
140
  //# sourceMappingURL=vest-utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"vest-utils.d.ts","sourceRoot":"","sources":["../src/vest-utils.ts","../src/bindNot.ts","../src/isNumeric.ts","../src/numberEquals.ts","../src/lengthEquals.ts","../src/greaterThan.ts","../src/longerThan.ts","../src/cache.ts","../src/isNull.ts","../src/isUndefined.ts","../src/isNullish.ts","../src/asArray.ts","../src/isFunction.ts","../src/optionalFunctionValue.ts","../src/defaultTo.ts","../src/isArrayValue.ts","../src/last.ts","../src/nestedArray.ts","../src/utilityTypes.ts","../src/callEach.ts","../src/hasOwnProperty.ts","../src/isPromise.ts","../src/assign.ts","../src/invariant.ts","../src/isStringValue.ts","../src/either.ts","../src/isBooleanValue.ts","../src/deferThrow.ts","../src/bus.ts","../src/seq.ts","../src/mapFirst.ts","../src/isEmpty.ts","../src/isPositive.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,ggBAA8B,CAAc;AAgCnD,YAAY,sCAAa,CAAqB"}
1
+ {"version":3,"file":"vest-utils.d.ts","sourceRoot":"","sources":["../src/vest-utils.ts","../src/bindNot.ts","../src/isNumeric.ts","../src/numberEquals.ts","../src/lengthEquals.ts","../src/greaterThan.ts","../src/longerThan.ts","../src/cache.ts","../src/utilityTypes.ts","../src/bus.ts","../src/isFunction.ts","../src/optionalFunctionValue.ts","../src/tinyState.ts","../src/isNull.ts","../src/isUndefined.ts","../src/isNullish.ts","../src/asArray.ts","../src/callEach.ts","../src/hasOwnProperty.ts","../src/isPromise.ts","../src/assign.ts","../src/defaultTo.ts","../src/invariant.ts","../src/isStringValue.ts","../src/either.ts","../src/isBooleanValue.ts","../src/last.ts","../src/deferThrow.ts","../src/seq.ts","../src/mapFirst.ts","../src/valueIsObject.ts","../src/isArrayValue.ts","../src/isEmpty.ts","../src/isPositive.ts","../src/text.ts","../src/SimpleStateMachine.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,+jBAA8B,CAAc;AAqCnD,YAAY,sCAAa,CAAqB"}
@@ -1,37 +0,0 @@
1
- import { bindNot } from 'vest-utils';
2
-
3
- describe('bindNot', () => {
4
- it('Should return return a function', () => {
5
- expect(typeof bindNot(jest.fn())).toBe('function');
6
- });
7
-
8
- test('calling returned function runs accepted function', () => {
9
- const fn = jest.fn();
10
-
11
- expect(fn).not.toHaveBeenCalled();
12
- const not = bindNot(fn);
13
- expect(fn).not.toHaveBeenCalled();
14
- not();
15
- expect(fn).toHaveBeenCalledTimes(1);
16
- });
17
-
18
- it('Should pass arguments to accepted function', () => {
19
- const fn = jest.fn();
20
-
21
- const not = bindNot(fn);
22
- not(1, 2, 3, 4);
23
- expect(fn).toHaveBeenCalledWith(1, 2, 3, 4);
24
- });
25
-
26
- it('Should return the boolean negation of the original function', () => {
27
- expect(bindNot(() => true)()).toBe(false);
28
- expect(bindNot(() => false)()).toBe(true);
29
- expect(bindNot(() => 'string')()).toBe(false);
30
- expect(bindNot(() => [])()).toBe(false);
31
- expect(bindNot(() => '')()).toBe(true);
32
- expect(bindNot(() => 0)()).toBe(true);
33
- expect(bindNot(() => NaN)()).toBe(true);
34
- expect(bindNot(() => null)()).toBe(true);
35
- expect(bindNot(() => undefined)()).toBe(true);
36
- });
37
- });