Merge pull request #4270 from coldacid/jsuri

jsUri definitions
This commit is contained in:
Masahiro Wakame
2015-05-06 23:42:14 +09:00
2 changed files with 221 additions and 0 deletions

82
jsuri/jsuri-tests.ts Normal file
View File

@@ -0,0 +1,82 @@
/// <reference path="jsuri.d.ts" />
function test_createUri() {
var uri = new jsuri.Uri('http://user:pass@www.test.com:81/index.html?q=books#fragment');
var getTests = {
protocol: 'http' == uri.protocol(),
userInfo: 'user:pass' == uri.userInfo(),
host: 'www.test.com' == uri.host(),
port: 81 == uri.port(),
path: '/index.html' == uri.path(),
query: 'q=books' == uri.query(),
anchor: 'fragment' == uri.anchor()
};
uri.protocol('https');
var setProtocolTest = uri.toString() == 'https://user:pass@www.test.com:81/index.html?q=books#fragment';
uri.host('mydomain.com');
var setHostTest = uri.toString() == 'https://user:pass@mydomain.com:81/index.html?q=books#fragment';
}
function test_chainable() {
new jsuri.Uri()
.setPath('/archives/1979/')
.setQuery('?page=1'); // /archives/1979?page=1
new jsuri.Uri()
.setPath('/index.html')
.setAnchor('content')
.setHost('www.test.com')
.setPort(8080)
.setUserInfo('username:password')
.setProtocol('https')
.setQuery('this=that&some=thing'); // https://username:password@www.test.com:8080/index.html?this=that&some=thing#content
new jsuri.Uri('http://www.test.com')
.setHost('www.yahoo.com')
.setProtocol('https'); // https://www.yahoo.com
}
function test_queryParams() {
new jsuri.Uri('?cat=1&cat=2&cat=3').getQueryParamValue('cat'); // 1
new jsuri.Uri('?cat=1&cat=2&cat=3').getQueryParamValues('cat'); // [1, 2, 3]
new jsuri.Uri().addQueryParam('q', 'books'); // ?q=books
new jsuri.Uri('http://www.github.com')
.addQueryParam('testing', '123')
.addQueryParam('one', 1); // http://www.github.com/?testing=123&one=1
// insert param at index 0
new jsuri.Uri('?b=2&c=3&d=4').addQueryParam('a', '1', 0); // ?a=1&b=2&c=3&d=4
new jsuri.Uri().replaceQueryParam('page', 2); // ?page=2
new jsuri.Uri('?a=1&b=2&c=3')
.replaceQueryParam('a', 'eh'); // ?a=eh&b=2&c=3
new jsuri.Uri('?a=1&b=2&c=3&c=4&c=5&c=6')
.replaceQueryParam('c', 'five', '5'); // ?a=1&b=2&c=3&c=4&c=five&c=6
new jsuri.Uri('?a=1&b=2&c=3')
.deleteQueryParam('a'); // ?b=2&c=3
new jsuri.Uri('test.com?a=1&b=2&c=3&a=eh')
.deleteQueryParam('a', 'eh'); // test.com/?a=1&b=2&c=3
new jsuri.Uri('?a=1&b=2&c=3')
.hasQueryParam('a'); // true
new jsuri.Uri('?a=1&b=2&c=3')
.hasQueryParam('d'); // false
}
function test_clone() {
var baseUri = new jsuri.Uri('http://localhost/');
baseUri.clone().setProtocol('https'); // https://localhost/
baseUri; // http://localhost/
}

139
jsuri/jsuri.d.ts vendored Normal file
View File

@@ -0,0 +1,139 @@
// Type definitions for jsUri 1.3+
// Project: https://github.com/derek-watson/jsUri
// Definitions by: Chris Charabaruk <http://github.com/coldacid>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module jsuri {
type Primitive = string | number | boolean;
export class Uri {
/**
* Creates a new Uri object
* @constructor
* @param {string} str
*/
constructor(str?: string);
/**
* Define getter/setter methods
*/
protocol(val?: string): string;
userInfo(val?: string): string;
host(val?: string): string;
port(val?: number): number;
path(val?: string): string;
anchor(val?: string): string;
/**
* if there is no protocol, the leading // can be enabled or disabled
* @param {Boolean} val
* @return {Boolean}
*/
hasAuthorityPrefix(val?: boolean): boolean;
isColonUri(val?: boolean): boolean;
/**
* Serializes the internal state of the query pairs
* @param {string} [val] set a new query string
* @return {string} query string
*/
query(val?: string): string;
/**
* returns the first query param value found for the key
* @param {string} key query key
* @return {string} first value found for key
*/
getQueryParamValue(key: string): string;
/**
* returns an array of query param values for the key
* @param {string} key query key
* @return {array} array of values
*/
getQueryParamValues(key: string): string[];
/**
* removes query parameters
* @param {string} key remove values for key
* @param {val} [val] remove a specific value, otherwise removes all
* @return {Uri} returns self for fluent chaining
*/
deleteQueryParam(key: string, val?: string): Uri;
/**
* adds a query parameter
* @param {string} key add values for key
* @param {string} val value to add
* @param {integer} [index] specific index to add the value at
* @return {Uri} returns self for fluent chaining
*/
addQueryParam(key: string, val: Primitive, index?: number): Uri;
/**
* test for the existence of a query parameter
* @param {string} key check values for key
* @return {Boolean} true if key exists, otherwise false
*/
hasQueryParam(key: string): boolean;
/**
* replaces query param values
* @param {string} key key to replace value for
* @param {string} newVal new value
* @param {string} [oldVal] replace only one specific value (otherwise replaces all)
* @return {Uri} returns self for fluent chaining
*/
replaceQueryParam(key: string, newVal: Primitive, oldVal?: Primitive): Uri;
/**
* Define fluent setter methods (setProtocol, setHasAuthorityPrefix, etc)
*/
setProtocol(val: string): Uri;
setHasAuthorityPrefix(val: boolean): Uri;
setIsColonUri(val: boolean): Uri;
setUserInfo(val: string): Uri;
setHost(val: string): Uri;
setPort(val: number): Uri;
setPath(val: string): Uri;
setQuery(val: string): Uri;
setAnchor(val: string): Uri;
/**
* Scheme name, colon and doubleslash, as required
* @return {string} http:// or possibly just //
*/
scheme(): string;
/**
* Same as Mozilla nsIURI.prePath
* @return {string} scheme://user:password@host:port
* @see https://developer.mozilla.org/en/nsIURI
*/
origin(): string;
/**
* Adds a trailing slash to the path
*/
addTrailingSlash(): Uri;
/**
* Serializes the internal state of the Uri object
* @return {string}
*/
toString(): string;
/**
* Clone a Uri object
* @return {Uri} duplicate copy of the Uri
*/
clone(): Uri;
}
}
declare var Uri : jsuri.Uri;
declare module 'jsuri' {
export = Uri;
}