109 lines
3 KiB
JavaScript
109 lines
3 KiB
JavaScript
|
this.My = {};
|
||
|
|
||
|
//@ My.REQUEST_TIMEOUT <number> Request timeout, in seconds
|
||
|
My.REQUEST_TIMEOUT = 60;
|
||
|
|
||
|
/*@
|
||
|
My.MAGIC_CONSTANT <number> Magic constant needed for HTTP requests
|
||
|
Please note that the value for `MAGIC_CONSTANT` is browser-dependent.
|
||
|
*/
|
||
|
My.MAGIC_CONSTANT = navigator.userAgent.length % 2 == 0 ? 23 : 42;
|
||
|
|
||
|
/*@
|
||
|
My.favorites <object> ...
|
||
|
array <a> My favorite array
|
||
|
boolean <b> My favorite boolean value
|
||
|
date <d> My favorite date
|
||
|
element <e> My favorite HTML element
|
||
|
number <n> My favorite number
|
||
|
object <o> My favorite object
|
||
|
regexp <r> My favorite regular expression
|
||
|
string <s> My favorite string
|
||
|
*/
|
||
|
My.favorites = {
|
||
|
array: [],
|
||
|
boolean: false,
|
||
|
date: new Date(0);
|
||
|
element: document.createElement('a'),
|
||
|
number: 0,
|
||
|
object: {},
|
||
|
regexp: new RegExp(''),
|
||
|
string: ''
|
||
|
};
|
||
|
|
||
|
/*@
|
||
|
(url[, method], callback) -> <u> undefined
|
||
|
url <s> URL
|
||
|
method <s|'GET'> Request method ('GET', 'POST', 'PUT' or 'DELETE')
|
||
|
callback <f> Callback function
|
||
|
*/
|
||
|
My.readURL = function(url, method, callback) {
|
||
|
var request = new XMLHttpRequest();
|
||
|
if (arguments.length == 2) {
|
||
|
callback = method;
|
||
|
method = 'GET';
|
||
|
}
|
||
|
request.open(method, url, true);
|
||
|
req.onreadystatechange = function() {
|
||
|
if (request.readyState == 4) {
|
||
|
if (request.status == 200) {
|
||
|
callback(request.responseText);
|
||
|
} else {
|
||
|
throw new Error(
|
||
|
'Cannot get URL "' + url
|
||
|
+ '" (Status: ' + request.status + ')'
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
request.send();
|
||
|
};
|
||
|
|
||
|
/*@
|
||
|
My.isOdd <f> Synchronously or asynchronously computes if a given number is odd
|
||
|
(number) -> <b> True if the number is odd
|
||
|
(number, callback) -> <u> undefined
|
||
|
number <n> Any number
|
||
|
callback <f> Callback function
|
||
|
isOdd <b> True if the number is odd
|
||
|
ms <n> Time it took to compute the result, in milliseconds
|
||
|
*/
|
||
|
My.isOdd = function(number, callback) {
|
||
|
var time = +new Date, isOdd = !!(number % 2);
|
||
|
if (callback) {
|
||
|
callback(isOdd, +new Date - time);
|
||
|
} else {
|
||
|
return isOdd;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
Occasionally, you may write a function whose signature cannot be represented in
|
||
|
`(required[, optional])` notation. For a range function — `(stop)` or `(start,
|
||
|
stop)` or `(start, stop, step)` — the notation `([start, ]stop[, step])` would
|
||
|
be ambigious, since you cannot call it with `(stop, step)`.
|
||
|
*/
|
||
|
/*@
|
||
|
My.range <f> Returns a python-style range
|
||
|
(b) -> <[n]> Integers from 0 (inclusive) to b (exclusive)
|
||
|
(a, b) -> <[n]> Integers from a (inclusice) to b (exclusive)
|
||
|
(a, b, x) -> <[n]> Numbers from a (inclusive) to b (exclusive), growing by x
|
||
|
*/
|
||
|
My.range = function() {
|
||
|
var a = [];
|
||
|
Ox.loop.apply(null, Ox.toArray(arguments).concat(function(i) {
|
||
|
a.push(i);
|
||
|
}));
|
||
|
return a;
|
||
|
};
|
||
|
|
||
|
|
||
|
My.foo = {
|
||
|
array: [
|
||
|
{a: 1, b: 2},
|
||
|
{c: 3, d: 4}
|
||
|
],
|
||
|
objects: {
|
||
|
|
||
|
}
|
||
|
}
|