From 89882db5c0f81d6fb48e2f83d5a89e62fc52b40f Mon Sep 17 00:00:00 2001 From: Chris Charabaruk Date: Tue, 5 May 2015 13:38:42 -0400 Subject: [PATCH 1/3] Added type bindings for jsUri library --- jsuri/jsuri.d.ts | 137 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 jsuri/jsuri.d.ts diff --git a/jsuri/jsuri.d.ts b/jsuri/jsuri.d.ts new file mode 100644 index 0000000000..16f3009527 --- /dev/null +++ b/jsuri/jsuri.d.ts @@ -0,0 +1,137 @@ +// Type definitions for jsUri 1.3+ +// Project: https://github.com/derek-watson/jsUri +// Definitions by: Chris Charabaruk +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module jsuri { + 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: string, 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: string, oldVal?:string): 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; +} From a86a44b28abde7f541663c4af9b855234a690b1e Mon Sep 17 00:00:00 2001 From: Chris Charabaruk Date: Tue, 5 May 2015 14:12:02 -0400 Subject: [PATCH 2/3] Add tests for jsUri --- jsuri/jsuri-tests.ts | 82 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 jsuri/jsuri-tests.ts diff --git a/jsuri/jsuri-tests.ts b/jsuri/jsuri-tests.ts new file mode 100644 index 0000000000..73ecbc50df --- /dev/null +++ b/jsuri/jsuri-tests.ts @@ -0,0 +1,82 @@ +/// + +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/ +} From bf45415511dba471e6ac824b90c9411e9f196b7f Mon Sep 17 00:00:00 2001 From: Chris Charabaruk Date: Tue, 5 May 2015 14:12:22 -0400 Subject: [PATCH 3/3] Update jsUri definitions to pass tests --- jsuri/jsuri.d.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/jsuri/jsuri.d.ts b/jsuri/jsuri.d.ts index 16f3009527..b843abf616 100644 --- a/jsuri/jsuri.d.ts +++ b/jsuri/jsuri.d.ts @@ -4,13 +4,15 @@ // Definitions: https://github.com/borisyankov/DefinitelyTyped declare module jsuri { - class Uri { + type Primitive = string | number | boolean; + + export class Uri { /** * Creates a new Uri object * @constructor * @param {string} str */ - constructor(str: string); + constructor(str?: string); /** * Define getter/setter methods @@ -67,7 +69,7 @@ declare module jsuri { * @param {integer} [index] specific index to add the value at * @return {Uri} returns self for fluent chaining */ - addQueryParam(key: string, val: string, index?: number): Uri; + addQueryParam(key: string, val: Primitive, index?: number): Uri; /** * test for the existence of a query parameter @@ -83,7 +85,7 @@ declare module jsuri { * @param {string} [oldVal] replace only one specific value (otherwise replaces all) * @return {Uri} returns self for fluent chaining */ - replaceQueryParam(key: string, newVal: string, oldVal?:string): Uri; + replaceQueryParam(key: string, newVal: Primitive, oldVal?: Primitive): Uri; /** * Define fluent setter methods (setProtocol, setHasAuthorityPrefix, etc)