{"name":"@arrows/composition","version":"0.2.4","main":"index.js","license":"ISC","scripts":{"build":"tsc","test":"jest","coverage":"jest --coverage && codecov -F composition"},"devDependencies":{"@types/jest":"^24.0.18","codecov":"^3.6.1","jest":"^24.9.0","ts-jest":"^24.1.0","typescript":"^3.6.3"},"dependencies":{"@arrows/array":"^0.4.9"},"description":"![CircleCI](https://img.shields.io/circleci/build/github/caderek/arrows) ![David (path)](https://img.shields.io/david/caderek/arrows?path=packages%2Fcomposition) ![Codecov](https://img.shields.io/codecov/c/github/caderek/arrows?token=c6adb715d638431786fef","_id":"@arrows/composition@0.2.4","_nodeVersion":"10.16.0","_npmVersion":"6.10.1","dist":{"integrity":"sha512-ObEDbuR0c8UF/92qMawhAREU+AG83GTSCEerQHVKF+HJ/jd9PlqanHymAwuTVebMfcstI6EwTjnmfYk0s5XdXg==","shasum":"329aeae54454cc52184fadaf9802b1073dda3c7a","tarball":"http://registry-npm.ywwl.com/@arrows/composition/download/@arrows/composition-0.2.4.tgz","fileCount":36,"unpackedSize":14091,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdhmXkCRA9TVsSAnZWagAAxqEQAJBMLyByWAF38x5ZJX7d\nXET1U9WY7eexDbCWH6eM3XRHTo9C+Qt1Jqxc1xQpzfIHEBs9Ii1plXwFJ5Nb\n3AMhZza3/dZC0nEzCGGlyVD4cvrP9rU/TVrgvdtuwGzFHgVwHrhqHCkl9peW\ngQ+Obeh/i1Rp54xhIBDWZnXCbGlY5v/yuMIS8O6xX8CtDKIuZ9P1qefz5Qbk\nwK29AkeIi0WaxBRCxYfQHrD3943mxEtbtV+WHUj7T7BqkeNnMQ+/8eswD0BX\nBvEtGNbIuVKwBd2DiPOc19YL/Kn7aPlz8Zd7XTGr0Y8+RPXa/+TcoAmQDVVu\nb42ZB46rWJEhZdq7ZEHHLNV3qqMZthuWpI254f3Mg7cgc/qySiGLL2Wbktwl\npX4QUAnQKc7AFHvAOBuitsj3qIPKoK8lpP7eWB/OBc16A2sqdM7HJ0sIiHQB\n+suFgaYHBnhhm53zHFkZG+w01BLHm2NAcb8/43M4MRXCxXvKrQsSMnc3L43c\n9vFeS/5xoGCk0UDZZCi/S8ry3V37QP8pJJXZtoPQm9bf39CrrcSMrx4fXI1c\ntduqF84D2oloXcwwpgq2qX6YCwWNr05/nmmPdmLXCmsN/yPIbBg+w721pLFF\njkj+y1mmWhdxBr1yvXa0rWQTUz5SHfr1swYXorb3z6m7cNMxzuKxgFdlkia9\nStw/\r\n=NgJq\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBI8qx3l1pBufP/qjzIkc+YYGQrRGPojxSiAuES8xIX6AiEAxZvps5Q82ypiTJim1KY7Sku65OBhm4vX6iJrc4NiPzM="}],"size":3893,"noattachment":false,"key":"/@arrows/composition/-/@arrows/composition-0.2.4.tgz"},"maintainers":[{"name":"caderek","email":"maciej.caderek@gmail.com"}],"_npmUser":{"name":"caderek","email":"maciej.caderek@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/composition_0.2.4_1569088995858_0.6829619652976331"},"_hasShrinkwrap":false,"readme":"![arrows - composition](https://raw.githubusercontent.com/caderek/arrows/master/assets/arrows-composition.svg?sanitize=true)\n\n# @arrows/composition\n\n![npm (scoped)](https://img.shields.io/npm/v/@arrows/composition)\n![CircleCI](https://img.shields.io/circleci/build/github/caderek/arrows)\n![David (path)](https://img.shields.io/david/caderek/arrows?path=packages%2Fcomposition)\n![Codecov](https://img.shields.io/codecov/c/github/caderek/arrows?token=c6adb715d638431786fefe69ca08ab00)\n![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@arrows/composition)\n![GitHub](https://img.shields.io/github/license/caderek/arrows)\n\n## Table of contents\n\n1. [Introduction](#introduction)\n2. [Installation](#installation)\n3. [API reference](#api-reference)\n   - [chain](#chain)\n   - [chainRight](#chainRight)\n   - [compose](#compose)\n   - [curry](#curry)\n   - [pipe](#pipe)\n   - [pipe.now](#pipenow)\n   - [rail](#rail)\n   - [rail.async](#rail.async)\n   - [railRight](#railRight)\n   - [railRight.async](#railRight.async)\n   - [tap](#tap)\n4. [License](#license)\n\n## Introduction\n\nThe library contains a collection of higher-order functions for composing your applications with reusable functions.\n\nAll chain-like functions are manually chunked (segmented). It is a deliberate decision (to not use automatic currying) to allow a function to accept a variadic number of arguments in a segment, which leads in my opinion to a nicer to use API (and is faster as a bonus).\n\nThe library has **built-in type definitions**, which provide an excellent IDE support.\n\n## Installation\n\nVia NPM:\n\n```sh\nnpm i @arrows/composition\n```\n\nVia Yarn:\n\n```sh\nyarn add @arrows/composition\n```\n\nAll modules can be imported independently (to reduce bundle size), here are some import methods (you can use either CommonJS or ES modules):\n\n```js\nimport composition from '@arrows/composition'\n```\n\n```js\nimport { pipe } from '@arrows/composition'\n```\n\n```js\nimport pipe from '@arrows/composition/pipe'\n```\n\n## API reference\n\n### chain\n\nAllows you to build your own, chain-like functions\n\nExecutes provided functions from left to right, passing the result from one function to the other. Accepts a wrapping function which can execute additional instructions before executing each passed function.\n\n#### Parameters\n\nArguments listed below have to be passed separately, as segments.\n\n- `wrappingFn` - two-argument function thats is responsible for executing each function in a chain.\n\n- `...fns` - an arbitrary number of one-argument functions to be executed in a chain\n\n- `initialArg` - initial argument, passed to the first function in a chain (through a wrapping function)\n\nArguments of a wrapping function:\n\n- `fn` - function in a chain\n\n- `input` - current value\n\n- `isLast` - boolean flag indicating if a function is a last one in a chain\n\n#### Returns\n\n- Returns a final value.\n\n#### Interface\n\n```\n(wrapping_function) => (fn, fn?, ..., fn?) => (initial_value) => result\n```\n\n#### Examples\n\n```javascript\nconst { chain } = require('@arrows/composition')\n\n// Create custom wrapping function:\nconst wrappingFn = (fn, input) => {\n  console.log(input)\n  return fn(input)\n}\n\n// Create reusable chain-like function:\nconst pipeWithLog = chain(wrappingFn)\n\n// Create concrete function\nconst calculate = pipeWithLog((x) => x + 1, (x) => x * 2)\n\nconsole.log(calculate(0))\n\n// -> 0\n// -> 1\n// -> 2 (final result)\n```\n\n---\n\n### chainRight\n\nWorks like [chain](#chain), but executes functions from right to left.\n\n#### Parameters\n\nArguments listed below have to be passed separately, as segments.\n\n- `wrappingFn` - two-argument function thats is responsible for executing each function in a chain.\n\n- `...fns` - an arbitrary number of one-argument functions to be executed in a chain\n\n- `initialArg` - initial argument, passed to the first function in a chain (through a wrapping function)\n\nArguments of a wrapping function:\n\n- `fn` - function in a chain\n\n- `input` - current value\n\n- `isLast` - boolean flag indicating if a function is a last one in a chain\n\n#### Returns\n\n- Returns a final value.\n\n#### Interface\n\n```\n(wrapping_function) => (fn, fn?, ..., fn?) => (initial_value) => result\n```\n\n#### Example\n\n```javascript\nconst { chainRight } = require('@arrows/composition')\n\n// Create custom wrapping function:\nconst wrappingFn = (fn, input) => {\n  console.log(input)\n  return fn(input)\n}\n\n// Create reusable chain-like function:\nconst composeWithLog = chainRight(wrappingFn)\n\n// Create concrete function\nconst calculateRight = composeWithLog((x) => x + 1, (x) => x * 2)\n\nconsole.log(calculateRight(0))\n\n// -> 0\n// -> 0\n// -> 1 (final result)\n```\n\n---\n\n### compose\n\nChains provided functions from right to left.\n\n#### Parameters\n\nArguments listed below have to be passed separately, as segments.\n\n- `...fns` - an arbitrary number of one-argument functions to be executed in a chain\n\n- `initialArg` - initial argument, passed to the first function in a chain\n\n#### Returns\n\n- Returns a final value.\n\n#### Interface\n\n```\n(fn, fn?, ..., fn?) => (initial_value) => result\n```\n\n#### Example\n\n```javascript\nconst { compose } = require('@arrows/composition')\n\nconst addPrefixes = compose(\n  (text) => `prefix1-${text}`,\n  (text) => `prefix2-${text}`,\n)\n\naddPrefixes('arrows') // -> \"prefix1-prefix2-arrows\"\n```\n\n---\n\n### curry\n\nWraps function as an automatically curried one.\n\n#### Parameters\n\n- `fn` - an arbitrary function\n\n#### Returns\n\n- Returns a curried version of the function.\n\n#### Interface\n\n```\n(fn) =>  curried_fn\n```\n\n#### Example\n\n```javascript\nconst { curry } = require('@arrows/composition')\n\nconst rawAdd = (a, b) => a + b\n\nconst add = curry(rawAdd)\n\nadd(1, 2) // -> 3\nadd(1)(2) // -> 3\n```\n\n---\n\n### pipe\n\nChains provided functions from left to right.\n\n#### Parameters\n\nArguments listed below have to be passed separately, as segments.\n\n- `...fns` - an arbitrary number of one-argument functions to be executed in a chain\n\n- `initialArg` - initial argument, passed to the first function in a chain\n\n#### Returns\n\n- Returns a final value.\n\n#### Interface\n\n```\n(fn, fn?, ..., fn?) => (initial_value) => result\n```\n\n#### Example\n\n```javascript\nconst { pipe } = require('@arrows/composition')\n\nconst addSuffixes = pipe(\n  (text) => `${text}-suffix1`,\n  (text) => `${text}-suffix2`,\n)\n\naddSuffixes('arrows') // -> \"arrows-suffix1-suffix2\"\n```\n\n---\n\n### pipe.now\n\nChains provided functions from left to right, takes an initial value as a first argument.\n\nProvides an alternative API with better type inference for immediately evaluated calculations.\n\n#### Parameters\n\n- `initialArg` - initial argument, passed to the first function in a chain\n- `...fns` - an arbitrary number of one-argument functions to be executed in a chain\n\n#### Returns\n\n- Returns a final value.\n\n#### Interface\n\n```\n(initial_value, fn, fn?, ..., fn?) => result\n```\n\n#### Example\n\n```javascript\nconst { pipe } = require('@arrows/composition')\n\nconst result = pipe.now(\n  'arrows',\n  (text) => `${text}-suffix1`,\n  (text) => `${text}-suffix2`,\n) // -> \"arrows-suffix1-suffix2\"\n```\n\n---\n\n### rail\n\nWorks like [pipe](#pipe), but additionally:\n\n- if one of the functions throws or returns an error - returns that error,\n- if one of the functions returns undefined - passes previous argument to the next function.\n\n#### Parameters\n\nArguments listed below have to be passed separately, as segments.\n\n- `...fns` - an arbitrary number of one-argument functions to be executed in a chain\n\n- `initialArg` - initial argument, passed to the first function in a chain\n\n#### Returns\n\n- Returns a final value.\n\n#### Interface\n\n```\n(fn, fn?, ..., fn?) => (initial_value) => result\n```\n\n#### Examples\n\nAutomatically passing down an error:\n\n```javascript\nconst { rail } = require('@arrows/composition')\nconst { filter, map, reduce } = require('@arrows/array')\n\nconst sumDogsAge = rail(\n  filter((pet) => pet.specie === 'dog'),\n  map((pet) => {\n    if (pet.age < 0) {\n      throw new Error('Wrong age!')\n    }\n    return pet.age\n  }),\n  reduce((a, b) => a + b, 0),\n)\n\nconst pets = [\n  { specie: 'dog', name: 'Charlie', age: 4 },\n  { specie: 'cat', name: 'Luna', age: 6 },\n  { specie: 'dog', name: 'Ollie', age: -10 },\n]\n\nconst result = sumDogsAge(pets)\n\nif (result instanceof Error) {\n  console.log('Oops!')\n} else {\n  console.log(`Sum: ${result}`)\n}\n\n// -> \"Oops!\"\n```\n\nAutomatically passing previous argument, if function returns `undefined`:\n\n```js\nconst { rail } = require('../../lib/index')\n\nconst addUser = rail(\n  (user) => ({ type: 'user', data: user }),\n  (entry) => {\n    console.log(`Saving ${entry.data.name} to the database.`)\n    // Returns undefined, argument will be automatically\n    // passed to the next function.\n  },\n  (entry) => `Saved user: ${entry.data.name}`,\n)\n\nconst user = { name: 'Joe' }\n\nconsole.log(addUser(user))\n\n/* Output:\nSaving Joe to the database.\nSaved user: Joe\n*/\n```\n\n---\n\n### rail.async\n\nWorks similar to [rail](#rail), but additionally, if argument is a promise - resolves that promise before passing to the next function.\n\n#### Parameters\n\nArguments listed below have to be passed separately, as segments.\n\n- `...fns` - an arbitrary number of one-argument functions to be executed in a chain\n\n- `initialArg` - initial argument, passed to the first function in a chain\n\n#### Returns\n\n- Returns a final value wrapped in a promise.\n\n#### Interface\n\n```\n(fn, fn?, ..., fn?) => (initial_value) => result\n```\n\n#### Example\n\nAutomatically passing down an error:\n\n```javascript\nconst { railAsync } = require('../../lib/index')\n\nconst dbFake = {\n  id: 0,\n  save() {\n    const result = Promise.resolve(this.id)\n    this.id++\n    return result\n  },\n}\n\nconst addArticle = railAsync(\n  (article) => ({ type: 'article', data: article }),\n  (entry) => dbFake.save(entry),\n  (id) => `Article id: ${id}`,\n)\n\nconst main = async () => {\n  const article = {\n    title: 'Railway oriented programming',\n    content: 'Lorem ipsum...',\n  }\n\n  const result = await addArticle(article)\n  console.log(result)\n}\n\nmain() // -> \"Article id: 0\"\n```\n\n### railRight\n\nWorks like [rail](#rail), but executes functions from right to left.\n\n### railRight.async\n\nWorks like [railAsync](#railAsync), but executes functions from right to left.\n\n### tap\n\nExecutes provided function with provided argument and returns an argument without changes.\n\nUseful for executing void functions without breaking the chain.\n\n_Note: `rail*` functions handle this automatically, so you can use void functions directly._\n\n#### Parameters\n\n- `fn` - an arbitrary function\n\n- `arg` - any value\n\n#### Returns\n\n- Returns argument as-is.\n\n#### Interface\n\n```\n(fn, arg) => arg\n```\n\nExample:\n\n```js\nconst { pipe, tap } = require('../../lib/index')\nconst { sort, reduce, _rest, _butLast } = require('@arrows/array')\n\nconst sumNotes = pipe(\n  sort((a, b) => a - b),\n  tap(console.log),\n  _rest,\n  tap(console.log),\n  _butLast,\n  tap(console.log),\n  reduce((a, b) => a + b, 0),\n)\n\nconst notes = [16, 17.5, 19, 15, 18]\n\nconsole.log(sumNotes(notes))\n\n/* Output:\n[ 15, 16, 17.5, 18, 19 ]\n[ 16, 17.5, 18, 19 ]\n[ 16, 17.5, 18 ]\n51.5\n*/\n```\n\n## License\n\nProject is under open, non-restrictive [ISC license](LICENSE).\n","publish_time":1569088995974,"_cnpm_publish_time":1569088995974,"dist-tags":{"latest":"1.2.2"}}