|
| 1 | +(function (global, factory) { |
| 2 | + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 3 | + typeof define === 'function' && define.amd ? define('composableNormalize', factory) : |
| 4 | + (global.composableNormalize = factory()); |
| 5 | +}(this, function () { 'use strict'; |
| 6 | + |
| 7 | + // decode :: String -> String |
| 8 | + var decode = decodeURIComponent; |
| 9 | + |
| 10 | + // _compose :: (b -> c) -> (a -> b) -> (a -> c) |
| 11 | + var _compose = function (f, g) { return function () { |
| 12 | + var args = [], len = arguments.length; |
| 13 | + while ( len-- ) args[ len ] = arguments[ len ]; |
| 14 | + |
| 15 | + return f(g.apply(void 0, args)); |
| 16 | +; } }; |
| 17 | + |
| 18 | + // compose :: [(d -> e) -> (c -> d) -> (b -> c) -> (a -> b)] -> (a -> e) |
| 19 | + var compose = function (fns) { return fns.reduce(_compose); }; |
| 20 | + |
| 21 | + // replace :: Regex -> String -> String -> String |
| 22 | + var replace = function (pattern, substr) { return function (str) { return str.replace(pattern, substr); }; }; |
| 23 | + |
| 24 | + // toLower :: String -> String |
| 25 | + var toLower = function (str) { return str.toLowerCase(); }; |
| 26 | + |
| 27 | + // append :: String -> String -> String |
| 28 | + var append = function (a) { return function (b) { return b.concat(a); }; }; |
| 29 | + |
| 30 | + var dedupeSlashes = /([^:]\/)\/+/g; |
| 31 | + var dedupeDots = /\/\.+/g; |
| 32 | + var defaultPorts = /(\:80|\:443)/; |
| 33 | + |
| 34 | + var main = compose([ |
| 35 | + replace(dedupeSlashes, '$1'), |
| 36 | + replace(dedupeDots, '/'), |
| 37 | + replace(defaultPorts, ''), |
| 38 | + toLower, |
| 39 | + append('/'), |
| 40 | + decode |
| 41 | + ]); |
| 42 | + |
| 43 | + return main; |
| 44 | + |
| 45 | +})); |
0 commit comments