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