-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (207 loc) · 7.21 KB
/
index.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Author: Henry Troutman, henguin1001@gmail.com, henrytroutman.com, hen.ventures //
// Henscript, a redefinition of javascript //
// It may not be practical, but it is certainly existant //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
// Here are all the global definitions (may cause conflict) //
//////////////////////////////////////////////////////////////
////////////////////////
// Object definitions //
////////////////////////
Object.prototype.map = function(callback){
var outcome = {}, key;
for(key in this) {
if(this.hasOwnProperty(key)) {
var callbackReturn = callback(this[key],key,this);
if(callbackReturn) outcome[key] = callbackReturn;
}
}
return outcome;
};
Object.prototype.size = function() {
var size = 0,
key;
for (key in this) {
if (this.hasOwnProperty(key)) size++;
}
return size;
};
Object.prototype.equals = function(obj, callback) {
if(!callback) return this==obj;
else if (this == obj) callback();
return new ConditionResponse(this == obj);
};
Object.prototype.equal = Object.prototype.equals;
Object.prototype.not_equal = function(obj, callback) {
if(!callback) return this!=obj;
else if (this != obj) callback();
return new ConditionResponse(this != obj);
};
/////////////////////////
// Boolean definitions //
/////////////////////////
Boolean.prototype.and = function(that){
return this && that;
};
Boolean.prototype.or = function(that){
return this || that;
};
Boolean.prototype.not = function(){
return !this;
};
Boolean.prototype.bang = Boolean.prototype.not;
Boolean.prototype.condition = function(callback_or_value_true, callback_or_value_false) {
// have to have equals since it would normally return true
// since it is a non-null value
if (this==true) {
if (typeof callback_or_value_true == 'function') return callback_or_value_true();
else return callback_or_value_true;
} else {
if (typeof callback_or_value_false == 'function') return callback_or_value_false();
else return callback_or_value_false;
}
};
////////////////////////
// Number definitions //
////////////////////////
/**
* gets whether an object is not equal to the contained object
* @param {object} object the object to compare to
* @param {Function} callback when it is true this is called
* @return {boolean} the outcome of the comparison
*/
Number.prototype.greater = function(number, callback) {
if (callback && this > obj) callback();
return new ConditionResponse(this > obj);
};
/**
* gets whether an number is greater than the contained number
* @param {number} number the number to compare to
* @param {Function} callback when it is true this is called
* @return {boolean} the outcome of the comparison
*/
Number.prototype.greater_equal = function(number, callback) {
if (callback && this >= obj) callback();
return new ConditionResponse(this >= obj);
};
/**
* gets whether an number is greater than or equal to the contained number
* @param {number} number the number to compare to
* @param {Function} callback when it is true this is called
* @return {boolean} the outcome of the comparison
*/
Number.prototype.less = function(number, callback) {
if (callback && this < obj) callback();
return new ConditionResponse(this < obj);
};
/**
* gets whether an number is less than the contained number
* @param {number} number the number to compare to
* @param {Function} callback when it is true this is called
* @return {boolean} the outcome of the comparison
*/
Number.prototype.less = function(number, callback) {
if (callback && this < number) return callback();
else return this < number;
};
/**
* gets whether an number is less than or equal to the contained number
* @param {number} number the number to compare to
* @param {Function} callback when it is true this is called
* @return {boolean} the outcome of the comparison
*/
Number.prototype.less_equal = function(number, callback) {
if (callback && this <= obj) callback();
return new ConditionResponse(this <= obj);
};
Number.prototype.add = function(b){
return (this+b);
};
Number.prototype.plus = Number.prototype.add;
Number.prototype.subtract = function(b){
return (this-b);
};
Number.prototype.minus = Number.prototype.subtract;
Number.prototype.multiply = function(b){
return (this*b);
};
Number.prototype.times = Number.prototype.multiply;
Number.prototype.divide = function(b){
return (this/b);
};
Number.prototype.over = Number.prototype.divide;
Number.prototype.modulus = function(b){
return (this%b);
}
Number.prototype.mod = Number.prototype.modulus;
Number.prototype.power = function(b){
return Math.pow(this,b);
}
Number.prototype.squared = function(){
return (this*this);
};
Number.prototype.negate = function(){
return -this;
};
/////////////////////////////////////////////////////////////
// Here all the henscript functions //
// (Attatched to an object so there should be no conflict) //
/////////////////////////////////////////////////////////////
function Henscript() {
}
/**
* when passed true the callback is called
* @param {boolean} condition whether the condition is met
* @param {Function} callback called when the condition is met
* @return {ConditionResponse} A condition response object
*/
Henscript.prototype.if = function(condition, callback) {
if (condition) callback();
return new ConditionResponse(condition);
};
// relies upon object redefinitions
Henscript.prototype.switch = function(value,callbacks){
var conditionMet = callbacks.map(function(element,index){
if(index==value){
element(index);
return element;
}
}).size() > 0;
return new ConditionResponse(conditionMet);
}
Henscript.prototype.while = function(condition_loop,callback_loop){
if(!condition_loop()) return new ConditionResponse(false);
while(condition_loop())callback_loop();
return new ConditionResponse(true);
}
Henscript.prototype.for = function(condition_loop,callback_loop,callback_sequence){
if(!condition_loop()) return new ConditionResponse(false);
while(condition_loop()){
callback_loop();
callback_sequence();
}
return new ConditionResponse(true);
}
/**
* An object that holds all sorts of functions usefull after a condition has been tested
* @param {boolean} condition the value of the previous operation
*/
function ConditionResponse(condition) {
this.value = condition;
}
ConditionResponse.prototype.elseif = function(condition,callback){
if(!this.value) return Henscript.prototype.if(condition, callback);
else return new ConditionResponse(this.value);
};
ConditionResponse.prototype.else = function(callback) {
if(!this.value && callback) callback();
return this.value;
}
Henscript.prototype.ConditionResponse = ConditionResponse;
// Check if browser or nodejs
if(typeof exports === 'undefined'){
this['henscript'] = new Henscript();
} else {
module.exports = new Henscript();
}