ts-data-forge 14.4.0 → 14.6.0

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 (123) hide show
  1. package/README.md +6 -0
  2. package/dist/array/impl/array-utils-element-access.mjs +1 -1
  3. package/dist/array/impl/array-utils-reducing-value.mjs +3 -3
  4. package/dist/array/impl/array-utils-search.d.mts.map +1 -1
  5. package/dist/array/impl/array-utils-search.mjs +1 -1
  6. package/dist/array/impl/array-utils-search.mjs.map +1 -1
  7. package/dist/collections/imap-mapped.mjs +1 -1
  8. package/dist/collections/imap.mjs +1 -1
  9. package/dist/entry-point.mjs +8 -6
  10. package/dist/entry-point.mjs.map +1 -1
  11. package/dist/functional/async-result/async-result.d.mts +16 -0
  12. package/dist/functional/async-result/async-result.d.mts.map +1 -0
  13. package/dist/functional/async-result/async-result.mjs +3 -0
  14. package/dist/functional/async-result/async-result.mjs.map +1 -0
  15. package/dist/functional/async-result/impl/async-result-flat-map.d.mts +39 -0
  16. package/dist/functional/async-result/impl/async-result-flat-map.d.mts.map +1 -0
  17. package/dist/functional/async-result/impl/async-result-flat-map.mjs +25 -0
  18. package/dist/functional/async-result/impl/async-result-flat-map.mjs.map +1 -0
  19. package/dist/functional/async-result/impl/async-result-from-promise.d.mts +47 -0
  20. package/dist/functional/async-result/impl/async-result-from-promise.d.mts.map +1 -0
  21. package/dist/functional/async-result/impl/async-result-from-promise.mjs +11 -0
  22. package/dist/functional/async-result/impl/async-result-from-promise.mjs.map +1 -0
  23. package/dist/functional/async-result/impl/async-result-from-throwable.d.mts +53 -0
  24. package/dist/functional/async-result/impl/async-result-from-throwable.d.mts.map +1 -0
  25. package/dist/functional/async-result/impl/async-result-from-throwable.mjs +17 -0
  26. package/dist/functional/async-result/impl/async-result-from-throwable.mjs.map +1 -0
  27. package/dist/functional/async-result/impl/async-result-map-err.d.mts +35 -0
  28. package/dist/functional/async-result/impl/async-result-map-err.d.mts.map +1 -0
  29. package/dist/functional/async-result/impl/async-result-map-err.mjs +23 -0
  30. package/dist/functional/async-result/impl/async-result-map-err.mjs.map +1 -0
  31. package/dist/functional/async-result/impl/async-result-map.d.mts +45 -0
  32. package/dist/functional/async-result/impl/async-result-map.d.mts.map +1 -0
  33. package/dist/functional/async-result/impl/async-result-map.mjs +23 -0
  34. package/dist/functional/async-result/impl/async-result-map.mjs.map +1 -0
  35. package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts +34 -0
  36. package/dist/functional/async-result/impl/async-result-unwrap-or.d.mts.map +1 -0
  37. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs +10 -0
  38. package/dist/functional/async-result/impl/async-result-unwrap-or.mjs.map +1 -0
  39. package/dist/functional/async-result/impl/index.d.mts +8 -0
  40. package/dist/functional/async-result/impl/index.d.mts.map +1 -0
  41. package/dist/functional/async-result/impl/index.mjs +7 -0
  42. package/dist/functional/async-result/impl/index.mjs.map +1 -0
  43. package/dist/functional/async-result/impl/types.d.mts +45 -0
  44. package/dist/functional/async-result/impl/types.d.mts.map +1 -0
  45. package/dist/functional/async-result/impl/types.mjs +2 -0
  46. package/dist/functional/async-result/impl/types.mjs.map +1 -0
  47. package/dist/functional/async-result/index.d.mts +2 -0
  48. package/dist/functional/async-result/index.d.mts.map +1 -0
  49. package/dist/functional/async-result/index.mjs +3 -0
  50. package/dist/functional/async-result/index.mjs.map +1 -0
  51. package/dist/functional/index.d.mts +1 -0
  52. package/dist/functional/index.d.mts.map +1 -1
  53. package/dist/functional/index.mjs +8 -6
  54. package/dist/functional/index.mjs.map +1 -1
  55. package/dist/functional/optional/impl/index.d.mts +1 -0
  56. package/dist/functional/optional/impl/index.d.mts.map +1 -1
  57. package/dist/functional/optional/impl/index.mjs +1 -0
  58. package/dist/functional/optional/impl/index.mjs.map +1 -1
  59. package/dist/functional/optional/impl/optional-match.d.mts +48 -0
  60. package/dist/functional/optional/impl/optional-match.d.mts.map +1 -0
  61. package/dist/functional/optional/impl/optional-match.mjs +22 -0
  62. package/dist/functional/optional/impl/optional-match.mjs.map +1 -0
  63. package/dist/functional/result/impl/index.d.mts +4 -0
  64. package/dist/functional/result/impl/index.d.mts.map +1 -1
  65. package/dist/functional/result/impl/index.mjs +4 -0
  66. package/dist/functional/result/impl/index.mjs.map +1 -1
  67. package/dist/functional/result/impl/result-from-optional.d.mts +41 -0
  68. package/dist/functional/result/impl/result-from-optional.d.mts.map +1 -0
  69. package/dist/functional/result/impl/result-from-optional.mjs +24 -0
  70. package/dist/functional/result/impl/result-from-optional.mjs.map +1 -0
  71. package/dist/functional/result/impl/result-from-promise.d.mts +5 -0
  72. package/dist/functional/result/impl/result-from-promise.d.mts.map +1 -1
  73. package/dist/functional/result/impl/result-from-promise.mjs.map +1 -1
  74. package/dist/functional/result/impl/result-match.d.mts +47 -0
  75. package/dist/functional/result/impl/result-match.d.mts.map +1 -0
  76. package/dist/functional/result/impl/result-match.mjs +24 -0
  77. package/dist/functional/result/impl/result-match.mjs.map +1 -0
  78. package/dist/functional/result/impl/result-safe-try.d.mts +51 -0
  79. package/dist/functional/result/impl/result-safe-try.d.mts.map +1 -0
  80. package/dist/functional/result/impl/result-safe-try.mjs +24 -0
  81. package/dist/functional/result/impl/result-safe-try.mjs.map +1 -0
  82. package/dist/functional/result/impl/result-safe-unwrap.d.mts +36 -0
  83. package/dist/functional/result/impl/result-safe-unwrap.d.mts.map +1 -0
  84. package/dist/functional/result/impl/result-safe-unwrap.mjs +14 -0
  85. package/dist/functional/result/impl/result-safe-unwrap.mjs.map +1 -0
  86. package/dist/json/json.mjs +2 -2
  87. package/dist/number/refined-number-utils.mjs +1 -1
  88. package/dist/object/object.mjs +1 -1
  89. package/dist/others/debounce-function.d.mts.map +1 -1
  90. package/dist/others/debounce-function.mjs.map +1 -1
  91. package/dist/others/fast-deep-equal.d.mts.map +1 -1
  92. package/dist/others/fast-deep-equal.mjs +1 -1
  93. package/dist/others/fast-deep-equal.mjs.map +1 -1
  94. package/package.json +10 -10
  95. package/src/array/impl/array-utils-modification.test.mts +3 -3
  96. package/src/array/impl/array-utils-search.mts +22 -8
  97. package/src/array/impl/array-utils-shape-invariants.test.mts +3 -3
  98. package/src/array/impl/array-utils-transformation.test.mts +1 -1
  99. package/src/functional/async-result/async-result.mts +17 -0
  100. package/src/functional/async-result/impl/async-result-flat-map.mts +99 -0
  101. package/src/functional/async-result/impl/async-result-from-promise.mts +69 -0
  102. package/src/functional/async-result/impl/async-result-from-throwable.mts +78 -0
  103. package/src/functional/async-result/impl/async-result-map-err.mts +84 -0
  104. package/src/functional/async-result/impl/async-result-map.mts +94 -0
  105. package/src/functional/async-result/impl/async-result-unwrap-or.mts +42 -0
  106. package/src/functional/async-result/impl/index.mts +7 -0
  107. package/src/functional/async-result/impl/types.mts +51 -0
  108. package/src/functional/async-result/index.mts +1 -0
  109. package/src/functional/async-result.test.mts +290 -0
  110. package/src/functional/index.mts +1 -0
  111. package/src/functional/optional/impl/index.mts +1 -0
  112. package/src/functional/optional/impl/optional-match.mts +108 -0
  113. package/src/functional/optional.test.mts +79 -0
  114. package/src/functional/result/impl/index.mts +4 -0
  115. package/src/functional/result/impl/result-from-optional.mts +80 -0
  116. package/src/functional/result/impl/result-from-promise.mts +5 -0
  117. package/src/functional/result/impl/result-match.mts +108 -0
  118. package/src/functional/result/impl/result-safe-try.mts +110 -0
  119. package/src/functional/result/impl/result-safe-unwrap.mts +50 -0
  120. package/src/functional/result.test.mts +331 -0
  121. package/src/guard/is-error.test.mts +7 -7
  122. package/src/others/debounce-function.mts +5 -1
  123. package/src/others/fast-deep-equal.mts +9 -2
@@ -1017,4 +1017,335 @@ describe('Result test', () => {
1017
1017
  >();
1018
1018
  });
1019
1019
  });
1020
+
1021
+ describe('match', () => {
1022
+ test('should apply the ok case for Ok', () => {
1023
+ const result = Result.match(Result.ok(21), {
1024
+ ok: (value) => value * 2,
1025
+ err: () => 0,
1026
+ });
1027
+
1028
+ expectType<typeof result, number>('=');
1029
+
1030
+ assert.deepStrictEqual(result, 42);
1031
+ });
1032
+
1033
+ test('should apply the err case for Err', () => {
1034
+ const result = Result.match(Result.err('failure'), {
1035
+ ok: () => 'ok',
1036
+ err: (error) => `error: ${error}`,
1037
+ });
1038
+
1039
+ assert.deepStrictEqual(result, 'error: failure');
1040
+ });
1041
+
1042
+ test('should support Result union inputs', () => {
1043
+ // Created through a function so that the const is not flow-narrowed to
1044
+ // the Ok side.
1045
+ const makeResult = (): Result<number, string> => Result.ok(2);
1046
+
1047
+ const result = makeResult();
1048
+
1049
+ const folded = Result.match(result, {
1050
+ ok: (value) => value + 1,
1051
+ err: (error) => error.length,
1052
+ });
1053
+
1054
+ expectType<typeof folded, number>('=');
1055
+
1056
+ assert.deepStrictEqual(folded, 3);
1057
+ });
1058
+
1059
+ test('curried version should work', () => {
1060
+ const matcher = Result.match({
1061
+ ok: (value: number) => value + 1,
1062
+ err: (error: string) => error.length,
1063
+ });
1064
+
1065
+ assert.deepStrictEqual(matcher(Result.ok(1)), 2);
1066
+
1067
+ assert.deepStrictEqual(matcher(Result.err('oops')), 4);
1068
+ });
1069
+
1070
+ test('curried version should compose with Array.map and pipe', () => {
1071
+ const matcher = Result.match({
1072
+ ok: (value: number) => `ok:${value}`,
1073
+ err: (error: string) => `err:${error}`,
1074
+ });
1075
+
1076
+ const results: readonly Result<number, string>[] = [
1077
+ Result.ok(1),
1078
+ Result.err('bad'),
1079
+ ] as const;
1080
+
1081
+ assert.deepStrictEqual(results.map(matcher), ['ok:1', 'err:bad']);
1082
+
1083
+ const single: Result<number, string> = Result.ok(3);
1084
+
1085
+ assert.deepStrictEqual(pipe(single).map(matcher).value, 'ok:3');
1086
+ });
1087
+ });
1088
+
1089
+ describe('fromOptional', () => {
1090
+ test('should convert Some to Ok', () => {
1091
+ const result = Result.fromOptional(Optional.some(42), 'missing');
1092
+
1093
+ expectType<typeof result, Result<42, 'missing'>>('=');
1094
+
1095
+ assert.deepStrictEqual(result, Result.ok(42));
1096
+ });
1097
+
1098
+ test('should convert None to Err with the provided error', () => {
1099
+ const result = Result.fromOptional(Optional.none, 'missing');
1100
+
1101
+ assert.isTrue(Result.isErr(result));
1102
+
1103
+ assert.deepStrictEqual(result.value, 'missing');
1104
+ });
1105
+
1106
+ test('should convert Some(undefined) to Ok(undefined), not Err', () => {
1107
+ const makeOptional = (): Optional<undefined> => Optional.some(undefined);
1108
+
1109
+ const result = Result.fromOptional(makeOptional(), 'missing');
1110
+
1111
+ assert.isTrue(Result.isOk(result));
1112
+
1113
+ assert.deepStrictEqual(result.value, undefined);
1114
+ });
1115
+
1116
+ test('should support Optional union inputs', () => {
1117
+ // Created through a function so that the const is not flow-narrowed to
1118
+ // the Some side.
1119
+ const makeOptional = (): Optional<number> => Optional.some(2);
1120
+
1121
+ const result = Result.fromOptional(makeOptional(), 'missing');
1122
+
1123
+ expectType<typeof result, Result<number, 'missing'>>('=');
1124
+
1125
+ assert.deepStrictEqual(result, Result.ok(2));
1126
+ });
1127
+
1128
+ test('curried version should work', () => {
1129
+ const withMissingError = Result.fromOptional('missing');
1130
+
1131
+ const result = withMissingError(Optional.some(1));
1132
+
1133
+ expectType<typeof result, Result<1, 'missing'>>('=');
1134
+
1135
+ assert.deepStrictEqual(result, Result.ok(1));
1136
+
1137
+ assert.deepStrictEqual(
1138
+ withMissingError(Optional.none),
1139
+ Result.err('missing'),
1140
+ );
1141
+ });
1142
+ });
1143
+
1144
+ describe('safeTry', () => {
1145
+ test('should return the body result when every yielded Result is Ok', () => {
1146
+ const result = Result.safeTry(function* () {
1147
+ const x = yield* Result.safeUnwrap(Result.ok(2));
1148
+
1149
+ const y = yield* Result.safeUnwrap(Result.ok(3));
1150
+
1151
+ return Result.ok(x + y);
1152
+ });
1153
+
1154
+ expectType<typeof result, Result<number, never>>('=');
1155
+
1156
+ assert.deepStrictEqual(result, Result.ok(5));
1157
+ });
1158
+
1159
+ test('should short-circuit on the first Err', () => {
1160
+ const makeFailing = (): Result<number, string> =>
1161
+ Result.err('first error');
1162
+
1163
+ const failing = makeFailing();
1164
+
1165
+ let mut_reached = false;
1166
+
1167
+ const result = Result.safeTry(function* () {
1168
+ const x = yield* Result.safeUnwrap(failing);
1169
+
1170
+ mut_reached = true;
1171
+
1172
+ return Result.ok(x);
1173
+ });
1174
+
1175
+ assert.isFalse(mut_reached);
1176
+
1177
+ assert.deepStrictEqual(result, Result.err('first error'));
1178
+ });
1179
+
1180
+ test('should collect error types from yields and the return', () => {
1181
+ const makeSource = (): Result<number, 'e1'> => Result.ok(1);
1182
+
1183
+ const source = makeSource();
1184
+
1185
+ const result = Result.safeTry(function* () {
1186
+ const x = yield* Result.safeUnwrap(source);
1187
+
1188
+ if (x > 0) {
1189
+ return Result.err('e2');
1190
+ }
1191
+
1192
+ return Result.ok(x);
1193
+ });
1194
+
1195
+ expectType<typeof result, Result<number, 'e1' | 'e2'>>('=');
1196
+
1197
+ assert.deepStrictEqual(result, Result.err('e2'));
1198
+ });
1199
+
1200
+ test('async generator should return a Promise of the body result', async () => {
1201
+ const result = await Result.safeTry(async function* () {
1202
+ const x = yield* Result.safeUnwrap(
1203
+ await Result.fromPromise(Promise.resolve(10)),
1204
+ );
1205
+
1206
+ return Result.ok(x + 1);
1207
+ });
1208
+
1209
+ assert.deepStrictEqual(result, Result.ok(11));
1210
+ });
1211
+
1212
+ test('async generator should short-circuit on Err', async () => {
1213
+ const makeFailing = (): Promise<Result<number, string>> =>
1214
+ Promise.resolve(Result.err('async error'));
1215
+
1216
+ let mut_reached = false;
1217
+
1218
+ const promise = Result.safeTry(async function* () {
1219
+ const x = yield* Result.safeUnwrap(await makeFailing());
1220
+
1221
+ mut_reached = true;
1222
+
1223
+ return Result.ok(x);
1224
+ });
1225
+
1226
+ expectType<typeof promise, Promise<Result<number, string>>>('=');
1227
+
1228
+ const result = await promise;
1229
+
1230
+ assert.isFalse(mut_reached);
1231
+
1232
+ assert.deepStrictEqual(result, Result.err('async error'));
1233
+ });
1234
+
1235
+ test('should run pending `finally` blocks when short-circuiting', () => {
1236
+ // Created through a function so that the const is not flow-narrowed to
1237
+ // the Err side.
1238
+ const makeFailing = (): Result<number, string> => Result.err('boom');
1239
+
1240
+ let mut_cleanedUp = false;
1241
+
1242
+ const result = Result.safeTry(function* () {
1243
+ try {
1244
+ const x = yield* Result.safeUnwrap(makeFailing());
1245
+
1246
+ return Result.ok(x);
1247
+ } finally {
1248
+ mut_cleanedUp = true;
1249
+ }
1250
+ });
1251
+
1252
+ assert.isTrue(mut_cleanedUp);
1253
+
1254
+ assert.deepStrictEqual(result, Result.err('boom'));
1255
+ });
1256
+
1257
+ test('should propagate an exception thrown by the body', () => {
1258
+ expect(() =>
1259
+ Result.safeTry(function* () {
1260
+ const x = yield* Result.safeUnwrap(Result.ok(1));
1261
+
1262
+ throw new Error(`boom: ${x}`);
1263
+ }),
1264
+ ).toThrow('boom: 1');
1265
+ });
1266
+
1267
+ test('async generator should reject on an exception thrown by the body', async () => {
1268
+ await expect(
1269
+ Result.safeTry(async function* () {
1270
+ const x = yield* Result.safeUnwrap(
1271
+ await Promise.resolve(Result.ok(1)),
1272
+ );
1273
+
1274
+ throw new Error(`boom: ${x}`);
1275
+ }),
1276
+ ).rejects.toThrow('boom: 1');
1277
+ });
1278
+
1279
+ test('should propagate through a delegating helper generator', () => {
1280
+ const makeSource = (): Result<number, 'e1'> => Result.err('e1');
1281
+
1282
+ const doubled = function* (): Generator<Err<'e1'>, number, unknown> {
1283
+ const x = yield* Result.safeUnwrap(makeSource());
1284
+
1285
+ return x * 2;
1286
+ };
1287
+
1288
+ const result = Result.safeTry(function* () {
1289
+ const y = yield* doubled();
1290
+
1291
+ return Result.ok(y);
1292
+ });
1293
+
1294
+ expectType<typeof result, Result<number, 'e1'>>('=');
1295
+
1296
+ assert.deepStrictEqual(result, Result.err('e1'));
1297
+ });
1298
+
1299
+ test('async generator should run pending `finally` blocks too', async () => {
1300
+ const makeFailing = (): Promise<Result<number, string>> =>
1301
+ Promise.resolve(Result.err('boom'));
1302
+
1303
+ let mut_cleanedUp = false;
1304
+
1305
+ const result = await Result.safeTry(async function* () {
1306
+ try {
1307
+ const x = yield* Result.safeUnwrap(await makeFailing());
1308
+
1309
+ return Result.ok(x);
1310
+ } finally {
1311
+ mut_cleanedUp = true;
1312
+ }
1313
+ });
1314
+
1315
+ assert.isTrue(mut_cleanedUp);
1316
+
1317
+ assert.deepStrictEqual(result, Result.err('boom'));
1318
+ });
1319
+ });
1320
+
1321
+ describe('safeUnwrap', () => {
1322
+ test('should return the value without yielding for an Ok', () => {
1323
+ const generator = Result.safeUnwrap(Result.ok(7));
1324
+
1325
+ assert.deepStrictEqual(generator.next(), { done: true, value: 7 });
1326
+ });
1327
+
1328
+ test('should yield the Err itself for an Err', () => {
1329
+ const makeFailing = (): Result<number, string> => Result.err('boom');
1330
+
1331
+ const generator = Result.safeUnwrap(makeFailing());
1332
+
1333
+ assert.deepStrictEqual(generator.next(), {
1334
+ done: false,
1335
+ value: Result.err('boom'),
1336
+ });
1337
+ });
1338
+
1339
+ test('should throw if resumed after yielding an Err', () => {
1340
+ const makeFailing = (): Result<number, string> => Result.err('boom');
1341
+
1342
+ const generator = Result.safeUnwrap(makeFailing());
1343
+
1344
+ generator.next();
1345
+
1346
+ expect(() => generator.next()).toThrow(
1347
+ 'it must only be used via `yield*` inside `safeTry()`',
1348
+ );
1349
+ });
1350
+ });
1020
1351
  });
@@ -1,4 +1,3 @@
1
- /* eslint-disable functional/immutable-data */
2
1
  /* eslint-disable functional/no-class-inheritance */
3
2
  /* eslint-disable unicorn/error-message */
4
3
  import { isError as isErrorBySindreSorhus } from '@sindresorhus/is';
@@ -129,14 +128,15 @@ describe('isError from @sindresorhus/is', () => {
129
128
  });
130
129
 
131
130
  test('should return false for errors created with Object.create', () => {
132
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
133
- const protoError = Object.create(Error.prototype);
131
+ // Built and then re-pointed rather than `Object.create(Error.prototype)`,
132
+ // which the strict standard library types as `unknown` — correctly, since
133
+ // it knows nothing about the resulting shape. The object under test is the
134
+ // same: Error.prototype in its chain, never constructed as an Error.
135
+ const mut_protoError = { message: 'proto error', name: 'ProtoError' };
134
136
 
135
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
136
- protoError.message = 'proto error';
137
+ Object.setPrototypeOf(mut_protoError, Error.prototype);
137
138
 
138
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
139
- protoError.name = 'ProtoError';
139
+ const protoError = mut_protoError;
140
140
 
141
141
  assert.isFalse(isErrorBySindreSorhus(protoError));
142
142
  });
@@ -11,7 +11,11 @@ export const debounce = <Args extends readonly unknown[]>(
11
11
  func: (...args: Args) => void,
12
12
  waitMilliseconds: number,
13
13
  ): ((...args: Args) => void) => {
14
- let mut_timeoutId: ReturnType<typeof setTimeout> | undefined;
14
+ // Not `ReturnType<typeof setTimeout>`: with both the DOM and Node
15
+ // declarations in scope that resolves to `unknown`, which `clearTimeout`
16
+ // will not take. Naming what `clearTimeout` accepts says the same thing and
17
+ // stays true in either environment.
18
+ let mut_timeoutId: Parameters<typeof clearTimeout>[0];
15
19
 
16
20
  return (...args: Args): void => {
17
21
  clearTimeout(mut_timeoutId);
@@ -94,6 +94,13 @@ export const fastDeepEqual = <T,>(a: T, b: T): boolean => {
94
94
  );
95
95
  }
96
96
 
97
- // true if both NaN, false otherwise
98
- return Number.isNaN(a) && Number.isNaN(b);
97
+ // true if both NaN, false otherwise. `Number.isNaN` takes a number: called
98
+ // with anything else it is always false, so the check belongs behind a
99
+ // `typeof` guard rather than relying on that.
100
+ return (
101
+ typeof a === 'number' &&
102
+ typeof b === 'number' &&
103
+ Number.isNaN(a) &&
104
+ Number.isNaN(b)
105
+ );
99
106
  };