@zelgadis87/utils-core 4.6.1 → 4.7.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/dist/Optional.d.ts +24 -16
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/esbuild/index.cjs +2960 -0
- package/esbuild/index.cjs.map +7 -0
- package/esbuild/index.mjs +2795 -0
- package/esbuild/index.mjs.map +7 -0
- package/package.json +2 -2
- package/src/Optional.ts +39 -36
package/src/Optional.ts
CHANGED
|
@@ -12,17 +12,21 @@ export class Optional<T> implements TOptional<T> {
|
|
|
12
12
|
this._present = defined;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
public
|
|
15
|
+
public getRawValue(): T | undefined {
|
|
16
16
|
return this._value;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
public get(): T
|
|
19
|
+
public get(): T {
|
|
20
|
+
return this.getOrElseThrow( () => new ErrorGetEmptyOptional() );
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public getOrElseThrow( errorProducer: TProducer<Error> ): T {
|
|
20
24
|
if ( this.isEmpty() )
|
|
21
|
-
throw
|
|
22
|
-
return this.
|
|
25
|
+
throw errorProducer();
|
|
26
|
+
return this._value!;
|
|
23
27
|
}
|
|
24
28
|
|
|
25
|
-
public set(t: T) {
|
|
29
|
+
public set( t: T ) {
|
|
26
30
|
if ( isNullOrUndefined( t ) )
|
|
27
31
|
throw new ErrorSetEmptyOptional();
|
|
28
32
|
this._value = t;
|
|
@@ -47,13 +51,13 @@ export class Optional<T> implements TOptional<T> {
|
|
|
47
51
|
}
|
|
48
52
|
public ifPresent( callback: TConsumer<T> ) {
|
|
49
53
|
if ( this.isPresent() )
|
|
50
|
-
callback( this.
|
|
54
|
+
callback( this.get() );
|
|
51
55
|
}
|
|
52
56
|
public apply( callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction ) {
|
|
53
57
|
if ( this.isEmpty() ) {
|
|
54
58
|
callbackIfEmpty();
|
|
55
59
|
} else {
|
|
56
|
-
callbackIfPresent( this.
|
|
60
|
+
callbackIfPresent( this.get() );
|
|
57
61
|
}
|
|
58
62
|
}
|
|
59
63
|
|
|
@@ -69,9 +73,9 @@ export class Optional<T> implements TOptional<T> {
|
|
|
69
73
|
}
|
|
70
74
|
return this as TPresentOptional<T>;
|
|
71
75
|
}
|
|
72
|
-
public orElseThrow(
|
|
76
|
+
public orElseThrow( errorProducer: TProducer<Error> ) {
|
|
73
77
|
if ( this.isEmpty() )
|
|
74
|
-
throw
|
|
78
|
+
throw errorProducer();
|
|
75
79
|
return this as TPresentOptional<T>;
|
|
76
80
|
}
|
|
77
81
|
|
|
@@ -79,23 +83,15 @@ export class Optional<T> implements TOptional<T> {
|
|
|
79
83
|
return this.flatMapTo( t => Optional.ofNullable( mapper( t ) ) );
|
|
80
84
|
}
|
|
81
85
|
public flatMapTo<R>( mapper: TFunction<T, TOptional<R>> ) {
|
|
82
|
-
|
|
83
|
-
const newValue = mapper( this.rawValue );
|
|
84
|
-
return newValue;
|
|
85
|
-
}
|
|
86
|
-
return this as TEmptyOptional<T>;
|
|
86
|
+
return this.isPresent() ? mapper( this.get() ) : Optional.empty<R>();
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
public filter( predicate: TPredicate<T> ): this | TEmptyOptional<T> {
|
|
90
|
-
if ( this.
|
|
91
|
-
if ( predicate( this.rawValue ) ) {
|
|
92
|
-
return this;
|
|
93
|
-
} else {
|
|
94
|
-
return Optional.empty();
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
90
|
+
if ( this.isEmpty() )
|
|
97
91
|
return this;
|
|
98
|
-
|
|
92
|
+
if ( predicate( this.get() ) )
|
|
93
|
+
return this;
|
|
94
|
+
return Optional.empty();
|
|
99
95
|
}
|
|
100
96
|
|
|
101
97
|
public static empty<T>(): TEmptyOptional<T> {
|
|
@@ -114,19 +110,26 @@ export class Optional<T> implements TOptional<T> {
|
|
|
114
110
|
export default Optional;
|
|
115
111
|
|
|
116
112
|
export type TOptional<T> = {
|
|
117
|
-
readonly rawValue: T | undefined;
|
|
118
113
|
|
|
119
114
|
/**
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
get(): T
|
|
115
|
+
* @returns the currently stored value, if any; throws {@link ErrorGetEmptyOptional} otherwise;
|
|
116
|
+
*/
|
|
117
|
+
get(): T;
|
|
118
|
+
/**
|
|
119
|
+
* @returns the currently stored value, if any; returns undefined otherwise;
|
|
120
|
+
*/
|
|
121
|
+
getRawValue(): T | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* @returns the currently stored value, if any; throws the error generated by errorProducer otherwise;
|
|
124
|
+
*/
|
|
125
|
+
getOrElseThrow: ( errorProducer: TProducer<Error> ) => T;
|
|
123
126
|
/**
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
set(t: T): void;
|
|
127
|
+
* Places a new value inside this optional. Throws {@link ErrorSetEmptyOptional} if t is null or undefined.
|
|
128
|
+
*/
|
|
129
|
+
set( t: T ): void;
|
|
127
130
|
/**
|
|
128
|
-
|
|
129
|
-
|
|
131
|
+
* Clears the current value from this optional, if any.
|
|
132
|
+
*/
|
|
130
133
|
clear(): void;
|
|
131
134
|
isEmpty(): boolean;
|
|
132
135
|
isPresent(): boolean;
|
|
@@ -135,9 +138,9 @@ export type TOptional<T> = {
|
|
|
135
138
|
ifPresent: ( callback: TConsumer<T> ) => void;
|
|
136
139
|
apply( callbackIfPresent: TConsumer<T>, callbackIfEmpty: TVoidFunction ): void;
|
|
137
140
|
|
|
138
|
-
orElse: (
|
|
141
|
+
orElse: ( newValue: T | null | undefined ) => TOptional<T>;
|
|
139
142
|
orElseGet: ( produceValue: TProducer<T | null | undefined> ) => TOptional<T>;
|
|
140
|
-
orElseThrow: (
|
|
143
|
+
orElseThrow: ( errorProducer: TProducer<Error> ) => TPresentOptional<T>;
|
|
141
144
|
|
|
142
145
|
mapTo<R>( mapper: TFunction<T, R> ): TOptional<R>;
|
|
143
146
|
flatMapTo<R>( mapper: TFunction<T, TOptional<R>> ): TOptional<R>;
|
|
@@ -145,19 +148,19 @@ export type TOptional<T> = {
|
|
|
145
148
|
filter( predicate: TPredicate<T> ): TOptional<T>;
|
|
146
149
|
}
|
|
147
150
|
export type TEmptyOptional<T> = TOptional<T> & {
|
|
148
|
-
|
|
151
|
+
get(): never;
|
|
149
152
|
isEmpty(): true;
|
|
150
153
|
isPresent(): false;
|
|
151
154
|
}
|
|
152
155
|
export type TPresentOptional<T> = TOptional<T> & {
|
|
153
|
-
|
|
156
|
+
get(): T;
|
|
154
157
|
isEmpty(): false;
|
|
155
158
|
isPresent(): true;
|
|
156
159
|
}
|
|
157
160
|
|
|
158
161
|
export class ErrorGetEmptyOptional extends Error {
|
|
159
162
|
constructor() {
|
|
160
|
-
super( "Cannot retrieve a value from an
|
|
163
|
+
super( "Cannot retrieve a value from an empty Optional." );
|
|
161
164
|
}
|
|
162
165
|
}
|
|
163
166
|
export class ErrorSetEmptyOptional extends Error {
|