From 15dd2b9cf16437f73d833c93ed2c87515fbb0c16 Mon Sep 17 00:00:00 2001 From: Kegan Myers Date: Wed, 26 Feb 2014 15:20:10 -0600 Subject: [PATCH] Modify HttpUrl interface --- README.md | 3 ++ bower.json | 2 +- lib/Typertext/Http/HttpQueryString.ts | 5 +++ lib/Typertext/Http/HttpUrl.ts | 45 +++++++++++++++++++++++++-- package.json | 2 +- 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 lib/Typertext/Http/HttpQueryString.ts diff --git a/README.md b/README.md index 5febf91..18de4b7 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,9 @@ Todo Changelog --------- +####0.2.0 +- Interface to decode QueryStrings +- Interface to create HttpUrl from string ####0.1.2 Bower support ####0.1.1 diff --git a/bower.json b/bower.json index 3f2c90e..ea0d27f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "Typertext", - "version": "0.1.2", + "version": "0.2.0", "homepage": "https://github.com/terribleplan/Typertext", "authors": [ "Kegan Myers " diff --git a/lib/Typertext/Http/HttpQueryString.ts b/lib/Typertext/Http/HttpQueryString.ts new file mode 100644 index 0000000..c06b7ee --- /dev/null +++ b/lib/Typertext/Http/HttpQueryString.ts @@ -0,0 +1,5 @@ +module Typertext.Http { + export interface HttpQueryString { + [index:string]:string + } +} \ No newline at end of file diff --git a/lib/Typertext/Http/HttpUrl.ts b/lib/Typertext/Http/HttpUrl.ts index 15dac56..0ee6087 100644 --- a/lib/Typertext/Http/HttpUrl.ts +++ b/lib/Typertext/Http/HttpUrl.ts @@ -12,9 +12,37 @@ module Typertext.Http { return ((protocol == HttpProtocol.http) ? 80 : 443) } - public static EncodeQueryString(query:{ - [index:string]:string - }) { + public static FromUrl(location:string):HttpUrl { + var l = document.createElement("a"); + l.href = location; + return new HttpUrl(l.hostname, HttpProtocol[l.protocol], l.pathname, HttpUrl.DecodeQueryString(l.search)) + } + + public static DecodeQueryString(queryString:string):HttpQueryString { + var returnValue = {}; + if (queryString.length == 0 || queryString == "?") { + return returnValue; + } + + if (queryString.indexOf("?") == 0) { + queryString = queryString.substring(1); + } + + var params:string[] = HttpUrl.splitString(queryString, "&"); + for (var i:number = 0; i < params.length; i++) { + var param = HttpUrl.splitString(params[i], "=", 2); + if (param.length == 1) { + returnValue[param[0]] = ""; + continue; + } + + returnValue[param[0]] = param[1]; + } + + return returnValue; + } + + public static EncodeQueryString(query:HttpQueryString) { var rs = "?" + HttpUrl.URLEncodeObject(query); return ((rs.length == 1) ? "" : rs); } @@ -32,6 +60,17 @@ module Typertext.Http { return rs.slice(0, -1); } + private static splitString(input:string, separator:string, limit:number = 0):string[] { + limit++; + var chunks:string[] = input.split(separator); + if (limit > 0 && chunks.length > limit) { + var ret = chunks.splice(0, limit); + ret.push(chunks.join(separator)); + return ret; + } + return chunks; + } + constructor(domain:string, protocol:HttpProtocol = HttpProtocol.http, path:string = "/", queryString:{ [index:string]:string } = {}, port:number = 0) { diff --git a/package.json b/package.json index 4e7490d..89b268e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "type" : "git", "url" : "https://github.com/terribleplan/Typertext.git" }, - "version": "0.1.2", + "version": "0.2.0", "devDependencies": { "grunt": "~0.4.2", "grunt-typescript": "~0.2.7"