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.
- package/dist/cjs/vest-utils.development.js +132 -138
- package/dist/cjs/vest-utils.production.js +1 -1
- package/dist/es/vest-utils.development.js +129 -136
- package/dist/es/vest-utils.production.js +1 -1
- package/dist/umd/vest-utils.development.js +132 -138
- package/dist/umd/vest-utils.production.js +1 -1
- package/package.json +2 -2
- package/types/vest-utils.d.ts +49 -20
- package/types/vest-utils.d.ts.map +1 -1
- package/src/__tests__/bindNot.test.ts +0 -37
- package/src/__tests__/bus.test.ts +0 -81
- package/src/__tests__/cache.test.ts +0 -113
- package/src/__tests__/defaultTo.test.ts +0 -50
- package/src/__tests__/deferThrow.test.ts +0 -24
- package/src/__tests__/greaterThan.test.ts +0 -59
- package/src/__tests__/invariant.test.ts +0 -45
- package/src/__tests__/isArray.test.ts +0 -15
- package/src/__tests__/isNull.test.ts +0 -25
- package/src/__tests__/isNumeric.test.ts +0 -26
- package/src/__tests__/isUndefined.test.ts +0 -26
- package/src/__tests__/lengthEquals.test.ts +0 -56
- package/src/__tests__/longerThan.test.ts +0 -56
- package/src/__tests__/mapFirst.test.ts +0 -29
- package/src/__tests__/numberEquals.test.ts +0 -59
- package/src/__tests__/optionalFunctionValue.test.ts +0 -27
- package/src/__tests__/seq.test.ts +0 -28
- package/src/asArray.ts +0 -3
- package/src/assign.ts +0 -1
- package/src/bindNot.ts +0 -3
- package/src/bus.ts +0 -32
- package/src/cache.ts +0 -57
- package/src/callEach.ts +0 -5
- package/src/defaultTo.ts +0 -8
- package/src/deferThrow.ts +0 -5
- package/src/either.ts +0 -3
- package/src/globals.d.ts +0 -3
- package/src/greaterThan.ts +0 -8
- package/src/hasOwnProperty.ts +0 -9
- package/src/invariant.ts +0 -24
- package/src/isArrayValue.ts +0 -11
- package/src/isBooleanValue.ts +0 -3
- package/src/isEmpty.ts +0 -17
- package/src/isFunction.ts +0 -5
- package/src/isNull.ts +0 -7
- package/src/isNullish.ts +0 -9
- package/src/isNumeric.ts +0 -11
- package/src/isPositive.ts +0 -5
- package/src/isPromise.ts +0 -5
- package/src/isStringValue.ts +0 -3
- package/src/isUndefined.ts +0 -7
- package/src/last.ts +0 -7
- package/src/lengthEquals.ts +0 -11
- package/src/longerThan.ts +0 -8
- package/src/mapFirst.ts +0 -25
- package/src/nestedArray.ts +0 -69
- package/src/numberEquals.ts +0 -11
- package/src/optionalFunctionValue.ts +0 -8
- package/src/seq.ts +0 -14
- package/src/utilityTypes.ts +0 -9
- package/src/vest-utils.ts +0 -36
- 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
|
|
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,145 +37,55 @@
|
|
|
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);
|
|
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(
|
|
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(
|
|
153
|
+
setTimeout(() => {
|
|
232
154
|
throw new Error(message);
|
|
233
155
|
}, 0);
|
|
234
156
|
}
|
|
235
157
|
|
|
236
158
|
function createBus() {
|
|
237
|
-
|
|
159
|
+
const listeners = {};
|
|
238
160
|
return {
|
|
239
|
-
emit
|
|
240
|
-
listener(event).forEach(
|
|
161
|
+
emit(event, data) {
|
|
162
|
+
listener(event).forEach(handler => {
|
|
241
163
|
handler(data);
|
|
242
164
|
});
|
|
243
165
|
},
|
|
244
|
-
on
|
|
166
|
+
on(event, handler) {
|
|
245
167
|
listeners[event] = listener(event).concat(handler);
|
|
246
168
|
return {
|
|
247
|
-
off
|
|
248
|
-
listeners[event] = listener(event).filter(
|
|
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
|
-
|
|
188
|
+
const seq = genSeq();
|
|
267
189
|
function genSeq(namespace) {
|
|
268
|
-
return (
|
|
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
|
-
|
|
275
|
-
|
|
276
|
-
for (
|
|
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 (
|
|
229
|
+
else if (isObject(value)) {
|
|
298
230
|
return lengthEquals(Object.keys(value), 0);
|
|
299
231
|
}
|
|
300
232
|
return false;
|
|
301
233
|
}
|
|
302
|
-
|
|
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
|
-
|
|
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
|
|
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
package/types/vest-utils.d.ts
CHANGED
|
@@ -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
|
-
|
|
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/
|
|
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
|
-
});
|