@zelgadis87/utils-core 4.11.2 → 4.12.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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package",
3
3
  "name": "@zelgadis87/utils-core",
4
- "version": "4.11.2",
4
+ "version": "4.12.0",
5
5
  "author": "Zelgadis87",
6
6
  "license": "ISC",
7
7
  "private": false,
package/src/Optional.ts CHANGED
@@ -61,18 +61,32 @@ export class Optional<T> implements TOptional<T> {
61
61
  }
62
62
  }
63
63
 
64
- public orElse( newValue: T | null | undefined ) {
64
+ public orElse( newValue: T ) {
65
+ if ( this.isEmpty() )
66
+ return Optional.of( newValue );
67
+ return this as TPresentOptional<T>;
68
+ }
69
+ public orElseGet( newValueProducer: TProducer<T> ) {
70
+ if ( this.isEmpty() ) {
71
+ const newValue = newValueProducer();
72
+ return Optional.of( newValue );
73
+ }
74
+ return this as TPresentOptional<T>;
75
+ }
76
+
77
+ public orElseNullable( newValue: T | null | undefined ) {
65
78
  if ( this.isEmpty() )
66
79
  return Optional.ofNullable( newValue );
67
80
  return this as TPresentOptional<T>;
68
81
  }
69
- public orElseGet( produceValue: TProducer<T | null | undefined> ) {
82
+ public orElseGetNullable( newValueProducer: TProducer<T | null | undefined> ) {
70
83
  if ( this.isEmpty() ) {
71
- const newValue = produceValue();
84
+ const newValue = newValueProducer();
72
85
  return Optional.ofNullable( newValue );
73
86
  }
74
87
  return this as TPresentOptional<T>;
75
88
  }
89
+
76
90
  public orElseThrow( errorProducer: TProducer<Error> ) {
77
91
  if ( this.isEmpty() )
78
92
  throw errorProducer();
@@ -138,8 +152,10 @@ export type TOptional<T> = {
138
152
  ifPresent: ( callback: TConsumer<T> ) => void;
139
153
  apply( callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction ): void;
140
154
 
141
- orElse: ( newValue: T | null | undefined ) => TOptional<T>;
142
- orElseGet: ( produceValue: TProducer<T | null | undefined> ) => TOptional<T>;
155
+ orElse: ( newValue: T ) => T;
156
+ orElseGet: ( newValueProducer: TProducer<T> ) => T;
157
+ orElseNullable: ( newValue: T | null | undefined ) => TOptional<T>;
158
+ orElseGetNullable: ( newValueProducer: TProducer<T | null | undefined> ) => TOptional<T>;
143
159
  orElseThrow: ( errorProducer: TProducer<Error> ) => TPresentOptional<T>;
144
160
 
145
161
  mapTo<R>( mapper: TFunction<T, R> ): TOptional<R>;