Skip to content

Commit 1fbd1f6

Browse files
author
David Chase
committed
export compose, change export api
1 parent 8fc0fa4 commit 1fbd1f6

File tree

6 files changed

+39
-12
lines changed

6 files changed

+39
-12
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,25 @@ Given two paths create a new union path.
1616

1717

1818
```js
19-
const union = require('path-union')
19+
import {union} from 'path-union'
2020

2121
const src = '/Users/davidchase/Desktop/github/path-union/fixtures/packages'
2222
const dest = '/Users/davidchase/Desktop/github/path-union/dist'
2323

24-
union(dest, src) // => /Users/davidchase/Desktop/github/path-union/dist/fixtures/packages
24+
union(dest, src) // => '/Users/davidchase/Desktop/github/path-union/dist/fixtures/packages'
25+
```
26+
27+
also as a bonus you can do functional composition right to left 🔥
28+
29+
```js
30+
import {union, compose} from 'path-union'
31+
import {dirname} from 'path'
32+
33+
const src = '/Users/davidchase/Desktop/github/path-union/fixtures/packages'
34+
const dest = '/Users/davidchase/Desktop/github/path-union/dist'
35+
36+
37+
compose(dirname, union)(dest, src) // => '/Users/davidchase/Desktop/github/path-union/dist/fixtures'
2538
```
2639

2740
## API

dist/path-union.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
Object.defineProperty(exports, '__esModule', { value: true });
4+
35
var _compose = function (f, g) { return function () {
46
var args = [], len = arguments.length;
57
while ( len-- ) args[ len ] = arguments[ len ];
@@ -11,9 +13,14 @@ var uniq = function (xs) { return xs.reduce(function (list, x) { return list.ind
1113

1214
var concat = function (xs, list) { return xs.concat(list); }
1315

14-
var compose = function (fns) { return fns.reduce(_compose); }
16+
var compose = function () {
17+
var fns = [], len = arguments.length;
18+
while ( len-- ) fns[ len ] = arguments[ len ];
19+
20+
return fns.reduce(_compose);
21+
}
1522

16-
var union = compose([uniq, concat])
23+
var union = compose(uniq, concat)
1724

1825
var join = function (separator) { return function (xs) { return xs.join(separator); }; }
1926

@@ -27,6 +34,7 @@ var filter = function (pred) { return function (xs) { return xs.filter(pred); };
2734

2835
var prepend = function (a) { return function (b) { return a.concat(b); }; }
2936

30-
var index = compose([prepend('/'), join('/'), filter(Boolean), union, chain(split('/')), of])
37+
var pathUnion = compose(prepend('/'), join('/'), filter(Boolean), union, chain(split('/')), of)
3138

32-
module.exports = index;
39+
exports.compose = compose;
40+
exports.union = pathUnion;

dist/path-union.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import {compose, prepend, join, filter, union, chain, split, of} from './prelude'
22

3-
export default compose([prepend('/'), join('/'), filter(Boolean), union, chain(split('/')), of])
3+
const pathUnion = compose(prepend('/'), join('/'), filter(Boolean), union, chain(split('/')), of)
4+
5+
export {compose, pathUnion as union}

prelude.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ const uniq = xs => xs.reduce((list, x) => list.indexOf(x) === -1 ? list.concat(x
66

77
const concat = (xs, list) => xs.concat(list)
88

9-
export const compose = fns => fns.reduce(_compose)
9+
export const compose = (...fns) => fns.reduce(_compose)
1010

11-
export const union = compose([uniq, concat])
11+
export const union = compose(uniq, concat)
1212

1313
export const join = separator => xs => xs.join(separator)
1414

test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import test from 'tape'
44
import path from 'path'
5-
import union from './index'
5+
import {union, compose} from './index'
66

7-
test('it works', function(t){
7+
test('union', function(t){
88
const expected = path.join(__dirname, 'output/src/images')
99
const expected2 = path.join(__dirname, 'output/images/src/stuff')
1010

@@ -13,3 +13,8 @@ test('it works', function(t){
1313
t.end()
1414
})
1515

16+
test('compose', function(t){
17+
t.equals(compose(x => x, a => a + 1, y => y + 10)(10), 21)
18+
t.end()
19+
})
20+

0 commit comments

Comments
 (0)