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:
rolux 2012-05-19 12:40:59 +04:00
parent 5cabb679f9
commit 5692195509
21 changed files with 88 additions and 88 deletions

View file

@ -1275,7 +1275,7 @@ Ox.Calendar = function(options, self) {
{id, {key: value, ...}} -> <o> Calendar object
@*/
that.editEvent = function() {
var args = Ox.makeArray(arguments),
var args = Ox.toArray(arguments),
id = args.shift(),
data = Ox.makeObject(args),
event = Ox.getObjectById(self.options.events, id),

View file

@ -53,7 +53,7 @@ Ox.App = function(options) {
},
navigator: {
cookieEnabled: navigator.cookieEnabled,
plugins: Ox.makeArray(navigator.plugins).map(function(plugin) {
plugins: Ox.toArray(navigator.plugins).map(function(plugin) {
return plugin.name;
}),
userAgent: navigator.userAgent

View file

@ -430,7 +430,7 @@ Ox.Element = function(options, self) {
@*/
that.toggleOption = function() {
var options = {};
Ox.toArray(arguments[0]).forEach(function(key) {
Ox.makeArray(arguments[0]).forEach(function(key) {
options[key] == !self.options[key];
});
that.options(options);
@ -483,7 +483,7 @@ Ox.Element = function(options, self) {
if (arguments.length == 0) {
self.$eventHandler.unbind();
} else {
Ox.makeArray(arguments).forEach(function(event) {
Ox.toArray(arguments).forEach(function(event) {
self.$eventHandler.unbind('ox_' + event);
});
}

View file

@ -84,7 +84,7 @@ Ox.Event = (function() {
if (arguments.length == 0) {
self.$eventHandler.unbind();
} else {
Ox.makeArray(arguments).forEach(function(event) {
Ox.toArray(arguments).forEach(function(event) {
self.$eventHandler.unbind('ox_' + event);
});
}

View file

@ -40,7 +40,7 @@ Ox.ButtonGroup = function(options, self) {
tooltip: button.tooltip,
width: button.width
}, self.options.selectable ? {
selected: Ox.toArray(self.options.value).indexOf(button.id || button) > -1
selected: Ox.makeArray(self.options.value).indexOf(button.id || button) > -1
} : {});
});

View file

@ -34,7 +34,7 @@ Ox.CheckboxGroup = function(options, self) {
self.options.checkboxes = self.options.checkboxes.map(function(checkbox) {
return {
checked: Ox.toArray(self.options.value).indexOf(checkbox.id || checkbox) > -1,
checked: Ox.makeArray(self.options.value).indexOf(checkbox.id || checkbox) > -1,
id: checkbox.id || checkbox,
title: checkbox.title || checkbox
};

View file

@ -67,7 +67,7 @@ Ox.Range = function(options, self) {
? self.options.values[self.options.value] : self.options.value;
}
self.options.arrowStep = options.arrowStep || self.options.step;
self.options.trackImages = Ox.toArray(self.options.trackImages);
self.options.trackImages = Ox.makeArray(self.options.trackImages);
self.trackColors = self.options.trackColors.length;
self.trackImages = self.options.trackImages.length;

View file

@ -77,7 +77,7 @@ Ox.Select = function(options, self) {
return Ox.isEmpty(item) ? item : {
id: isObject ? item.id : item,
title: isObject ? item.title : item,
checked: Ox.toArray(self.options.value).indexOf(
checked: Ox.makeArray(self.options.value).indexOf(
isObject ? item.id : item
) > -1,
disabled: isObject ? item.disabled : false
@ -213,7 +213,7 @@ Ox.Select = function(options, self) {
if (self.options.type == 'text' && !self.options.title) {
self.$title.html(getItem(value).title);
}
value !== '' && Ox.toArray(value).forEach(function(value) {
value !== '' && Ox.makeArray(value).forEach(function(value) {
self.$menu.checkItem(value);
});
self.options.value = self.optionGroup.value();
@ -248,7 +248,7 @@ Ox.Select = function(options, self) {
() -> <o> returns array of selected items with id and title
@*/
that.selected = function() {
return Ox.toArray(self.optionGroup.value()).map(function(id) {
return Ox.makeArray(self.optionGroup.value()).map(function(id) {
return {
id: id,
title: getItem(id).title

View file

@ -297,7 +297,7 @@ Ox.List = function(options, self) {
function addToSelection(pos) {
var triggerEvent = false;
Ox.toArray(pos).forEach(function(pos) {
Ox.makeArray(pos).forEach(function(pos) {
if (!isSelected(pos)) {
self.selected.push(pos);
!Ox.isUndefined(self.$items[pos])
@ -364,7 +364,7 @@ Ox.List = function(options, self) {
function deselect(pos) {
var triggerEvent = false;
Ox.toArray(pos).forEach(function(pos) {
Ox.makeArray(pos).forEach(function(pos) {
if (isSelected(pos)) {
self.selected.splice(self.selected.indexOf(pos), 1);
!Ox.isUndefined(self.$items[pos])

View file

@ -41,7 +41,7 @@ Ox.MenuItem = function(options, self) {
})
.options(Ox.extend(Ox.clone(options), {
keyboard: parseKeyboard(options.keyboard || self.defaults.keyboard),
title: Ox.toArray(options.title || self.defaults.title)
title: Ox.makeArray(options.title || self.defaults.title)
}))
.addClass('OxItem' + (self.options.disabled ? ' OxDisabled' : ''))
/*
@ -135,7 +135,7 @@ Ox.MenuItem = function(options, self) {
} else if (key == 'disabled') {
that.toggleClass('OxDisabled');
} else if (key == 'title') {
self.options.title = Ox.toArray(value);
self.options.title = Ox.makeArray(value);
that.$title.html(self.options.title[0]);
}
};

View file

@ -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] = [];

View file

@ -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

View file

@ -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;

View file

@ -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);

View file

@ -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)/*,

View file

@ -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;

View file

@ -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('');

View file

@ -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];
});
}

View file

@ -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) {

View 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;

View file

@ -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();
}
};