relateurl
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -19,8 +19,3 @@ var constants = require("./constants"); | ||
{ | ||
if (urlObj.hash) | ||
{ | ||
return urlObj.hash; | ||
} | ||
return ""; | ||
return urlObj.hash ? urlObj.hash : ""; | ||
} | ||
@@ -46,20 +41,32 @@ | ||
//if (!urlObj.extra.hrefInfo.minimumResourceOnly && !urlObj.extra.hrefInfo.minimumQueryOnly && !urlObj.extra.hrefInfo.minimumHashOnly) | ||
//{ | ||
var absolutePath = urlObj.path.absolute.string; | ||
var relativePath = urlObj.path.relative.string; | ||
var absolutePath = urlObj.path.absolute.string; | ||
var relativePath = urlObj.path.relative.string; | ||
if (urlObj.extra.relation.maximumHost || options.output==constants.ABSOLUTE || options.output==constants.ROOT_RELATIVE) | ||
{ | ||
str += absolutePath; | ||
} | ||
else if (relativePath.length<=absolutePath.length && options.output==constants.SHORTEST || options.output==constants.PATH_RELATIVE) | ||
{ | ||
str = relativePath; | ||
if (urlObj.extra.relation.maximumHost || options.output==constants.ABSOLUTE || options.output==constants.ROOT_RELATIVE) | ||
if (str == "") | ||
{ | ||
str += absolutePath; | ||
var resource = showResource(urlObj,options); | ||
var query = showQuery(urlObj,options) && !!getQuery(urlObj,options); | ||
if (urlObj.extra.relation.maximumPath && !resource) | ||
{ | ||
str = "./"; | ||
} | ||
else if (urlObj.extra.relation.overridesQuery && !resource && !query) | ||
{ | ||
str = "./"; | ||
} | ||
} | ||
else if (relativePath.length<=absolutePath.length && options.output==constants.SHORTEST || options.output==constants.PATH_RELATIVE) | ||
{ | ||
str = relativePath; | ||
} | ||
else | ||
{ | ||
str = absolutePath; | ||
} | ||
//} | ||
} | ||
else | ||
{ | ||
str = absolutePath; | ||
} | ||
@@ -85,5 +92,3 @@ return str; | ||
{ | ||
var stripQuery = options.removeEmptyQueries && urlObj.extra.relation.minimumPort; | ||
return urlObj.query.string[ stripQuery ? "stripped" : "full" ]; | ||
return showQuery(urlObj,options) ? getQuery(urlObj, options) : ""; | ||
} | ||
@@ -95,15 +100,3 @@ | ||
{ | ||
var resource = urlObj.resource; | ||
if (resource) | ||
{ | ||
if ( options.removeDirectoryIndexes && urlObj.extra.resourceIsIndex ) | ||
{ | ||
resource = ""; | ||
} | ||
return resource; | ||
} | ||
return ""; | ||
return showResource(urlObj,options) ? urlObj.resource : ""; | ||
} | ||
@@ -147,8 +140,2 @@ | ||
if (url == "") | ||
{ | ||
// TEMP -- might need to add resource and/or query because "./" seems to denote only the current DIR | ||
url = "./"; | ||
} | ||
return url; | ||
@@ -159,2 +146,28 @@ } | ||
function getQuery(urlObj, options) | ||
{ | ||
var stripQuery = options.removeEmptyQueries && urlObj.extra.relation.minimumPort; | ||
return urlObj.query.string[ stripQuery ? "stripped" : "full" ]; | ||
} | ||
function showQuery(urlObj, options) | ||
{ | ||
return !urlObj.extra.relation.minimumQuery || options.output==constants.ABSOLUTE || options.output==constants.ROOT_RELATIVE; | ||
} | ||
function showResource(urlObj, options) | ||
{ | ||
var removeIndex = options.removeDirectoryIndexes && urlObj.extra.resourceIsIndex; | ||
var removeMatchingResource = urlObj.extra.relation.minimumResource && options.output!=constants.ABSOLUTE && options.output!=constants.ROOT_RELATIVE; | ||
return !!urlObj.resource && !removeMatchingResource && !removeIndex; | ||
} | ||
module.exports = formatUrl; |
@@ -12,3 +12,3 @@ var constants = require("./constants"); | ||
{ | ||
this.instanceOptions = getOptions(options, | ||
this.options = getOptions(options, | ||
{ | ||
@@ -27,3 +27,3 @@ defaultPorts: {ftp:21, http:80, https:443}, | ||
this.from = parseUrl.from(from, this.instanceOptions, null); | ||
this.from = parseUrl.from(from, this.options, null); | ||
} | ||
@@ -52,3 +52,3 @@ | ||
options = getOptions(options, this.instanceOptions); | ||
options = getOptions(options, this.options); | ||
from = parseUrl.from(from, options, this.from); | ||
@@ -94,3 +94,3 @@ | ||
{ | ||
RelateUrl[i] = constants[i]; | ||
Object.defineProperty(RelateUrl, i, {value:constants[i]}); | ||
} | ||
@@ -97,0 +97,0 @@ } |
function parseHost(urlObj, options) | ||
{ | ||
//if (options.ignore_www) | ||
//{ | ||
// TWEAK :: condition only for speed optimization | ||
if (options.ignore_www) | ||
{ | ||
var host = urlObj.host.full; | ||
@@ -18,3 +19,3 @@ | ||
} | ||
//} | ||
} | ||
} | ||
@@ -21,0 +22,0 @@ |
@@ -7,2 +7,3 @@ function hrefInfo(urlObj) | ||
var minimumHashOnly = (minimumQueryOnly && !urlObj.query.string.full.length); | ||
var empty = (minimumHashOnly && !urlObj.hash); | ||
@@ -13,2 +14,3 @@ urlObj.extra.hrefInfo.minimumPathOnly = minimumPathOnly; | ||
urlObj.extra.hrefInfo.minimumHashOnly = minimumHashOnly; | ||
urlObj.extra.hrefInfo.empty = empty; | ||
} | ||
@@ -15,0 +17,0 @@ |
@@ -33,19 +33,36 @@ function isDirectoryIndex(resource, options) | ||
{ | ||
urlObj.resource = path.substr(lastSlash); | ||
var resource = path.substr(lastSlash); | ||
urlObj.extra.resourceIsIndex = isDirectoryIndex(urlObj.resource, options); | ||
urlObj.path.absolute.string = path.substr(0, lastSlash); | ||
if (resource!="." && resource!="..") | ||
{ | ||
urlObj.resource = resource; | ||
path = path.substr(0, lastSlash); | ||
} | ||
else | ||
{ | ||
path += "/"; | ||
} | ||
} | ||
urlObj.path.absolute.array = splitPath(urlObj.path.absolute.string); | ||
urlObj.path.absolute.string = path; | ||
urlObj.path.absolute.array = splitPath(path); | ||
} | ||
else if (path=="." || path=="..") | ||
{ | ||
// "..?var", "..#anchor", etc ... not "..index.html" | ||
path += "/"; | ||
urlObj.path.absolute.string = path; | ||
urlObj.path.absolute.array = splitPath(path); | ||
} | ||
else | ||
{ | ||
// No slash means resource-only | ||
// Resource-only | ||
urlObj.resource = path; | ||
urlObj.path.absolute.string = null; | ||
} | ||
urlObj.extra.resourceIsIndex = isDirectoryIndex(urlObj.resource, options); | ||
} | ||
// Else: no path means hash- or query-only | ||
// Else: query/hash-only or empty | ||
} | ||
@@ -57,4 +74,5 @@ | ||
{ | ||
//if (path != "/") | ||
//{ | ||
// TWEAK :: condition only for speed optimization | ||
if (path != "/") | ||
{ | ||
var cleaned = []; | ||
@@ -72,3 +90,3 @@ | ||
return cleaned; | ||
/*} | ||
} | ||
else | ||
@@ -78,3 +96,3 @@ { | ||
return []; | ||
}*/ | ||
} | ||
} | ||
@@ -81,0 +99,0 @@ |
@@ -5,6 +5,7 @@ function parseQuery(urlObj, options) | ||
//if (options.removeEmptyQueries) | ||
//{ | ||
// TWEAK :: condition only for speed optimization | ||
if (options.removeEmptyQueries) | ||
{ | ||
urlObj.query.string.stripped = stringify(urlObj.query.object, true); | ||
//} | ||
} | ||
} | ||
@@ -11,0 +12,0 @@ |
@@ -16,3 +16,3 @@ // For client-side support, can switch to https://github.com/kevincox/url.js | ||
* urlObj.protocol is inaccurate; should be called "scheme" | ||
* urlObj.search is useless | ||
* urlObj.search is mostly useless | ||
*/ | ||
@@ -34,2 +34,3 @@ function clean(urlObj) | ||
{ | ||
// TODO :: unescape(encodeURIComponent(s)) ? ... http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html | ||
full: urlObj.hostname, | ||
@@ -70,3 +71,6 @@ stripped: null | ||
minimumQueryOnly: null, | ||
minimumHashOnly: null | ||
minimumHashOnly: null, | ||
empty: null, | ||
separatorOnlyQuery: urlObj.search=="?" | ||
}, | ||
@@ -92,3 +96,5 @@ portIsDefault: null, | ||
minimumQuery: null, | ||
minimumHash: null | ||
minimumHash: null, | ||
overridesQuery: null | ||
}, | ||
@@ -95,0 +101,0 @@ resourceIsIndex: null, |
@@ -34,3 +34,2 @@ var findRelation = require("./findRelation"); | ||
{ | ||
// TODO :: remove the minimumResourceOnly part if possible | ||
if (urlObj.extra.relation.maximumHost || !urlObj.extra.hrefInfo.minimumResourceOnly) | ||
@@ -64,3 +63,3 @@ { | ||
{ | ||
// Resource-, query- or hash-only | ||
// Resource-, query- or hash-only or empty | ||
urlObj.path = objUtils.clone(siteUrlObj.path); | ||
@@ -67,0 +66,0 @@ } |
@@ -7,26 +7,22 @@ function findRelation_upToPath(urlObj, siteUrlObj, options) | ||
// Matching scheme, scheme-relative or path-only | ||
var sameScheme = (urlObj.scheme==siteUrlObj.scheme || !urlObj.scheme); | ||
var minimumScheme = (urlObj.scheme==siteUrlObj.scheme || !urlObj.scheme); | ||
// Matching auth, ignoring auth or path-only | ||
var sameAuth = sameScheme && (urlObj.auth==siteUrlObj.auth || options.removeAuth || pathOnly); | ||
var minimumAuth = minimumScheme && (urlObj.auth==siteUrlObj.auth || options.removeAuth || pathOnly); | ||
// Matching host or path-only | ||
var www = options.ignore_www ? "stripped" : "full"; | ||
var sameHost = sameAuth && (urlObj.host[www]==siteUrlObj.host[www] || pathOnly); | ||
var minimumHost = minimumAuth && (urlObj.host[www]==siteUrlObj.host[www] || pathOnly); | ||
// Matching port or path-only | ||
var samePort = sameHost && (urlObj.port==siteUrlObj.port || pathOnly); | ||
var minimumPort = minimumHost && (urlObj.port==siteUrlObj.port || pathOnly); | ||
//var atLeastRootRelative = samePort && !urlObj.extra.hrefInfo.minimumResourceOnly; | ||
urlObj.extra.relation.minimumScheme = minimumScheme; | ||
urlObj.extra.relation.minimumAuth = minimumAuth; | ||
urlObj.extra.relation.minimumHost = minimumHost; | ||
urlObj.extra.relation.minimumPort = minimumPort; | ||
urlObj.extra.relation.minimumScheme = sameScheme; | ||
urlObj.extra.relation.minimumAuth = sameAuth; | ||
urlObj.extra.relation.minimumHost = sameHost; | ||
urlObj.extra.relation.minimumPort = samePort; | ||
//urlObj.extra.relation.minimumPath = samePort && atLeastRootRelative; // gets changed in _pathOn() ... TEMP, i hope | ||
urlObj.extra.relation.maximumScheme = !sameScheme || sameScheme && !sameAuth; | ||
urlObj.extra.relation.maximumAuth = !sameScheme || sameScheme && !sameHost; | ||
urlObj.extra.relation.maximumHost = !sameScheme || sameScheme && !samePort; | ||
//urlObj.extra.relation.maximumPort = !sameScheme || sameScheme && !atLeastRootRelative; // gets changed in _pathOn() ... TEMP, i hope | ||
urlObj.extra.relation.maximumScheme = !minimumScheme || minimumScheme && !minimumAuth; | ||
urlObj.extra.relation.maximumAuth = !minimumScheme || minimumScheme && !minimumHost; | ||
urlObj.extra.relation.maximumHost = !minimumScheme || minimumScheme && !minimumPort; | ||
} | ||
@@ -40,28 +36,36 @@ | ||
var hashOnly = urlObj.extra.hrefInfo.minimumHashOnly; | ||
var empty = urlObj.extra.hrefInfo.empty; // not required, but self-documenting | ||
// From upToPath() | ||
var sameScheme = urlObj.extra.relation.minimumScheme; | ||
var minimumPort = urlObj.extra.relation.minimumPort; | ||
var minimumScheme = urlObj.extra.relation.minimumScheme; | ||
// Matching port and path | ||
var samePath = urlObj.extra.relation.minimumPort && urlObj.path.absolute.string==siteUrlObj.path.absolute.string; | ||
var minimumPath = minimumPort && urlObj.path.absolute.string==siteUrlObj.path.absolute.string; | ||
// Matching resource or removing default indexes or query/hash-only | ||
var sameResource = samePath && (urlObj.resource==siteUrlObj.resource || (options.removeDirectoryIndexes && urlObj.extra.resourceIsIndex) || queryOnly || hashOnly); | ||
// Matching resource or query/hash-only or empty | ||
var matchingResource = (urlObj.resource==siteUrlObj.resource || !urlObj.resource && siteUrlObj.extra.resourceIsIndex) || (options.removeDirectoryIndexes && urlObj.extra.resourceIsIndex && !siteUrlObj.resource); | ||
var minimumResource = minimumPath && (matchingResource || queryOnly || hashOnly || empty); | ||
// Matching query or hash-only | ||
var query = options.removeEmptyQueries ? "stripepd" : "full"; | ||
var sameQuery = sameResource && urlObj.query.string[query] && urlObj.query.string[query]==siteUrlObj.query.string[query] || hashOnly; | ||
// Matching query or hash-only/empty | ||
var query = options.removeEmptyQueries ? "stripped" : "full"; | ||
var urlQuery = urlObj.query.string[query]; | ||
var siteUrlQuery = siteUrlObj.query.string[query]; | ||
var minimumQuery = (minimumResource && !!urlQuery && urlQuery==siteUrlQuery) || ((hashOnly || empty) && !urlObj.extra.hrefInfo.separatorOnlyQuery); | ||
var sameHash = sameQuery && urlObj.hash==siteUrlObj.hash; | ||
var minimumHash = minimumQuery && urlObj.hash==siteUrlObj.hash; | ||
urlObj.extra.relation.minimumPath = samePath; | ||
urlObj.extra.relation.minimumResource = sameResource; | ||
urlObj.extra.relation.minimumQuery = sameQuery; | ||
urlObj.extra.relation.minimumHash = sameHash; | ||
urlObj.extra.relation.minimumPath = minimumPath; | ||
urlObj.extra.relation.minimumResource = minimumResource; | ||
urlObj.extra.relation.minimumQuery = minimumQuery; | ||
urlObj.extra.relation.minimumHash = minimumHash; | ||
urlObj.extra.relation.maximumPort = !sameScheme || sameScheme && !samePath; | ||
urlObj.extra.relation.maximumPath = !sameScheme || sameScheme && !sameResource; | ||
urlObj.extra.relation.maximumResource = !sameScheme || sameScheme && !sameQuery; | ||
urlObj.extra.relation.maximumQuery = !sameScheme || sameScheme && !sameHash; | ||
urlObj.extra.relation.maximumHash = !sameScheme || sameScheme && sameHash; | ||
urlObj.extra.relation.maximumPort = !minimumScheme || minimumScheme && !minimumPath; | ||
urlObj.extra.relation.maximumPath = !minimumScheme || minimumScheme && !minimumResource; | ||
urlObj.extra.relation.maximumResource = !minimumScheme || minimumScheme && !minimumQuery; | ||
urlObj.extra.relation.maximumQuery = !minimumScheme || minimumScheme && !minimumHash; | ||
urlObj.extra.relation.maximumHash = !minimumScheme || minimumScheme && !minimumHash; // there's nothing after hash, so it's the same as maximumQuery | ||
// Matching path and/or resource with existing but non-matching site query | ||
urlObj.extra.relation.overridesQuery = minimumPath && urlObj.extra.relation.maximumResource && !minimumQuery && !!siteUrlQuery; | ||
} | ||
@@ -68,0 +72,0 @@ |
@@ -41,4 +41,2 @@ var pathUtils = require("../util/path"); | ||
{ | ||
//relativePath.push( (i>parentIndex) ? dir : ".." ); | ||
if (i > parentIndex) | ||
@@ -60,11 +58,5 @@ { | ||
var pathArray = relatePath(urlObj.path.absolute.array, siteUrlObj.path.absolute.array); | ||
var pathString = pathUtils.join(pathArray); | ||
/*if (pathString.indexOf("../") != 0) | ||
{ | ||
pathString = "./" + pathString; | ||
}*/ | ||
urlObj.path.relative.array = pathArray; | ||
urlObj.path.relative.string = pathString; | ||
urlObj.path.relative.string = pathUtils.join(pathArray); | ||
} | ||
@@ -71,0 +63,0 @@ } |
@@ -0,4 +1,8 @@ | ||
var inspect = require("util").inspect; | ||
function log(data) | ||
{ | ||
console.log( require("util").inspect(data, {depth:null, colors:true}) ); | ||
console.log( inspect(data, {depth:null, colors:true}) ); | ||
} | ||
@@ -10,3 +14,3 @@ | ||
{ | ||
console.log( require("util").inspect(data, {depth:null, showHidden:true, colors:true}) ); | ||
console.log( inspect(data, {depth:null, showHidden:true, colors:true}) ); | ||
} | ||
@@ -13,0 +17,0 @@ |
{ | ||
"name": "relateurl", | ||
"description": "Shorten URLs by converting them from absolute to relative.", | ||
"version": "0.2.0", | ||
"description": "Minify URLs by converting them from absolute to relative.", | ||
"version": "0.2.1", | ||
"homepage": "https://github.com/stevenvachon/relateurl", | ||
@@ -28,8 +28,9 @@ "author": { | ||
"keywords": [ | ||
"absolute", | ||
"url", | ||
"minifier", | ||
"minify", | ||
"lint", | ||
"relative", | ||
"minify", | ||
"shorten", | ||
"url" | ||
"absolute" | ||
] | ||
} |
# relateurl [![NPM Version](http://badge.fury.io/js/relateurl.png)](http://badge.fury.io/js/relateurl) [![Build Status](https://secure.travis-ci.org/stevenvachon/relateurl.png)](http://travis-ci.org/stevenvachon/relateurl) | ||
> Shorten URLs by converting them from absolute to relative. | ||
> Minify URLs by converting them from absolute to relative. | ||
If you were to use this library on a website like *http;//example.com/dir1/dir1-1/*, you would get results like these: | ||
If you were to use this library on a website like `http://example.com/dir1/dir1-1/`, you would get results such as: | ||
| Before | After | | ||
| :---------------------------------------- | :--------------------------------- | | ||
| http;//example.com/dir1/dir1-2/index.html | ../dir1-2/ | | ||
| http;//example.com/dir2/dir2-1/ | /dir2/dir2-1/ | | ||
| http;//example.com/dir1/dir1-1/ | ./ | | ||
| *https*;//example.com/dir1/dir1-1/ | *https*;//example.com/dir1/dir1-1/ | | ||
| http;//google.com/dir/ | //google.com/dir/ | | ||
| ../../../../../../../../#anchor | /#anchor | | ||
| Before | After | | ||
| :------------------------------------------ | :----------------------------------- | | ||
| `http://example.com/dir1/dir1-2/index.html` | `../dir1-2/` | | ||
| `http://example.com/dir2/dir2-1/` | `/dir2/dir2-1/` | | ||
| `http://example.com/dir1/dir1-1/` | ` ` | | ||
| `https://example.com/dir1/dir1-1/` | `https://example.com/dir1/dir1-1/` | | ||
| `http://google.com:80/dir/` | `//google.com/dir/` | | ||
| `../../../../../../../../#anchor` | `/#anchor` | | ||
**All string parsing.** *No* directory browsing. It is very fast and lightweight with zero dependencies. | ||
**All string parsing.** *No* directory browsing. It is thoroughly tested, very fast and lightweight with zero external dependencies. | ||
@@ -53,3 +53,3 @@ ## Getting Started | ||
`RelateUrl.ROOT_RELATIVE` will produce something like `/child-of-root/etc/`. | ||
`RelateUrl.SHORTEST` will choose whichever is shortest between root- or path-relative. | ||
`RelateUrl.SHORTEST` will choose whichever is shortest between root- and path-relative. | ||
@@ -95,3 +95,3 @@ #### options.rejectedSchemes | ||
Upon successful conversion, a `String` will be returned. If an issue is encountered, `false` will be returned. | ||
Upon successful conversion, a `String` will be returned. If an issue is encountered while parsing `from`, an error will be thrown. | ||
@@ -116,3 +116,11 @@ #### Single Instance | ||
## FAQ | ||
1. **Why bother writing/using this?** | ||
To aid in further minifying HTML, mainly for the purpose of faster page loads and SEO. | ||
2. **Why not just use Node's `url.parse`, `url.resolve` and `path.relative`?** | ||
`url.parse` *is* used, but `url.resolve` and `path.relative` are both slower and less powerful than this library. | ||
## Release History | ||
* 0.2.1 shorten resource- and query-relative URLs, test variations list with other site URLs | ||
* 0.2.0 code cleanup, `options.removeEmptyQueries=true` only applied to unrelated URLs | ||
@@ -122,3 +130,2 @@ * 0.1.0 initial release | ||
## Roadmap | ||
* 0.2.1 try to shorten query-relative URLs, test variations list with other site URLs | ||
* 0.2.2 decipher and return invalid input (special cases) to complete test suite | ||
@@ -125,0 +132,0 @@ * 0.3.0 test `options.slashesDenoteHost=false`, add something like `options.externalDirectoryIndexes=[]` for external sites |
28999
868
132