# thesmo-calc-fields-base

> Basic types and functionality for calculating fields

Latest version **5.0.4** (published 2020-05-09) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install thesmo-calc-fields-base
pnpm add thesmo-calc-fields-base
yarn add thesmo-calc-fields-base
bun add thesmo-calc-fields-base
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 5.0.4 |
| Published | 2020-05-09 |
| First published | 2020-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 55 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | revrulb |
| Maintainers | revrulb |
| Keywords | data-manipulation, calculating-fields, cf, functional, fp, state |

## Links

- npm: https://www.npmjs.com/package/thesmo-calc-fields-base
- Repository: https://github.com/revrulb/thesmo-calc-fields-base
- Homepage: https://github.com/revrulb/thesmo-calc-fields-base#readme
- Issues: https://github.com/revrulb/thesmo-calc-fields-base/issues
- npm.io page: https://npm.io/package/thesmo-calc-fields-base

## Recent versions

- 5.0.4 (latest) — 2020-05-09
- 5.0.3 — 2020-04-29
- 5.0.2 — 2020-04-22
- 5.0.1 — 2020-04-19
- 5.0.0 — 2020-04-19
- 4.0.0 — 2020-04-12
- 3.1.1 — 2020-04-05
- 3.1.0 — 2020-04-05
- 3.0.8 — 2020-04-05
- 3.0.7 — 2020-04-05
- 3.0.6 — 2020-03-31
- 3.0.5 — 2020-03-31
- 3.0.4 — 2020-03-31
- 3.0.3 — 2020-03-31
- 3.0.2 — 2020-03-31
- … 15 more at https://npm.io/package/thesmo-calc-fields-base/versions

## README

<p align="center">
    <img src='https://raw.githubusercontent.com/revrulb/thesmo-calc-fields-base/master/logo.svg?sanitize=true'></img>
</p>

![npm](https://img.shields.io/npm/v/thesmo-calc-fields-base)
![GitHub](https://img.shields.io/github/license/revrulb/thesmo-calc-fields-base)
![GitHub last commit](https://img.shields.io/github/last-commit/revrulb/thesmo-calc-fields-base)
![npm](https://img.shields.io/npm/dm/thesmo-calc-fields-base)
![GitHub top language](https://img.shields.io/github/languages/top/revrulb/thesmo-calc-fields-base)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)


# 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

```typescript
type SafeCF<Source, Result = Source> = (source: Source) => Result | null;
```

## **UnsafeCF**

Mapper from unknown type to Result type, exception can be
thrown hypothetically

```typescript
type UnsafeCF<Result> = (source: unknown) => Result;
```

## **Guarantor**

Function which takes UnsafeCF and returns SafeCF

```typescript
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)

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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

```typescript
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
>;
```

---
_Source: https://npm.io/package/thesmo-calc-fields-base · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
