Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
I always want to shoot myself in the head when looking at code like the following:
var url = "http://example.org/foo?bar=baz";
var separator = url.indexOf('?') > -1 ? '&' : '?';
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 as a native URL management library. But it relies on the DOM element <a>, it's slow and doesn't offer any convenience at all.
How about a nice, clean and simple API for mutating URIs:
var url = new URI("http://example.org/foo?bar=baz");
url.addQuery("foo", "bar");
URI.js is here to help with that.
// mutating URLs
URI("http://example.org/foo.html?hello=world")
.username("rodneyrehm")
// -> http://rodneyrehm@example.org/foo.html?hello=world
.username("")
// -> http://example.org/foo.html?hello=world
.directory("bar")
// -> http://example.org/bar/foo.html?hello=world
.suffix("xml")
// -> http://example.org/bar/foo.xml?hello=world
.query("")
// -> http://example.org/bar/foo.xml
.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
// cleaning things up
URI("?&foo=bar&&foo=bar&foo=baz&")
.normalizeQuery();
// -> ?foo=bar&foo=baz
// working with relative paths
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/world.html");
// -> ./baz.html
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/sub/world.html")
// -> ../baz.html
.absoluteTo("/foo/bar/sub/world.html");
// -> /foo/bar/baz.html
// URI Templates
URI.expand("/foo/{dir}/{file}", {
dir: "bar",
file: "world.html"
});
// -> /foo/bar/world.html
See the About Page and API Docs for more stuff.
URI.js (without plugins) has a gzipped weight of about 7KB - if you include all extensions you end up at about 13KB. So unless you need second level domain support and use URI templates, we suggest you don't include them in your build. If you don't need a full featured URI mangler, it may be worth looking into the much smaller parser-only alternatives listed below.
URI.js is available through npm, bower, Jam, spm and manually from the build page:
# using bower
bower install uri.js
# using Jam
jam install URIjs
# using npm
npm install URIjs
# using spm
spm install urijs
I guess you'll manage to use the build tool or follow the instructions below to combine and minify the various files into URI.min.js - and I'm fairly certain you know how to <script src=".../URI.min.js"></script>
that sucker, too.
Install with npm install URIjs
or add "URIjs"
to the dependencies in your package.json
.
// load URI.js
var URI = require('URIjs');
// load an optional module (e.g. URITemplate)
var URITemplate = require('URIjs/src/URITemplate');
URI("/foo/bar/baz.html")
.relativeTo("/foo/bar/sub/world.html")
// -> ../baz.html
Clone the URI.js repository or use a package manager to get URI.js into your project.
require.config({
paths: {
URIjs: 'where-you-put-uri.js/src'
}
});
require(['URIjs/URI'], function(URI) {
console.log("URI.js and dependencies: ", URI("//amazon.co.uk").is('sld') ? 'loaded' : 'failed');
});
require(['URIjs/URITemplate'], function(URITemplate) {
console.log("URITemplate.js and dependencies: ", URITemplate._cache ? 'loaded' : 'failed');
});
See the build tool or use Google Closure Compiler:
// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name URI.min.js
// @code_url http://medialize.github.io/URI.js/src/IPv6.js
// @code_url http://medialize.github.io/URI.js/src/punycode.js
// @code_url http://medialize.github.io/URI.js/src/SecondLevelDomains.js
// @code_url http://medialize.github.io/URI.js/src/URI.js
// @code_url http://medialize.github.io/URI.js/src/URITemplate.js
// ==/ClosureCompiler==
Documents specifying how URLs work:
mailto:
URL SchemeInformal stuff
How other environments do things
window.URL
constructor for NodeIf you don't like URI.js, you may like one of the following libraries. (If yours is not listed, drop me a line…)
URI.js is published under the MIT license. Until version 1.13.2 URI.js was also published under the GPL v3 license - but as this dual-licensing causes more questions than helps anyone, it was dropped with version 1.14.0.
URI()
to match behavior of new URI()
(caused by #196) - Issue #205URI.removeQuery()
to accept RegExp for name and value arguments - (Issue #204, peterwillis)URI(undefined)
to throw TypeError - (Issue #189, Issue #196, eakron) - tiny backward-compatibility-break.absoluteTo()
- (Issue #200, giltayar).pathname()
to properly en/decode URN paths - (Issue #201, mlefoster).normalize()
and .normalizePath()
to properly normalize URN pathsURI.encodeUrnPathSegment()
URI.decodeUrnPathSegment()
URI.decodeUrnPath()
URI.recodeUrnPath()
URI.parseHost()
to not interpret colon in path as IPv6 hostname - (Issue #190)bower.json
.watch()
interfering with .parseQuery()
- (Issue #169)addQuery()
to not throw error on null value - (Issue #171)<audio src="…">
and <video src="…">
to supported DOM attributes - (Issue #160), (Issue #161)file://hostname/path
parsing - (Issue #158).decodePathSegment()
to not throw malformed URI error - (Issue #163)'use strict';
everywhereURI.parseHost()
and URI.buildHost()
to properly parse and build the IPv6 examples given in RFC2732 Format for Literal IPv6 Addresses in URL's - (Issue #144).encodeQuery()
and .decodeQuery()
to respect URI.escapeQuerySpace
- (Issue #137).absoluteTo()
to comply with RFC3986 Reference Resolution Examples - (Issue #113).normalizePath()
to maintain leading parent references (../
) for relative paths, while removing them for absolute paths - (Issue #133)URI.protocol_expression
to properly accept .
in compliance with RFC 3986 - Scheme - (Issue #132)URI.withinString()
to not use backtracking prone regular expression URI.find_uri_expression
anymore - (Issue #131)URI.withinString()
to accept options ignore
and ignoreHtml
to allow better control over which detected URLs get handled - (Issue #117)URI.withinString()
to accept option start
to specify the RegExp used for finding the beginning of an URL (defaults to /\b(?:([a-z][a-z0-9.+-]*:\/\/)|www\.)/gi
) - (Issue #115)fixing unsafe eval by using UMD's root
- (Issue #107).relativeTo()
results caused by inconsistent URI component handling - (Issue #103).segment()
to allow appending an empty element - (Issue #106).segment()
to collapse empty elements in array notation.segmentCoded()
to provide en/decoding interface to .segment()
- (Issue #79).relativeTo()
results - (Issue #78, Issue #95)URI.parse()
and relativeTo()
- (Issue #100)URI.escapeQuerySpace
to control if query string should escape spaces using +
or %20
- (Issue #74)strictEncodeURIComponent()
to work in Firefox 3.6 - (Issue #91).normalizePath()
to properly resolve /.
and /.//
to /
- (Issue #97).path()
to return empty string if there is no path - (Issue #82)URI.decodeQuery()
on malformed input - now returns original undecoded data - (Issue #87, Issue #92)URI.noConflict()
- (Issue #84).readable()
to decode the hash value as well - (Issue #90)jquery.URI.js
from temporarily using window.location
as the href
of an empty attribute of a DOM element - (Issue #94)getType()
for IE8 with undefined value - (Issue #96)jquery.URI.js
to use new DOM element infrastructurerelativeTo()
- (Issue #75)normalizePath()
to not prepend ./
to relative paths - (Issue #76)absoluteTo()
to properly resolve relative scheme - (Issue #71)hasQuery()
- (Issue #71)jquery.URI.js
) - (Issue #69)URI(location)
.setQuery()
- (Issue #64).query()
.fragmentPrefix()
to configure prefix of fragmentURI and fragmentQuery extensions - (Issue #55).toString()
, .valueOf()
and .href()
- (Issue #56).relativeTo()
for descendants - (Issue #57).resource()
as compound of [path, query, fragment].duplicateQueryParameters()
to control if key=value
duplicates have to be preserved or reduced (Issue #51).addQuery("empty")
to properly add ?empty
- (Issue #46)http://username:pass:word@hostname
file://C:/WINDOWS/foo.txt
URI(location)
to properly parse the URL - (Issue #52)Note: QUnit seems to be having some difficulties on IE8. While the jQuery-plugin tests fail, the plugin itself works. We're still trying to figure out what's making QUnit "lose its config state".
/wiki/Help:IPA
- (Issue #49)strictEncodeURIComponent()
to properly encode *
to %2A
img.href
being available - (Issue #48).tld()
- foot.se
would detect t.se
- (Issue #42).absoluteTo()
to comply with RFC 3986 Section 5.2.2 - (Issue #41)location
not being available in non-browser environments like node.js (Issue #45 grimen).segment()
's append operation - (Issue #39)base
- (Issue #33 LarryBattle).segment()
accessor - (Issue #34)URI.encode()
to strict URI encoding according to RFC3986URI.encodeReserved()
to exclude reserved characters (according to RFC3986) from being encodedURITemplate()
.absoluteTo()
to join two relative paths properly - (Issue #29).clone()
to copy an URI instance.directory()
now returns empty string if there is no directory.absoluteTo()
to join two relative paths properly - (Issue #29)javascript:
, mailto:
, ...) support.scheme()
as alias of .protocol()
.userinfo()
to comply with terminology of RFC 3986src/jquery.URI.js
URI.iso8859()
and URI.unicode()
to switch base charsets - (Issue #10, mortenn).iso8859()
and .unicode()
to convert an URI's escape encoding.hostname()
.subdomain()
convenience accessorURI("http://example.org").query(true)
- (Issue #6).equals()
for URL comparison.pathname()
, .directory()
, .filename()
and .suffix()
according to RFC 3986 3.3+
according to application/x-www-form-urlencodedURI.buildQuery()
to build duplicate key=value combinationsURI(string, string)
constructor to conform with the specification.readable()
for humanly readable representation of encoded URIsURI.withinString()
.normalizeProtocol()
to lowercase protocols.normalizeHostname()
to lowercase hostnames{foo: null}
Algorithm for collecting URL parameters{foo: null, bar: ""}
to "?foo&bar=" Algorithm for serializing URL parameters1.15.1 (April 5th 2015) ###
URI()
to match behavior of new URI()
(caused by #196) - Issue #205URI.removeQuery()
to accept RegExp for name and value arguments - (Issue #204, peterwillis)FAQs
package renamed to , please update dependencies!
The npm package URIjs receives a total of 46,283 weekly downloads. As such, URIjs popularity was classified as popular.
We found that URIjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.