@umituz/react-native-storage 1.3.0 → 1.5.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 +1 -1
- package/src/domain/entities/StorageResult.ts +16 -2
- package/src/index.ts +2 -0
package/package.json
CHANGED
|
@@ -34,11 +34,25 @@ export const failure = <T>(error: StorageError, fallback?: T): StorageResult<T>
|
|
|
34
34
|
fallback,
|
|
35
35
|
});
|
|
36
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Type guard for success result
|
|
39
|
+
*/
|
|
40
|
+
export const isSuccess = <T>(result: StorageResult<T>): result is { success: true; data: T } => {
|
|
41
|
+
return result.success === true;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Type guard for failure result
|
|
46
|
+
*/
|
|
47
|
+
export const isFailure = <T>(result: StorageResult<T>): result is { success: false; error: StorageError; fallback?: T } => {
|
|
48
|
+
return result.success === false;
|
|
49
|
+
};
|
|
50
|
+
|
|
37
51
|
/**
|
|
38
52
|
* Unwrap result with default value
|
|
39
53
|
*/
|
|
40
54
|
export const unwrap = <T>(result: StorageResult<T>, defaultValue: T): T => {
|
|
41
|
-
if (result
|
|
55
|
+
if (isSuccess(result)) {
|
|
42
56
|
return result.data;
|
|
43
57
|
}
|
|
44
58
|
return result.fallback !== undefined ? result.fallback : defaultValue;
|
|
@@ -51,7 +65,7 @@ export const map = <T, U>(
|
|
|
51
65
|
result: StorageResult<T>,
|
|
52
66
|
fn: (data: T) => U
|
|
53
67
|
): StorageResult<U> => {
|
|
54
|
-
if (result
|
|
68
|
+
if (isSuccess(result)) {
|
|
55
69
|
return success(fn(result.data));
|
|
56
70
|
}
|
|
57
71
|
return failure(result.error, result.fallback !== undefined ? fn(result.fallback) : undefined);
|