-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra.js
117 lines (117 loc) · 3.49 KB
/
extra.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
function extra()
{
var self=this;
this.cascade=function(methods, callback)
{
var index=0,
hasCallback=false;
if (typeof callback=="function") hasCallback=true;
if (!methods || !methods.length)
if (hasCallback) callback("cascade:no_methods");
else
{
console.log("cascade:no_methods");
return;
}
var next=function()
{
index++;
if (typeof arguments[0]!="undefined" && arguments[0]!=null)
{
if (hasCallback) callback(arguments[0]);
}
else if (index<methods.length) run.apply(null, arguments);
else if (hasCallback) callback.apply(null, arguments);
}
var run=function()
{
arguments[0]=next;
if (typeof methods[index]=="function")
{
var args=[];
for (var i in arguments) args.push(arguments[i]);
methods[index].apply(null, args);
}
}
run();
}
this.cookies={
parse: (input)=>
{
if (!input) return {};
let list=input.split(';');
let output={};
for (item of list)
{
matches=item.match(/ ?(.*?)=(.*?)(;|$)/);
if (matches && matches.length>=4) output[matches[1]]=matches[2];
}
return output;
},
stringify: (cookies)=>
{
let list=[];
for (key in cookies) list.push(`${key}=${cookies[key]}`);
return list.join(';');
}
}
this.bindProperties=function(firstObject, firstProperty, secondObject, secondProperty)
{
if (typeof firstObject!="object" || typeof secondObject!="object" ||
typeof firstProperty!="string" || typeof secondProperty!="string")
{
console.log("invalid input for bindProperties method");
return;
}
var getProtoDesc=function(object, property)
{
var output=
{
prototype: object,
descriptor: null
};
output.descriptor=Object.getOwnPropertyDescriptor(output.prototype, property);
if (output.descriptor) return output;
else return getProtoDesc(Object.getPrototypeOf(output.prototype), property);
}
var bind=function(protoDesc, oldProtoDesc, property, otherProtoDesc, otherObject, otherProperty)
{
if (protoDesc.descriptor.writable)
{
delete protoDesc.descriptor.writable;
protoDesc.ownValue=protoDesc.descriptor.value;
delete protoDesc.descriptor.value;
protoDesc.descriptor.get=function()
{
return protoDesc.ownValue;
}
}
protoDesc.descriptor.configurable=true;
protoDesc.descriptor.set=function()
{
console.log(arguments[0]);
if (typeof protoDesc.ownValue!="undefined") protoDesc.ownValue=arguments[0];
if (!protoDesc.noBind)
{
otherProtoDesc.noBind=1;
otherObject[otherProperty]=arguments[0];
}
else protoDesc.noBind=0;
if (typeof oldProtoDesc.descriptor.set=="function") oldProtoDesc.descriptor.set.apply(this, arguments);
}
Object.defineProperty(protoDesc.prototype, property, protoDesc.descriptor);
}
var firstOldProtoDesc=getProtoDesc(firstObject, firstProperty),
secondOldProtoDesc=getProtoDesc(secondObject, secondProperty),
firstProtoDesc=getProtoDesc(firstObject, firstProperty),
secondProtoDesc=getProtoDesc(secondObject, secondProperty);
console.log(firstProtoDesc.descriptor.set);
console.log(secondProtoDesc.descriptor.set);
bind(firstProtoDesc, firstOldProtoDesc, firstProperty, secondProtoDesc, secondObject, secondProperty);
bind(secondProtoDesc, secondOldProtoDesc, secondProperty, firstProtoDesc, firstObject, firstProperty);
console.log(firstProtoDesc.descriptor.set);
console.log(secondProtoDesc.descriptor.set);
console.log("cheese");
}
}
if (typeof module!="undefined" && module.exports) module.exports=new extra();