-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookies-WithDynamic.hx
68 lines (61 loc) · 1.96 KB
/
Cookies-WithDynamic.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package;
import haxe.ds.StringMap;
import js.Browser;
import js.Cookie;
/**
* ...
* @author Michael
*/
@:expose
class Cookies
{
static var cookies:Dynamic = {};
static function main()
{
Cookie.set("complex", "a=b,b=c");
var cookiesMap:StringMap<String> = Cookie.all();
for (name in cookiesMap.keys()) {
var subValues:Array<String> = cookiesMap.get(name).split(",");
if (subValues.length > 1) {
Reflect.setField(cookies, name, { } );
for (pair in subValues) {
Reflect.setField(Reflect.getProperty(cookies,name),pair.split("=")[0], pair.split("=")[1]);
}
}else {
Reflect.setField(cookies,name, subValues[0]);
}
}
}
/* static function set(name:String, value:String, ?expiry:Int, ?path:String, ?domain:String) {
Cookie.set(name, value, expiry, path, domain);
}*/
static function set(name:String, subName:String, value:String, ?expiry:Int, ?path:String, ?domain:String) {
if (Cookie.exists(name)) {
if (Reflect.hasField(Reflect.getProperty(cookies, name), subName)) {
Reflect.setField(Reflect.getProperty(cookies, name), subName, value);
Cookie.set(name, cookieToString(name), expiry, path, domain);
}else {
trace('no have sub cookie named \'$subName\'');
}
}else {
trace('no have cookie named \'$name\'');
}
}
static private function cookieToString(name:String):String {
var value:String = "";
var subCookieObject = Reflect.getProperty(cookies, name);
var subCookie:Array<String> = Reflect.fields(subCookieObject);
for (i in 0...(subCookie.length - 1)) {
value += subCookie[i] + "=" + Reflect.getProperty(subCookieObject, subCookie[i]) + ",";
}
var lastcell:Int = subCookie.length - 1;
value += subCookie[lastcell] + "=" + Reflect.getProperty(subCookieObject, subCookie[lastcell]);
return value;
}
static function remove(name:String, ?path:String, ?domain:String) {
Cookie.remove(name, path, domain);
}
static function alert() {
Browser.alert(cookies);
}
}