{"name":"@arrows/composition","version":"0.2.1","main":"lib/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.1"},"description":"![CircleCI](https://img.shields.io/circleci/build/github/caderek/arrows) ![David (path)](https://img.shields.io/david/caderek/arrows?path=packages%2Fcomposition)","_id":"@arrows/composition@0.2.1","_nodeVersion":"10.16.0","_npmVersion":"6.10.1","dist":{"integrity":"sha512-43FsRcesON9d+tLQsvM5Ac5ZIhqjsIaj1/1tXnPbQqf7X7est7vlKA7+pGSdxIZdmO/X0WJWtpHutzRH769xxw==","shasum":"4693333c01c7bf0f6bb507e590547ee7ecf0763d","tarball":"http://registry-npm.ywwl.com/@arrows/composition/download/@arrows/composition-0.2.1.tgz","fileCount":36,"unpackedSize":13436,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdhZYaCRA9TVsSAnZWagAAm3QP+gJeFsH5WRSfJRNgo878\nNmYsS6mkOfL/sNHUMan25f4MT3voEcUCRruw5xNcWS7Qcdj+rrAP+c1SrF7p\n+h8fMeszIBTXH0dw+IOp/RjAqXxu965qKLYVyy9b+HakRehFPdTTfgSZ1/r8\nZpfEX2KWZbwmRoLSR/1ujC4jpEutWhcHr8lou5rdQI94afbitDTr0KcHJDCU\ncKnohcIJHz6Ms1vpTVd1vn/EaHhWOrxM4Y3HGpHuaTRasWz00oTvX2M+SXEA\nxud+Y5ci1nep/jTAuazlgvJ+qRg/bgMJbGcB6ML3yawbAI8zj3vJCrVfMhZP\nLWJICrHF2/fmSwPV4t8p6QWgU9VsTBJO9HUmhG5g4P5bRGEo5ZLis9H5ywxc\n/kVUAjxWtSMbCMqkCXBUN8jJxOJOORhK1gxsc+vI8GSo9nr4ARyj36GhjVi1\njssH3Ha12C9idzaI61Ai0gicJ3ZYo4T1IpfV+aoLxtWHHeqXQYOLR5UwbEEd\noJTb/k+qMdtiRXlKKZ2rr0M9WPV7wrP8mAXwwUoqiyco/Dxp2wSdsyi5sYJ6\nPLOwtrgANQ2xIaUz3QUW7fduxiWkKuB8mhqagEeM8B8NS+NwUSBC7Rp0lm3B\nkEexmbEcxVbfbZyw4MVU4pm5XK17En92KJxZiC4Q8eIxGZLruS7OTPbO1Y8j\nHCqn\r\n=WfbH\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFkXmecxEjUY5zJc3HF9sEsAhGfW0eFGYZvd2Bx9BLN0AiAe36erAU2jtPKm0AmXzD0J+r3heVR3sztYzjINd8/FjQ=="}],"size":3725,"noattachment":false,"key":"/@arrows/composition/-/@arrows/composition-0.2.1.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.1_1569035802236_0.9889241642907454"},"_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":1569035802364,"_cnpm_publish_time":1569035802364,"dist-tags":{"latest":"1.2.2"}}