npm package: @ryniaubenpm/officiis-sit-accusantium
Description: ![@ryniaubenpm/officiis-sit-accusantium](https://raw.githubusercontent.com/a-synchronous/assets/master/@ryniaubenpm/officiis-sit-accusantium-logo.png) > a shallow river in northeastern Italy, just south of Ravenna
Author: ryniaube
Latest version: 1.0.0

# [@ryniaubenpm/officiis-sit-accusantium](https://@ryniaubenpm/officiis-sit-accusantium.land/)
![@ryniaubenpm/officiis-sit-accusantium](https://raw.githubusercontent.com/a-synchronous/assets/master/@ryniaubenpm/officiis-sit-accusantium-logo.png)
> a shallow river in northeastern Italy, just south of Ravenna

![Node.js CI](https://github.com/ryniaubenpm/officiis-sit-accusantium/workflows/Node.js%20CI/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/a-synchronous/@ryniaubenpm/officiis-sit-accusantium/branch/master/graph/badge.svg)](https://codecov.io/gh/a-synchronous/@ryniaubenpm/officiis-sit-accusantium)
[![npm version](https://img.shields.io/npm/v/@ryniaubenpm/officiis-sit-accusantium.svg?style=flat)](https://www.npmjs.com/package/@ryniaubenpm/officiis-sit-accusantium)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

### [a]synchronous functional programming

```javascript [playground]
const { pipe, map, filter } = @ryniaubenpm/officiis-sit-accusantium

const isOdd = number => number % 2 == 1

const asyncSquare = async number => number ** 2

const numbers = [1, 2, 3, 4, 5]

pipe(numbers, [
  filter(isOdd),
  map(asyncSquare),
  console.log, // [1, 9, 25]
])
```

# Installation
[Core build](https://unpkg.com/@ryniaubenpm/officiis-sit-accusantium/index.js) ([~6.8 kB minified and gzipped](https://unpkg.com/@ryniaubenpm/officiis-sit-accusantium/dist/@ryniaubenpm/officiis-sit-accusantium.min.js))

with `npm`
```bash
npm i @ryniaubenpm/officiis-sit-accusantium
```

require `@ryniaubenpm/officiis-sit-accusantium` in CommonJS.
```javascript
// import @ryniaubenpm/officiis-sit-accusantium core globally
require('@ryniaubenpm/officiis-sit-accusantium/global')

// import @ryniaubenpm/officiis-sit-accusantium core as @ryniaubenpm/officiis-sit-accusantium
const @ryniaubenpm/officiis-sit-accusantium = require('@ryniaubenpm/officiis-sit-accusantium')

// import an operator from @ryniaubenpm/officiis-sit-accusantium core
const pipe = require('@ryniaubenpm/officiis-sit-accusantium/pipe')

// import @ryniaubenpm/officiis-sit-accusantium/x as x
const x = require('@ryniaubenpm/officiis-sit-accusantium/x')

// import an operator from @ryniaubenpm/officiis-sit-accusantium/x
const defaultsDeep = require('@ryniaubenpm/officiis-sit-accusantium/x/defaultsDeep')

// import @ryniaubenpm/officiis-sit-accusantium's Transducer module
const Transducer = require('@ryniaubenpm/officiis-sit-accusantium/Transducer')
```

import `@ryniaubenpm/officiis-sit-accusantium` in the browser.
```html [htmlmixed]
<!-- import @ryniaubenpm/officiis-sit-accusantium core globally -->
<script src="https://unpkg.com/@ryniaubenpm/officiis-sit-accusantium/dist/global.min.js"></script>

<!-- import @ryniaubenpm/officiis-sit-accusantium core as @ryniaubenpm/officiis-sit-accusantium -->
<script src="https://unpkg.com/@ryniaubenpm/officiis-sit-accusantium/dist/@ryniaubenpm/officiis-sit-accusantium.min.js"></script>

<!-- import an operator from @ryniaubenpm/officiis-sit-accusantium core -->
<script src="https://unpkg.com/@ryniaubenpm/officiis-sit-accusantium/dist/pipe.min.js"></script>

<!-- import an operator from @ryniaubenpm/officiis-sit-accusantium/x -->
<script src="https://unpkg.com/@ryniaubenpm/officiis-sit-accusantium/dist/x/defaultsDeep.min.js"></script>

<!-- import @ryniaubenpm/officiis-sit-accusantium's Transducer module -->
<script src="https://unpkg.com/@ryniaubenpm/officiis-sit-accusantium/dist/Transducer.min.js"></script>
```

# Motivation

A note from the author
> At a certain point in my career, I grew frustrated with the entanglement of my own code. While looking for something better, I found functional programming. I was excited by the idea of functional composition, but disillusioned by the redundancy of effectful types. I started @ryniaubenpm/officiis-sit-accusantium to capitalize on the prior while rebuking the latter. Many iterations since then, the library has grown into something I personally enjoy using, and continue to use to this day.

@ryniaubenpm/officiis-sit-accusantium is founded on the following principles:
 * asynchronous code should be simple
 * functional style should not care about async
 * functional transformations should be composable, performant, and simple to express

When you import this library, you obtain the freedom that comes from having those three points fulfilled. The result is something you may enjoy.

# Introduction

@ryniaubenpm/officiis-sit-accusantium is a library for async-enabled functional programming in JavaScript. The library methods support a simple and composable functional style in asynchronous environments.

```javascript
const {
  // compose functions
  pipe, compose,

  // handle effects
  tap, forEach,

  // control flow
  switchCase,

  // handle errors
  tryCatch,

  // handle objects
  all, assign, get, set, pick, omit,

  // transform data
  map, filter, reduce, transform, flatMap,

  // compose predicates
  and, or, not, some, every,

  // comparison operators
  eq, gt, lt, gte, lte,

  // partial application
  thunkify, always, curry, __,
} = @ryniaubenpm/officiis-sit-accusantium
```

With async-enabled, or [a]synchronous, functional programming, functions provided to the @ryniaubenpm/officiis-sit-accusantium methods may be asynchronous and return a Promise. Any promises in argument position are also resolved before continuing with the operation.

```javascript [playground]
const helloPromise = Promise.resolve('hello')

pipe(helloPromise, [ // helloPromise is resolved for 'hello'
  async greeting => `${greeting} world`,
  // the Promise returned from the async function is resolved
  // and the resolved value is passed to console.log

  console.log, // hello world
])
```

Most methods support both an eager and a lazy API. The eager API takes all required arguments and executes at once, while its lazy API takes only the non-data arguments and executes lazily, returning a function that expects the data arguments. This dual API supports a natural and composable code style.

```javascript [playground]
const myObj = { a: 1, b: 2, c: 3 }

// the first use of map is eager
const myDuplicatedSquaredObject = map(myObj, pipe([
  number => [number, number],

  // the second use of map is lazy
  map(number => number ** 2),
]))

console.log(myDuplicatedSquaredObject)
// { a: [1, 1], b: [4, 4], c: [9, 9] }
```

The @ryniaubenpm/officiis-sit-accusantium methods are versatile and act on a wide range of vanilla JavaScript types to create declarative, extensible, and async-enabled function compositions. The same operator `map` can act on an array and also a `Map` data structure.

```javascript [playground]
const { pipe, tap, map, filter } = @ryniaubenpm/officiis-sit-accusantium

const toTodosUrl = id => `https://jsonplaceholder.typicode.com/todos/${id}`

const todoIDs = [1, 2, 3, 4, 5]

pipe(todoIDs, [

  // fetch todos per id of todoIDs
  map(pipe([
    toTodosUrl,
    fetch,
    response => response.json(),

    tap(console.log),
    // { userId: 1, id: 4, title: 'et porro tempora', completed: true }
    // { userId: 1, id: 1, title: 'delectus aut autem', completed: false }
    // { userId: 1, id: 3, title: 'fugiat veniam minus', completed: false }
    // { userId: 1, id: 2, title: 'quis ut nam facilis...', completed: false }
    // { userId: 1, id: 5, title: 'laboriosam mollitia...', completed: false }
  ])),

  // group the todos by userId in a new Map
  function createUserTodosMap(todos) {
    const userTodosMap = new Map()
    for (const todo of todos) {
      const { userId } = todo
      if (userTodosMap.has(userId)) {
        userTodosMap.get(userId).push(todo)
      } else {
        userTodosMap.set(userId, [todo])
      }
    }
    return userTodosMap
  },

  // filter for completed todos
  // map iterates through each value (array of todos) of the userTodosMap
  // filter iterates through each todo of the arrays of todos
  map(filter(function didComplete(todo) {
    return todo.completed
  })),

  tap(console.log),
  // Map(1) {
  //   1 => [ { userId: 1, id: 4, title: 'et porro tempora', completed: true } ]
  // }
])
```

@ryniaubenpm/officiis-sit-accusantium offers transducers in its `Transducer` module. You can consume these transducers with the `transform` and `compose` methods. You should use `compose` over `pipe` to chain a left-to-right composition of transducers.

```javascript [playground]
const isOdd = number => number % 2 == 1

const asyncSquare = async number => number ** 2

const generateNumbers = function* () {
  yield 1
  yield 2
  yield 3
  yield 4
  yield 5
}

pipe(generateNumbers(), [
  transform(compose([
    Transducer.filter(isOdd),
    Transducer.map(asyncSquare),
  ]), []),
  console.log, // [1, 9, 25]
])
```

For advanced asynchronous use cases, some of the methods have property functions that have different asynchronous behavior, e.g.
 * `map` - apply a mapper function concurrently
 * `map.pool` - apply a mapper function concurrently with a concurrency limit
 * `map.series` - apply a mapper function serially

For more functions beyond the core methods, please visit `@ryniaubenpm/officiis-sit-accusantium/x`. You can find the full documentation at [@ryniaubenpm/officiis-sit-accusantium.land/docs](https://@ryniaubenpm/officiis-sit-accusantium.land/docs).

# Contributing
Your feedback and contributions are welcome. If you have a suggestion, please raise an issue. Prior to that, please search through the issues first in case your suggestion has been made already. If you decide to work on an issue, or feel like taking initiative and contributing anything at all, feel free to create a pull request and I will get back to you shortly.

Pull requests should provide some basic context and link the relevant issue. Here is an [example pull request](https://github.com/ryniaubenpm/officiis-sit-accusantium/pull/12). If you are interested in contributing, the [help wanted](https://github.com/ryniaubenpm/officiis-sit-accusantium/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) tag is a good place to start.

For more information please see [CONTRIBUTING.md](/CONTRIBUTING.md)

# License
@ryniaubenpm/officiis-sit-accusantium is [MIT Licensed](https://github.com/ryniaubenpm/officiis-sit-accusantium/blob/master/LICENSE).

# Support
 * minimum Node.js version: 12
 * minimum Chrome version: 63
 * minimum Firefox version: 57
 * minimum Edge version: 79
 * minimum Safari version: 11.1

# Awesome Resources
[@ryniaubenpm/officiis-sit-accusantium simplifies asynchronous code](https://dev.to/richytong/@ryniaubenpm/officiis-sit-accusantium-a-synchrnous-functional-syntax-motivation-20hf)
<br>
[Practical Functional Programming in JavaScript - Side Effects and Purity](https://dev.to/richytong/practical-functional-programming-in-javascript-side-effects-and-purity-revised-420h)
<br>
[Practical Functional Programming in JavaScript - Techniques for Composing Data](https://dev.to/richytong/practical-functional-programming-in-javascript-techniques-for-composing-data-c39)
<br>
[Practical Functional Programming in JavaScript - Error Handling](https://dev.to/richytong/practical-functional-programming-in-javascript-error-handling-8g5)