From 88b29b7995a27a11dfc241cb236c926e151b0a3e Mon Sep 17 00:00:00 2001 From: Alexey Gorshkov Date: Mon, 11 May 2015 01:51:24 +0300 Subject: [PATCH 1/6] Added jsurl --- jsurl/jsurl-tests.ts | 67 ++++++++++++++++++++++++++++++++++++++++++++ jsurl/jsurl.d.ts | 18 ++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 jsurl/jsurl-tests.ts create mode 100644 jsurl/jsurl.d.ts diff --git a/jsurl/jsurl-tests.ts b/jsurl/jsurl-tests.ts new file mode 100644 index 000000000..f573feb49 --- /dev/null +++ b/jsurl/jsurl-tests.ts @@ -0,0 +1,67 @@ +/// + +var u = new Url; // curent document URL will be used +// or we can instantiate as +var u2 = new Url("http://example.com/some/path?a=b&c=d#someAnchor"); +// it should support relative URLs also +var u3 = new Url("/my/site/doc/path?foo=bar#baz"); + +// get the value of some query string parameter +alert(u2.query.a); +// or +alert(u3.query["foo"]); + +// Manupulating query string parameters +u.query.a = [1, 2, 3]; // adds/replaces in query string params a=1&a=2&a=3 +u.query.b = 'woohoo'; // adds/replaces in query string param b=woohoo + +if (u.query.a instanceof Array) { // the way to add a parameter + u.query.a.push(4); // now it's "a=1&a=2&a=3&a=4&b=woohoo" +} + +else { // if not an array but scalar value here is a way how to convert to array + u.query.a = [u.query.a]; + u.query.a.push(8) +} + + +// The way to remove the parameter: +delete u.query.a +// or: +delete u.query["a"] + +// If you need to remove all query string params: +u.query.clear(); +alert(u); + +// Lookup URL parts: +alert( + 'protocol = ' + u.protocol + '\n' + + 'user = ' + u.user + '\n' + + 'pass = ' + u.pass + '\n' + + 'host = ' + u.host + '\n' + + 'port = ' + u.port + '\n' + + 'path = ' + u.path + '\n' + + 'query = ' + u.query + '\n' + + 'hash = ' + u.hash + ); + +// Manipulating URL parts +u.path = '/some/new/path'; // the way to change URL path +u.protocol = 'https' // the way to force https protocol on the source URL + +// inject into string +var str = 'My Cool Link'; + +// or use in DOM context +var a = document.createElement('a'); +a.href = u; +a.innerHTML = 'test'; +document.body.appendChild(a); + +// Stringify +u += ''; +String(u); +u.toString(); +// NOTE, that usually it will be done automatically, so only in special +// cases direct stringify is required \ No newline at end of file diff --git a/jsurl/jsurl.d.ts b/jsurl/jsurl.d.ts new file mode 100644 index 000000000..a5f790257 --- /dev/null +++ b/jsurl/jsurl.d.ts @@ -0,0 +1,18 @@ +// Type definitions for jsurl 1.2.2 +// Project: https://github.com/Mikhus/jsurl +// Definitions by: Alexey Gorshkov +// Definitions: https://github.com/agorshkov23/DefinitelyTyped + +declare class Url { + constructor(url?: string); + query: any; + protocol: string; + user: string; + pass: string; + host: string; + port: string; + path: string; + hash: string; + href: string; + toString(): string; +} \ No newline at end of file From 14fd30b019df680b7c78b28070f208fa41d05106 Mon Sep 17 00:00:00 2001 From: Alexey Gorshkov Date: Sun, 3 Jan 2016 01:15:27 +0300 Subject: [PATCH 2/6] Updating to version 1.2.7 --- jsurl/jsurl-tests.ts | 16 ++++++++++------ jsurl/jsurl.d.ts | 11 ++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/jsurl/jsurl-tests.ts b/jsurl/jsurl-tests.ts index f573feb49..177399d34 100644 --- a/jsurl/jsurl-tests.ts +++ b/jsurl/jsurl-tests.ts @@ -1,8 +1,12 @@ /// -var u = new Url; // curent document URL will be used +interface U2Model { + a: any; +} + +var u = new Url (); // curent document URL will be used // or we can instantiate as -var u2 = new Url("http://example.com/some/path?a=b&c=d#someAnchor"); +var u2 = new Url("http://example.com/some/path?a=b&c=d#someAnchor"); // it should support relative URLs also var u3 = new Url("/my/site/doc/path?foo=bar#baz"); @@ -55,13 +59,13 @@ var str = 'My Cool Link'; // or use in DOM context var a = document.createElement('a'); -a.href = u; +a.href = u.toString(); a.innerHTML = 'test'; document.body.appendChild(a); // Stringify -u += ''; -String(u); -u.toString(); +var su1 = u + ''; +var su2 = String(u); +var su3 = u.toString(); // NOTE, that usually it will be done automatically, so only in special // cases direct stringify is required \ No newline at end of file diff --git a/jsurl/jsurl.d.ts b/jsurl/jsurl.d.ts index a5f790257..2dbac988f 100644 --- a/jsurl/jsurl.d.ts +++ b/jsurl/jsurl.d.ts @@ -1,11 +1,12 @@ -// Type definitions for jsurl 1.2.2 +// Type definitions for jsurl 1.2.7 // Project: https://github.com/Mikhus/jsurl // Definitions by: Alexey Gorshkov // Definitions: https://github.com/agorshkov23/DefinitelyTyped -declare class Url { - constructor(url?: string); - query: any; +declare class Url { + constructor(); + constructor(url: string); + query: T; protocol: string; user: string; pass: string; @@ -14,5 +15,5 @@ declare class Url { path: string; hash: string; href: string; - toString(): string; + toString: () => string; } \ No newline at end of file From fbc04377c198ed8cd8267642daf30b18038a5190 Mon Sep 17 00:00:00 2001 From: Alexey Gorshkov Date: Sun, 3 Jan 2016 01:20:55 +0300 Subject: [PATCH 3/6] add .travis.yml --- jsurl/.travis.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 jsurl/.travis.yml diff --git a/jsurl/.travis.yml b/jsurl/.travis.yml new file mode 100644 index 000000000..e69de29bb From 6259fa1e5b45fc59859963dd4ac02fee5fdac71a Mon Sep 17 00:00:00 2001 From: Alexey Gorshkov Date: Sun, 3 Jan 2016 01:35:15 +0300 Subject: [PATCH 4/6] remove .travis.yml --- jsurl/.travis.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 jsurl/.travis.yml diff --git a/jsurl/.travis.yml b/jsurl/.travis.yml deleted file mode 100644 index e69de29bb..000000000 From 4de193b13625dce7b03abc9db54b69f7e4d44be3 Mon Sep 17 00:00:00 2001 From: Alexey Gorshkov Date: Sun, 3 Jan 2016 02:13:24 +0300 Subject: [PATCH 5/6] fixed jsurl/jsurl-tests.ts(16,7): error TS7017: Index signature of object type implicitly has an 'any' type. --- jsurl/jsurl-tests.ts | 11 ++++++++--- jsurl/jsurl.d.ts | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/jsurl/jsurl-tests.ts b/jsurl/jsurl-tests.ts index 177399d34..b0297bda0 100644 --- a/jsurl/jsurl-tests.ts +++ b/jsurl/jsurl-tests.ts @@ -1,10 +1,15 @@ /// -interface U2Model { +interface UModel extends UrlQuery { + a: any; + b: string; +} + +interface U2Model extends UrlQuery { a: any; } -var u = new Url (); // curent document URL will be used +var u = new Url(); // curent document URL will be used // or we can instantiate as var u2 = new Url("http://example.com/some/path?a=b&c=d#someAnchor"); // it should support relative URLs also @@ -48,7 +53,7 @@ alert( 'path = ' + u.path + '\n' + 'query = ' + u.query + '\n' + 'hash = ' + u.hash - ); +); // Manipulating URL parts u.path = '/some/new/path'; // the way to change URL path diff --git a/jsurl/jsurl.d.ts b/jsurl/jsurl.d.ts index 2dbac988f..cbe985851 100644 --- a/jsurl/jsurl.d.ts +++ b/jsurl/jsurl.d.ts @@ -3,6 +3,10 @@ // Definitions by: Alexey Gorshkov // Definitions: https://github.com/agorshkov23/DefinitelyTyped +interface UrlQuery { + clear: () => void; +} + declare class Url { constructor(); constructor(url: string); From b59734a434cb167e75f59acad6d7d4705df4c15e Mon Sep 17 00:00:00 2001 From: Alexey Gorshkov Date: Sun, 3 Jan 2016 02:16:42 +0300 Subject: [PATCH 6/6] FIXED: jsurl/jsurl-tests.ts(21,7): error TS7017 --- jsurl/jsurl-tests.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jsurl/jsurl-tests.ts b/jsurl/jsurl-tests.ts index b0297bda0..a10d0a763 100644 --- a/jsurl/jsurl-tests.ts +++ b/jsurl/jsurl-tests.ts @@ -9,11 +9,15 @@ interface U2Model extends UrlQuery { a: any; } +interface U3Model extends UrlQuery { + foo: string; +} + var u = new Url(); // curent document URL will be used // or we can instantiate as var u2 = new Url("http://example.com/some/path?a=b&c=d#someAnchor"); // it should support relative URLs also -var u3 = new Url("/my/site/doc/path?foo=bar#baz"); +var u3 = new Url("/my/site/doc/path?foo=bar#baz"); // get the value of some query string parameter alert(u2.query.a);