resolve-pathname
Advanced tools
+74
| 'use strict'; | ||
| exports.__esModule = true; | ||
| function isAbsolute(pathname) { | ||
| return pathname.charAt(0) === '/'; | ||
| } | ||
| // About 1.5x faster than the two-arg version of Array#splice() | ||
| function spliceOne(list, index) { | ||
| for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) { | ||
| list[i] = list[k]; | ||
| } | ||
| list.pop(); | ||
| } | ||
| // This implementation is based heavily on node's url.parse | ||
| function resolvePathname(to) { | ||
| var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
| var toParts = to && to.split('/') || []; | ||
| var fromParts = from && from.split('/') || []; | ||
| var isToAbs = to && isAbsolute(to); | ||
| var isFromAbs = from && isAbsolute(from); | ||
| var mustEndAbs = isToAbs || isFromAbs; | ||
| if (to && isAbsolute(to)) { | ||
| // to is absolute | ||
| fromParts = toParts; | ||
| } else if (toParts.length) { | ||
| // to is relative, drop the filename | ||
| fromParts.pop(); | ||
| fromParts = fromParts.concat(toParts); | ||
| } | ||
| if (!fromParts.length) return '/'; | ||
| var hasTrailingSlash = void 0; | ||
| if (fromParts.length) { | ||
| var last = fromParts[fromParts.length - 1]; | ||
| hasTrailingSlash = last === '.' || last === '..' || last === ''; | ||
| } else { | ||
| hasTrailingSlash = false; | ||
| } | ||
| var up = 0; | ||
| for (var i = fromParts.length; i >= 0; i--) { | ||
| var part = fromParts[i]; | ||
| if (part === '.') { | ||
| spliceOne(fromParts, i); | ||
| } else if (part === '..') { | ||
| spliceOne(fromParts, i); | ||
| up++; | ||
| } else if (up) { | ||
| spliceOne(fromParts, i); | ||
| up--; | ||
| } | ||
| } | ||
| if (!mustEndAbs) for (; up--; up) { | ||
| fromParts.unshift('..'); | ||
| }if (mustEndAbs && fromParts[0] !== '' && (!fromParts[0] || !isAbsolute(fromParts[0]))) fromParts.unshift(''); | ||
| var result = fromParts.join('/'); | ||
| if (hasTrailingSlash && result.substr(-1) !== '/') result += '/'; | ||
| return result; | ||
| } | ||
| exports.default = resolvePathname; | ||
| module.exports = exports['default']; |
+10
-10
@@ -1,16 +0,16 @@ | ||
| 'use strict'; | ||
| var isAbsolute = function isAbsolute(pathname) { | ||
| function isAbsolute(pathname) { | ||
| return pathname.charAt(0) === '/'; | ||
| }; | ||
| } | ||
| // About 1.5x faster than the two-arg version of Array#splice() | ||
| var spliceOne = function spliceOne(list, index) { | ||
| function spliceOne(list, index) { | ||
| for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) { | ||
| list[i] = list[k]; | ||
| }list.pop(); | ||
| }; | ||
| } | ||
| list.pop(); | ||
| } | ||
| // This implementation is based heavily on node's url.parse | ||
| var resolvePathname = function resolvePathname(to) { | ||
| function resolvePathname(to) { | ||
| var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
@@ -68,4 +68,4 @@ | ||
| return result; | ||
| }; | ||
| } | ||
| module.exports = resolvePathname; | ||
| export default resolvePathname; |
+6
-10
| { | ||
| "name": "resolve-pathname", | ||
| "version": "2.1.0", | ||
| "version": "2.2.0", | ||
| "description": "Resolve URL pathnames using JavaScript", | ||
@@ -9,6 +9,8 @@ "repository": "mjackson/resolve-pathname", | ||
| "files": [ | ||
| "es", | ||
| "cjs", | ||
| "index.js", | ||
| "umd" | ||
| ], | ||
| "main": "cjs/index.js", | ||
| "module": "index.js", | ||
| "scripts": { | ||
@@ -20,3 +22,3 @@ "build": "node ./tools/build.js", | ||
| "lint": "eslint modules", | ||
| "test": "karma start --single-run" | ||
| "test": "mocha --compilers js:babel-core/register modules/**/*-test.js" | ||
| }, | ||
@@ -28,2 +30,3 @@ "devDependencies": { | ||
| "babel-loader": "^6.2.3", | ||
| "babel-plugin-add-module-exports": "^0.2.1", | ||
| "babel-preset-es2015": "^6.5.0", | ||
@@ -36,9 +39,2 @@ "babel-preset-stage-1": "^6.24.1", | ||
| "in-publish": "^2.0.0", | ||
| "karma": "^1.1.2", | ||
| "karma-browserstack-launcher": "^1.0.0", | ||
| "karma-chrome-launcher": "^2.0.0", | ||
| "karma-mocha": "^1.0.1", | ||
| "karma-mocha-reporter": "^2.0.3", | ||
| "karma-sourcemap-loader": "^0.3.7", | ||
| "karma-webpack": "^1.7.0", | ||
| "mocha": "^3.0.0", | ||
@@ -45,0 +41,0 @@ "pretty-bytes": "^4.0.2", |
@@ -59,15 +59,18 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
| var isAbsolute = function isAbsolute(pathname) { | ||
| exports.__esModule = true; | ||
| function isAbsolute(pathname) { | ||
| return pathname.charAt(0) === '/'; | ||
| }; | ||
| } | ||
| // About 1.5x faster than the two-arg version of Array#splice() | ||
| var spliceOne = function spliceOne(list, index) { | ||
| function spliceOne(list, index) { | ||
| for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) { | ||
| list[i] = list[k]; | ||
| }list.pop(); | ||
| }; | ||
| } | ||
| list.pop(); | ||
| } | ||
| // This implementation is based heavily on node's url.parse | ||
| var resolvePathname = function resolvePathname(to) { | ||
| function resolvePathname(to) { | ||
| var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
@@ -125,5 +128,5 @@ | ||
| return result; | ||
| }; | ||
| } | ||
| module.exports = resolvePathname; | ||
| exports.default = resolvePathname; | ||
@@ -130,0 +133,0 @@ /***/ }) |
@@ -1,1 +0,1 @@ | ||
| !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.resolvePathname=e():t.resolvePathname=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e){"use strict";var r=function(t){return"/"===t.charAt(0)},n=function(t,e){for(var r=e,n=r+1,o=t.length;n<o;r+=1,n+=1)t[r]=t[n];t.pop()},o=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",o=t&&t.split("/")||[],i=e&&e.split("/")||[],f=t&&r(t),s=e&&r(e),u=f||s;if(t&&r(t)?i=o:o.length&&(i.pop(),i=i.concat(o)),!i.length)return"/";var p=void 0;if(i.length){var a=i[i.length-1];p="."===a||".."===a||""===a}else p=!1;for(var c=0,l=i.length;l>=0;l--){var v=i[l];"."===v?n(i,l):".."===v?(n(i,l),c++):c&&(n(i,l),c--)}if(!u)for(;c--;c)i.unshift("..");!u||""===i[0]||i[0]&&r(i[0])||i.unshift("");var d=i.join("/");return p&&"/"!==d.substr(-1)&&(d+="/"),d};t.exports=o}])}); | ||
| !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.resolvePathname=t():e.resolvePathname=t()}(this,function(){return function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={exports:{},id:o,loaded:!1};return e[o].call(r.exports,r,r.exports,t),r.loaded=!0,r.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t){"use strict";function n(e){return"/"===e.charAt(0)}function o(e,t){for(var n=t,o=n+1,r=e.length;o<r;n+=1,o+=1)e[n]=e[o];e.pop()}function r(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",r=e&&e.split("/")||[],f=t&&t.split("/")||[],i=e&&n(e),u=t&&n(t),s=i||u;if(e&&n(e)?f=r:r.length&&(f.pop(),f=f.concat(r)),!f.length)return"/";var a=void 0;if(f.length){var l=f[f.length-1];a="."===l||".."===l||""===l}else a=!1;for(var p=0,c=f.length;c>=0;c--){var d=f[c];"."===d?o(f,c):".."===d?(o(f,c),p++):p&&(o(f,c),p--)}if(!s)for(;p--;p)f.unshift("..");!s||""===f[0]||f[0]&&n(f[0])||f.unshift("");var h=f.join("/");return a&&"/"!==h.substr(-1)&&(h+="/"),h}t.__esModule=!0,t.default=r}])}); |
| ## [HEAD] | ||
| - **Breakage:** Changed default `from` path to `/` | ||
| - Added LOTS more test cases from node's `url.parse` test suite | ||
| [HEAD]: https://github.com/mjackson/resolve-pathname/compare/latest...HEAD |
-68
| var isAbsolute = function isAbsolute(pathname) { | ||
| return pathname.charAt(0) === '/'; | ||
| }; | ||
| // About 1.5x faster than the two-arg version of Array#splice() | ||
| var spliceOne = function spliceOne(list, index) { | ||
| for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) { | ||
| list[i] = list[k]; | ||
| }list.pop(); | ||
| }; | ||
| // This implementation is based heavily on node's url.parse | ||
| var resolvePathname = function resolvePathname(to) { | ||
| var from = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
| var toParts = to && to.split('/') || []; | ||
| var fromParts = from && from.split('/') || []; | ||
| var isToAbs = to && isAbsolute(to); | ||
| var isFromAbs = from && isAbsolute(from); | ||
| var mustEndAbs = isToAbs || isFromAbs; | ||
| if (to && isAbsolute(to)) { | ||
| // to is absolute | ||
| fromParts = toParts; | ||
| } else if (toParts.length) { | ||
| // to is relative, drop the filename | ||
| fromParts.pop(); | ||
| fromParts = fromParts.concat(toParts); | ||
| } | ||
| if (!fromParts.length) return '/'; | ||
| var hasTrailingSlash = void 0; | ||
| if (fromParts.length) { | ||
| var last = fromParts[fromParts.length - 1]; | ||
| hasTrailingSlash = last === '.' || last === '..' || last === ''; | ||
| } else { | ||
| hasTrailingSlash = false; | ||
| } | ||
| var up = 0; | ||
| for (var i = fromParts.length; i >= 0; i--) { | ||
| var part = fromParts[i]; | ||
| if (part === '.') { | ||
| spliceOne(fromParts, i); | ||
| } else if (part === '..') { | ||
| spliceOne(fromParts, i); | ||
| up++; | ||
| } else if (up) { | ||
| spliceOne(fromParts, i); | ||
| up--; | ||
| } | ||
| } | ||
| if (!mustEndAbs) for (; up--; up) { | ||
| fromParts.unshift('..'); | ||
| }if (mustEndAbs && fromParts[0] !== '' && (!fromParts[0] || !isAbsolute(fromParts[0]))) fromParts.unshift(''); | ||
| var result = fromParts.join('/'); | ||
| if (hasTrailingSlash && result.substr(-1) !== '/') result += '/'; | ||
| return result; | ||
| }; | ||
| module.exports = resolvePathname; |
16
-27.27%215
2.87%11943
-3%6
-14.29%