@zelgadis87/utils-core 5.5.0-beta.4 → 6.0.0-beta.6

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/.rollup/index.cjs CHANGED
@@ -213,7 +213,7 @@ function throwIfNullOrUndefined(source, errorProducer = () => new Error(`Unexpec
213
213
  return ifNullOrUndefined(source, () => { throw errorProducer(); });
214
214
  }
215
215
 
216
- class Optional {
216
+ class OptionalClz {
217
217
  _present;
218
218
  _value;
219
219
  constructor(t) {
@@ -221,10 +221,6 @@ class Optional {
221
221
  this._value = defined ? t : undefined;
222
222
  this._present = defined;
223
223
  }
224
- getRawValue() {
225
- return this._value;
226
- }
227
- /** @deprecated[2026.04.07]: Replace with {@link getOrElseThrow} (drop-in replacement with no arguments) */
228
224
  get() {
229
225
  return this.getOrElseThrow();
230
226
  }
@@ -233,22 +229,6 @@ class Optional {
233
229
  throw errorProducer();
234
230
  return this._value;
235
231
  }
236
- set(t) {
237
- if (isNullOrUndefined(t))
238
- throw new ErrorSetEmptyOptional();
239
- this._value = t;
240
- this._present = true;
241
- }
242
- setNullable(t) {
243
- if (isDefined(t)) {
244
- return this.set(t);
245
- }
246
- return this;
247
- }
248
- clear() {
249
- this._value = undefined;
250
- this._present = false;
251
- }
252
232
  isEmpty() {
253
233
  return !this._present;
254
234
  }
@@ -263,12 +243,13 @@ class Optional {
263
243
  if (this.isPresent())
264
244
  return callback(this.get());
265
245
  }
266
- ifPresentThenClear(callback) {
267
- if (this.isPresent()) {
268
- callback(this.get());
269
- this.clear();
270
- }
271
- }
246
+ /**
247
+ * Dispatches to one of two callbacks based on whether a value is present.
248
+ * Equivalent to `fold` (fp-ts), `match` (effect/Scala), and `ifPresentOrElse` (Java, but with return values).
249
+ * @param callbackIfPresent - called with the current value when present; its return value is forwarded
250
+ * @param callbackIfEmpty - called when empty; its return value is forwarded
251
+ * @returns the return value of whichever callback was invoked
252
+ */
272
253
  apply(callbackIfPresent, callbackIfEmpty) {
273
254
  if (this.isEmpty()) {
274
255
  return callbackIfEmpty();
@@ -285,7 +266,6 @@ class Optional {
285
266
  return newValue;
286
267
  }
287
268
  }
288
- orElse = this.orElseReturn.bind(this);
289
269
  orElseReturnNull() {
290
270
  return this.isPresent() ? this.get() : null;
291
271
  }
@@ -300,59 +280,18 @@ class Optional {
300
280
  return newValueProducer();
301
281
  }
302
282
  }
303
- orElseGet = this.orElseProduce.bind(this);
304
- orElseReturnAndApply(newValue) {
305
- if (this.isPresent()) {
306
- return this.get();
307
- }
308
- else {
309
- this.set(newValue);
310
- return newValue;
311
- }
312
- }
313
- orElseProduceAndApply(newValueProducer) {
314
- if (this.isPresent()) {
315
- return this.get();
316
- }
317
- else {
318
- const newValue = newValueProducer();
319
- this.set(newValue);
320
- return newValue;
321
- }
322
- }
323
- orElseReturnNullableAndApply(newValue) {
324
- if (this.isPresent()) {
325
- return this;
326
- }
327
- else {
328
- this.setNullable(newValue);
329
- return this;
330
- }
331
- }
332
- orElseProduceNullableAndApply(newValueProducer) {
333
- if (this.isPresent()) {
334
- return this;
335
- }
336
- else {
337
- const newValue = newValueProducer();
338
- this.setNullable(newValue);
339
- return this;
340
- }
341
- }
342
283
  orElseReturnNullable(newValue) {
343
284
  if (this.isEmpty())
344
- return Optional.ofNullable(newValue);
285
+ return OptionalClz.ofNullable(newValue);
345
286
  return this;
346
287
  }
347
- orElseNullable = this.orElseReturnNullable.bind(this);
348
288
  orElseProduceNullable(newValueProducer) {
349
289
  if (this.isEmpty()) {
350
290
  const newValue = newValueProducer();
351
- return Optional.ofNullable(newValue);
291
+ return OptionalClz.ofNullable(newValue);
352
292
  }
353
293
  return this;
354
294
  }
355
- orElseGetNullable = this.orElseProduceNullable.bind(this);
356
295
  orElseThrow(errorProducer) {
357
296
  if (this.isEmpty())
358
297
  throw errorProducer();
@@ -364,35 +303,48 @@ class Optional {
364
303
  throw errorProducer(this.get());
365
304
  }
366
305
  mapTo(mapper) {
367
- return this.apply(() => Optional.ofNullable(mapper(this.get())), () => Optional.empty());
306
+ return this.apply(() => OptionalClz.of(mapper(this.get())), () => OptionalClz.empty());
368
307
  }
369
308
  flatMapTo(mapper) {
370
- return this.apply(() => mapper(this.get()), () => Optional.empty());
309
+ return this.apply(() => mapper(this.get()), () => OptionalClz.empty());
371
310
  }
372
311
  filter(predicate) {
373
312
  if (this.isEmpty())
374
313
  return this;
375
314
  if (predicate(this.get()))
376
315
  return this;
377
- return Optional.empty();
316
+ return OptionalClz.empty();
378
317
  }
379
318
  static empty() {
380
- return new Optional(undefined);
319
+ return new OptionalClz(undefined);
381
320
  }
321
+ /**
322
+ * Creates a present optional wrapping a non-null value.
323
+ * Throws {@link ErrorCannotInstantiatePresentOptionalWithEmptyValue} if `t` is null or undefined.
324
+ *
325
+ * ⚠️ Do NOT use this method when the value may be null/undefined.
326
+ * Anti-pattern: `value ? Optional.of(value!) : Optional.empty()` → use {@link ofNullable} instead.
327
+ * Only use this when you are certain the value is defined (e.g. a literal, a validated input, or a narrowed type).
328
+ */
382
329
  static of(t) {
383
330
  if (isNullOrUndefined(t))
384
331
  throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
385
- return new Optional(t);
332
+ return new OptionalClz(t);
386
333
  }
334
+ /**
335
+ * Creates an optional that is present when `t` is defined, or empty when null/undefined.
336
+ * This is the idiomatic replacement for the anti-pattern:
337
+ * `value ? Optional.of(value!) : Optional.empty()` → `Optional.ofNullable(value)`
338
+ */
387
339
  static ofNullable(t) {
388
- return new Optional(t);
340
+ return new OptionalClz(t);
389
341
  }
390
342
  static findInArray(arr, predicate) {
391
- return Optional.ofNullable(arr.find(predicate));
343
+ return OptionalClz.ofNullable(arr.find(predicate));
392
344
  }
393
345
  static findIndexInArray(arr, predicate) {
394
346
  const idx = arr.findIndex(predicate);
395
- return idx === -1 ? Optional.empty() : Optional.of(idx);
347
+ return idx === -1 ? OptionalClz.empty() : OptionalClz.of(idx);
396
348
  }
397
349
  }
398
350
  class ErrorGetEmptyOptional extends Error {
@@ -400,11 +352,6 @@ class ErrorGetEmptyOptional extends Error {
400
352
  super("Cannot retrieve a value from an empty Optional.");
401
353
  }
402
354
  }
403
- class ErrorSetEmptyOptional extends Error {
404
- constructor() {
405
- super("Cannot set a null or undefined value.");
406
- }
407
- }
408
355
  class ErrorCannotInstantiatePresentOptionalWithEmptyValue extends Error {
409
356
  constructor() {
410
357
  super("Cannot initialize a PresentOptional with a null or undefined value.");
@@ -841,11 +788,11 @@ function shallowArrayEquals(a, b) {
841
788
  }
842
789
  /** @deprecated[2026.03.01]: Use {@link Optional.findInArray} instead. */
843
790
  function findInArray(arr, predicate) {
844
- return Optional.findInArray(arr, predicate);
791
+ return OptionalClz.findInArray(arr, predicate);
845
792
  }
846
793
  /** @deprecated[2026.03.01]: Use {@link Optional.findIndexInArray} instead. */
847
794
  function findIndexInArray(arr, predicate) {
848
- return Optional.findIndexInArray(arr, predicate);
795
+ return OptionalClz.findIndexInArray(arr, predicate);
849
796
  }
850
797
  function zip(ts, rs) {
851
798
  if (ts.length !== rs.length)
@@ -866,8 +813,8 @@ function unzip(arr) {
866
813
  */
867
814
  function arrayGet(arr, index) {
868
815
  if (index < 0 || index >= arr.length)
869
- return Optional.empty();
870
- return Optional.of(arr[index]);
816
+ return OptionalClz.empty();
817
+ return OptionalClz.of(arr[index]);
871
818
  }
872
819
 
873
820
  function isTrue(x) {
@@ -1446,7 +1393,7 @@ const NEVER = new Promise(_resolve => { });
1446
1393
 
1447
1394
  /**
1448
1395
  * Returns a random integer in the range [min, max].
1449
- * @deprecated Use randomIntegerInInterval or randomDecimalInInterval instead
1396
+ * @deprecated[2026.03.17]: Use randomIntegerInInterval or randomDecimalInInterval instead.
1450
1397
  */
1451
1398
  function randomNumberInInterval(min, max) {
1452
1399
  return Math.floor(Math.random() * (max - min + 1) + min);
@@ -1501,8 +1448,6 @@ function entriesToDict(entries) {
1501
1448
  function entriesToEntries(dict, mapper) {
1502
1449
  return entriesToDict(dictToEntries(dict).map((entry) => mapper(entry)));
1503
1450
  }
1504
- /** @deprecated[2025.08.01]: Compatibility layer. */
1505
- const mapEntries = entriesToEntries;
1506
1451
  function entriesToList(dict, mapper) {
1507
1452
  return dictToEntries(dict).map((entry) => mapper(entry));
1508
1453
  }
@@ -2508,7 +2453,7 @@ class TimeInstant extends TimeBase {
2508
2453
  });
2509
2454
  }
2510
2455
  /**
2511
- * @deprecated [2025.10.19]: Use fromIso8601 instead.
2456
+ * @deprecated[2025.10.19]: Use fromIso8601 instead.
2512
2457
  */
2513
2458
  static tryFromIso8601 = this.fromIso8601;
2514
2459
  static now() {
@@ -4119,7 +4064,6 @@ exports.Deferred = Deferred;
4119
4064
  exports.DeferredCanceledError = DeferredCanceledError;
4120
4065
  exports.ErrorCannotInstantiatePresentOptionalWithEmptyValue = ErrorCannotInstantiatePresentOptionalWithEmptyValue;
4121
4066
  exports.ErrorGetEmptyOptional = ErrorGetEmptyOptional;
4122
- exports.ErrorSetEmptyOptional = ErrorSetEmptyOptional;
4123
4067
  exports.Lazy = Lazy;
4124
4068
  exports.LazyAsync = LazyAsync;
4125
4069
  exports.LazyDictionary = LazyDictionary;
@@ -4128,7 +4072,7 @@ exports.NEVER = NEVER;
4128
4072
  exports.NonExhaustiveSwitchError = NonExhaustiveSwitchError;
4129
4073
  exports.Operation = Operation;
4130
4074
  exports.OperationAggregateError = OperationAggregateError;
4131
- exports.Optional = Optional;
4075
+ exports.Optional = OptionalClz;
4132
4076
  exports.PredicateBuilder = PredicateBuilder;
4133
4077
  exports.RandomTimeDuration = RandomTimeDuration;
4134
4078
  exports.RateThrottler = RateThrottler;
@@ -4241,7 +4185,6 @@ exports.jsonCloneDeep = jsonCloneDeep;
4241
4185
  exports.last = last$1;
4242
4186
  exports.listToDict = listToDict;
4243
4187
  exports.mapDefined = mapDefined;
4244
- exports.mapEntries = mapEntries;
4245
4188
  exports.mapFirstTruthy = mapFirstTruthy;
4246
4189
  exports.mapTruthys = mapTruthys;
4247
4190
  exports.max = max;