# comp-check

> A Javascript library to help with null checking in functional composition

Latest version **0.0.1** (published 2018-02-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install comp-check
pnpm add comp-check
yarn add comp-check
bun add comp-check
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2018-02-08 |
| First published | 2018-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | mmcglone |
| Keywords | composition, null checking |

## Links

- npm: https://www.npmjs.com/package/comp-check
- Repository: https://github.com/mmcglone/comp-check
- Homepage: https://github.com/mmcglone/comp-check#readme
- Issues: https://github.com/mmcglone/comp-check/issues
- npm.io page: https://npm.io/package/comp-check

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.4

## Recent versions

- 0.0.1 (latest) — 2018-02-08
- 0.0.1-alpha-1.0.0 — 2018-02-03

## README

# comp-check
[![Build Status](https://travis-ci.org/mmcglone/comp-check.svg?branch=master)](https://travis-ci.org/mmcglone/comp-check)
[![Coverage Status](https://coveralls.io/repos/mmcglone/comp-check/badge.svg?branch=master)](https://coveralls.io/r/mmcglone/comp-check?branch=master)
[![npm version](https://badge.fury.io/js/comp-check.svg)](https://badge.fury.io/js/comp-check)

A Javascript library to help with null checking in functional composition

## Example Usage
Suppose we have the following data
```javascript
const usersById = {
  123: {
    id: 123,
    firstName: 'John',
    lastName: 'Smith',
    father: 456,
  },
  456: {
    id: 123,
    firstName: 'Fred',
    lastName: 'Smith',
    father: null,
  }
};
```
and the following functions:
```javascript
const userFromId = id => usersById[id] ? usersById[id] : null;

const father = user => userFromId(user.father);

const fullName = user => {
  const { firstName, lastName } = user;
  if (firstName === undefined || lastName === undefined) {
    return null
  }
  return `${firstName} ${lastName}`;
};
```
And suppose we want to compose these functions into a *pure* function that returns
the full name of a user's father given the user's id.

The following won't work, because it's impure:
```javascript
const pipe = require('lodash/fp/pipe');

const fatherFullName = pipe(
  userFromId,
  father,
  fullName
);
```
It's impure because it throws errors that it doesn't catch, for example,
when `userFromId` or `father` returns null:
```javascript
fatherFullName(123); // Fred Smith
fatherFullName(789); // TypeError: Cannot read property 'father' of null
fatherFullName(456); // TypeError: Cannot destructure property `firstName` of 'undefined' or 'null'.
```
To achieve purity we need to handle these null values without uncaught errors.

In effect, we need something along the following lines:
```javascript
const pipe = require('lodash/fp/pipe');

const fatherFullName = pipe(
   userFromId,
   value => {
     if (value === null) return null;
     return father(value);
   },
   value => {
     if (value === null) return null;
     return fullName(value);
   }
);

fatherFullName(123); // Fred Smith
fatherFullName(789); // null
fatherFullName(456); // null
```
But achieving purity in this way exposes repetitive null checking details
that distract from the main positive focus of our function.
Using comp-check, we can achieve the same result without all the mess:
```javascript
const { always, map, maybe } = require('comp-check');
const pipe = require('lodash/fp/pipe');
const identity = require('lodash/fp/identity');

const fatherFullName = pipe(
  maybe, // wraps our argument in a new object called a "Maybe"
  map(userFromId), // applies userFromId to our argument if not null
  map(father), // applies father to the result if not null
  map(fullName),  // does the same but with fullName
  always(identity) // returns our final result, null or not
);
```

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