- add loadAsync and use in Ox.loadFile, Ox.getJSON

- add Ox.getJSONP
- fix Ox.parseHTML
- fix Ox.Doc
- add more documentation
This commit is contained in:
j 2012-05-23 01:17:17 +02:00
commit 1b08732fa7
8 changed files with 106 additions and 103 deletions

View file

@ -188,8 +188,8 @@ Ox.forEach = function(col, fn, that) {
}
} else {
for (i = 0; i < col.length; i++) {
// fn.call(that, col[i], i, col);
if (fn.call(that, col[i], i, col) === false) {
// i in col && fn.call(that, col[i], i, col);
if (i in col && fn.call(that, col[i], i, col) === false) {
console.warn('Returning false in Ox.forEach is deprecated.')
break;
}
@ -304,8 +304,12 @@ Ox.indexOf = function(col, fn) {
return index == col.length ? -1 : index;
};
// FIXME: use this instead of `Ox.filter(Ox.map())` when it's just about getting
// the original indices
/*@
Ox.indicesOf <f> return indices of array elements that pass a test
> Ox.indicesOf([1, 2, 3, 4], function(val) { return val % 2 == 0; })
[1, 3]
@*/
Ox.indicesOf = function(col, fn) {
return Ox.map(col, function(val, i) {
return fn(val) ? i : null;
@ -339,31 +343,6 @@ Ox.isEmpty = function(val) {
return Ox.len(val) === 0;
};
/*@
Ox.keys <f> Returns the keys of a collection
Unlike <code>Object.keys()</code>, <code>Ox.keys()</code> works for arrays,
objects and strings.
> Ox.keys([1, 2, 3])
[0, 1, 2]
> Ox.keys([1,,3])
[0, 2]
# fixme?
# > Ox.keys([,])
# [0]
> Ox.keys({a: 1, b: 2, c: 3})
['a', 'b', 'c']
> Ox.keys('abc')
[0, 1, 2]
@*/
// fixme: is this really needed? arrays... ok... but strings??
Ox.keys = function(obj) {
var keys = [];
Ox.forEach(obj, function(v, k) {
keys.push(k);
});
return keys.sort();
};
/*@
Ox.last <f> Gets or sets the last element of an array
Unlike <code>arrayWithALongName[arrayWithALongName.length - 1]</code>,
@ -395,7 +374,7 @@ Ox.last = function(arr, val) {
Ox.len <f> Returns the length of an array, node list, object or string
Not to be confused with <code>Ox.length</code>, which is the
<code>length</code> property of the <code>Ox</code> function
(<code>1</code>). // FIXME: 1 becomes 67 in DocPanel
(<code>1</code>).
> Ox.len((function() { return arguments; }(1, 2, 3)))
3
> Ox.len([1, 2, 3])
@ -480,9 +459,8 @@ Ox.map <f> Transforms the values of an array, object or string
[true, false, false]
> Ox.map([0, 1, 2, 4], function(v, i) { return v ? i == v : null; })
[true, true, false]
# fixme?
# > Ox.map([,], function(v, i) { return i; })
# [0]
> Ox.map([,], function(v, i) { return i; })
[,]
@*/
Ox.map = function(col, fn, that) {
var type = Ox.typeOf(col), ret;
@ -593,23 +571,25 @@ Ox.shuffle <f> Randomizes the order of values within a collection
> Ox.shuffle('123').split('').sort().join('')
'123'
@*/
// FIXME: this doesn't actually randomize the order
Ox.shuffle = function(col) {
var keys, ret, type = Ox.typeOf(col), values;
function sort() {
return Math.random() - 0.5;
}
if (type == 'array') {
ret = col.sort(sort);
} else if (type == 'object') {
if (type == 'object') {
keys = Object.keys(col);
values = Ox.values(col).sort(sort);
values = Ox.shuffle(Ox.values(col));
ret = {};
keys.forEach(function(key, i) {
ret[key] = values[i]
});
} else if (type == 'string') {
ret = col.split('').sort(sort).join('');
} else {
ret = [];
Ox.toArray(col).forEach(function(v, i) {
var random = Math.floor(Math.random() * (i + 1));
ret[i] = ret[random];
ret[random] = v;
});
if (type == 'string') {
ret = ret.join('');
}
}
return ret;
};