vest-utils 1.5.0 → 2.0.1

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 (157) hide show
  1. package/README.md +7 -2
  2. package/dist/chunk-CLMFDpHK.mjs +18 -0
  3. package/dist/exports/minifyObject.cjs +114 -0
  4. package/dist/exports/minifyObject.cjs.map +1 -0
  5. package/dist/exports/minifyObject.mjs +113 -0
  6. package/dist/exports/minifyObject.mjs.map +1 -0
  7. package/dist/exports/standardSchemaSpec.cjs +0 -0
  8. package/dist/exports/standardSchemaSpec.mjs +1 -0
  9. package/dist/isEmpty-BBxAFjjm.mjs +103 -0
  10. package/dist/isEmpty-BBxAFjjm.mjs.map +1 -0
  11. package/dist/isEmpty-BuEa-96Q.cjs +235 -0
  12. package/dist/isEmpty-BuEa-96Q.cjs.map +1 -0
  13. package/dist/vest-utils.cjs +510 -0
  14. package/dist/vest-utils.cjs.map +1 -0
  15. package/dist/vest-utils.mjs +421 -0
  16. package/dist/vest-utils.mjs.map +1 -0
  17. package/minifyObject/package.json +12 -8
  18. package/package.json +43 -58
  19. package/src/Brand.ts +9 -0
  20. package/src/IO.ts +2 -0
  21. package/src/Predicates.ts +13 -0
  22. package/src/Result.ts +121 -0
  23. package/src/SimpleStateMachine.ts +157 -0
  24. package/src/StringObject.ts +6 -0
  25. package/src/__tests__/Architecture.test.ts +69 -0
  26. package/src/__tests__/Predicates.test.ts +118 -0
  27. package/src/__tests__/Result.test.ts +284 -0
  28. package/src/__tests__/SimpleStateMachine.test.ts +425 -0
  29. package/src/__tests__/StringObject.test.ts +18 -0
  30. package/src/__tests__/asArray.test.ts +14 -0
  31. package/src/__tests__/bindNot.test.ts +39 -0
  32. package/src/__tests__/bus.test.ts +135 -0
  33. package/src/__tests__/cache.test.ts +139 -0
  34. package/src/__tests__/callEach.test.ts +20 -0
  35. package/src/__tests__/defaultTo.test.ts +52 -0
  36. package/src/__tests__/deferThrow.test.ts +26 -0
  37. package/src/__tests__/either.test.ts +17 -0
  38. package/src/__tests__/freezeAssign.test.ts +24 -0
  39. package/src/__tests__/greaterThan.test.ts +68 -0
  40. package/src/__tests__/invariant.test.ts +47 -0
  41. package/src/__tests__/isArray.test.ts +16 -0
  42. package/src/__tests__/isBoolean.test.ts +16 -0
  43. package/src/__tests__/isEmpty.test.ts +55 -0
  44. package/src/__tests__/isEmptySet.test.ts +22 -0
  45. package/src/__tests__/isNull.test.ts +26 -0
  46. package/src/__tests__/isNumeric.test.ts +27 -0
  47. package/src/__tests__/isPositive.test.ts +38 -0
  48. package/src/__tests__/isPromise.test.ts +17 -0
  49. package/src/__tests__/isString.test.ts +13 -0
  50. package/src/__tests__/isUndefined.test.ts +27 -0
  51. package/src/__tests__/isUnsafeKey.test.ts +22 -0
  52. package/src/__tests__/lengthEquals.test.ts +58 -0
  53. package/src/__tests__/longerThan.test.ts +58 -0
  54. package/src/__tests__/mapFirst.test.ts +31 -0
  55. package/src/__tests__/nonnullish.test.ts +25 -0
  56. package/src/__tests__/noop.test.ts +12 -0
  57. package/src/__tests__/numberEquals.test.ts +67 -0
  58. package/src/__tests__/optionalFunctionValue.test.ts +29 -0
  59. package/src/__tests__/seq.test.ts +29 -0
  60. package/src/__tests__/text.test.ts +41 -0
  61. package/src/__tests__/tinyState.test.ts +68 -0
  62. package/src/__tests__/toNumber.test.ts +39 -0
  63. package/src/__tests__/vest-utils.test.ts +13 -0
  64. package/src/__tests__/withCatch.test.ts +17 -0
  65. package/src/__tests__/withResolvers.test.ts +45 -0
  66. package/src/asArray.ts +3 -0
  67. package/src/assign.ts +1 -0
  68. package/src/bindNot.ts +3 -0
  69. package/src/bus.ts +52 -0
  70. package/src/cache.ts +68 -0
  71. package/src/callEach.ts +5 -0
  72. package/src/defaultTo.ts +9 -0
  73. package/src/deferThrow.ts +7 -0
  74. package/src/dynamicValue.ts +9 -0
  75. package/src/either.ts +3 -0
  76. package/src/exports/__tests__/minifyObject.security.test.ts +65 -0
  77. package/src/exports/__tests__/minifyObject.test.ts +281 -0
  78. package/src/exports/minifyObject.ts +198 -0
  79. package/src/exports/standardSchemaSpec.ts +70 -0
  80. package/src/freezeAssign.ts +5 -0
  81. package/src/globals.d.ts +3 -0
  82. package/src/greaterThan.ts +8 -0
  83. package/src/hasOwnProperty.ts +9 -0
  84. package/src/invariant.ts +19 -0
  85. package/src/isArrayValue.ts +11 -0
  86. package/src/isBooleanValue.ts +3 -0
  87. package/src/isEmpty.ts +18 -0
  88. package/src/isEmptySet.ts +15 -0
  89. package/src/isFunction.ts +5 -0
  90. package/src/isNull.ts +7 -0
  91. package/src/isNullish.ts +10 -0
  92. package/src/isNumeric.ts +11 -0
  93. package/src/isPositive.ts +5 -0
  94. package/src/isPromise.ts +5 -0
  95. package/src/isStringValue.ts +3 -0
  96. package/src/isUndefined.ts +7 -0
  97. package/src/isUnsafeKey.ts +3 -0
  98. package/src/lengthEquals.ts +11 -0
  99. package/src/longerThan.ts +8 -0
  100. package/src/mapFirst.ts +25 -0
  101. package/src/nonnullish.ts +9 -0
  102. package/src/noop.ts +1 -0
  103. package/src/numberEquals.ts +11 -0
  104. package/src/seq.ts +16 -0
  105. package/src/text.ts +20 -0
  106. package/src/tinyState.ts +28 -0
  107. package/src/toNumber.ts +11 -0
  108. package/src/utilityTypes.ts +25 -0
  109. package/src/valueIsObject.ts +5 -0
  110. package/src/vest-utils.ts +73 -0
  111. package/src/withCatch.ts +11 -0
  112. package/src/withResolvers.ts +33 -0
  113. package/standardSchemaSpec/package.json +14 -0
  114. package/types/{minifyObject.d.ts → exports/minifyObject.d.cts} +4 -2
  115. package/types/exports/minifyObject.d.cts.map +1 -0
  116. package/types/exports/minifyObject.d.mts +7 -0
  117. package/types/exports/minifyObject.d.mts.map +1 -0
  118. package/types/exports/standardSchemaSpec.d.cts +59 -0
  119. package/types/exports/standardSchemaSpec.d.cts.map +1 -0
  120. package/types/exports/standardSchemaSpec.d.mts +59 -0
  121. package/types/exports/standardSchemaSpec.d.mts.map +1 -0
  122. package/types/vest-utils.d.cts +296 -0
  123. package/types/vest-utils.d.cts.map +1 -0
  124. package/types/vest-utils.d.mts +295 -0
  125. package/types/vest-utils.d.mts.map +1 -0
  126. package/types/vest-utils.d.ts +245 -143
  127. package/vitest.config.ts +9 -45
  128. package/dist/cjs/minifyObject.development.js +0 -217
  129. package/dist/cjs/minifyObject.development.js.map +0 -1
  130. package/dist/cjs/minifyObject.js +0 -6
  131. package/dist/cjs/minifyObject.production.js +0 -2
  132. package/dist/cjs/minifyObject.production.js.map +0 -1
  133. package/dist/cjs/package.json +0 -1
  134. package/dist/cjs/vest-utils.development.js +0 -378
  135. package/dist/cjs/vest-utils.development.js.map +0 -1
  136. package/dist/cjs/vest-utils.js +0 -6
  137. package/dist/cjs/vest-utils.production.js +0 -2
  138. package/dist/cjs/vest-utils.production.js.map +0 -1
  139. package/dist/es/minifyObject.development.js +0 -214
  140. package/dist/es/minifyObject.development.js.map +0 -1
  141. package/dist/es/minifyObject.production.js +0 -2
  142. package/dist/es/minifyObject.production.js.map +0 -1
  143. package/dist/es/package.json +0 -1
  144. package/dist/es/vest-utils.development.js +0 -330
  145. package/dist/es/vest-utils.development.js.map +0 -1
  146. package/dist/es/vest-utils.production.js +0 -2
  147. package/dist/es/vest-utils.production.js.map +0 -1
  148. package/dist/umd/minifyObject.development.js +0 -223
  149. package/dist/umd/minifyObject.development.js.map +0 -1
  150. package/dist/umd/minifyObject.production.js +0 -2
  151. package/dist/umd/minifyObject.production.js.map +0 -1
  152. package/dist/umd/vest-utils.development.js +0 -384
  153. package/dist/umd/vest-utils.development.js.map +0 -1
  154. package/dist/umd/vest-utils.production.js +0 -2
  155. package/dist/umd/vest-utils.production.js.map +0 -1
  156. package/types/minifyObject.d.ts.map +0 -1
  157. package/types/vest-utils.d.ts.map +0 -1
@@ -0,0 +1,55 @@
1
+ import { describe, it, expect } from 'vitest';
2
+
3
+ import { isEmpty } from '../vest-utils';
4
+
5
+ describe('Tests isEmpty rule', () => {
6
+ describe('Expect true', () => {
7
+ it('Should return false for a non-empty array', () => {
8
+ expect(isEmpty([1, 2, 3, 4, 5, 6])).toBe(false);
9
+ });
10
+
11
+ it('Should return true for an empty array', () => {
12
+ expect(isEmpty([])).toBe(true);
13
+ });
14
+
15
+ it('Should return false for a non-empty objecd', () => {
16
+ expect(isEmpty({ a: 1 })).toBe(false);
17
+ });
18
+
19
+ it('Should return true for an empty object', () => {
20
+ expect(isEmpty({})).toBe(true);
21
+ });
22
+
23
+ it('Should return true for an empty string', () => {
24
+ expect(isEmpty('')).toBe(true);
25
+ });
26
+
27
+ it('Should return false for a non empty string', () => {
28
+ expect(isEmpty('hey')).toBe(false);
29
+ });
30
+
31
+ it('Should return true for zero', () => {
32
+ expect(isEmpty(0)).toBe(true);
33
+ });
34
+
35
+ it('Should return false for one', () => {
36
+ expect(isEmpty(1)).toBe(false);
37
+ });
38
+
39
+ it('Should return true for undefined', () => {
40
+ expect(isEmpty(undefined)).toBe(true);
41
+ });
42
+
43
+ it('Should return true for null', () => {
44
+ expect(isEmpty(null)).toBe(true);
45
+ });
46
+
47
+ it('Should return true for NaN', () => {
48
+ expect(isEmpty(NaN)).toBe(true);
49
+ });
50
+
51
+ it('Should return false for a Symbol', () => {
52
+ expect(isEmpty(Symbol('hey'))).toBe(false);
53
+ });
54
+ });
55
+ });
@@ -0,0 +1,22 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { isEmptySet, isNotEmptySet } from '../isEmptySet';
3
+
4
+ describe('isEmptySet', () => {
5
+ it('Should return true for an empty Set', () => {
6
+ expect(isEmptySet(new Set())).toBe(true);
7
+ });
8
+
9
+ it('Should return false for a non-empty Set', () => {
10
+ expect(isEmptySet(new Set([1]))).toBe(false);
11
+ });
12
+ });
13
+
14
+ describe('isNotEmptySet', () => {
15
+ it('Should return false for an empty Set', () => {
16
+ expect(isNotEmptySet(new Set())).toBe(false);
17
+ });
18
+
19
+ it('Should return true for a non-empty Set', () => {
20
+ expect(isNotEmptySet(new Set([1]))).toBe(true);
21
+ });
22
+ });
@@ -0,0 +1,26 @@
1
+ import { isNull } from '../isNull';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('Tests isNull rule', () => {
5
+ it('Should return true for `null` value', () => {
6
+ expect(isNull(null)).toBe(true);
7
+ });
8
+
9
+ it.each([
10
+ undefined,
11
+ NaN,
12
+ false,
13
+ true,
14
+ Object,
15
+ Array(0),
16
+ '',
17
+ ' ',
18
+ 0,
19
+ 1,
20
+ '0',
21
+ '1',
22
+ Function.prototype,
23
+ ])('Should return false for %s value', v => {
24
+ expect(isNull(v)).toBe(false);
25
+ });
26
+ });
@@ -0,0 +1,27 @@
1
+ import { isNumeric } from '../isNumeric';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ const NUMERICS = ['-10', '0', 0xff, '0xFF', '8e5', '3.1415', +10, '0144'];
5
+
6
+ const NON_NUMERICS = [
7
+ '-0x42',
8
+ '7.2acdgs',
9
+ '',
10
+ {},
11
+ NaN,
12
+ null,
13
+ true,
14
+ Infinity,
15
+ undefined,
16
+ ];
17
+
18
+ describe('Tests isNumeric rule', () => {
19
+ it('Should return true for numeric values', () => {
20
+ NUMERICS.forEach(value => expect(isNumeric(value)).toBe(true));
21
+ });
22
+
23
+ it('Should return false for non numeric values', () => {
24
+ // @ts-expect-error - testing bad usage
25
+ NON_NUMERICS.forEach(value => expect(isNumeric(value)).toBe(false));
26
+ });
27
+ });
@@ -0,0 +1,38 @@
1
+ import { isPositive } from '../isPositive';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('Test isPositive rule', () => {
5
+ it('Should return false for zero', () => {
6
+ expect(isPositive(0)).toBe(false);
7
+ });
8
+
9
+ describe('When argument is a positive number', () => {
10
+ it('Should return true for positive number', () => {
11
+ expect(isPositive(10)).toBe(true);
12
+ });
13
+ it('should return true for positive desimal number', () => {
14
+ expect(isPositive(10.1)).toBe(true);
15
+ });
16
+ it('should return true for positive string number', () => {
17
+ expect(isPositive('10')).toBe(true);
18
+ });
19
+ it('should return true for positive decimal string number', () => {
20
+ expect(isPositive('10.10')).toBe(true);
21
+ });
22
+ });
23
+
24
+ describe('When argument is a negative number', () => {
25
+ it('Should return false for negative number', () => {
26
+ expect(isPositive(-1)).toBe(false);
27
+ });
28
+ it('should return false for negative desimal number', () => {
29
+ expect(isPositive(-1.1)).toBe(false);
30
+ });
31
+ it('should return false for negative string number', () => {
32
+ expect(isPositive('-1')).toBe(false);
33
+ });
34
+ it('should return false for negative decimal string number', () => {
35
+ expect(isPositive('-1.10')).toBe(false);
36
+ });
37
+ });
38
+ });
@@ -0,0 +1,17 @@
1
+ import isPromise from '../isPromise';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('isPromise', () => {
5
+ it('should return true for a Promise', () => {
6
+ expect(isPromise(new Promise(() => {}))).toBe(true);
7
+ });
8
+
9
+ it('should return false for a non-Promise value', () => {
10
+ expect(isPromise('not a Promise')).toBe(false);
11
+ });
12
+
13
+ it('should return false for null or undefined', () => {
14
+ expect(isPromise(null)).toBe(false);
15
+ expect(isPromise(undefined)).toBe(false);
16
+ });
17
+ });
@@ -0,0 +1,13 @@
1
+ import isStringValue from '../isStringValue';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('Tests isString rule', () => {
5
+ it('Should return false for non-string values', () => {
6
+ expect(isStringValue(42)).toBe(false);
7
+ expect(isStringValue([])).toBe(false);
8
+ });
9
+
10
+ it('Should return true for string values', () => {
11
+ expect(isStringValue('I love you')).toBe(true);
12
+ });
13
+ });
@@ -0,0 +1,27 @@
1
+ import { isUndefined } from '../isUndefined';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('Tests isUndefined rule', () => {
5
+ it('Should return true for `undefined` value', () => {
6
+ expect(isUndefined(undefined)).toBe(true);
7
+ expect(isUndefined()).toBe(true);
8
+ });
9
+
10
+ it.each([
11
+ null,
12
+ NaN,
13
+ false,
14
+ true,
15
+ Object,
16
+ Array(0),
17
+ '',
18
+ ' ',
19
+ 0,
20
+ 1,
21
+ '0',
22
+ '1',
23
+ () => undefined,
24
+ ])('Should return false for %s value', v => {
25
+ expect(isUndefined(v)).toBe(false);
26
+ });
27
+ });
@@ -0,0 +1,22 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import isUnsafeKey from '../isUnsafeKey';
3
+
4
+ describe('isUnsafeKey', () => {
5
+ it('should return true for __proto__', () => {
6
+ expect(isUnsafeKey('__proto__')).toBe(true);
7
+ });
8
+
9
+ it('should return true for constructor', () => {
10
+ expect(isUnsafeKey('constructor')).toBe(true);
11
+ });
12
+
13
+ it('should return true for prototype', () => {
14
+ expect(isUnsafeKey('prototype')).toBe(true);
15
+ });
16
+
17
+ it('should return false for other keys', () => {
18
+ expect(isUnsafeKey('someKey')).toBe(false);
19
+ expect(isUnsafeKey('toString')).toBe(false);
20
+ expect(isUnsafeKey('valueOf')).toBe(false);
21
+ });
22
+ });
@@ -0,0 +1,58 @@
1
+ import { faker } from '@faker-js/faker';
2
+ import { lengthEquals } from '../lengthEquals';
3
+ import { describe, it, expect } from 'vitest';
4
+
5
+ describe('Tests lengthEquals rule', () => {
6
+ const length = 10;
7
+ const word = faker.lorem.word();
8
+ const boolean = faker.datatype.boolean();
9
+
10
+ describe('First argument is array or string', () => {
11
+ describe('When first argument is equal to a given value', () => {
12
+ it('Should return true for an array equal to length', () => {
13
+ expect(lengthEquals(new Array(length), length)).toBe(true);
14
+ });
15
+
16
+ it('Should return true for a string equal to word length', () => {
17
+ expect(lengthEquals(word, word.length)).toBe(true);
18
+ });
19
+ });
20
+
21
+ describe('When first argument is shorter', () => {
22
+ it('Should return false for an array shorter than length', () => {
23
+ expect(lengthEquals(new Array(length), length + 1)).toBe(false);
24
+ });
25
+
26
+ it('Should return false for a string shorter than word length', () => {
27
+ expect(lengthEquals(word, word.length + 1)).toBe(false);
28
+ });
29
+ });
30
+
31
+ describe('When first argument is longer', () => {
32
+ it('Should return false for an array longer than length', () => {
33
+ expect(lengthEquals(new Array(length), length - 1)).toBe(false);
34
+ });
35
+
36
+ it('Should return false for a string longer than word length', () => {
37
+ expect(lengthEquals(word, word.length - 1)).toBe(false);
38
+ });
39
+ });
40
+ });
41
+
42
+ describe("First argument isn't array or string", () => {
43
+ it('Should throw error', () => {
44
+ // @ts-expect-error - testing wrong input
45
+ expect(() => lengthEquals(undefined, 0)).toThrow(TypeError);
46
+ });
47
+
48
+ it('Should return false for number argument', () => {
49
+ // @ts-expect-error - testing wrong input
50
+ expect(lengthEquals(100, 0)).toBe(false);
51
+ });
52
+
53
+ it('Should return false for boolean argument', () => {
54
+ // @ts-expect-error - testing wrong input
55
+ expect(lengthEquals(boolean, 0)).toBe(false);
56
+ });
57
+ });
58
+ });
@@ -0,0 +1,58 @@
1
+ import { faker } from '@faker-js/faker';
2
+ import { longerThan } from '../longerThan';
3
+ import { describe, it, expect } from 'vitest';
4
+
5
+ describe('Tests longerThan rule', () => {
6
+ const length = 10;
7
+ const word = faker.lorem.word();
8
+ const boolean = faker.datatype.boolean();
9
+
10
+ describe('First argument is array or string', () => {
11
+ describe('When first argument is longer', () => {
12
+ it('Should return true for an array longer than length', () => {
13
+ expect(longerThan(new Array(length), length - 1)).toBe(true);
14
+ });
15
+
16
+ it('Should return true for a string longer than word length', () => {
17
+ expect(longerThan(word, word.length - 1)).toBe(true);
18
+ });
19
+ });
20
+
21
+ describe('When first argument is shorter', () => {
22
+ it('Should return false for an array shorter than length', () => {
23
+ expect(longerThan(new Array(length), length + 1)).toBe(false);
24
+ });
25
+
26
+ it('Should return false for a string shorter than word length', () => {
27
+ expect(longerThan(word, word.length + 1)).toBe(false);
28
+ });
29
+ });
30
+
31
+ describe('When first argument is equal to a given value', () => {
32
+ it('Should return false for an array equal to length', () => {
33
+ expect(longerThan(new Array(length), length)).toBe(false);
34
+ });
35
+
36
+ it('Should return false for a string equal to word length', () => {
37
+ expect(longerThan(word, word.length)).toBe(false);
38
+ });
39
+ });
40
+ });
41
+
42
+ describe("First argument isn't array or string", () => {
43
+ it('Should throw error', () => {
44
+ // @ts-expect-error - testing wrong input
45
+ expect(() => longerThan(undefined, 0)).toThrow(TypeError);
46
+ });
47
+
48
+ it('Should return false for number argument', () => {
49
+ // @ts-expect-error - testing wrong input
50
+ expect(longerThan(length, 0)).toBe(false);
51
+ });
52
+
53
+ it('Should return false for boolean argument', () => {
54
+ // @ts-expect-error - testing wrong input
55
+ expect(longerThan(boolean, 0)).toBe(false);
56
+ });
57
+ });
58
+ });
@@ -0,0 +1,31 @@
1
+ import { describe, it, expect } from 'vitest';
2
+
3
+ import { mapFirst } from '../vest-utils';
4
+
5
+ describe('mapFirst', () => {
6
+ it('should return the broken out result', () => {
7
+ const result = mapFirst([1, 2, 3], (value, breakout) => {
8
+ breakout(value === 3, { pass: true });
9
+ });
10
+
11
+ expect(result).toEqual({ pass: true });
12
+ });
13
+
14
+ it('Should respect the breakout conditional', () => {
15
+ const result = mapFirst([1, 2, 3], (_, breakout) => {
16
+ breakout(false, 0);
17
+ breakout(false, 0);
18
+ breakout(true, 1);
19
+ });
20
+
21
+ expect(result).toBe(1);
22
+ });
23
+
24
+ describe('When not broken out', () => {
25
+ it('Should return undefined', () => {
26
+ const result = mapFirst([1, 2, 3], () => {});
27
+
28
+ expect(result).toBeUndefined();
29
+ });
30
+ });
31
+ });
@@ -0,0 +1,25 @@
1
+ import { nonnullish } from '../nonnullish';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('nonnullish', () => {
5
+ it('should return the value if it is not null or undefined', () => {
6
+ const value = 'hello';
7
+ expect(nonnullish(value)).toBe(value);
8
+ });
9
+
10
+ it('should throw an error if the value is null', () => {
11
+ const value = null;
12
+ expect(() => nonnullish(value)).toThrow();
13
+ });
14
+
15
+ it('should throw an error if the value is undefined', () => {
16
+ const value = undefined;
17
+ expect(() => nonnullish(value)).toThrow();
18
+ });
19
+
20
+ it('should throw a custom error message if provided', () => {
21
+ const value = null;
22
+ const errorMessage = 'Value must be defined';
23
+ expect(() => nonnullish(value, errorMessage)).toThrow(errorMessage);
24
+ });
25
+ });
@@ -0,0 +1,12 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { noop } from '../noop';
3
+
4
+ describe('noop', () => {
5
+ it('should be a function', () => {
6
+ expect(typeof noop).toBe('function');
7
+ });
8
+
9
+ it('should return undefined', () => {
10
+ expect(noop()).toBeUndefined();
11
+ });
12
+ });
@@ -0,0 +1,67 @@
1
+ import { faker } from '@faker-js/faker';
2
+ import { numberEquals } from '../numberEquals';
3
+ import { describe, it, expect, beforeEach } from 'vitest';
4
+
5
+ describe('Tests numberEquals rule', () => {
6
+ describe('Arguments are numbers', () => {
7
+ let arg0: number;
8
+ beforeEach(() => {
9
+ arg0 = faker.number.int();
10
+ });
11
+
12
+ describe('When first argument is larger', () => {
13
+ it('Should return false', () => {
14
+ expect(numberEquals(arg0, arg0 - 1)).toBe(false);
15
+ });
16
+ });
17
+
18
+ describe('When first argument is smaller', () => {
19
+ it('Should return false', () => {
20
+ expect(numberEquals(arg0, arg0 + 1)).toBe(false);
21
+ });
22
+ });
23
+
24
+ describe('When values are equal', () => {
25
+ it('Should return true', () => {
26
+ expect(numberEquals(arg0, arg0)).toBe(true);
27
+ });
28
+ });
29
+ });
30
+
31
+ describe('Arguments are numeric strings', () => {
32
+ let arg0: string;
33
+
34
+ beforeEach(() => {
35
+ arg0 = faker.number.int().toString();
36
+ });
37
+
38
+ describe('When first argument is larger', () => {
39
+ it('Should return false', () => {
40
+ expect(numberEquals(`${arg0}`, `${Number(arg0) - 1}`)).toBe(false);
41
+ });
42
+ });
43
+
44
+ describe('When first argument is smaller', () => {
45
+ it('Should return false', () => {
46
+ expect(numberEquals(`${arg0}`, `${arg0 + 1}`)).toBe(false);
47
+ });
48
+ });
49
+
50
+ describe('When values are equal', () => {
51
+ it('Should return true', () => {
52
+ expect(numberEquals('100', '100')).toBe(true);
53
+ });
54
+ });
55
+ });
56
+
57
+ describe('Arguments are non numeric', () => {
58
+ [faker.lorem.word(), `${faker.number.int()}`.split(''), {}].forEach(
59
+ element => {
60
+ it('Should return false', () => {
61
+ // @ts-expect-error - testing invalid input
62
+ expect(numberEquals(element, 0)).toBe(false);
63
+ });
64
+ },
65
+ );
66
+ });
67
+ });
@@ -0,0 +1,29 @@
1
+ import { describe, it, expect, vi } from 'vitest';
2
+
3
+ import { dynamicValue } from '../vest-utils';
4
+
5
+ describe('dynamicValue', () => {
6
+ describe('When not a function', () => {
7
+ it.each([0, undefined, false, true, 1, [], {}, null, NaN])(
8
+ 'Should return the same value',
9
+ value => {
10
+ expect(dynamicValue(value)).toBe(value);
11
+ },
12
+ );
13
+ });
14
+
15
+ describe('When value is a function', () => {
16
+ it('Should call the function and return its return value', () => {
17
+ const value = vi.fn(() => 'return value');
18
+
19
+ expect(dynamicValue(value)).toBe('return value');
20
+ expect(value).toHaveBeenCalled();
21
+ });
22
+ it('Should run with arguments array', () => {
23
+ const value = vi.fn((...args) => args.join('|'));
24
+ const args = [1, 2, 3, 4];
25
+ expect(dynamicValue(value, ...args)).toBe('1|2|3|4');
26
+ expect(value).toHaveBeenCalledWith(...args);
27
+ });
28
+ });
29
+ });
@@ -0,0 +1,29 @@
1
+ import seq, { genSeq } from '../seq';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('lib:seq', () => {
5
+ it('Should return a new id on each run', () => {
6
+ Array.from({ length: 100 }, () => seq()).reduce((existing, seq) => {
7
+ expect(existing).not.toHaveProperty(seq.toString());
8
+ Object.assign(existing, { [seq]: true });
9
+ expect(existing).toHaveProperty(seq.toString());
10
+ return existing;
11
+ }, {});
12
+ });
13
+
14
+ describe('genSeq', () => {
15
+ it('Creates a namespaced sequence', () => {
16
+ const seq = genSeq('test');
17
+ expect(seq()).toBe('test_0');
18
+ expect(seq()).toBe('test_1');
19
+ expect(seq()).toBe('test_2');
20
+
21
+ const seq2 = genSeq('test2');
22
+ expect(seq2()).toBe('test2_0');
23
+ expect(seq2()).toBe('test2_1');
24
+ expect(seq2()).toBe('test2_2');
25
+
26
+ expect(seq()).toBe('test_3');
27
+ });
28
+ });
29
+ });
@@ -0,0 +1,41 @@
1
+ import { text } from '../text';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('text', () => {
5
+ describe('named substitutions (object)', () => {
6
+ describe('When all substitutions exist', () => {
7
+ it('Should replace delimited placeholders', () => {
8
+ expect(text('this {t} an example', { t: 'was' })).toBe(
9
+ 'this was an example',
10
+ );
11
+ expect(text('this {t1} {t2} example', { t1: 'was', t2: 'one' })).toBe(
12
+ 'this was one example',
13
+ );
14
+ });
15
+ });
16
+
17
+ describe('When a placeholder repeats', () => {
18
+ it('Should use the same substitution multiple times', () => {
19
+ expect(text('{n} {n} {n}', { n: 1 })).toBe('1 1 1');
20
+ });
21
+ });
22
+
23
+ describe('When a substitution is missing', () => {
24
+ it('Should keep the placeholder', () => {
25
+ expect(text('{no}', { yes: 'yes' })).toBe('{no}');
26
+ });
27
+ });
28
+ });
29
+
30
+ describe('Positional substitutions', () => {
31
+ it('should use substitutions one by one', () => {
32
+ expect(text('{n} {n} {x} {y}', 1, 2, 3, 4)).toBe('1 2 3 4');
33
+ });
34
+
35
+ describe('When there are not enough substitutions', () => {
36
+ it('Should default to the placeholder', () => {
37
+ expect(text('{n} {n} {x} {y}', 1, 2)).toBe('1 2 {x} {y}');
38
+ });
39
+ });
40
+ });
41
+ });
@@ -0,0 +1,68 @@
1
+ import { createTinyState } from '../tinyState';
2
+ import { describe, it, expect } from 'vitest';
3
+
4
+ describe('tinyTest', () => {
5
+ it('Should be a function', () => {
6
+ expect(typeof createTinyState).toBe('function');
7
+ });
8
+
9
+ it('Should return a function', () => {
10
+ expect(typeof createTinyState(1)).toBe('function');
11
+ });
12
+
13
+ it('Should return a function that returns an array', () => {
14
+ expect(Array.isArray(createTinyState(1)())).toBe(true);
15
+ });
16
+
17
+ it('Should return a function that returns an array with three items', () => {
18
+ expect(createTinyState(1)()).toHaveLength(3);
19
+ });
20
+
21
+ it('Should return a function that returns an array with three items, the first being the initial value', () => {
22
+ expect(createTinyState('initial_value')()[0]).toBe('initial_value');
23
+
24
+ const initialValue = {};
25
+ expect(createTinyState(initialValue)()[0]).toBe(initialValue);
26
+ });
27
+
28
+ it('Should return a function that returns an array with three items, the second being a function', () => {
29
+ expect(typeof createTinyState(1)()[1]).toBe('function');
30
+ });
31
+
32
+ it('Should return a function that returns an array with three items, the third being a function', () => {
33
+ expect(typeof createTinyState(1)()[2]).toBe('function');
34
+ });
35
+
36
+ it('Updates the value when the second item is called', () => {
37
+ const testState = createTinyState('initial_value');
38
+
39
+ {
40
+ const [, setValue] = testState();
41
+ setValue('new_value');
42
+ }
43
+
44
+ {
45
+ const [value] = testState();
46
+ expect(value).toBe('new_value');
47
+ }
48
+ });
49
+
50
+ it('resets the value when the third item is called', () => {
51
+ const testState = createTinyState('initial_value');
52
+
53
+ {
54
+ const [, setValue] = testState();
55
+ setValue('new_value');
56
+ }
57
+
58
+ {
59
+ const [, , resetValue] = testState();
60
+ resetValue();
61
+ }
62
+
63
+ {
64
+ const [value] = testState();
65
+ expect(value).toBe('initial_value');
66
+ }
67
+ });
68
+ });