Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for server-side JS #14

Merged
merged 2 commits into from
Jan 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var url = "http://example.org/foo?bar=baz",
url += separator + encodeURIComponent("foo") + "=" + encodeURIComponent("bar");
```

I still can't believe javascript - the f**ing backbone-language of the web - doesn't offer an API for mutating URLs. Browsers (Firefox) don't expose the `Location` object (the structure behind window.location). Yes, one could think of [decomposed IDL attributes](http://www.whatwg.org/specs/web-apps/current-work/multipage/urls.html#url-decomposition-idl-attributes) as a native URL management library. But it relies on the DOM element <a>, it's slow and doesn't offer any convenienve at all.
I still can't believe javascript - the f**ing backbone-language of the web - doesn't offer an API for mutating URLs. Browsers (Firefox) don't expose the `Location` object (the structure behind window.location). Yes, one could think of [decomposed IDL attributes](http://www.whatwg.org/specs/web-apps/current-work/multipage/urls.html#url-decomposition-idl-attributes) as a native URL management library. But it relies on the DOM element <a>, it's slow and doesn't offer any convenienve at all.

How about a nice, clean and simple API for mutating URIs:

Expand All @@ -31,17 +31,17 @@ URI.js is here to help with that.
```javascript
// mutating URLs
URI("http://example.org/foo.html?hello=world")
.username("rodneyrehm")
.username("rodneyrehm")
// -> http://rodneyrehm@example.org/foo.html?hello=world
.username("")
.username("")
// -> http://example.org/foo.html?hello=world
.directory("bar")
// -> http://example.org/bar/foo.html?hello=world
.suffix("xml")
.suffix("xml")
// -> http://example.org/bar/foo.xml?hello=world
.query("")
.query("")
// -> http://example.org/bar/foo.xml
.tld("com")
.tld("com")
// -> http://example.com/bar/foo.xml
.query({ foo: "bar", hello: ["world", "mars"] });
// -> http://example.com/bar/foo.xml?foo=bar&hello=world&hello=mars
Expand All @@ -65,6 +65,22 @@ URI("/foo/bar/baz.html")

See the [About Page](http://medialize.github.com/URI.js/) and [API Docs](http://medialize.github.com/URI.js/docs.html) for more stuff.

## npm ##

```
npm install URIjs
```


## Server-side JS ##

```javascript
var URI = require('URIJS');

URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/sub/world.html")
// -> ../baz.html
```

## Minify ##

Expand All @@ -77,7 +93,7 @@ use [Google Closure Compiler](http://closure-compiler.appspot.com/home):
// @code_url http://medialize.github.com/URI.js/src/IPv6.js
// @code_url http://medialize.github.com/URI.js/src/punycode.js
// @code_url http://medialize.github.com/URI.js/src/URI.js
// ==/ClosureCompiler==
// ==/ClosureCompiler==
```

## Resources ##
Expand Down Expand Up @@ -152,7 +168,7 @@ I built this sucker during Christmas 2011. It was a nice excuse to get away from

```
Quote from java doc:
A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. The mailto, news, and isbn URIs shown above are examples of URNs.
A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. The mailto, news, and isbn URIs shown above are examples of URNs.
```

URI.js only handles URLs - but since Firefox already used window.URL for some (yet undocumented) MozURLProperty, I named it URI anyways.
Expand All @@ -179,7 +195,7 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m
* Updated Punycode.js to version 0.3.0
* added edge-case tests ("jim")
* fixed edge-cases in .protocol(), .port(), .subdomain(), .domain(), .tld(), .filename()
* fixed parsing of hostname in .hostname()
* fixed parsing of hostname in .hostname()

### 1.3.0 ###

Expand Down
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "URI.js",
"name": "URIjs",
"version": "1.4.1",
"title": "URI.js - Mutating URLs",
"author": {
Expand All @@ -14,7 +14,7 @@
{
"type": "GPL",
"url": "http://opensource.org/licenses/GPL-3.0"
}
}
],
"description": "URI.js is a Javascript library for working with URLs.",
"keywords": [
Expand All @@ -26,11 +26,14 @@
"unified resource identifier",
"query string"
],
"main": "./src/URI",
"homepage": "http://medialize.github.com/URI.js/",
"contributors": [],
"contributors": [
"Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com)"
],
"files": [
"src/URI.js",
"src/IPv6.js",
"src/punycode.js"
]
}
}
62 changes: 33 additions & 29 deletions src/IPv6.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
(function(undefined){

var global = typeof module !== 'undefined' && module.exports ?
module.exports
: window


var best = function(address) {
// based on:
// Javascript to test an IPv6 address for proper format, and to
// Javascript to test an IPv6 address for proper format, and to
// present the "best text representation" according to IETF Draft RFC at
// http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04
// 8 Feb 2010 Rich Brown, Dartware, LLC
// Please feel free to use this code as long as you provide a link to
// Please feel free to use this code as long as you provide a link to
// http://www.intermapper.com
// http://intermapper.com/support/tools/IPV6-Validator.aspx
// http://download.dartware.com/thirdparty/ipv6validator.js

var _address = address.toLowerCase(),
segments = _address.split(':'),
length = segments.length,
total = 8;

// trim colons (:: or ::a:b:c… or …a:b:c::)
if (segments[0] === '' && segments[1] === '' && segments[2] === '') {
// must have been ::
// remove first two items
segments.shift();
segments.shift();
segments.shift();
} else if (segments[0] === '' && segments[1] === '') {
// must have been ::xxxx
Expand All @@ -31,33 +34,33 @@ var best = function(address) {
} else if (segments[length - 1] === '' && segments[length - 2] === '') {
// must have been xxxx::
segments.pop();
}
}

length = segments.length;

// adjust total segments for IPv4 trailer
if (segments[length - 1].indexOf('.') !== -1) {
// found a "." which means IPv4
total = 7;
}

// fill empty segments them with "0000"
var pos;
for (pos = 0; pos < length; pos++) {
if (segments[pos] === '') {
break;
}
}

if (pos < total) {
segments.splice(pos, 1, '0000');
while (segments.length < total) {
segments.splice(pos, 0, '0000');
}

length = segments.length;
}

// strip leading zeros
var _segments;
for (var i = 0; i < total; i++) {
Expand All @@ -69,10 +72,10 @@ var best = function(address) {
break;
}
}

segments[i] = _segments.join("");
}

// find longest sequence of zeroes and coalesce them into one segment
var best = -1,
_best = 0,
Expand All @@ -87,46 +90,46 @@ var best = function(address) {
_current += 1;
} else {
inzeroes = false;
if (_current > _best) {
best = current;
if (_current > _best) {
best = current;
_best = _current;
}
}
} else {
if (segments[i] == '0') {
inzeroes = true;
current = i;
_current = 1;
inzeroes = true;
current = i;
_current = 1;
}
}
}

if (_current > _best) {
best = current;
best = current;
_best = _current;
}

if (_best > 1) {
segments.splice(best, _best, "");
}

length = segments.length;

// assemble remaining segments
var result = '';
if (segments[0] === '') {
beststr = ":";
}

for (i = 0; i < length; i++) {
result += segments[i];
if (i === length - 1) {
break;
}

result += ':';
}

if (segments[length - 1] === '') {
result += ":";
}
Expand All @@ -135,14 +138,15 @@ var best = function(address) {
};


window.IPv6 = {
global.IPv6 = {
best: best
};
})();

/*
var _in = "fe80:0000:0000:0000:0204:61ff:fe9d:f156",
_out = IPv6.best(_in),
_expected = "fe80::204:61ff:fe9d:f156";

console.log(_in, _out, _expected, _out === _expected);
*/
*/
8 changes: 4 additions & 4 deletions src/URI.fragmentQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// --------------------------------------------------------------------------------
// EXAMPLE: storing application/x-www-form-urlencoded data in the fragment
// possibly helpful for Google's hashbangs
// possibly helpful for Google's hashbangs
// see http://code.google.com/web/ajaxcrawling/
// --------------------------------------------------------------------------------

Expand Down Expand Up @@ -46,7 +46,7 @@ p.addFragment = function(name, value, build) {
if (typeof name !== "string") {
build = value;
}

this.build(!build);
return this;
};
Expand All @@ -57,11 +57,11 @@ p.removeFragment = function(name, value, build) {
if (typeof name !== "string") {
build = value;
}

this.build(!build);
return this;
};
p.addHash = p.addFragment;
p.removeHash = p.removeFragment;

})(window.URI);
})(window.URI);
Loading