This repository was archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdecaf.js
462 lines (456 loc) · 14.3 KB
/
decaf.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*!
* Created by mschwartz on 12/2/13.
*/
/*global toString */
/**
* # Bulitin global decaf singleton
*
* This singleton contains a number of general purpose static methods. The decaf instance (and methods) are available in any JavaScript context within a DecafJS program.
*
* @class decaf
* @singleton
*/
/** @private */
var decaf = {
/**
* ## decaf.each(o, fn)
*
* Iterate over an object or array, calling the specified function for each member.
*
* #### Arguments:
* - o - the object to iterate over
* - fn - the function to be called with each member of the object
*
* The called function has the following signature:
*
* function callback(item, index) {
* // use item
* }
*
* If the function returns false, the iteration will not continue.
*
* @method each
* @param {Object|Array} o - object or array to iterate over
* @param {Function} fn - function to be called for each
*/
each : function ( o, fn ) {
for ( var key in o ) {
if ( o.hasOwnProperty && o.hasOwnProperty(key) ) {
if ( fn.call(o, o[ key ], key, o) === false ) {
return;
}
}
}
},
/**
* ## decaf.extend(dest, src ...src) : dest (chainable)
*
* Merge one or more Objects to a destination object
*
* This can be used to extend a JavaScript class or prototype. It is heavily used throughout the DecafJS source
* code.
*
* This function is smart enough to merge getter and setter functions rather than the values those get or set. Unlike
* AngularJS and jQuery implementations.
*
* #### Arguments:
* - {object} dest - the object that will be the result of the object merges.
* - {object} src... - one or more objects to be merged into the result (dest) object
*
* #### Returns:
* - {object} - dest - the destination/result object
*
* @method extend
* @param {Object} me - the destination object
* @param {Object...} objects - the objects to merge
* @returns {Object} the merged (destination) object.
*/
extend : function ( me ) {
var args = Array.prototype.slice.call(arguments, 1);
decaf.each(args, function ( o ) {
for ( var key in o ) {
if ( o.hasOwnProperty(key) ) {
var desc = Object.getOwnPropertyDescriptor(o, key),
g = desc.get, s = desc.set;
if ( g || s ) {
Object.defineProperty(me, key, { get : g, set : s, enumerable : true });
}
else {
me[ key ] = o[ key ];
}
}
}
});
return me;
},
/**
* Get current time as a Unix timestamp.
*
* Unix timestamp is SECONDS since the epoch.
*
* @returns {Number}
*/
timestamp: function() {
return parseInt(Date.now()/1000, 10);
},
/**
* Convert a JavaScript string or array into a Java ByteArray.
*
* ### Arguments:
* - {String|Array} thing - what to convert to ByteArray
* - {String} encoding - optional encoding for the ByteArray (e.g. UTF-8, etc.)
*
* ### Returns:
* - {Java ByteArray} thing as a Java ByteArray
* @method toJavaByteArray
* @param {String|Array} thing - what to convert
* @param {String} encoding - how to encode the array (e.g. UTF-8, etc.)
* @returns The Java Byte Array
*/
toJavaByteArray : function ( thing, encoding ) {
if ( typeof thing === 'string' ) {
return encoding ? new java.lang.String(thing).getBytes(encoding) : new java.lang.String(thing).getBytes();
}
else {
var len = thing.length;
var v = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, len);
try {
for ( var i = 0; i < len; i++ ) {
v[ i ] = thing[ i ];
}
}
catch ( e ) {
throw new Error('Array.toJavaByteArray - array contains invalid values');
}
return v;
}
},
/**
* Allocate a new Java Byte Array
*
* ### Arguments:
* - {Number} len - size of the array to be created
*
* ### Returns:
* - {Java Byte Array} - the allocated Java ByteArray
*
* @param len
* @returns {*}
*/
newJavaByteArray : function ( len ) {
return java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, len);
},
/**
* ## decaf.isEmpty(value) : boolean
*
* Test a variable to see if it's "empty."
*
* ### Arguments:
* - {mixed} value - a JavaScript variable to be tested
*
* ### Returns:
* - true if the passed value is empty.
*
* The value is deemed to be empty if it is:
*
* * null
* * undefined
* * an empty array
* * a zero length string (unless the allowBlank parameter is true)
*
* @method isEmpty
* @param {Mixed} value - The value to test
* @param {Boolean} allowBlank - (optional) true to allow empty strings (defaults to false)
* @return {Boolean} result
*/
isEmpty : function ( value, allowBlank ) {
return value === null || value === undefined || ((decaf.isArray(value) && !value.length)) || (!allowBlank ? value === '' : false);
},
/**
* ## decaf.isArray(value) : boolean
*
* Test a variable to see if it's an array
*
* ### Arguments:
* - {mixed} value - the variable to be tested
*
* ### Returns:
* - true if the passed value is a JavaScript array, otherwise false.
*
* @method isArray
* @param {Mixed} value - The value to test
* @return {Boolean} result
*/
isArray : function ( value ) {
return toString.apply(value) === '[object Array]';
},
/**
* ## decaf.isDate(value) : boolean
*
* Test a variable to see if it's a JavaScript Date object
*
* ### Arguments:
* - {Mixed} value - the variable to be tested
*
* ### Returns:
* - true if the passed object is a JavaScript date object, otherwise false.
*
* @method isDate
* @param {Object} value - The object to test
* @return {Boolean} result
*/
isDate : function ( value ) {
return toString.apply(value) === '[object Date]';
},
/**
* ## decaf.isObject(value) : boolean
*
* Test a variable to see if it is a JavaScript Object.
*
* ### Arguments:
* - {Mixed} value - variable to test
*
* ### Returns:
* - true if the passed value is a JavaScript Object, otherwise false.
*
* @method isObject
* @param {Mixed} value - The value to test
* @return {Boolean} result
*/
isObject : function ( value ) {
return !!value && Object.prototype.toString.call(value) === '[object Object]';
},
/**
* ## decaf.isPrimitive(value) : boolean
*
* Test a variable to see if it's a JavaScript primitive.
*
* A primitive is:
* - a string,
* - a number
* - a boolean
*
* ### Arguments:
* - {Mixed} value - the variable to be tested.
*
* ### Returns:
* - true if the passed value is a JavaScript 'primitive'
*
* @method isPrimitive
* @param {Mixed} value - value The value to test
* @return {Boolean} - result
*/
isPrimitive : function ( value ) {
return Util.isString(value) || Util.isNumber(value) || Util.isBoolean(value);
},
/**
* ## decaf.isFunction(value) : boolean
*
* Test a variable to see if it is a Function.
*
* ### Arguments
* - {Mixed} value - variable to be tested.
*
* ### Returns:
* - true if the passed value is a JavaScript Function, otherwise false
*
* @param {Mixed} value The value to test
* @return {Boolean}
*/
isFunction : function ( value ) {
return toString.apply(value) === '[object Function]';
},
/**
* ## decaf.isNumber(value) : boolean
*
* Test a variable to see if it is a number.
*
* ### Arguments:
* - {Mixed} value - value to test
*
* ### Returns:
* - true if the passed value is a number. Returns false for non-finite numbers.
*
* @param {Mixed} value The value to test
* @return {Boolean}
*/
isNumber : function ( value ) {
return typeof value === 'number' && isFinite(value);
},
/**
* decaf.isString(value) : boolean
*
* Test a variable to see if it is a String.
*
* ### Arguments:
* - {Mixed} value - variable to test
*
* ###Returns:
* - true if the passed value is a string.
*
* @param {Mixed} value The value to test
* @return {Boolean}
*/
isString : function ( value ) {
return typeof value === 'string';
},
/**
* ## decaf.isBoolean(value) : boolean
*
* Test a variable to see if it is a boolean type.
*
* ### Arguments:
* - {Mixed} value - the variable to test
*
* ### Returns:
* - true if the passed value is a boolean.
*
* @param {Mixed} value The value to test
* @return {Boolean}
*/
isBoolean : function ( value ) {
return typeof value === 'boolean';
},
/**
* ## decaf.isDefined(variable) : boolean
*
* Test a variable to see if it is defined (not undefined)
*
* ### Arguments:
* - {Mixed} value
* ### Returns:
* - true if the passed value is not undefined.
*
* @param {Mixed} value The value to test
* @return {Boolean}
*/
isDefined : function ( value ) {
return typeof value !== 'undefined';
},
/**
* This is an observable mixin. It provides a mechanism to add and remove multiple event listeners to, and to fire events on, any object that implements the mixin.
*
* Event names are any arbitrary string. Classes that include this mixin may define the event names they supports.
*
* ### Example:
*
* ```javascript
* function SomeClass() {
* this.name = 'SomeClass';
* ...
* }
* decaf.extend(SomeClass.prototype, decaf.observable);
*
* function eventHandler(s) {
* console.log(this.name + ': ' + s);
* }
*
* var c = new SomeClass();
* c.on('anyOldString', eventHandler);
* c.fire('anyOldString', 'foo');
* // -> the string "SomeClass: foo" is printed to the console by the event handler
* c.un('anyOldString', eventHandler);
* c.fire('anyOldString', 'foo');
* // -> nothing is printed to the console since the event handler has been removed.
* ```
*
* For purposes of the rest of the observable mixin documentation, the term "observable" implies an instance of an object that has applied the mixin.
* @inheritable
*/
observable : {
/**
* ## observable.on(name, handler) : observable (chainable)
*
* Add an event handler/listener to an observable.
*
* ### Arguments:
* - {String} name - name of event
* - {Function) handler - the function to handle the event
*
* ### Returns:
* - {Object} the observable (the function becomes chainable)
*
* @param name
* @param handler
*/
on : function ( name, handler ) {
var me = this;
if ( !me.__eventHandlers__ ) {
me.__eventHandlers__ = [];
}
if ( !me.__eventHandlers__[ name ] ) {
me.__eventHandlers__[ name ] = [];
}
me.__eventHandlers__[ name ].push(handler);
return me;
},
/**
* ## observable.fire(event, ...) : observable (chainable)
*
* Fire an event with arbitrary additional arguments.
*
* All event handlers for the named event on the observable are called with the optional arguments.
*
* ### Arguments:
* - {String} name - the name of the event to fire
* - {Mixed} ... - zero or more arbitrary arguments to be passed to the event handlers.
*
* ### Returns:
* - {Object} the observable (the function becomes chainable)
*
* @param event
* @returns {decaf}
*/
fire : function ( event ) {
var me = this;
if ( !me.__eventHandlers__ ) {
me.__eventHandlers__ = [];
}
if ( me.__eventHandlers__[ event ] ) {
var args = Array.prototype.splice.call(arguments, 1);
decaf.each(me.__eventHandlers__[ event ] || [], function ( fn ) {
fn.apply(me, args);
});
}
return me;
},
/**
* ## observable.un(name, handler) : observable (chainable)
*
* Remove a listener/event handler from an observable.
*
* THe event handler will no longer be called for the specified event name.
*
* ### Arguments:
* - {String} name - the name of the event
* - {Function handler - the event handler function to remove
*
* Note that the arguments to un() should be the same as those to on() that installed the handler.
*
* ### Returns:
* - {Object} the observable (the function becomes chainable)
*
* @param {String} name
* @param {Function} handler
* @returns {decaf}
*/
un : function ( name, handler ) {
var me = this,
newHandlers = [];
if ( !me.__eventHandlers__ ) {
me.__eventHandlers__ = [];
}
if ( !me.__eventHandlers__[ name ] ) {
me.__eventHandlers__[ name ] = [];
}
decaf.each(me.__eventHandlers__[ name ], function ( existing ) {
if ( handler !== existing ) {
newHandlers.push(existing);
}
});
me.__eventHandlers__[ name ] = newHandlers;
return me;
}
}
};
global.decaf = decaf;