Skip to content

Commit 55cf4f8

Browse files
author
David Chase
committed
add tests expose compose, change exporting api, change compose api
1 parent 1bd9a51 commit 55cf4f8

File tree

7 files changed

+62
-18
lines changed

7 files changed

+62
-18
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,28 @@ npm i --save-dev composable-normalize
1515
## Usage
1616

1717
```js
18-
import normalize from 'composable-normalize'
18+
import {normalize} from 'composable-normalize'
1919

2020
normalize('HTTP://www.Github.com/')
2121
//=> 'http://www.github.com/'
2222

2323
normalize('http://www.github.com/../a/b/../c/./d')
24-
// => 'http://www.github.com/a/c/d'
24+
// => 'http://www.github.com/a/b/c/d/'
2525

2626
normalize('http://www.github.com:80/bar')
27-
// => 'http://www.github.com/bar'
27+
// => 'http://www.github.com/bar/'
2828

2929
normalize('http://www.github.com/%7Eusername/')
30-
// => 'http://www.github.com/~username'
30+
// => 'http://www.github.com/~username/'
31+
```
32+
33+
also as a bonus you can do functional composition right to left
34+
35+
```js
36+
import {normalize, compose} from 'composable-normalize'
37+
38+
compose(str => `${str}bar`, normalize)('HTTP://github.com////foo/')
39+
// =>'http://github.com/foo/bar'
3140
```
3241

3342
## Current Features
@@ -40,5 +49,6 @@ normalize('http://www.github.com/%7Eusername/')
4049
- [x] Adds trailing slash
4150

4251
## Todos
43-
- [ ] Add Tests
52+
- [x] Add Tests
4453
- [x] Publish to npm
54+
- [ ] Add Options?

dist/composable-normalize.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
(function (global, factory) {
2-
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3-
typeof define === 'function' && define.amd ? define(factory) :
4-
(global.composableNormalize = factory());
5-
}(this, (function () { 'use strict';
2+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
4+
(factory((global.composableNormalize = global.composableNormalize || {})));
5+
}(this, (function (exports) { 'use strict';
66

77
// decode :: String -> String
88
var decode = decodeURIComponent
@@ -16,7 +16,12 @@ var _compose = function (f, g) { return function () {
1616
; } }
1717

1818
// compose :: (a -> c) -> [(a -> a)] -> (a -> c)
19-
var compose = function (fns) { return fns.reduce(_compose); }
19+
var compose = function () {
20+
var fns = [], len = arguments.length;
21+
while ( len-- ) fns[ len ] = arguments[ len ];
22+
23+
return fns.reduce(_compose);
24+
}
2025

2126
// replace :: Regex -> String -> String -> String
2227
var replace = function (pattern, substr) { return function (str) { return str.replace(pattern, substr); }; }
@@ -31,15 +36,18 @@ var dedupeSlashes = /([^:]\/)\/+/g
3136
var dedupeDots = /\/\.+/g
3237
var defaultPorts = /(:80|:443)/
3338

34-
var index = compose([
39+
var normalize = compose(
3540
replace(dedupeSlashes, '$1'),
3641
replace(dedupeDots, '/'),
3742
replace(defaultPorts, ''),
3843
toLower,
3944
append('/'),
4045
decode
41-
])
46+
)
47+
48+
exports.normalize = normalize;
49+
exports.compose = compose;
4250

43-
return index;
51+
Object.defineProperty(exports, '__esModule', { value: true });
4452

4553
})));

dist/composable-normalize.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ const dedupeSlashes = /([^:]\/)\/+/g
66
const dedupeDots = /\/\.+/g
77
const defaultPorts = /(:80|:443)/
88

9-
export default compose([
9+
const normalize = compose(
1010
replace(dedupeSlashes, '$1'),
1111
replace(dedupeDots, '/'),
1212
replace(defaultPorts, ''),
1313
toLower,
1414
append('/'),
1515
decode
16-
])
16+
)
17+
18+
export { normalize, compose }

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
"url": "git+https://github.com/davidchase/composable-normalize.git"
2121
},
2222
"scripts": {
23-
"build": "rollup -c && uglifyjs dist/composable-normalize.js -o dist/composable-normalize.min.js"
23+
"build": "rollup -c && uglifyjs dist/composable-normalize.js -o dist/composable-normalize.min.js",
24+
"test": "standard index.js prelude.js && tape --require buba/register test.js | tap-spec"
2425
},
2526
"devDependencies": {
27+
"buba": "2.0.3",
2628
"rollup": "0.34.10",
2729
"rollup-plugin-buble": "0.13.0",
30+
"standard": "8.0.0",
31+
"tap-spec": "4.1.1",
32+
"tape": "4.6.0",
2833
"uglifyjs": "2.4.10"
2934
}
3035
}

prelude.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const decode = decodeURIComponent
77
const _compose = (f, g) => (...args) => f(g(...args))
88

99
// compose :: (a -> c) -> [(a -> a)] -> (a -> c)
10-
const compose = fns => fns.reduce(_compose)
10+
const compose = (...fns) => fns.reduce(_compose)
1111

1212
// replace :: Regex -> String -> String -> String
1313
const replace = (pattern, substr) => str => str.replace(pattern, substr)

test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { normalize, compose } from './index'
2+
import test from 'tape'
3+
4+
test('normalize', function (t) {
5+
t.equals(normalize('HTTP://github.com'), 'http://github.com/')
6+
t.equals(normalize('http://www.github.com/../a/b/../c/./d'), 'http://www.github.com/a/b/c/d/')
7+
t.equals(normalize('http://www.github.com:80/bar'), 'http://www.github.com/bar/')
8+
t.equals(normalize('http://www.github.com/%7Eusername/'), 'http://www.github.com/~username/')
9+
t.equals(normalize('http://github.com////foo/bar'), 'http://github.com/foo/bar/')
10+
t.end()
11+
})
12+
13+
test('compose', function (t) {
14+
const inc = y => y + 1
15+
const add = x => y => x + y
16+
t.equals(compose(inc, add(10))(10), 21)
17+
t.equals(compose(str => `${str}bar`, normalize)('HTTP://github.com////foo/'), 'http://github.com/foo/bar')
18+
t.end()
19+
})

0 commit comments

Comments
 (0)