swap the meaning of makeArray and toArray: makeArray, like makeObject, is a helper function for arguments processing (that wraps any non-array in an array), toArray, like in other libraries, is an alias for Array.prototype.slice.call
This commit is contained in:
parent
5cabb679f9
commit
5692195509
21 changed files with 88 additions and 88 deletions
|
|
@ -238,7 +238,7 @@ Ox.api = function(items, options) {
|
|||
return conditions.map(function(condition) {
|
||||
var key = condition.key,
|
||||
operator = condition.operator,
|
||||
values = Ox.toArray(condition.value);
|
||||
values = Ox.makeArray(condition.value);
|
||||
if (condition.conditions) {
|
||||
condition.conditions = parseConditions(condition.conditions);
|
||||
} else {
|
||||
|
|
@ -370,9 +370,9 @@ Ox.merge <f> Merges an array with one or more other arrays
|
|||
@*/
|
||||
// FIXME: a1.push.apply(a1, a2) should be much faster
|
||||
Ox.merge = function(arr) {
|
||||
arr = Ox.toArray(arr);
|
||||
arr = Ox.makeArray(arr);
|
||||
Ox.forEach(Array.prototype.slice.call(arguments, 1), function(arg) {
|
||||
Ox.forEach(Ox.toArray(arg), function(val) {
|
||||
Ox.forEach(Ox.makeArray(arg), function(val) {
|
||||
arr.push(val);
|
||||
});
|
||||
});
|
||||
|
|
@ -406,7 +406,7 @@ Ox.range <f> Python-style range
|
|||
@*/
|
||||
Ox.range = function() {
|
||||
var arr = [];
|
||||
Ox.loop.apply(null, Ox.merge(Ox.makeArray(arguments), function(i) {
|
||||
Ox.loop.apply(null, Ox.merge(Ox.toArray(arguments), function(i) {
|
||||
arr.push(i);
|
||||
}));
|
||||
return arr;
|
||||
|
|
@ -504,7 +504,7 @@ Ox.range = function() {
|
|||
@*/
|
||||
Ox.sortBy = function(arr, by, fn) {
|
||||
var values = {};
|
||||
by = Ox.toArray(by);
|
||||
by = Ox.makeArray(by);
|
||||
fn = fn || {};
|
||||
by = by.map(function(v) {
|
||||
return Ox.isString(v) ? {
|
||||
|
|
@ -562,7 +562,7 @@ Ox.zip <f> Zips an array of arrays
|
|||
[[0, 3], [1, 4], [2, 5]]
|
||||
@*/
|
||||
Ox.zip = function() {
|
||||
var args = arguments.length == 1 ? arguments[0] : Ox.makeArray(arguments),
|
||||
var args = arguments.length == 1 ? arguments[0] : Ox.toArray(arguments),
|
||||
arr = [];
|
||||
args[0].forEach(function(v, i) {
|
||||
arr[i] = [];
|
||||
|
|
|
|||
|
|
@ -174,7 +174,7 @@ Ox.forEach <f> forEach loop
|
|||
Ox.forEach = function(col, fn, includePrototype) {
|
||||
var ind = 0, isObject = Ox.isObject(col), key;
|
||||
// Safari will not loop through an arguments array
|
||||
col = Ox.isArguments(col) ? Ox.makeArray(col) : col;
|
||||
col = Ox.isArguments(col) ? Ox.toArray(col) : col;
|
||||
for (key in col) {
|
||||
key = isObject ? key : parseInt(key);
|
||||
// fixme: fn.call(context, obj[key], key, obj) may be more standard...
|
||||
|
|
@ -418,35 +418,23 @@ Ox.len = function(col) {
|
|||
};
|
||||
|
||||
/*@
|
||||
Ox.makeArray <f> Takes an array-like object and returns a true array
|
||||
Alias for <code>Array.prototype.slice.call</code>
|
||||
(value) -> <a> True array
|
||||
value <*> Array-like object
|
||||
> (function() { return Ox.makeArray(arguments); }("foo", "bar"))
|
||||
["foo", "bar"]
|
||||
> Ox.makeArray("foo")
|
||||
["f", "o", "o"]
|
||||
> Ox.makeArray({0: "f", 1: "o", 2: "o", length: 3})
|
||||
["f", "o", "o"]
|
||||
Ox.makeArray <f> Wraps any non-array in an array.
|
||||
> Ox.makeArray('foo')
|
||||
['foo']
|
||||
> Ox.makeArray(['foo'])
|
||||
['foo']
|
||||
@*/
|
||||
// rewrite this so that it uses a try/catch test
|
||||
Ox.makeArray = /MSIE/.test(navigator.userAgent)
|
||||
? function(col) {
|
||||
var i, len, ret = [];
|
||||
try {
|
||||
ret = Array.prototype.slice.call(col);
|
||||
} catch(e) {
|
||||
// handle MSIE NodeLists
|
||||
len = col.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
ret[i] = col[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
: function(col) {
|
||||
return Array.prototype.slice.call(col);
|
||||
};
|
||||
Ox.makeArray = function(obj) {
|
||||
var arr;
|
||||
if (Ox.isArray(obj)) {
|
||||
arr = obj;
|
||||
} else if (Ox.isArguments(obj)) {
|
||||
arr = Ox.toArray(obj);
|
||||
} else {
|
||||
arr = [obj];
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.makeObject <f> Takes an array and returns an object
|
||||
|
|
@ -672,7 +660,7 @@ Ox.sum <f> Returns the sum of the values of a collection
|
|||
@*/
|
||||
Ox.sum = function(col) {
|
||||
var sum = 0;
|
||||
col = arguments.length > 1 ? Ox.makeArray(arguments) : col;
|
||||
col = arguments.length > 1 ? Ox.toArray(arguments) : col;
|
||||
Ox.forEach(col, function(val) {
|
||||
val = +val;
|
||||
sum += Ox.isNumber(val) ? val : 0;
|
||||
|
|
@ -681,23 +669,35 @@ Ox.sum = function(col) {
|
|||
};
|
||||
|
||||
/*@
|
||||
Ox.toArray <f> Wraps any non-array in an array.
|
||||
> Ox.toArray('foo')
|
||||
['foo']
|
||||
> Ox.toArray(['foo'])
|
||||
['foo']
|
||||
Ox.toArray <f> Takes an array-like object and returns a true array
|
||||
Alias for <code>Array.prototype.slice.call</code>
|
||||
(value) -> <a> True array
|
||||
value <*> Array-like object
|
||||
> (function() { return Ox.toArray(arguments); }("foo", "bar"))
|
||||
["foo", "bar"]
|
||||
> Ox.toArray("foo")
|
||||
["f", "o", "o"]
|
||||
> Ox.toArray({0: "f", 1: "o", 2: "o", length: 3})
|
||||
["f", "o", "o"]
|
||||
@*/
|
||||
Ox.toArray = function(obj) {
|
||||
var arr;
|
||||
if (Ox.isArray(obj)) {
|
||||
arr = obj;
|
||||
} else if (Ox.isArguments(obj)) {
|
||||
arr = Ox.makeArray(obj);
|
||||
} else {
|
||||
arr = [obj];
|
||||
}
|
||||
return arr;
|
||||
};
|
||||
// rewrite this so that it uses a try/catch test
|
||||
Ox.toArray = /MSIE/.test(navigator.userAgent)
|
||||
? function(col) {
|
||||
var i, len, ret = [];
|
||||
try {
|
||||
ret = Array.prototype.slice.call(col);
|
||||
} catch(e) {
|
||||
// handle MSIE NodeLists
|
||||
len = col.length;
|
||||
for (i = 0; i < len; i++) {
|
||||
ret[i] = col[i];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
: function(col) {
|
||||
return Array.prototype.slice.call(col);
|
||||
};
|
||||
|
||||
/*@
|
||||
Ox.values <f> Returns the values of a collection
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ Ox.hsl <f> Takes RGB values and returns HSL values
|
|||
@*/
|
||||
Ox.hsl = function(rgb) {
|
||||
if (arguments.length == 3) {
|
||||
rgb = Ox.makeArray(arguments);
|
||||
rgb = Ox.toArray(arguments);
|
||||
}
|
||||
rgb = Ox.clone(rgb).map(function(val) {
|
||||
return val / 255;
|
||||
|
|
@ -64,7 +64,7 @@ Ox.rgb <f> Takes HSL values and returns RGB values
|
|||
|
||||
Ox.rgb = function(hsl) {
|
||||
if (arguments.length == 3) {
|
||||
hsl = Ox.makeArray(arguments);
|
||||
hsl = Ox.toArray(arguments);
|
||||
}
|
||||
hsl = Ox.clone(hsl);
|
||||
hsl[0] /= 360;
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ Ox.SHORT_MONTHS = Ox.MONTHS.map(function(val) {
|
|||
return val.substr(0, 3);
|
||||
});
|
||||
//@ Ox.PATH <str> Path of Ox.js
|
||||
Ox.PATH = Ox.makeArray(
|
||||
Ox.PATH = Ox.toArray(
|
||||
document.getElementsByTagName('script')
|
||||
).reverse().filter(function(element) {
|
||||
return /Ox\.js$/.test(element.src);
|
||||
|
|
|
|||
|
|
@ -185,13 +185,13 @@ Ox.Log = (function() {
|
|||
that.filter = function(val) {
|
||||
if (!Ox.isUndefined(val)) {
|
||||
that.filter.enable();
|
||||
log.filter = Ox.toArray(val);
|
||||
log.filter = Ox.makeArray(val);
|
||||
storage('log', log);
|
||||
}
|
||||
return log.filter;
|
||||
};
|
||||
that.filter.add = function(val) {
|
||||
return that.filter(Ox.unique(Ox.merge(log.filter, Ox.toArray(val))));
|
||||
return that.filter(Ox.unique(Ox.merge(log.filter, Ox.makeArray(val))));
|
||||
};
|
||||
that.filter.disable = function() {
|
||||
log.filterEnabled = false;
|
||||
|
|
@ -202,13 +202,13 @@ Ox.Log = (function() {
|
|||
storage('log', log);
|
||||
};
|
||||
that.filter.remove = function(val) {
|
||||
val = Ox.toArray(val);
|
||||
val = Ox.makeArray(val);
|
||||
return that.filter(log.filter.filter(function(v) {
|
||||
return val.indexOf(v) == -1;
|
||||
}));
|
||||
};
|
||||
that.log = function() {
|
||||
var args = Ox.makeArray(arguments), date, ret;
|
||||
var args = Ox.toArray(arguments), date, ret;
|
||||
if (!log.filterEnabled || log.filter.indexOf(args[0]) > -1) {
|
||||
date = new Date();
|
||||
args.unshift(
|
||||
|
|
@ -264,7 +264,7 @@ Ox.loop = function() {
|
|||
};
|
||||
|
||||
Ox._loop = function() {
|
||||
var type = Ox.toArray(arguments).map(function(arg) {
|
||||
var type = Ox.makeArray(arguments).map(function(arg) {
|
||||
return Ox.typeOf(arg);
|
||||
}),
|
||||
fnIndex = type.indexOf('function'),
|
||||
|
|
@ -315,7 +315,7 @@ Ox.print <f> Prints its arguments to the console
|
|||
@*/
|
||||
|
||||
Ox.print = function() {
|
||||
var args = Ox.makeArray(arguments),
|
||||
var args = Ox.toArray(arguments),
|
||||
date = new Date();
|
||||
args.unshift(
|
||||
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)/*,
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ Ox.$ = Ox.element = function(val) {
|
|||
@*/
|
||||
append: function() {
|
||||
var that = this;
|
||||
Ox.toArray(arguments[0]).forEach(function(element) {
|
||||
Ox.makeArray(arguments[0]).forEach(function(element) {
|
||||
that[0].appendChild(element[0]);
|
||||
});
|
||||
return this;
|
||||
|
|
@ -254,7 +254,7 @@ Ox.$ = Ox.element = function(val) {
|
|||
key <s> The attribute
|
||||
@*/
|
||||
removeAttr: function() {
|
||||
Ox.toArray(arguments[0]).forEach(function(key) {
|
||||
Ox.makeArray(arguments[0]).forEach(function(key) {
|
||||
this[0].removeAttribute(key);
|
||||
});
|
||||
return this;
|
||||
|
|
|
|||
|
|
@ -252,7 +252,7 @@
|
|||
// Unfortunately, we can't synchronously set the source of an image,
|
||||
// draw it onto a canvas, and read its data.
|
||||
image.onload = function() {
|
||||
str = Ox.makeArray(Ox.canvas(image).data).map(function(v, i) {
|
||||
str = Ox.toArray(Ox.canvas(image).data).map(function(v, i) {
|
||||
// Read one character per RGB byte, ignore ALPHA.
|
||||
return i % 4 < 3 ? Ox.char(v) : '';
|
||||
}).join('');
|
||||
|
|
|
|||
|
|
@ -16,12 +16,12 @@ Ox.cache = function(fn, options) {
|
|||
}, options || {})
|
||||
var cache = {},
|
||||
ret = function() {
|
||||
var args = Ox.makeArray(arguments),
|
||||
var args = Ox.toArray(arguments),
|
||||
callback,
|
||||
key = options.key(args);
|
||||
function callback() {
|
||||
// cache all arguments passed to callback
|
||||
cache[key] = Ox.makeArray(arguments);
|
||||
cache[key] = Ox.toArray(arguments);
|
||||
// call the original callback
|
||||
Ox.last(args).apply(this, arguments);
|
||||
}
|
||||
|
|
@ -44,7 +44,7 @@ Ox.cache = function(fn, options) {
|
|||
if (arguments.length == 0) {
|
||||
cache = {};
|
||||
} else {
|
||||
Ox.toArray(arguments).forEach(function(key) {
|
||||
Ox.makeArray(arguments).forEach(function(key) {
|
||||
delete cache[key];
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -290,7 +290,7 @@ Ox.doc = (function() {
|
|||
}
|
||||
return function(/* source | file, callback | files, callback*/) {
|
||||
var source = arguments.length == 1 ? arguments[0] : void 0,
|
||||
files = arguments.length == 2 ? Ox.toArray(arguments[0]) : void 0,
|
||||
files = arguments.length == 2 ? Ox.makeArray(arguments[0]) : void 0,
|
||||
callback = arguments[1],
|
||||
counter = 0, items = [];
|
||||
files && files.forEach(function(file) {
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ Ox.getJSON = (function() {
|
|||
});
|
||||
}
|
||||
return function(url, callback) {
|
||||
var urls = Ox.toArray(url), data = {}, i = 0, n = urls.length;
|
||||
var urls = Ox.makeArray(url), data = {}, i = 0, n = urls.length;
|
||||
urls.forEach(function(url) {
|
||||
getJSON(url, function(data_) {
|
||||
data[url] = data_;
|
||||
|
|
@ -128,7 +128,7 @@ Ox.loadFile = (function() {
|
|||
}
|
||||
}
|
||||
function findFileInHead() {
|
||||
return Ox.makeArray(
|
||||
return Ox.toArray(
|
||||
document.getElementsByTagName(type == 'css' ? 'link' : 'script')
|
||||
).map(function(element) {
|
||||
return element[type == 'css' ? 'href' : 'src'] == file;
|
||||
|
|
@ -163,9 +163,9 @@ Ox.loadFiles <f> Loads multiple files (images, scripts or stylesheets)
|
|||
@*/
|
||||
Ox.loadFiles = (function() {
|
||||
function loadFiles(files, callback) {
|
||||
files = Ox.toArray(files);
|
||||
files = Ox.makeArray(files);
|
||||
var i = 0, n = files.length, images = {};
|
||||
Ox.toArray(files).forEach(function(file) {
|
||||
Ox.makeArray(files).forEach(function(file) {
|
||||
Ox.loadFile(file, function(image) {
|
||||
if (image) {
|
||||
images[file] = image;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ Ox.checkType <f> Throws a TypeError if a value is not of a given type
|
|||
type <s> Type
|
||||
@*/
|
||||
Ox.checkType = function(val, type) {
|
||||
if (!Ox.in(Ox.makeArray(type), Ox.typeOf(val))) {
|
||||
if (!Ox.in(Ox.toArray(type), Ox.typeOf(val))) {
|
||||
throw new TypeError();
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue