README
About
This package contains basic calculating fields, types and providers for it. Calculating field is an abstraction which provides you formalized calculations upon not formalized source.
Types
SafeCF
Mapper from Source to Result types, null will be returned in case of runtime error
type SafeCF<Source, Result = Source> = (source: Source) => Result | null;
UnsafeCF
Mapper from unknown type to Result type, exception can be thrown hypothetically
type UnsafeCF<Result> = (source: unknown) => Result;
Guarantor
Function which takes UnsafeCF and returns SafeCF
type Guarantor<Source> = <Result>(
unsafeCF: UnsafeCF<Result>
) => SafeCF<Source, Result>;
Functions
blank
Blank calculating field which does nothing with source and just returns it (with additional logic possibly)
type Args = {
/**
* Additional logic will be used when this calculating
* field is applied
*/
additionalLogic?: () => void;
};
type Signature = ({ additionalLogic }: Args) => SafeCF<any>;
defaultValue
Calculating field which returns given value in case of source was undefined and/or null and returns source otherwise
type Args<T> = {
/**
* Value will be returned if source is undefined and/or null
* (only for undefined by default)
*/
value: T;
/**
* If this flag is set to true, value will be returned also
* in case of source is null
*/
replaceNullValues?: boolean;
};
type Signature = <Value, SupposedSourceValue = Value>({
value,
replaceNullValues = false
}: Args<Value>) => SafeCF<
SupposedSourceValue,
SupposedSourceValue | Value
>;
fixed
Calculating field which always returns given value
type Args<T> = {
/**
* Value will be returned in any case
*/
value: T;
};
type Signature = <T>({ value }: Args<T>) => SafeCF<unknown, T>;
getGuarantor
Function which returns guarantor to make SafeCF from UnsafeCF
type Args<Source> = {
/**
* This function will be used to check if source
* is of required type
*/
typeCheck: (target: any) => boolean;
/**
* This function will be used to map source of
* unknown type to required type if typeCheck
* function returned false
*/
sourceTypeMapper?: (target: unknown) => Source | null;
/**
* Custom exception handler
*/
exceptionHandler?: (exception: Error) => void;
};
type Signature = <Source>({
typeCheck,
sourceTypeMapper,
exceptionHandler
}: Args<Source>) => Guarantor<Source>;
pipe
Function which takes array of SafeCFs and returns one SafeCF which stores given calculating fields applying logic according to their order in argument array
type Args<Source> = {
/**
* This function will be used to check if source
* is of required type
*/
typeCheck: (target: any) => boolean;
/**
* This function will be used to map source of
* unknown type to required type if typeCheck
* function returned false
*/
sourceTypeMapper?: (target: unknown) => Source | null;
/**
* Custom exception handler
*/
exceptionHandler?: (exception: Error) => void;
};
type Signature = <Source>({
typeCheck,
sourceTypeMapper,
exceptionHandler
}: Args<Source>) => Guarantor<Source>;
selector
Function which applies one of two given calculating fields according to source check result
type Args<Source, PositiveCaseResult, NegativeCaseResult> = {
/**
* Check will be used to select positive or
* negative case CF
*/
check: (source: Source) => boolean;
/**
* Calculating field will be applied if
* check returned true
*/
positiveCase: SafeCF<Source, PositiveCaseResult>;
/**
* Calculating field will be applied if
* check returned false
*/
negativeCase: SafeCF<Source, NegativeCaseResult>;
};
type Signature = <Source, PositiveCaseResult, NegativeCaseResult>({
check,
negativeCase,
positiveCase
}: Args<Source, PositiveCaseResult, NegativeCaseResult>) => SafeCF<
Source,
PositiveCaseResult | NegativeCaseResult
>;