url-lib
Advanced tools
Comparing version 1.0.3 to 2.0.0
@@ -7,3 +7,3 @@ ## v1.0.3 (April 1, 2016) | ||
## v1.0.1 (May 29, 2016) | ||
## v1.0.1 (March 29, 2016) | ||
- [minor] Use new [`eslint-config-benmvp`](https://github.com/benmvp/eslint-config-benmvp) | ||
@@ -14,4 +14,3 @@ - [fix] Rename `queryParamsAsArray` -> `normalizedQueryParams` in `formatUrl()` | ||
## v1.0.0 (May 27, 2016) | ||
## v1.0.0 (March 27, 2016) | ||
- Initial release |
@@ -7,2 +7,2 @@ /** | ||
*/ | ||
!function(r,n){"function"==typeof define&&define.amd?define(n):"object"==typeof module&&module.exports?module.exports=n():r.urllib=n()}(this,function(){"use strict";function r(r){return null!=r?decodeURIComponent(r):""}function n(r){return encodeURIComponent(r+"")}function e(r){for(var n=new Object(r),e=0;++e<arguments.length;){var t=arguments[e];if(t)for(var o in t)a.call(t,o)&&(n[o]=t[o])}return n}function t(r,n){var e=(r||"")+"",t=e.indexOf("?");return 0>t&&!n&&(t=e.length),{urlPath:e.slice(0,t),queryString:e.slice(t+1)}}function o(r){var t=[],o=r;Array.isArray(o)&&(o=o.length<2?o[0]:e.apply(null,o));for(var i in o)if(i){var u=o[i];null!=u&&t.push(n(i)+"="+n(u))}return t.join("&")}function i(n,e){var o={},i=t(n,e!==!1).queryString;if(i)for(var u=i.split("&"),a=u.length,l=-1;++l<a;){var f=u[l].split("="),c=f[0];c&&(o[r(c)]=r(f[1]))}return o}function u(r,n){var e,u,a,f=r,c=n;return Array.isArray(f)&&(c=f.slice(1).concat(c||l),f=f[0]),e=i(f,!1),u=Array.isArray(c)?c:[c],c=[e].concat(u),a=o(c),t(f).urlPath+(a?"?":"")+a}var a={}.hasOwnProperty,l=[];return{parseQuery:i,formatUrl:u,formatQuery:o}}); | ||
!function(r){"function"==typeof define&&define.amd?define(r):"object"==typeof module&&module.exports?module.exports=r():window.urllib=r()}(function(){"use strict";function r(r){return null!=r?decodeURIComponent(r):""}function n(r){return encodeURIComponent(r+"")}function e(r){for(var n,e,t=new Object(r),u=0;++u<arguments.length;)if(n=arguments[u])for(e in n)l.call(n,e)&&(t[e]=n[e]);return t}function t(r,n){var e=(r||"")+"",t=e.indexOf("?");return 0>t&&!n&&(t=e.length),{urlPath:e.slice(0,t),queryString:e.slice(t+1)}}function u(r){var t=r;return Array.isArray(t)&&(t=t.length<2?t[0]:e.apply(null,t)),t=t||c,Object.keys(t).reduce(function(r,e){var u,o=r;return e&&(u=t[e],null!=u&&o.push(n(e)+"="+n(u))),o},[]).join("&")}function o(n,e){var u=t(n,e!==!1).queryString||"";return u.split("&").reduce(function(n,e){var t=n,u=e.split("="),o=u[0];return o&&(t[r(o)]=r(u[1])),t},{})}function i(r,n){var e,i,c,l=r,f=n;return Array.isArray(l)&&(f=l.slice(1).concat(f||a),l=l[0]),e=o(l,!1),i=Array.isArray(f)?f:[f],f=[e].concat(i),c=u(f),t(l).urlPath+(c?"?":"")+c}var c={},a=[],l=c.hasOwnProperty;return{parseQuery:o,formatUrl:i,formatQuery:u}}); |
@@ -1,7 +0,1 @@ | ||
/** | ||
@preserve Copyright (c) 2016 Ben Ilegbodu. | ||
Licensed under the MIT License (MIT). | ||
See: https://github.com/benmvp/url-lib. | ||
Adapted from the Uize.Url module, a part of the UIZE JavaScript Framework. | ||
*/ | ||
!function(e,r){"function"==typeof define&&define.amd?define(r):"object"==typeof module&&module.exports?module.exports=r():e.urllib=r()}(this,function(){"use strict";function e(e){return null!=e?decodeURIComponent(e):""}function r(e){return encodeURIComponent(e+"")}function n(e){for(var r=new Object(e),n=0;++n<arguments.length;){var t=arguments[n];if(t)for(var o in t)a.call(t,o)&&(r[o]=t[o])}return r}function t(e,r){var n=(e||"")+"",t=n.indexOf("?");return 0>t&&!r&&(t=n.length),{urlPath:n.slice(0,t),queryString:n.slice(t+1)}}function o(e){var t=[],o=e;Array.isArray(o)&&(o=o.length<2?o[0]:n.apply(null,o));for(var u in o)if(u){var i=o[u];null!=i&&t.push(r(u)+"="+r(i))}return t.join("&")}function u(r,n){var o={},u=t(r,n!==!1).queryString;if(u)for(var i=u.split("&"),a=i.length,f=-1;++f<a;){var l=i[f].split("="),c=l[0];c&&(o[e(c)]=e(l[1]))}return o}function i(e,r){var n,i,a,l=e,c=r;return Array.isArray(l)&&(c=l.slice(1).concat(c||f),l=l[0]),n=u(l,!1),i=Array.isArray(c)?c:[c],c=[n].concat(i),a=o(c),t(l).urlPath+(a?"?":"")+a}var a={}.hasOwnProperty,f=[];return{parseQuery:u,formatUrl:i,formatQuery:o}}),function(e,r){"function"==typeof define&&define.amd?define(r):"object"==typeof module&&module.exports?module.exports=r():e.urllib.getCacheDefeatStr=r()}(this,function(){"use strict";function e(){return+new Date+""+Math.round(1e3*Math.random())+r++}var r=0;return e}),function(e,r){"function"==typeof define&&define.amd?define(r):"object"==typeof module&&module.exports?module.exports=r():e.urllib.parseUrl=r()}(this,function(){"use strict";function e(e){function t(e){return o?o[e]||"":""}for(var o=e&&e.match(r),u={},i=-1;++i<n.length;){var a=n[i];a&&(u[n[i]]=t(i))}return u}var r=/^(([^:\\\/]+:)\/\/(([^:\\\/]*)(:(\d+))?)?)?(([^\?#]*[\\\/])?(([^\\\/\?#]*?)(\.([^\.\?#]+))?))(\?([^#]*))?(#(.*))?$/,n=["href","fullDomain","protocol","host","hostname","","port","pathname","folderPath","file","fileName","extension","fileType","search","query","hash","anchor"];return e}); | ||
!function(e){"function"==typeof define&&define.amd?define(e):"object"==typeof module&&module.exports?module.exports=e():window.urllib=e()}(function(){"use strict";function e(e){return null!=e?decodeURIComponent(e):""}function n(e){return encodeURIComponent(e+"")}function r(e){for(var n,r,t=new Object(e),o=0;++o<arguments.length;)if(n=arguments[o])for(r in n)c.call(n,r)&&(t[r]=n[r]);return t}function t(e,n){var r=(e||"")+"",t=r.indexOf("?");return 0>t&&!n&&(t=r.length),{urlPath:r.slice(0,t),queryString:r.slice(t+1)}}function o(e){var t=e;return Array.isArray(t)&&(t=t.length<2?t[0]:r.apply(null,t)),t=t||f,Object.keys(t).reduce(function(e,r){var o,u=e;return r&&(o=t[r],null!=o&&u.push(n(r)+"="+n(o))),u},[]).join("&")}function u(n,r){var o=t(n,r!==!1).queryString||"";return o.split("&").reduce(function(n,r){var t=n,o=r.split("="),u=o[0];return u&&(t[e(u)]=e(o[1])),t},{})}function i(e,n){var r,i,f,c=e,l=n;return Array.isArray(c)&&(l=c.slice(1).concat(l||a),c=c[0]),r=u(c,!1),i=Array.isArray(l)?l:[l],l=[r].concat(i),f=o(l),t(c).urlPath+(f?"?":"")+f}var f={},a=[],c=f.hasOwnProperty;return{parseQuery:u,formatUrl:i,formatQuery:o}}),function(e){"function"==typeof define&&define.amd?define(e):"object"==typeof module&&module.exports?module.exports=e():window.urllib.getCacheDefeatStr=e()}(function(){"use strict";function e(){return+new Date+""+Math.round(1e3*Math.random())+n++}var n=0;return e}),function(e){"function"==typeof define&&define.amd?define(e):"object"==typeof module&&module.exports?module.exports=e():window.urllib.parseUrl=e()}(function(){"use strict";function e(e){function t(e){return o?o[e]||"":""}var o=e&&e.match(n);return r.reduce(function(e,n,r){var o=e;return n&&(o[n]=t(r)),o},{})}var n=/^(([^:\\\/]+:)\/\/(([^:\\\/]*)(:(\d+))?)?)?(([^\?#]*[\\\/])?(([^\\\/\?#]*?)(\.([^\.\?#]+))?))(\?([^#]*))?(#(.*))?$/,r=["href","fullDomain","protocol","host","hostname","","port","pathname","folderPath","file","fileName","extension","fileType","search","query","hash","anchor"];return e}); |
@@ -7,3 +7,4 @@ /** | ||
*/ | ||
(function(root, factory) { | ||
(function(factory) { | ||
/* istanbul ignore next */ | ||
if (typeof define === 'function' && define.amd) { | ||
@@ -16,5 +17,5 @@ define(factory); | ||
else { | ||
root.urllib.getCacheDefeatStr = factory(); | ||
window.urllib.getCacheDefeatStr = factory(); | ||
} | ||
}(this, function() { | ||
})(function() { | ||
'use strict'; | ||
@@ -37,2 +38,2 @@ | ||
return getCacheDefeatStr; | ||
})); | ||
}); |
82
index.js
@@ -7,3 +7,4 @@ /** | ||
*/ | ||
(function(global, factory) { | ||
(function(factory) { | ||
/* istanbul ignore next */ | ||
if (typeof define === 'function' && define.amd) { | ||
@@ -16,9 +17,10 @@ define(factory); | ||
else { | ||
global.urllib = factory(); | ||
window.urllib = factory(); | ||
} | ||
}(this, function() { | ||
})(function() { | ||
'use strict'; | ||
var hasOwnProperty = {}.hasOwnProperty, | ||
sacredEmptyArray = []; | ||
var immutableEmptyObject = {}, | ||
immutableEmptyArray = [], | ||
hasOwnProperty = immutableEmptyObject.hasOwnProperty; | ||
@@ -35,9 +37,13 @@ function _decode(str) { | ||
function _merge(target) { | ||
var output = new Object(target); | ||
var output = new Object(target), | ||
argNo = 0, | ||
source, | ||
sourceKey; | ||
for (var argNo = 0; ++argNo < arguments.length;) { | ||
var source = arguments[argNo]; | ||
for (; ++argNo < arguments.length;) { | ||
source = arguments[argNo]; | ||
if (source) { | ||
for (var sourceKey in source) { | ||
for (sourceKey in source) { | ||
/* istanbul ignore else */ | ||
if (hasOwnProperty.call(source, sourceKey)) { | ||
@@ -77,4 +83,3 @@ output[sourceKey] = source[sourceKey]; | ||
function formatQuery(urlParams) { | ||
var urlParamPairs = [], | ||
paramsObj = urlParams; | ||
var paramsObj = urlParams; | ||
@@ -87,13 +92,20 @@ if (Array.isArray(paramsObj)) { | ||
for (var paramName in paramsObj) { | ||
if (paramName) { | ||
var paramValue = paramsObj[paramName]; | ||
paramsObj = paramsObj || immutableEmptyObject; | ||
if (paramValue != null) { | ||
urlParamPairs.push(_encode(paramName) + '=' + _encode(paramValue)); | ||
return Object.keys(paramsObj) | ||
.reduce(function(prevUrlParamPairs, paramName) { | ||
var urlParamPairs = prevUrlParamPairs, | ||
paramValue; | ||
if (paramName) { | ||
paramValue = paramsObj[paramName]; | ||
if (paramValue != null) { | ||
urlParamPairs.push(_encode(paramName) + '=' + _encode(paramValue)); | ||
} | ||
} | ||
} | ||
} | ||
return urlParamPairs.join('&'); | ||
return urlParamPairs; | ||
}, []) | ||
.join('&'); | ||
} | ||
@@ -108,26 +120,18 @@ | ||
function parseQuery(strToParse, favorQuery) { | ||
var urlParams = {}, | ||
// Ensure that all we parse is a query string | ||
var queryString = _splitOnQuery(strToParse, favorQuery !== false).queryString || ''; | ||
// Ensure that all we parse is a query string | ||
queryString = _splitOnQuery( | ||
strToParse, | ||
favorQuery !== false | ||
).queryString; | ||
return queryString | ||
.split('&') | ||
.reduce(function(prevUrlParams, serializedUrlParamPair) { | ||
var urlParams = prevUrlParams, | ||
urlParamPair = serializedUrlParamPair.split('='), | ||
urlParamNameEncoded = urlParamPair[0]; | ||
if (queryString) { | ||
var urlParamPairs = queryString.split('&'), | ||
urlParamPairsLength = urlParamPairs.length; | ||
// Loop through all of the pairs and add to urlParams the decoded name & value | ||
for (var urlParamPairNo = -1; ++urlParamPairNo < urlParamPairsLength;) { | ||
var urlParamPair = urlParamPairs[urlParamPairNo].split('='); | ||
var urlParamNameEncoded = urlParamPair[0]; | ||
if (urlParamNameEncoded) { | ||
urlParams[_decode(urlParamNameEncoded)] = _decode(urlParamPair[1]); | ||
} | ||
} | ||
} | ||
return urlParams; | ||
return urlParams; | ||
}, {}); | ||
} | ||
@@ -151,3 +155,3 @@ | ||
if (Array.isArray(formattedUrl)) { | ||
queryParams = formattedUrl.slice(1).concat(queryParams || sacredEmptyArray); | ||
queryParams = formattedUrl.slice(1).concat(queryParams || immutableEmptyArray); | ||
formattedUrl = formattedUrl[0]; | ||
@@ -180,2 +184,2 @@ } | ||
}; | ||
})); | ||
}); |
{ | ||
"name": "url-lib", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "A simple, lightweight string utility for Node and browsers that supports serializing and parsing URLs and query strings.", | ||
@@ -12,3 +12,3 @@ "main": "./index.js", | ||
"minify": "mkdir -p dist && npm run minify-core && npm run minify-all", | ||
"minify-all": "cat index.js getCacheDefeatStr.js parseUrl.js | uglifyjs --compress --mangle --comments --output dist/url-lib.min.js", | ||
"minify-all": "cat index.js getCacheDefeatStr.js parseUrl.js | uglifyjs --compress --mangle --output dist/url-lib.min.js", | ||
"minify-core": "uglifyjs --compress --mangle --comments --output dist/url-lib-core.min.js index.js", | ||
@@ -52,3 +52,3 @@ "lint": "eslint .", | ||
"eslint": "^2.4.0", | ||
"eslint-config-benmvp": "^1.0.0", | ||
"eslint-config-benmvp": "^2.0.0", | ||
"istanbul": "^0.4.2", | ||
@@ -55,0 +55,0 @@ "mocha": "^2.4.5", |
@@ -7,3 +7,4 @@ /** | ||
*/ | ||
(function(root, factory) { | ||
(function(factory) { | ||
/* istanbul ignore next */ | ||
if (typeof define === 'function' && define.amd) { | ||
@@ -16,9 +17,9 @@ define(factory); | ||
else { | ||
root.urllib.parseUrl = factory(); | ||
window.urllib.parseUrl = factory(); | ||
} | ||
}(this, function() { | ||
})(function() { | ||
'use strict'; | ||
var urlRegExp = /^(([^:\\\/]+:)\/\/(([^:\\\/]*)(:(\d+))?)?)?(([^\?#]*[\\\/])?(([^\\\/\?#]*?)(\.([^\.\?#]+))?))(\?([^#]*))?(#(.*))?$/, | ||
urlSegments = [ // * properties marked '*' are consistent with browser's location object | ||
var URL_REG_EXP = /^(([^:\\\/]+:)\/\/(([^:\\\/]*)(:(\d+))?)?)?(([^\?#]*[\\\/])?(([^\\\/\?#]*?)(\.([^\.\?#]+))?))(\?([^#]*))?(#(.*))?$/, | ||
URL_SEGMENTS = [ // * properties marked '*' are consistent with browser's location object | ||
'href', // * eg. http://benmvp.com:80/docs/url-lib.html?param=value#anchor | ||
@@ -49,4 +50,3 @@ 'fullDomain', // eg. http://benmvp.com:80 | ||
function parseUrl(url) { | ||
var urlSegmentsMatch = url && url.match(urlRegExp), | ||
parsedUrl = {}; | ||
var urlSegmentsMatch = url && url.match(URL_REG_EXP); | ||
@@ -59,14 +59,14 @@ function getUrlSegment(segmentNo) { | ||
for (var segmentNo = -1; ++segmentNo < urlSegments.length;) { | ||
var segmentName = urlSegments[segmentNo]; | ||
return URL_SEGMENTS.reduce(function(prevParsedUrl, segmentName, segmentNo) { | ||
var parsedUrl = prevParsedUrl; | ||
if (segmentName) { | ||
parsedUrl[urlSegments[segmentNo]] = getUrlSegment(segmentNo); | ||
parsedUrl[segmentName] = getUrlSegment(segmentNo); | ||
} | ||
} | ||
return parsedUrl; | ||
return parsedUrl; | ||
}, {}); | ||
} | ||
return parseUrl; | ||
})); | ||
}); |
@@ -66,7 +66,13 @@ # url-lib | ||
- `Array.isArray`: see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) for details about unsupported older browsers (e.g. IE8-) and a simple polyfill (or use [`es5-shim`](https://github.com/es-shims/es5-shim)) | ||
`url-lib` uses a number of ES5 features that are unsupported in older browsers (e.g. IE8-). The easiest way to add support to non-ES5 browsers is to use [`es5-shim`](https://github.com/es-shims/es5-shim). | ||
Specifically the ES5 features in use are: | ||
- [`Array.isArray`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) | ||
- [`Array.prototype.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) | ||
- [`Object.keys`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) | ||
## Contributing | ||
Coming soon... 😀 | ||
Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md) for more details. | ||
@@ -77,3 +83,3 @@ ## Project philosophy | ||
All updates must not break the [CI build](https://travis-ci.org/benmvp/url-lib) nor go below the [90%+ code coverage](https://coveralls.io/github/benmvp/url-lib?branch=master). | ||
All updates must pass the [CI build](https://travis-ci.org/benmvp/url-lib) while maintaining [100% code coverage](https://coveralls.io/github/benmvp/url-lib). | ||
@@ -80,0 +86,0 @@ ## License |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24258
12
87
248