@zelgadis87/utils-core 6.0.0-beta.5 → 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
@@ -221,9 +221,6 @@ class OptionalClz {
221
221
  this._value = defined ? t : undefined;
222
222
  this._present = defined;
223
223
  }
224
- getRawValue() {
225
- return this._value;
226
- }
227
224
  get() {
228
225
  return this.getOrElseThrow();
229
226
  }
@@ -232,22 +229,6 @@ class OptionalClz {
232
229
  throw errorProducer();
233
230
  return this._value;
234
231
  }
235
- set(t) {
236
- if (isNullOrUndefined(t))
237
- throw new ErrorSetEmptyOptional();
238
- this._value = t;
239
- this._present = true;
240
- }
241
- setNullable(t) {
242
- if (isDefined(t)) {
243
- return this.set(t);
244
- }
245
- return this;
246
- }
247
- clear() {
248
- this._value = undefined;
249
- this._present = false;
250
- }
251
232
  isEmpty() {
252
233
  return !this._present;
253
234
  }
@@ -262,6 +243,13 @@ class OptionalClz {
262
243
  if (this.isPresent())
263
244
  return callback(this.get());
264
245
  }
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
+ */
265
253
  apply(callbackIfPresent, callbackIfEmpty) {
266
254
  if (this.isEmpty()) {
267
255
  return callbackIfEmpty();
@@ -292,44 +280,6 @@ class OptionalClz {
292
280
  return newValueProducer();
293
281
  }
294
282
  }
295
- orElseReturnAndApply(newValue) {
296
- if (this.isPresent()) {
297
- return this.get();
298
- }
299
- else {
300
- this.set(newValue);
301
- return newValue;
302
- }
303
- }
304
- orElseProduceAndApply(newValueProducer) {
305
- if (this.isPresent()) {
306
- return this.get();
307
- }
308
- else {
309
- const newValue = newValueProducer();
310
- this.set(newValue);
311
- return newValue;
312
- }
313
- }
314
- orElseReturnNullableAndApply(newValue) {
315
- if (this.isPresent()) {
316
- return this;
317
- }
318
- else {
319
- this.setNullable(newValue);
320
- return this;
321
- }
322
- }
323
- orElseProduceNullableAndApply(newValueProducer) {
324
- if (this.isPresent()) {
325
- return this;
326
- }
327
- else {
328
- const newValue = newValueProducer();
329
- this.setNullable(newValue);
330
- return this;
331
- }
332
- }
333
283
  orElseReturnNullable(newValue) {
334
284
  if (this.isEmpty())
335
285
  return OptionalClz.ofNullable(newValue);
@@ -353,7 +303,7 @@ class OptionalClz {
353
303
  throw errorProducer(this.get());
354
304
  }
355
305
  mapTo(mapper) {
356
- return this.apply(() => OptionalClz.ofNullable(mapper(this.get())), () => OptionalClz.empty());
306
+ return this.apply(() => OptionalClz.of(mapper(this.get())), () => OptionalClz.empty());
357
307
  }
358
308
  flatMapTo(mapper) {
359
309
  return this.apply(() => mapper(this.get()), () => OptionalClz.empty());
@@ -368,11 +318,24 @@ class OptionalClz {
368
318
  static empty() {
369
319
  return new OptionalClz(undefined);
370
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
+ */
371
329
  static of(t) {
372
330
  if (isNullOrUndefined(t))
373
331
  throw new ErrorCannotInstantiatePresentOptionalWithEmptyValue();
374
332
  return new OptionalClz(t);
375
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
+ */
376
339
  static ofNullable(t) {
377
340
  return new OptionalClz(t);
378
341
  }
@@ -389,11 +352,6 @@ class ErrorGetEmptyOptional extends Error {
389
352
  super("Cannot retrieve a value from an empty Optional.");
390
353
  }
391
354
  }
392
- class ErrorSetEmptyOptional extends Error {
393
- constructor() {
394
- super("Cannot set a null or undefined value.");
395
- }
396
- }
397
355
  class ErrorCannotInstantiatePresentOptionalWithEmptyValue extends Error {
398
356
  constructor() {
399
357
  super("Cannot initialize a PresentOptional with a null or undefined value.");
@@ -4106,7 +4064,6 @@ exports.Deferred = Deferred;
4106
4064
  exports.DeferredCanceledError = DeferredCanceledError;
4107
4065
  exports.ErrorCannotInstantiatePresentOptionalWithEmptyValue = ErrorCannotInstantiatePresentOptionalWithEmptyValue;
4108
4066
  exports.ErrorGetEmptyOptional = ErrorGetEmptyOptional;
4109
- exports.ErrorSetEmptyOptional = ErrorSetEmptyOptional;
4110
4067
  exports.Lazy = Lazy;
4111
4068
  exports.LazyAsync = LazyAsync;
4112
4069
  exports.LazyDictionary = LazyDictionary;