Modify HttpUrl interface

This commit is contained in:
Kegan Myers 2014-02-26 15:20:10 -06:00
parent 7f812636c3
commit 15dd2b9cf1
5 changed files with 52 additions and 5 deletions

View file

@ -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

View file

@ -1,6 +1,6 @@
{
"name": "Typertext",
"version": "0.1.2",
"version": "0.2.0",
"homepage": "https://github.com/terribleplan/Typertext",
"authors": [
"Kegan Myers <kegan@keganmyers.com>"

View file

@ -0,0 +1,5 @@
module Typertext.Http {
export interface HttpQueryString {
[index:string]:string
}
}

View file

@ -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) {

View file

@ -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"