/
brototype.js
175 lines (157 loc) · 5.52 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*global 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, callback, options) {
if (!(callback instanceof Function)) {
options = callback;
}
var optionsBro = Bro(options || {});
if (!(key instanceof Array)) {
key = [key];
}
var self = this;
if (key.every(function(k) {
var bro = self.iCanHaz(k);
return (Bro(bro).isThatEvenAThing() === Bro.TOTALLY);
})) {
optionsBro.iDontAlways('forSure').butWhenIdo();
// Perform callback function
if (callback) {
for (var i = 0; i < key.length; i++) {
callback(self.obj[key[i]], key[i]);
}
}
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 (key.hasOwnProperty(index)) {
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++) {
if (typeof item === "undefined" || item === null || Bro(item = item[props[i]]).isThatEvenAThing() === Bro.NOWAY) {
return undefined;
}
}
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);
},
"makeItHappen": function(key, value) {
var brobj = this.obj;
var props = key.split('.');
for (var i = 0; i < props.length - 1; ++i) {
if (brobj[props[i]] === undefined) {
brobj[props[i]] = {};
}
brobj = brobj[props[i]];
}
// the deepest key is set to either an empty object or the value provided
brobj[props[props.length - 1]] = value === undefined ? {} : value;
}
};
(function() {
if (typeof define === 'function' && typeof define.amd === 'object') {
define(function() {
return Bro;
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = Bro;
} else if (typeof window !== 'undefined') {
window.Bro = Bro;
}
if (typeof(angular) !== 'undefined') {
angular.module('brototype', []).factory('Bro', function() { return Bro; });
}
})();
})();