├── test.js ├── .babelrc ├── .coveralls.yml ├── .travis.yml ├── .editorconfig ├── .gitignore ├── module └── index.js ├── .jshintrc ├── test └── test.js ├── LICENSE.md ├── package.json └── README.md /test.js: -------------------------------------------------------------------------------- 1 | import './test/test.js'; 2 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "auxiliaryCommentBefore": "istanbul ignore next", 3 | "stage": 0 4 | } 5 | -------------------------------------------------------------------------------- /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-pro 2 | repo_token: YBEWol9FuAggTwnAXR4jXe7kydslWSJjE 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | before_install: 2 | git config user.name "Travis CI" 3 | 4 | language: 5 | node_js 6 | node_js: 7 | - stable 8 | - iojs 9 | 10 | after_script: 11 | npm run coveralls 12 | 13 | sudo: false 14 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | charset = utf-8 7 | 8 | # Unix-style newlines with a newline ending every file 9 | [*] 10 | end_of_line = lf 11 | insert_final_newline = true 12 | 13 | indent_style = space 14 | indent_size = 2 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules 16 | coverage 17 | 18 | *.js 19 | !module.js 20 | !test.js 21 | !module/*.js 22 | !module/*/*.js 23 | !module/*/*/*.js 24 | !test/*.js 25 | !test/*/*.js 26 | !test/*/*/*.js 27 | .es5/index.js 28 | .es5/test.js 29 | .es5/test/test.js 30 | -------------------------------------------------------------------------------- /module/index.js: -------------------------------------------------------------------------------- 1 | import arityN from 'arity-n'; 2 | 3 | 4 | let compose2 = (f, g) => (...args) => f(g(...args)); 5 | 6 | export default function compose(...functions) { 7 | 8 | const funcs = functions.filter(fn => typeof fn === 'function'); 9 | 10 | let lastIdx = funcs.length - 1; 11 | let arity = 0; 12 | 13 | if (funcs.length <= 0) { 14 | throw new Error('No funcs passed'); 15 | } 16 | 17 | 18 | if (lastIdx >= 0 && funcs[lastIdx]) { 19 | arity = funcs[lastIdx].length; 20 | } 21 | 22 | return arityN(funcs.reduce(compose2), arity); 23 | } 24 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "curly": true, 6 | "eqeqeq": true, 7 | "eqnull": true, 8 | "immed": true, 9 | "latedef": "nofunc", 10 | "newcap": false, 11 | "noarg": true, 12 | "undef": true, 13 | "strict": false, 14 | "trailing": true, 15 | "smarttabs": true, 16 | "indent": 2, 17 | "white": false, 18 | "quotmark": "single", 19 | "laxbreak": true, 20 | "globals" : { 21 | /* MOCHA */ 22 | "describe" : false, 23 | "it" : false, 24 | "before" : false, 25 | "beforeEach" : false, 26 | "after" : false, 27 | "afterEach" : false 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import compose from '../module'; 2 | import { curry, _ } from 'curry-this'; 3 | 4 | import test from 'tape-catch'; 5 | 6 | test('#compose', ( { deepEqual: deep, equal: eq, throws, end } ) => { 7 | const sqr = x => x * x; 8 | const inc = x => x + 1; 9 | 10 | eq(typeof compose, 'function'); 11 | 12 | throws(compose.bind(1)); 13 | 14 | eq(compose(sqr, inc)(2), sqr(inc(2))); 15 | eq(sqr(inc(2)), 9); 16 | eq(compose(sqr, inc)(2), 9); 17 | 18 | const add = (x, y) => x + y; 19 | 20 | eq(compose( 21 | add::curry(6), 22 | sqr, 23 | add::curry(2) 24 | )(2), 25 | add(6, sqr(add(2, 2))) 26 | ); 27 | 28 | const even = x => x%2 === 0; 29 | const map = (arr, fn) => arr.map(fn); 30 | const filter = (arr, fn) => arr.filter(fn); 31 | 32 | deep(compose( 33 | map::curry(_, sqr), 34 | filter::curry(_, even), 35 | )([1,2,3,4,5,6,7,8]), [4, 16, 36, 64]); 36 | 37 | end(); 38 | }); 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright © 2015 Christoph Hermann 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "compose-function", 3 | "version": "3.0.3", 4 | "description": "Compose new functions f(g(x))", 5 | "main": "index.js", 6 | "scripts": { 7 | "clean": "git reset && echo '/node_modules/' > .gitignore && git add .gitignore && git stash save --include-untracked --keep-index '`npm run clean` trash can' && git clean --force -d && git reset --hard && echo '\nclean: Uncommitted and ignored files have been moved to gitâ™s stash. To restore them run `git stash pop --quiet; git checkout .gitignore`.'", 8 | "coverage": "rm -rf coverage && npm run test:transpile && cd .es5 && istanbul cover test.js && mv coverage ..", 9 | "coveralls": "npm run coverage && cat ./coverage/lcov.info | coveralls", 10 | "develop": "nodangel --ignore node_modules --ignore coverage --exec 'npm run --silent test:lite'", 11 | "prepublish": "npm run --silent clean && npm run transpile", 12 | "patch-release": "npm version patch && npm publish && git push --follow-tags", 13 | "minor-release": "npm version minor && npm publish && git push --follow-tags", 14 | "major-release": "npm version major && npm publish && git push --follow-tags", 15 | "test": "eslint --ignore-path .gitignore .; npm run test:transpile && node .es5/test.js | tap-spec", 16 | "test:lite": "babel-node --optional es7.functionBind test.js | tap-spec", 17 | "test:transpile": "rm -rf .es5 && babel --optional es7.functionBind test.js test/*.js --out-dir .es5 && babel module/*.js --out-dir .es5", 18 | "transpile": "babel module --out-dir .", 19 | "view-coverage": "echo 'Generating coverage reports…'; npm run coverage >/dev/null && echo '…done.' && opn ./coverage/lcov-report/index.html >/dev/null" 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "http://github.com/stoeffel/compose-function" 24 | }, 25 | "files": [ 26 | "/*.js", 27 | "/module/", 28 | "/README.md", 29 | "/LICENSE.md" 30 | ], 31 | "keywords": [ 32 | "function", 33 | "compose", 34 | "functional" 35 | ], 36 | "author": "stoeffel", 37 | "license": "MIT", 38 | "bugs": { 39 | "url": "http://github.com/stoeffel/compose-function/issues" 40 | }, 41 | "homepage": "http://github.com/stoeffel/compose-function", 42 | "devDependencies": { 43 | "babel": "^5.8.21", 44 | "babel-eslint": "^4.0.10", 45 | "coveralls": "^2.11.4", 46 | "curry-this": "^3.0.2", 47 | "es6-symbol": "^2.0.1", 48 | "eslint": "^1.2.0", 49 | "istanbul": "^0.3.18", 50 | "nodangel": "1.3.8", 51 | "opn-cli": "1.0.0", 52 | "tap-spec": "^4.0.2", 53 | "tape-catch": "1.0.4" 54 | }, 55 | "dependencies": { 56 | "arity-n": "^1.0.4" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Travis](https://img.shields.io/travis/stoeffel/compose-function.svg?style=flat-square)](https://travis-ci.org/stoeffel/compose-function) 2 | [![npm](https://img.shields.io/npm/v/compose-function.svg?style=flat-square)](https://www.npmjs.com/package/compose-function) 3 | [![Dependency Status](https://david-dm.org/stoeffel/compose-function.svg?style=flat-square)](https://david-dm.org/stoeffel/compose-function) 4 | [![Coveralls](https://img.shields.io/coveralls/stoeffel/compose-function.svg?style=flat-square)](https://coveralls.io/github/stoeffel/compose-function) 5 | 6 | 7 |

Compose-Function

8 | 9 |

10 | Installation | 11 | Usage | 12 | Related | 13 | License 14 |

15 | 16 |
17 | logo by Justin Mezzell 18 |

Compose a new function from smaller functions `f(g(x))`
19 |

20 | 21 | Installation 22 | ------------ 23 | 24 | `npm install compose-function --save` 25 | 26 | Usage 27 | ----- 28 | 29 | ## Basic usage 30 | 31 | ```js 32 | import compose from 'compose-function'; 33 | 34 | const composition = compose(sqr, add2); // sqr(add2(x)) 35 | 36 | composition(2) // => 16 37 | 38 | compose(sqr, inc)(2); // => 9 39 | compose(inc, sqr)(2); // => 5 40 | ``` 41 | 42 | 43 | 44 | ## with curry 45 | 46 | ```js 47 | import compose from 'compose-function'; 48 | import { curry, _ } from 'curry-this'; 49 | 50 | 51 | const add = (x, y) => x + y; 52 | 53 | // add(6, sqr(add(2, x))) 54 | compose( 55 | add::curry(6), 56 | sqr, 57 | add::curry(2), 58 | ); 59 | 60 | // map(filter(list, even), sqr) 61 | compose( 62 | map::curry(_, sqr), 63 | filter::curry(_, even), 64 | )([1,2,3,4,5,6,7,8]) // => [4, 16, 36, 64] 65 | ``` 66 | 67 | ### `::` huh? 68 | 69 | If you’re wondering what the `::` thing means, you’d better read this excellent [overview](https://github.com/jussi-kalliokoski/trine/blob/5b735cbfb6b28ae94bac0446d9ecd5ce51fb149b/README.md#why) by [@jussi-kalliokoski](https://github.com/jussi-kalliokoski) or have a look at the [function bind syntax proposal](https://github.com/zenparsing/es-function-bind). 70 | Or checkout the [curry-this docs][ct]. 71 | 72 | 73 | Related 74 | ---- 75 | 76 | * [curry-this][ct] 77 | 78 | License 79 | ---- 80 | 81 | MIT © [Christoph Hermann](http://stoeffel.github.io) 82 | 83 | [r]: http://ramdajs.com 84 | [ct]: https://github.com/stoeffel/curry-this 85 | --------------------------------------------------------------------------------