@luvio/utils
Advanced tools
Comparing version 5.2.0 to 5.3.0
export type DeepPartial<T> = (T extends object ? { | ||
[P in keyof T]?: DeepPartial<T[P]>; | ||
} : T extends Array<infer U> ? Array<DeepPartial<U>> : T) | undefined; | ||
export type WithErrors<Data> = { | ||
dataComplete: true; | ||
data: Data; | ||
errors: Error[]; | ||
} | { | ||
dataComplete: false; | ||
data: DeepPartial<Data>; | ||
errors: Error[]; | ||
}; | ||
/** | ||
* A Result can either be an `Ok` or an `Err` variant. | ||
* The usage of `this is Ok<T,E>` and `this is Err<T,E>` on the return | ||
* values of `isOk` and `isErr` act as a type guard on the specific result | ||
* instance, e.g. | ||
* | ||
* function(result: Result<string, unknown>) { | ||
* result.value // this is a type error because value only exists on `Ok` results, and `Result` can be `Ok` or `Err`. | ||
* result.error // this is also a type error because `error` only exists on `Err`, and `Result` can be `Ok` | ||
* if(result.isOk()){ | ||
* result.value // this is not a type error because we checked first, and the result said result is an `Ok` | ||
* result.error // this is a type error because we know that result is an `Ok` | ||
* } else { | ||
* result.error // this is also okay because type script knows that result is not an `Ok` in this `else`, so it must be an `Err` | ||
* result.value // this is a type error because we know that result is an `Err` | ||
* } | ||
* } | ||
* | ||
*/ | ||
interface IResult<T, E> { | ||
/** | ||
* Used to check if a `Result` is an `OK` | ||
* | ||
* @returns `true` if the result is an `OK` variant of Result | ||
*/ | ||
isOk(): this is Ok<T, E>; | ||
/** | ||
* Used to check if a `Result` is an `Err` | ||
* | ||
* @returns `true` if the result is an `Err` variant of Result | ||
*/ | ||
isErr(): this is Err<T, E>; | ||
} | ||
export declare class Ok<T, E> implements IResult<T, E> { | ||
readonly value: T; | ||
constructor(value: T); | ||
isOk(): this is Ok<T, E>; | ||
isErr(): this is Err<T, E>; | ||
} | ||
export declare class Err<T, E> implements IResult<T, E> { | ||
readonly error: E; | ||
constructor(error: E); | ||
isOk(): this is Ok<T, E>; | ||
isErr(): this is Err<T, E>; | ||
} | ||
export declare const ok: <T, E = never>(value: T) => Ok<T, E>; | ||
export declare const err: <T = never, E = unknown>(err: E) => Err<T, E>; | ||
export type Result<T, E> = Ok<T, E> | Err<T, E>; | ||
export type Callback<Result> = (result: Result) => void; | ||
@@ -17,6 +56,12 @@ export declare class DataNotFoundError extends Error { | ||
} | ||
export declare class DataIncompleteError<Data> extends Error { | ||
partialData?: Data | undefined; | ||
constructor(message?: string, partialData?: Data | undefined); | ||
} | ||
export declare function isDataNotFoundError(error: any): error is DataNotFoundError; | ||
export declare function isCacheHitOrError<CacheReturn extends WithErrors<any>>(value: CacheReturn): boolean; | ||
export declare function isDataIncompleteError(error: any): error is DataNotFoundError; | ||
export declare function isCacheHitOrError<CacheReturn extends Result<any, any>>(value: CacheReturn): boolean; | ||
export declare function isCacheMiss<CacheReturn extends Result<any, any>>(value: CacheReturn): boolean; | ||
export * from './sync-or-async'; | ||
export * from './utils'; | ||
export * from './language'; |
@@ -10,12 +10,2 @@ export { IncludeExclude, includes } from './include-exclude'; | ||
export declare function isPromiseLike<T = any>(x: any): x is PromiseLike<T>; | ||
/** | ||
* Concretion<T> is a type that takes an abstract class T and returns a concrete | ||
* implementation of T. | ||
* | ||
* @param T abstract class | ||
* @returns concrete implementation of T | ||
*/ | ||
export type Concretion<T extends abstract new (...args: any) => InstanceType<T>> = { | ||
new (...args: any): InstanceType<T>; | ||
}; | ||
export * from './bfs'; | ||
@@ -27,1 +17,2 @@ export * from './logger'; | ||
export * from './set'; | ||
export * from './subscribable'; |
@@ -355,3 +355,27 @@ /** | ||
// an error to indicate that the data inside a WithErrors construct | ||
class Ok { | ||
constructor(value) { | ||
this.value = value; | ||
} | ||
isOk() { | ||
return true; | ||
} | ||
isErr() { | ||
return !this.isOk(); | ||
} | ||
} | ||
class Err { | ||
constructor(error) { | ||
this.error = error; | ||
} | ||
isOk() { | ||
return false; | ||
} | ||
isErr() { | ||
return !this.isOk(); | ||
} | ||
} | ||
const ok = (value) => new Ok(value); | ||
const err = (err) => new Err(err); | ||
// an error to indicate that the data inside a Result construct | ||
// is missing or incomplete | ||
@@ -364,12 +388,24 @@ class DataNotFoundError extends Error { | ||
} | ||
class DataIncompleteError extends Error { | ||
constructor(message, partialData) { | ||
super(message); | ||
this.partialData = partialData; | ||
this.name = 'DataIncompleteError'; | ||
} | ||
} | ||
function isDataNotFoundError(error) { | ||
return error instanceof DataNotFoundError || error.name === 'DataNotFoundError'; | ||
} | ||
function isDataIncompleteError(error) { | ||
return error instanceof DataIncompleteError || error.name === 'DataIncompleteError'; | ||
} | ||
function isCacheHitOrError(value) { | ||
// return cache result if data was found or error was encountered | ||
const { data, errors } = value; | ||
const cacheHit = data !== undefined && errors.length === 0; | ||
const errorEncountered = errors.length > 0 && !isDataNotFoundError(errors[0]); | ||
return cacheHit || errorEncountered; | ||
if (value.isErr() && (isDataIncompleteError(value.error) || isDataNotFoundError(value.error))) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
function isCacheMiss(value) { | ||
return !isCacheHitOrError(value); | ||
} | ||
@@ -399,2 +435,6 @@ function setOverlaps(setA, setB) { | ||
function isSubscribable(x) { | ||
return 'subscribe' in x; | ||
} | ||
/** | ||
@@ -410,2 +450,2 @@ * Indicates if an object is PromiseLike. | ||
export { isArray as ArrayIsArray, indexOf as ArrayPrototypeIndexOf, push as ArrayPrototypePush, slice as ArrayPrototypeSlice, ConsoleFileParserLogger, ConsoleLogger, DataNotFoundError, stringify as JSONStringify, LogLevelMap, create as ObjectCreate, freeze as ObjectFreeze, keys as ObjectKeys, hasOwnProperty as ObjectPrototypeHasOwnProperty, addAllToSet, bfs, capitalize, deepEquals, includes, isCacheHitOrError, isDataNotFoundError, isPromiseLike, loggerService, rejectedPromiseLike, resolvedPromiseLike, satisfies, setDifference, setOverlaps, stableJSONStringify, toCamelCase, toError, toPascalCase, toScreamingSnakeCase }; | ||
export { isArray as ArrayIsArray, indexOf as ArrayPrototypeIndexOf, push as ArrayPrototypePush, slice as ArrayPrototypeSlice, ConsoleFileParserLogger, ConsoleLogger, DataIncompleteError, DataNotFoundError, Err, stringify as JSONStringify, LogLevelMap, create as ObjectCreate, freeze as ObjectFreeze, keys as ObjectKeys, hasOwnProperty as ObjectPrototypeHasOwnProperty, Ok, addAllToSet, bfs, capitalize, deepEquals, err, includes, isCacheHitOrError, isCacheMiss, isDataIncompleteError, isDataNotFoundError, isPromiseLike, isSubscribable, loggerService, ok, rejectedPromiseLike, resolvedPromiseLike, satisfies, setDifference, setOverlaps, stableJSONStringify, toCamelCase, toError, toPascalCase, toScreamingSnakeCase }; |
{ | ||
"name": "@luvio/utils", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "Luvio utils", | ||
@@ -5,0 +5,0 @@ "repository": { |
26136
15
727