包详细信息

rubico

a-synchronous4.1kMIT2.7.6

[a]synchronous functional programming

function-composition, asynchronous, transducers, parallel

自述文件

rubico

rubico

a shallow river in northeastern Italy, just south of Ravenna

Node.js CI codecov npm version License: MIT

[a]synchronous functional programming

```javascript [playground] const { pipe, map, filter } = rubico

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/rubico/index.js) ([~7.7 kB minified and gzipped](https://unpkg.com/rubico/dist/rubico.min.js)) [Transducer module](https://unpkg.com/rubico/dist/Transducer.js) ([~1.5kb minified and gzipped](https://unpkg.com/rubico/dist/Transducer.min.js))

with [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm):
```bash
npm i rubico

require rubico in CommonJS:

// import rubico core globally
require('rubico/global')

// import rubico core as rubico
const rubico = require('rubico')

// import an operator from rubico core
const pipe = require('rubico/pipe')

// import rubico/x as x
const x = require('rubico/x')

// import an operator from rubico/x
const defaultsDeep = require('rubico/x/defaultsDeep')

// import rubico's Transducer module
const Transducer = require('rubico/Transducer')

import rubico in the browser: ```html [htmlmixed]

<script src="https://unpkg.com/rubico/dist/global.min.js"></script> <script src="https://unpkg.com/rubico/dist/rubico.min.js"></script> <script src="https://unpkg.com/rubico/dist/pipe.min.js"></script> <script src="https://unpkg.com/rubico/dist/x/defaultsDeep.min.js"></script>

<script src="https://unpkg.com/rubico/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 rubico 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.

rubico 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

rubico is a library for [A]synchronous Functional Programming in JavaScript. The library supports a simple and composable functional style in asynchronous environments.

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

  // control flow
  switchCase,

  // handle errors
  tryCatch,

  // compose data
  all, assign, get, set, pick, omit,

  // iterate
  forEach,

  // 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, __,
} = rubico

With [A]synchronous Functional Programming, any function may be asynchronous and return a promise. All promises are resolved for their values 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 ])


All rubico operators support both eager and lazy APIs. The eager API takes all required arguments and executes at once, while the lazy API takes only the setup arguments and returns a function that executes later. 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 rubico operators 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 } = rubico

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 } ] // } ])


rubico offers transducers through its `Transducer` module. You can consume these transducers with rubico's `transform` and `compose` operators. You can use `compose` 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 rubico operators have property functions that have various asynchronous behavior, e.g.

  • map - applies a mapper function concurrently
  • map.pool - applies a mapper function concurrently with a concurrency limit
  • map.series - applies a mapper function serially

For more functions beyond the core operators, please visit rubico/x. You can find the full documentation at rubico.land/docs.

Benchmarks

Please find the published benchmark output inside the benchmark-output folder. You can run the benchmarks on your own system with the following command:

npm run bench

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, please create a pull request.

Pull requests should provide some basic context and link the relevant issue. Here is an example pull request. If you are interested in contributing, the help wanted tag is a good place to start.

For more information please see CONTRIBUTING.md

License

rubico is MIT Licensed.

Support

  • minimum Node.js version: 16
  • minimum Chrome version: 63
  • minimum Firefox version: 57
  • minimum Edge version: 79
  • minimum Safari version: 11.1

Awesome Resources

rubico simplifies asynchronous code

Blog

Check out the rubico blog at https://rubico.land/blog

更新日志

v2.3.1

  • generate d.ts for x - #269 - kalagin
  • add d.ts command before ./build - #269 - kalagin
  • convert monad/Instance.js to class to fix private name conflict with ts - #269 - kalagin

v2.3.0

  • Add d.ts files for all core functions and _internal - #241 - kalagin
  • Consolidate chat to this issue: #29

v2.2.3

  • more docs amendments for v2

v2.2.2

  • prepare core docs for v2

v2.2.1

  • add monad to dist
  • include commit step in contributing

v2.2.0

  • improve transform string performance - #260
  • eager API for tap - #258
  • smaller package size with files in package.json - #259

v2.1.0

  • Transducer.tryCatch

v2.0.0

  • Refactor all transducer functionality into Transducer.js from pipe, map, filter, and flatMap
  • Remove GeneratorFunction and AsyncGeneratorFunction functionality in map, filter, flatMap, and reduce
  • Refactor reducer chaining functionality into AggregateReducer.js from reduce
  • Remove Duplex stream handling from flatMap
  • Remove binary handling from flatMap
  • Promises as arguments for all core functions
  • Eager API for all core functions
  • Automate benchmarks
  • Remove the .sync property functions
  • Remove the .withIndex property functions
  • Deprecate map.own
  • fork renamed to all
  • all renamed to every
  • any renamed to some
  • new dist-test only validates syntax with require
  • migrate forEach into core API.
  • refactor transducer functionality from forEach into Transducer.forEach
  • async performance improvements
  • _internal/arrayExtend stops treating strings like arrays so transform creates arrays of strings as expected

v1.9.3

  • property value passed to set may be a resolver - #224
  • rubico/x/isIn - counterpart to includes - #228 - lulldev
  • rubico/x/maxBy - finds the item that is the max by a property denoted by path - #229
  • eager map - #225
  • eager filter - #226
  • eager reduce - #227

v1.9.2

  • eager tryCatch - #222

v1.9.1

  • more performant uniq - #218 - jasir
  • and and or handle nonfunction values - #221

v1.9.0

  • switchCase handles non-function values - #220

v1.8.15

v1.8.12

v1.8.11

  • eq, gt, gte, lt, lte should return a boolean when both arguments are values (not functions) - #203 - lulldev
  • not behaves like ! when passed a value (not function) - #204

v1.8.10

  • Mux.race - yield value after adding promise versus yielding before
  • isDeepEqual takes functions as arguments, returning a partially applied function
  • rubico/x/filterOut - the inverse of filter

v1.8.9

  • FlatMappingAsyncIterator account for indefinite buffer loading promises

v1.8.8

  • FlatMappingAsyncIterator don't await if async iterator is done already

v1.8.7

  • FlatMappingAsyncIterator use Promise.race over promiseAnyRejectOnce

v1.8.6

  • fix bug where FlatMappingAsyncIterator wasn't fully consuming the input async iterator

v1.8.5

  • fix bug where map and filter weren't recognizing async iterator and iterator objects

v1.8.3

  • distribute rest of the functions with .mjs extension as ESM

v1.8.2

  • distribute rubico.mjs and rubico.min.mjs as ESM

v1.8.1

  • map supplies key and reference to original value for sets
  • filter supplies key and reference to original value for sets

v1.8.0

  • map supplies index/key and reference to original value for arrays, objects, and maps
  • filter supplies index/key and reference to original value for arrays, objects, and maps
  • reduce supplies index/key and reference to original value for arrays, objects, and maps
  • reduce object performance improvements

v1.7.3

v1.7.2

v1.7.1

v1.7.0

v1.6.30

v1.6.29

  • fix syntax issue with map filter iterators and async iterators

v1.6.28

  • stop supporting node 10

v1.6.22

  • x/callProp

v1.6.17

  • x/has
  • x/includes

v1.6.16

  • fix omit throwing on nullish

v1.6.15

  • omit supports nested fields #181

v1.6.13

  • map.entries
  • License (MIT) and Chat (Gitter) readme badges
  • fix x/trace altering output
  • patch transform - strings and binary correspond 1 character = 1 byte
  • patch internal/binaryExtend - construct Buffers as per Node.js warning

v1.6.11

  • fix: tap.if handles promises in its executor

v1.6.10

  • and defers to .and if method is present
  • or defers to .or if method is present
  • not defers to .not if method is present
  • eq defers to .eq if method is present
  • gt defers to .gt if method is present
  • lt defers to .lt if method is present
  • gte defers to .gte if method is present
  • lte defers to .lte if method is present
  • eq compares by SameValueZero
  • isDeepEqual compares by SameValueZero
  • isObject checks for language type Object

v1.6.9 - latest

  • x/groupBy
  • x/values

v1.6.8

  • fix: assign creates a new object on Promise

v1.6.6

  • new distribution setup
  • x/differenceWith

v1.6.0

  • curry

  • thunkify
  • always
  • project structure reorganization around _internal/
  • new generated documentation format
  • x/size rewrite + refresh benchmarks
  • x/find works on all foldables
  • x/forEach rewrite + benchmarks
  • x/defaultsDeep rewrite + refresh benchmarks
  • x/flatten rewrite + refresh benchmarks
  • x/isDeepEqual rewrite + refresh benchmarks
  • x/unionWith rewrite + refresh benchmarks
  • better pipe performance
  • fix: x/isDeepEqual was throwing for nullish
  • map.own - LMulvey

v1.5.19 - latest

  • pipe.sync

v1.5.18

  • fix: Set and Map mapping

v1.5.17

  • fix: string mapping

v1.5.16

  • tap.if

v1.5.15

  • get rewrite + benchmarks
  • pick rewrite + benchmarks
  • omit rewrite + benchmarks
  • not rewrite + benchmarks
  • any rewrite + benchmarks
  • all rewrite + benchmarks
  • and rewrite + benchmarks
  • or rewrite + benchmarks
  • eq rewrite + benchmarks
  • gt rewrite + benchmarks
  • lt rewrite + benchmarks
  • gte rewrite + benchmarks
  • lte rewrite + benchmarks

v1.5.14

  • fix: flatMap async muxing

v1.5.13

  • flatMap rewrite + benchmarks
  • improved tryCatch performance
  • improved switchCase performance

v1.5.12

  • transform rewrite + benchmarks

v1.5.11

  • x/heapUsedInLoop - max and avg heap used in loop
  • monad/Cancellable - make a function return cancellable Promises
  • deprecated x/tracef
  • x/trace(reducer) - lazy trace, good for adding custom formatting
  • reduce rewrite + benchmarks
  • tap accepts multiple arguments + more benchmarks

v1.5.10

  • filter.withIndex rewrite + benchmarks
  • monad/BrokenPromise - a Promise that never comes back
  • isEmpty stops throwing TypeErrors

v1.5.9

  • filter rewrite + benchmarks
  • map.series rewrite + benchmarks
  • map.pool rewrite + benchmarks
  • map.withIndex rewrite + benchmarks

v1.5.8

  • fix a failing test; node10 AsyncIterator constructors were Objects

v1.5.7

  • map rewrite + benchmarks

v1.5.6

  • fork rewrite + benchmarks
  • fork.series rewrite + benchmarks
  • assign rewrite + benchmarks
  • tap rewrite + benchmarks
  • tryCatch rewrite + benchmarks
  • switchCase rewrite + benchmarks
  • switchCase supports even numbers of functions

v1.5.5

  • pipe rewrite + benchmarks

v1.5.3

  • x/uniq - export in x/index

v1.5.2

  • x/unionWith performance revamp + benchmarks
  • x/uniq - Get unique values of a collection - FredericHeem

v1.5.0

  • monad/PossiblePromise - Possibly a promise
  • monad/Instance - Type checking
  • monad/Struct - Finite data structure
  • monad/Mux - Multiplexing for Sequences of Sequences
  • x/size - Get the size of a collection
  • x/isFunction - Tell if Function
  • x/timeInLoop.async - Like timeInLoop, but await all calls
  • x/find performance revamp + benchmarks
  • x/defaultsDeep performance revamp + benchmarks
  • x/flatten performance revamp + benchmarks
  • x/isDeepEqual performance revamp + benchmarks
  • x/isEmpty performance revamp + benchmarks
  • x/isEqual performance revamp + benchmarks
  • x/isObject performance revamp + benchmarks
  • x/isString performance revamp + benchmarks
  • x/first performance revamp + benchmarks
  • x/last performance revamp + benchmarks
  • improve PossiblePromise.args stack trace
  • fine tune PossiblePromise performance
  • fine tune core performance

v1.3.2

  • x/defaultsDeep - enforce Array or Object inputs

v1.3.0

  • large rubico core refactor (~75% done) to PossiblePromise
  • large rubico core documentation initiative (~75% done)
  • rubico/x/index.js for comfier times with rubico/x
  • benchmark groundwork (and, or, and switchCase)
  • x/timeInLoop - measures time a function takes in a loop
  • flatMap includes unflattenable elements instead of throwing

v1.2.5

  • tap no longer transduces; use map(tap) or x/forEach instead if you want to use tap in a transducer

v1.2.2

  • x/isEmpty - check if a collection is empty
  • x/first - get first item from a collection
  • x/last - get last item from a collection

v1.2.0

  • flatMap; map + flatten
  • transform(f, init), reduce(f, init); init can be a function
  • x/defaultsDeep - deeply assign defaults
  • x/isDeepEqual - left deeply equals right? eager version of eq.deep
  • x/find - get the first item in a collection that passes the test
  • x/forEach - execute a function for each item of a collection, returning input
  • x/is - directly checks the constructor
  • x/isEqual - left strictly equals right? eager version of eq
  • x/isObject - is object?
  • x/isString - is string?
  • x/pluck - create a new collection by getting a path from every item of an old collection
  • x/unionWith - create a flattened unique array with uniques given by a binary predicate