{"name":"@arrows/composition","version":"0.2.3","main":"index.js","license":"ISC","scripts":{"build":"tsc","test":"jest","coverage":"jest --coverage && codecov -F composition","prepublish":"yarn build"},"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.7"},"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.3","_nodeVersion":"10.16.0","_npmVersion":"6.10.1","dist":{"integrity":"sha512-pQz/XGHb8nDdhfIsUC7yXUcEou1mAHR4MYkntESnhp4s9G8oIzo/6Rul00dgApGF+BRw1p5Pol9HrAVanPtESA==","shasum":"11f3db2d6269cba08895a35914481974437b4be5","tarball":"http://registry-npm.ywwl.com/@arrows/composition/download/@arrows/composition-0.2.3.tgz","fileCount":54,"unpackedSize":18603,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdhmPnCRA9TVsSAnZWagAAEn0QAIHgyDjGShrf5Ey+Bh6v\nz8tL/wtJeAFDtzbt9fFAP7Rgd76slyvREQg1hCaDqrqvBMI4wUCRnbbVwULh\n3BPiUGLllOozhJ2vagqzD5mMKtsgsM0XD/azC8TtPtyxF6ikXmdJCkim08dj\nAxh9Jl+bW9G/JAES2hjGLrjp1QXBjPoDhmdIfEgIhmxek/LRgtx1q+d/cIXL\ntYg2pHkiVHIS/FDmLgIBJzOY1+LbPcq9y6q+FteohnLWEny0r3MDBS7Kx2vU\nwtxh8tPSUohr32tJFLeA009fC8HnCnMDs4nFAiI70DRREFFUSvfMoXf8JumQ\nJkFKrF9FQQc1V+dOybPreNQSQVKX4hUzCyeOclgMyihK1RK13XvIwyWV8dc/\n0PoBsVMSVxpETGZweQcpKf8tH2m51A4F6h5Q/Mffxfxvgkk7eppfvy1CrvWr\nS8AbCSjyphOvVzPh0AiaBhxTrVDb7MsppzBIISoLVx1IoGAs19f5NOZleUYI\nTbOonN/9U7Scbp9yYIJA60NzaobkvSEUuppna3RhHrJjMxXf+4GzMFnWP56y\n0vNAoewZNHjajutUWHFRA/gyOwkfjH3x+3ghKE1bzARBzHah8z+M9ai5lh2v\n8B13UaBlmHFTqnU5CbjjW7ozLLjZ//l6eLDrCpcDqNh8IGsY4UMidf0mqgEv\nCOdq\r\n=7dfb\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDKEJAnoFndKczbSzPvE8xJOPnA6kfLg8AAXthVIuDzkAiAPetdRPVS+ZcffRbpLHYaDzoCSrUd1jiHr1aa3WMw0JA=="}],"size":4338,"noattachment":false,"key":"/@arrows/composition/-/@arrows/composition-0.2.3.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.3_1569088487250_0.8103020912558656"},"_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":1569088487387,"_cnpm_publish_time":1569088487387,"dist-tags":{"latest":"1.2.2"}}