forked from letsgetrandy/brototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brototype.js
155 lines (140 loc) · 4.37 KB
/
brototype.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*global exports:true, module:true, window:true, require:false, define:false*/
(function() {
'use strict';
// Bromise... it's stronger than a Promise
function Bromise(object, method, args) {
this.object = object;
this.method = method;
this.args = args.length > 1 ? args.slice(1) : [];
}
Bromise.brototype = Bromise.prototype = {
"butWhenIdo": function(callback, context) {
if (this.method instanceof Function) {
var returnValue = this.method.apply(this.object, this.args);
if (returnValue) {
(callback || function(){}).call(context || this.object, returnValue);
}
}
return context;
},
"hereComeTheErrors": function(callback) {
if (this.method instanceof Function) {
try {
this.method.apply(this.object, this.args);
} catch(e) {
callback(e);
}
} else {
callback(this.method + ' is not a function.');
}
},
"errorsAreComing": function () {
this.hereComeTheErrors.apply(this, arguments);
}
};
function Bro(obj) {
if (this instanceof Bro) {
this.obj = obj;
} else {
return new Bro(obj);
}
}
Bro.TOTALLY = true;
Bro.NOWAY = false;
Bro.brototype = Bro.prototype = {
"isThatEvenAThing": function() {
return this.obj !== void 0;
},
"doYouEven": function(key, options) {
var optionsBro = Bro(options || {}),
bro = this.iCanHaz(key);
if (Bro(bro).isThatEvenAThing() === Bro.TOTALLY) {
optionsBro.iDontAlways('forSure').butWhenIdo();
return Bro.TOTALLY;
} else {
optionsBro.iDontAlways('sorryBro').butWhenIdo();
return Bro.NOWAY;
}
},
"iCanHaz": function(key) {
if (Array.isArray(key)) {
var index, value, result = [];
for (index in key) {
if (value = this.iCanHaz(key[index])) {
result.push(value);
}
}
return result;
}
var props = key.split('.'),
item = this.obj;
for (var i = 0; i < props.length; i++) {
item = item[props[i]];
if (Bro(item).isThatEvenAThing() === Bro.NOWAY) {
return item;
}
}
return item;
},
"comeAtMe": function(brobject) {
var i, prop,
bro = Bro(brobject),
keys = bro.giveMeProps(),
obj = (this instanceof Bro) ? this.obj : Bro.prototype;
for (i = 0; i < keys.length; i++) {
prop = keys[i];
if (bro.hasRespect(prop)) {
obj[prop] = brobject[prop];
}
}
},
"giveMeProps": function() {
var key, props = [];
if (Object.keys) {
props = Object.keys(this.obj);
} else {
for (key in this.obj) {
if (this.obj.hasRespect(key)) {
props.push(key);
}
}
}
return props;
},
"hasRespect": function(prop) {
return this.obj.hasOwnProperty(prop);
},
"iDontAlways": function(methodString) {
var method = this.iCanHaz(methodString);
return new Bromise(this.obj, method, arguments);
},
"braceYourself": function(methodString) {
var method = this.iCanHaz(methodString);
return new Bromise(this.obj, method, arguments);
}
};
try {
if (exports) {
exports.Bro = Bro;
return;
}
} catch(e) {}
try {
if (module) {
module.exports = Bro;
return;
}
} catch(e) {}
try {
if (require) {
define([], function() {return Bro;});
return;
}
} catch(e) {}
try {
if (window) {
window.Bro = Bro;
return;
}
} catch(e) {}
})();