From 0957d02747f89a0c3d21710ff2e8a3f04f097c10 Mon Sep 17 00:00:00 2001 From: Mykhailo Stadnyk Date: Fri, 12 Aug 2016 15:30:00 +0300 Subject: [PATCH] domurl type defs added --- domurl/domurl-tests.ts | 80 ++++++++++++++++++++++++++++++++++++++++++ domurl/domurl.d.ts | 30 ++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 domurl/domurl-tests.ts create mode 100644 domurl/domurl.d.ts diff --git a/domurl/domurl-tests.ts b/domurl/domurl-tests.ts new file mode 100644 index 0000000000..20070b0395 --- /dev/null +++ b/domurl/domurl-tests.ts @@ -0,0 +1,80 @@ +/// + +interface UModel extends QueryString { + a: any; + b: string; +} + +interface U2Model extends QueryString { + a: any; +} + +interface U3Model extends QueryString { + foo: string; +} + +var u = new Url(); // current 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"]); + +// Manipulating 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.toString(); +a.innerHTML = 'test'; +document.body.appendChild(a); + +// Stringify +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 diff --git a/domurl/domurl.d.ts b/domurl/domurl.d.ts new file mode 100644 index 0000000000..ad1051d7fb --- /dev/null +++ b/domurl/domurl.d.ts @@ -0,0 +1,30 @@ +// Type definitions for domurl +// Project: https://github.com/Mikhus/domurl +// Definitions by: Mykhailo Stadnyk +// Definitions: https://github.com/Mikhus/DefinitelyTyped + +interface QueryString { + (qs?: string); + toString(): string; +} + +declare class Url { + constructor(url?: string); + query: T; + protocol: string; + user: string; + pass: string; + host: string; + port: string; + path: string; + hash: string; + href: string; + toString(): string; + encode(s: string): string; + decode(s: string): string; + isAbsolute(): boolean; + paths(paths?: [string]): [string]; + isEmptyQuery(): boolean; + queryLength(): number; + clearQuery(): Url; +}