Skip to content

Commit fdff2df

Browse files
committed
Merge pull request #14 from FGRibreau/gh-pages
Support for server-side JS
2 parents fc2b5a3 + f06773a commit fdff2df

6 files changed

Lines changed: 218 additions & 183 deletions

File tree

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var url = "http://example.org/foo?bar=baz",
1414
url += separator + encodeURIComponent("foo") + "=" + encodeURIComponent("bar");
1515
```
1616

17-
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.
17+
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.
1818

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

@@ -31,17 +31,17 @@ URI.js is here to help with that.
3131
```javascript
3232
// mutating URLs
3333
URI("http://example.org/foo.html?hello=world")
34-
.username("rodneyrehm")
34+
.username("rodneyrehm")
3535
// -> http://rodneyrehm@example.org/foo.html?hello=world
36-
.username("")
36+
.username("")
3737
// -> http://example.org/foo.html?hello=world
3838
.directory("bar")
3939
// -> http://example.org/bar/foo.html?hello=world
40-
.suffix("xml")
40+
.suffix("xml")
4141
// -> http://example.org/bar/foo.xml?hello=world
42-
.query("")
42+
.query("")
4343
// -> http://example.org/bar/foo.xml
44-
.tld("com")
44+
.tld("com")
4545
// -> http://example.com/bar/foo.xml
4646
.query({ foo: "bar", hello: ["world", "mars"] });
4747
// -> http://example.com/bar/foo.xml?foo=bar&hello=world&hello=mars
@@ -65,6 +65,22 @@ URI("/foo/bar/baz.html")
6565

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

68+
## npm ##
69+
70+
```
71+
npm install URIjs
72+
```
73+
74+
75+
## Server-side JS ##
76+
77+
```javascript
78+
var URI = require('URIJS');
79+
80+
URI("/foo/bar/baz.html")
81+
.relativeTo("/foo/bar/sub/world.html")
82+
// -> ../baz.html
83+
```
6884

6985
## Minify ##
7086

@@ -77,7 +93,7 @@ use [Google Closure Compiler](http://closure-compiler.appspot.com/home):
7793
// @code_url http://medialize.github.com/URI.js/src/IPv6.js
7894
// @code_url http://medialize.github.com/URI.js/src/punycode.js
7995
// @code_url http://medialize.github.com/URI.js/src/URI.js
80-
// ==/ClosureCompiler==
96+
// ==/ClosureCompiler==
8197
```
8298

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

153169
```
154170
Quote from java doc:
155-
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.
171+
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.
156172
```
157173

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

184200
### 1.3.0 ###
185201

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "URI.js",
2+
"name": "URIjs",
33
"version": "1.4.1",
44
"title": "URI.js - Mutating URLs",
55
"author": {
@@ -14,7 +14,7 @@
1414
{
1515
"type": "GPL",
1616
"url": "http://opensource.org/licenses/GPL-3.0"
17-
}
17+
}
1818
],
1919
"description": "URI.js is a Javascript library for working with URLs.",
2020
"keywords": [
@@ -26,11 +26,14 @@
2626
"unified resource identifier",
2727
"query string"
2828
],
29+
"main": "./src/URI",
2930
"homepage": "http://medialize.github.com/URI.js/",
30-
"contributors": [],
31+
"contributors": [
32+
"Francois-Guillaume Ribreau <npm@fgribreau.com> (http://fgribreau.com)"
33+
],
3134
"files": [
3235
"src/URI.js",
3336
"src/IPv6.js",
3437
"src/punycode.js"
3538
]
36-
}
39+
}

src/IPv6.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
(function(undefined){
22

3+
var global = typeof module !== 'undefined' && module.exports ?
4+
module.exports
5+
: window
36

47

58
var best = function(address) {
69
// based on:
7-
// Javascript to test an IPv6 address for proper format, and to
10+
// Javascript to test an IPv6 address for proper format, and to
811
// present the "best text representation" according to IETF Draft RFC at
912
// http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-04
1013
// 8 Feb 2010 Rich Brown, Dartware, LLC
11-
// Please feel free to use this code as long as you provide a link to
14+
// Please feel free to use this code as long as you provide a link to
1215
// http://www.intermapper.com
1316
// http://intermapper.com/support/tools/IPV6-Validator.aspx
1417
// http://download.dartware.com/thirdparty/ipv6validator.js
15-
18+
1619
var _address = address.toLowerCase(),
1720
segments = _address.split(':'),
1821
length = segments.length,
1922
total = 8;
20-
23+
2124
// trim colons (:: or ::a:b:c… or …a:b:c::)
2225
if (segments[0] === '' && segments[1] === '' && segments[2] === '') {
2326
// must have been ::
2427
// remove first two items
25-
segments.shift();
28+
segments.shift();
2629
segments.shift();
2730
} else if (segments[0] === '' && segments[1] === '') {
2831
// must have been ::xxxx
@@ -31,33 +34,33 @@ var best = function(address) {
3134
} else if (segments[length - 1] === '' && segments[length - 2] === '') {
3235
// must have been xxxx::
3336
segments.pop();
34-
}
35-
37+
}
38+
3639
length = segments.length;
37-
40+
3841
// adjust total segments for IPv4 trailer
3942
if (segments[length - 1].indexOf('.') !== -1) {
4043
// found a "." which means IPv4
4144
total = 7;
4245
}
43-
46+
4447
// fill empty segments them with "0000"
4548
var pos;
4649
for (pos = 0; pos < length; pos++) {
4750
if (segments[pos] === '') {
4851
break;
4952
}
5053
}
51-
54+
5255
if (pos < total) {
5356
segments.splice(pos, 1, '0000');
5457
while (segments.length < total) {
5558
segments.splice(pos, 0, '0000');
5659
}
57-
60+
5861
length = segments.length;
5962
}
60-
63+
6164
// strip leading zeros
6265
var _segments;
6366
for (var i = 0; i < total; i++) {
@@ -69,10 +72,10 @@ var best = function(address) {
6972
break;
7073
}
7174
}
72-
75+
7376
segments[i] = _segments.join("");
7477
}
75-
78+
7679
// find longest sequence of zeroes and coalesce them into one segment
7780
var best = -1,
7881
_best = 0,
@@ -87,46 +90,46 @@ var best = function(address) {
8790
_current += 1;
8891
} else {
8992
inzeroes = false;
90-
if (_current > _best) {
91-
best = current;
93+
if (_current > _best) {
94+
best = current;
9295
_best = _current;
9396
}
9497
}
9598
} else {
9699
if (segments[i] == '0') {
97-
inzeroes = true;
98-
current = i;
99-
_current = 1;
100+
inzeroes = true;
101+
current = i;
102+
_current = 1;
100103
}
101104
}
102105
}
103-
106+
104107
if (_current > _best) {
105-
best = current;
108+
best = current;
106109
_best = _current;
107110
}
108111

109112
if (_best > 1) {
110113
segments.splice(best, _best, "");
111114
}
112-
115+
113116
length = segments.length;
114-
117+
115118
// assemble remaining segments
116119
var result = '';
117120
if (segments[0] === '') {
118121
beststr = ":";
119122
}
120-
123+
121124
for (i = 0; i < length; i++) {
122125
result += segments[i];
123126
if (i === length - 1) {
124127
break;
125128
}
126-
129+
127130
result += ':';
128131
}
129-
132+
130133
if (segments[length - 1] === '') {
131134
result += ":";
132135
}
@@ -135,14 +138,15 @@ var best = function(address) {
135138
};
136139

137140

138-
window.IPv6 = {
141+
global.IPv6 = {
139142
best: best
140143
};
141144
})();
145+
142146
/*
143147
var _in = "fe80:0000:0000:0000:0204:61ff:fe9d:f156",
144148
_out = IPv6.best(_in),
145149
_expected = "fe80::204:61ff:fe9d:f156";
146-
150+
147151
console.log(_in, _out, _expected, _out === _expected);
148-
*/
152+
*/

src/URI.fragmentQuery.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

@@ -46,7 +46,7 @@ p.addFragment = function(name, value, build) {
4646
if (typeof name !== "string") {
4747
build = value;
4848
}
49-
49+
5050
this.build(!build);
5151
return this;
5252
};
@@ -57,11 +57,11 @@ p.removeFragment = function(name, value, build) {
5757
if (typeof name !== "string") {
5858
build = value;
5959
}
60-
60+
6161
this.build(!build);
6262
return this;
6363
};
6464
p.addHash = p.addFragment;
6565
p.removeHash = p.removeFragment;
6666

67-
})(window.URI);
67+
})(window.URI);

0 commit comments

Comments
 (0)