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

View file

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

View file

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

View file

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

View file

@ -40,7 +40,7 @@ Ox.ButtonGroup = function(options, self) {
tooltip: button.tooltip, tooltip: button.tooltip,
width: button.width width: button.width
}, self.options.selectable ? { }, 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) { self.options.checkboxes = self.options.checkboxes.map(function(checkbox) {
return { 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, id: checkbox.id || checkbox,
title: checkbox.title || 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.values[self.options.value] : self.options.value;
} }
self.options.arrowStep = options.arrowStep || self.options.step; 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.trackColors = self.options.trackColors.length;
self.trackImages = self.options.trackImages.length; self.trackImages = self.options.trackImages.length;

View file

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

View file

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

View file

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

View file

@ -238,7 +238,7 @@ Ox.api = function(items, options) {
return conditions.map(function(condition) { return conditions.map(function(condition) {
var key = condition.key, var key = condition.key,
operator = condition.operator, operator = condition.operator,
values = Ox.toArray(condition.value); values = Ox.makeArray(condition.value);
if (condition.conditions) { if (condition.conditions) {
condition.conditions = parseConditions(condition.conditions); condition.conditions = parseConditions(condition.conditions);
} else { } 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 // FIXME: a1.push.apply(a1, a2) should be much faster
Ox.merge = function(arr) { Ox.merge = function(arr) {
arr = Ox.toArray(arr); arr = Ox.makeArray(arr);
Ox.forEach(Array.prototype.slice.call(arguments, 1), function(arg) { 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); arr.push(val);
}); });
}); });
@ -406,7 +406,7 @@ Ox.range <f> Python-style range
@*/ @*/
Ox.range = function() { Ox.range = function() {
var arr = []; 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); arr.push(i);
})); }));
return arr; return arr;
@ -504,7 +504,7 @@ Ox.range = function() {
@*/ @*/
Ox.sortBy = function(arr, by, fn) { Ox.sortBy = function(arr, by, fn) {
var values = {}; var values = {};
by = Ox.toArray(by); by = Ox.makeArray(by);
fn = fn || {}; fn = fn || {};
by = by.map(function(v) { by = by.map(function(v) {
return Ox.isString(v) ? { return Ox.isString(v) ? {
@ -562,7 +562,7 @@ Ox.zip <f> Zips an array of arrays
[[0, 3], [1, 4], [2, 5]] [[0, 3], [1, 4], [2, 5]]
@*/ @*/
Ox.zip = function() { Ox.zip = function() {
var args = arguments.length == 1 ? arguments[0] : Ox.makeArray(arguments), var args = arguments.length == 1 ? arguments[0] : Ox.toArray(arguments),
arr = []; arr = [];
args[0].forEach(function(v, i) { args[0].forEach(function(v, i) {
arr[i] = []; arr[i] = [];

View file

@ -174,7 +174,7 @@ Ox.forEach <f> forEach loop
Ox.forEach = function(col, fn, includePrototype) { Ox.forEach = function(col, fn, includePrototype) {
var ind = 0, isObject = Ox.isObject(col), key; var ind = 0, isObject = Ox.isObject(col), key;
// Safari will not loop through an arguments array // 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) { for (key in col) {
key = isObject ? key : parseInt(key); key = isObject ? key : parseInt(key);
// fixme: fn.call(context, obj[key], key, obj) may be more standard... // fixme: fn.call(context, obj[key], key, obj) may be more standard...
@ -418,34 +418,22 @@ Ox.len = function(col) {
}; };
/*@ /*@
Ox.makeArray <f> Takes an array-like object and returns a true array Ox.makeArray <f> Wraps any non-array in an array.
Alias for <code>Array.prototype.slice.call</code> > Ox.makeArray('foo')
(value) -> <a> True array ['foo']
value <*> Array-like object > Ox.makeArray(['foo'])
> (function() { return Ox.makeArray(arguments); }("foo", "bar")) ['foo']
["foo", "bar"]
> Ox.makeArray("foo")
["f", "o", "o"]
> Ox.makeArray({0: "f", 1: "o", 2: "o", length: 3})
["f", "o", "o"]
@*/ @*/
// rewrite this so that it uses a try/catch test Ox.makeArray = function(obj) {
Ox.makeArray = /MSIE/.test(navigator.userAgent) var arr;
? function(col) { if (Ox.isArray(obj)) {
var i, len, ret = []; arr = obj;
try { } else if (Ox.isArguments(obj)) {
ret = Array.prototype.slice.call(col); arr = Ox.toArray(obj);
} catch(e) { } else {
// handle MSIE NodeLists arr = [obj];
len = col.length;
for (i = 0; i < len; i++) {
ret[i] = col[i];
} }
} return arr;
return ret;
}
: function(col) {
return Array.prototype.slice.call(col);
}; };
/*@ /*@
@ -672,7 +660,7 @@ Ox.sum <f> Returns the sum of the values of a collection
@*/ @*/
Ox.sum = function(col) { Ox.sum = function(col) {
var sum = 0; var sum = 0;
col = arguments.length > 1 ? Ox.makeArray(arguments) : col; col = arguments.length > 1 ? Ox.toArray(arguments) : col;
Ox.forEach(col, function(val) { Ox.forEach(col, function(val) {
val = +val; val = +val;
sum += Ox.isNumber(val) ? val : 0; sum += Ox.isNumber(val) ? val : 0;
@ -681,22 +669,34 @@ Ox.sum = function(col) {
}; };
/*@ /*@
Ox.toArray <f> Wraps any non-array in an array. Ox.toArray <f> Takes an array-like object and returns a true array
> Ox.toArray('foo') Alias for <code>Array.prototype.slice.call</code>
['foo'] (value) -> <a> True array
> Ox.toArray(['foo']) value <*> Array-like object
['foo'] > (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) { // rewrite this so that it uses a try/catch test
var arr; Ox.toArray = /MSIE/.test(navigator.userAgent)
if (Ox.isArray(obj)) { ? function(col) {
arr = obj; var i, len, ret = [];
} else if (Ox.isArguments(obj)) { try {
arr = Ox.makeArray(obj); ret = Array.prototype.slice.call(col);
} else { } catch(e) {
arr = [obj]; // handle MSIE NodeLists
len = col.length;
for (i = 0; i < len; i++) {
ret[i] = col[i];
} }
return arr; }
return ret;
}
: function(col) {
return Array.prototype.slice.call(col);
}; };
/*@ /*@

View file

@ -17,7 +17,7 @@ Ox.hsl <f> Takes RGB values and returns HSL values
@*/ @*/
Ox.hsl = function(rgb) { Ox.hsl = function(rgb) {
if (arguments.length == 3) { if (arguments.length == 3) {
rgb = Ox.makeArray(arguments); rgb = Ox.toArray(arguments);
} }
rgb = Ox.clone(rgb).map(function(val) { rgb = Ox.clone(rgb).map(function(val) {
return val / 255; return val / 255;
@ -64,7 +64,7 @@ Ox.rgb <f> Takes HSL values and returns RGB values
Ox.rgb = function(hsl) { Ox.rgb = function(hsl) {
if (arguments.length == 3) { if (arguments.length == 3) {
hsl = Ox.makeArray(arguments); hsl = Ox.toArray(arguments);
} }
hsl = Ox.clone(hsl); hsl = Ox.clone(hsl);
hsl[0] /= 360; hsl[0] /= 360;

View file

@ -77,7 +77,7 @@ Ox.SHORT_MONTHS = Ox.MONTHS.map(function(val) {
return val.substr(0, 3); return val.substr(0, 3);
}); });
//@ Ox.PATH <str> Path of Ox.js //@ Ox.PATH <str> Path of Ox.js
Ox.PATH = Ox.makeArray( Ox.PATH = Ox.toArray(
document.getElementsByTagName('script') document.getElementsByTagName('script')
).reverse().filter(function(element) { ).reverse().filter(function(element) {
return /Ox\.js$/.test(element.src); return /Ox\.js$/.test(element.src);

View file

@ -185,13 +185,13 @@ Ox.Log = (function() {
that.filter = function(val) { that.filter = function(val) {
if (!Ox.isUndefined(val)) { if (!Ox.isUndefined(val)) {
that.filter.enable(); that.filter.enable();
log.filter = Ox.toArray(val); log.filter = Ox.makeArray(val);
storage('log', log); storage('log', log);
} }
return log.filter; return log.filter;
}; };
that.filter.add = function(val) { 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() { that.filter.disable = function() {
log.filterEnabled = false; log.filterEnabled = false;
@ -202,13 +202,13 @@ Ox.Log = (function() {
storage('log', log); storage('log', log);
}; };
that.filter.remove = function(val) { that.filter.remove = function(val) {
val = Ox.toArray(val); val = Ox.makeArray(val);
return that.filter(log.filter.filter(function(v) { return that.filter(log.filter.filter(function(v) {
return val.indexOf(v) == -1; return val.indexOf(v) == -1;
})); }));
}; };
that.log = function() { 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) { if (!log.filterEnabled || log.filter.indexOf(args[0]) > -1) {
date = new Date(); date = new Date();
args.unshift( args.unshift(
@ -264,7 +264,7 @@ Ox.loop = function() {
}; };
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); return Ox.typeOf(arg);
}), }),
fnIndex = type.indexOf('function'), fnIndex = type.indexOf('function'),
@ -315,7 +315,7 @@ Ox.print <f> Prints its arguments to the console
@*/ @*/
Ox.print = function() { Ox.print = function() {
var args = Ox.makeArray(arguments), var args = Ox.toArray(arguments),
date = new Date(); date = new Date();
args.unshift( args.unshift(
Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)/*, Ox.formatDate(date, '%H:%M:%S.') + (+date).toString().substr(-3)/*,

View file

@ -107,7 +107,7 @@ Ox.$ = Ox.element = function(val) {
@*/ @*/
append: function() { append: function() {
var that = this; var that = this;
Ox.toArray(arguments[0]).forEach(function(element) { Ox.makeArray(arguments[0]).forEach(function(element) {
that[0].appendChild(element[0]); that[0].appendChild(element[0]);
}); });
return this; return this;
@ -254,7 +254,7 @@ Ox.$ = Ox.element = function(val) {
key <s> The attribute key <s> The attribute
@*/ @*/
removeAttr: function() { removeAttr: function() {
Ox.toArray(arguments[0]).forEach(function(key) { Ox.makeArray(arguments[0]).forEach(function(key) {
this[0].removeAttribute(key); this[0].removeAttribute(key);
}); });
return this; return this;

View file

@ -252,7 +252,7 @@
// Unfortunately, we can't synchronously set the source of an image, // Unfortunately, we can't synchronously set the source of an image,
// draw it onto a canvas, and read its data. // draw it onto a canvas, and read its data.
image.onload = function() { 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. // Read one character per RGB byte, ignore ALPHA.
return i % 4 < 3 ? Ox.char(v) : ''; return i % 4 < 3 ? Ox.char(v) : '';
}).join(''); }).join('');

View file

@ -16,12 +16,12 @@ Ox.cache = function(fn, options) {
}, options || {}) }, options || {})
var cache = {}, var cache = {},
ret = function() { ret = function() {
var args = Ox.makeArray(arguments), var args = Ox.toArray(arguments),
callback, callback,
key = options.key(args); key = options.key(args);
function callback() { function callback() {
// cache all arguments passed to callback // cache all arguments passed to callback
cache[key] = Ox.makeArray(arguments); cache[key] = Ox.toArray(arguments);
// call the original callback // call the original callback
Ox.last(args).apply(this, arguments); Ox.last(args).apply(this, arguments);
} }
@ -44,7 +44,7 @@ Ox.cache = function(fn, options) {
if (arguments.length == 0) { if (arguments.length == 0) {
cache = {}; cache = {};
} else { } else {
Ox.toArray(arguments).forEach(function(key) { Ox.makeArray(arguments).forEach(function(key) {
delete cache[key]; delete cache[key];
}); });
} }

View file

@ -290,7 +290,7 @@ Ox.doc = (function() {
} }
return function(/* source | file, callback | files, callback*/) { return function(/* source | file, callback | files, callback*/) {
var source = arguments.length == 1 ? arguments[0] : void 0, 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], callback = arguments[1],
counter = 0, items = []; counter = 0, items = [];
files && files.forEach(function(file) { files && files.forEach(function(file) {

View file

@ -41,7 +41,7 @@ Ox.getJSON = (function() {
}); });
} }
return function(url, callback) { 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) { urls.forEach(function(url) {
getJSON(url, function(data_) { getJSON(url, function(data_) {
data[url] = data_; data[url] = data_;
@ -128,7 +128,7 @@ Ox.loadFile = (function() {
} }
} }
function findFileInHead() { function findFileInHead() {
return Ox.makeArray( return Ox.toArray(
document.getElementsByTagName(type == 'css' ? 'link' : 'script') document.getElementsByTagName(type == 'css' ? 'link' : 'script')
).map(function(element) { ).map(function(element) {
return element[type == 'css' ? 'href' : 'src'] == file; return element[type == 'css' ? 'href' : 'src'] == file;
@ -163,9 +163,9 @@ Ox.loadFiles <f> Loads multiple files (images, scripts or stylesheets)
@*/ @*/
Ox.loadFiles = (function() { Ox.loadFiles = (function() {
function loadFiles(files, callback) { function loadFiles(files, callback) {
files = Ox.toArray(files); files = Ox.makeArray(files);
var i = 0, n = files.length, images = {}; var i = 0, n = files.length, images = {};
Ox.toArray(files).forEach(function(file) { Ox.makeArray(files).forEach(function(file) {
Ox.loadFile(file, function(image) { Ox.loadFile(file, function(image) {
if (image) { if (image) {
images[file] = 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 type <s> Type
@*/ @*/
Ox.checkType = function(val, 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(); throw new TypeError();
} }
}; };