From 56921955097cb8b105f57de3db54b523e8036e5e Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 19 May 2012 12:40:59 +0400 Subject: [PATCH] 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 --- source/Ox.UI/js/Calendar/Ox.Calendar.js | 2 +- source/Ox.UI/js/Core/Ox.App.js | 2 +- source/Ox.UI/js/Core/Ox.Element.js | 4 +- source/Ox.UI/js/Core/Ox.Event.js | 2 +- source/Ox.UI/js/Form/Ox.ButtonGroup.js | 2 +- source/Ox.UI/js/Form/Ox.CheckboxGroup.js | 2 +- source/Ox.UI/js/Form/Ox.Range.js | 2 +- source/Ox.UI/js/Form/Ox.Select.js | 6 +- source/Ox.UI/js/List/Ox.List.js | 4 +- source/Ox.UI/js/Menu/Ox.MenuItem.js | 4 +- source/Ox/js/Array.js | 12 ++-- source/Ox/js/Collection.js | 92 ++++++++++++------------ source/Ox/js/Color.js | 4 +- source/Ox/js/Constants.js | 2 +- source/Ox/js/Core.js | 12 ++-- source/Ox/js/DOM.js | 4 +- source/Ox/js/Encoding.js | 2 +- source/Ox/js/Function.js | 6 +- source/Ox/js/JavaScript.js | 2 +- source/Ox/js/Request.js | 8 +-- source/Ox/js/Type.js | 2 +- 21 files changed, 88 insertions(+), 88 deletions(-) diff --git a/source/Ox.UI/js/Calendar/Ox.Calendar.js b/source/Ox.UI/js/Calendar/Ox.Calendar.js index ee2efd16..7a3e7c4e 100644 --- a/source/Ox.UI/js/Calendar/Ox.Calendar.js +++ b/source/Ox.UI/js/Calendar/Ox.Calendar.js @@ -1275,7 +1275,7 @@ Ox.Calendar = function(options, self) { {id, {key: value, ...}} -> 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), diff --git a/source/Ox.UI/js/Core/Ox.App.js b/source/Ox.UI/js/Core/Ox.App.js index ce2f2cea..ebd36880 100644 --- a/source/Ox.UI/js/Core/Ox.App.js +++ b/source/Ox.UI/js/Core/Ox.App.js @@ -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 diff --git a/source/Ox.UI/js/Core/Ox.Element.js b/source/Ox.UI/js/Core/Ox.Element.js index a4f42a06..a8e10dae 100644 --- a/source/Ox.UI/js/Core/Ox.Element.js +++ b/source/Ox.UI/js/Core/Ox.Element.js @@ -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); }); } diff --git a/source/Ox.UI/js/Core/Ox.Event.js b/source/Ox.UI/js/Core/Ox.Event.js index 30ecf08c..a7ed921e 100644 --- a/source/Ox.UI/js/Core/Ox.Event.js +++ b/source/Ox.UI/js/Core/Ox.Event.js @@ -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); }); } diff --git a/source/Ox.UI/js/Form/Ox.ButtonGroup.js b/source/Ox.UI/js/Form/Ox.ButtonGroup.js index 5a0fc0b9..78afdc36 100644 --- a/source/Ox.UI/js/Form/Ox.ButtonGroup.js +++ b/source/Ox.UI/js/Form/Ox.ButtonGroup.js @@ -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 } : {}); }); diff --git a/source/Ox.UI/js/Form/Ox.CheckboxGroup.js b/source/Ox.UI/js/Form/Ox.CheckboxGroup.js index 10ac8c6d..8ad2f388 100644 --- a/source/Ox.UI/js/Form/Ox.CheckboxGroup.js +++ b/source/Ox.UI/js/Form/Ox.CheckboxGroup.js @@ -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 }; diff --git a/source/Ox.UI/js/Form/Ox.Range.js b/source/Ox.UI/js/Form/Ox.Range.js index 4cdf57ee..72209a4c 100644 --- a/source/Ox.UI/js/Form/Ox.Range.js +++ b/source/Ox.UI/js/Form/Ox.Range.js @@ -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; diff --git a/source/Ox.UI/js/Form/Ox.Select.js b/source/Ox.UI/js/Form/Ox.Select.js index 9d1666db..ed6d61f9 100644 --- a/source/Ox.UI/js/Form/Ox.Select.js +++ b/source/Ox.UI/js/Form/Ox.Select.js @@ -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) { () -> 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 diff --git a/source/Ox.UI/js/List/Ox.List.js b/source/Ox.UI/js/List/Ox.List.js index ea1476fe..0b182c40 100644 --- a/source/Ox.UI/js/List/Ox.List.js +++ b/source/Ox.UI/js/List/Ox.List.js @@ -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]) diff --git a/source/Ox.UI/js/Menu/Ox.MenuItem.js b/source/Ox.UI/js/Menu/Ox.MenuItem.js index ba0eaf83..b35894c8 100644 --- a/source/Ox.UI/js/Menu/Ox.MenuItem.js +++ b/source/Ox.UI/js/Menu/Ox.MenuItem.js @@ -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]); } }; diff --git a/source/Ox/js/Array.js b/source/Ox/js/Array.js index 28c161ad..aca78d33 100644 --- a/source/Ox/js/Array.js +++ b/source/Ox/js/Array.js @@ -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 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 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 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] = []; diff --git a/source/Ox/js/Collection.js b/source/Ox/js/Collection.js index b6053e58..d8711a66 100644 --- a/source/Ox/js/Collection.js +++ b/source/Ox/js/Collection.js @@ -174,7 +174,7 @@ Ox.forEach 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 Takes an array-like object and returns a true array - Alias for Array.prototype.slice.call - (value) -> 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 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 Takes an array and returns an object @@ -672,7 +660,7 @@ Ox.sum 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 Wraps any non-array in an array. - > Ox.toArray('foo') - ['foo'] - > Ox.toArray(['foo']) - ['foo'] +Ox.toArray Takes an array-like object and returns a true array + Alias for Array.prototype.slice.call + (value) -> 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 Returns the values of a collection diff --git a/source/Ox/js/Color.js b/source/Ox/js/Color.js index 2a2791b1..1c26e37b 100644 --- a/source/Ox/js/Color.js +++ b/source/Ox/js/Color.js @@ -17,7 +17,7 @@ Ox.hsl 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 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; diff --git a/source/Ox/js/Constants.js b/source/Ox/js/Constants.js index 8fd17ade..ac23c33a 100644 --- a/source/Ox/js/Constants.js +++ b/source/Ox/js/Constants.js @@ -77,7 +77,7 @@ Ox.SHORT_MONTHS = Ox.MONTHS.map(function(val) { return val.substr(0, 3); }); //@ Ox.PATH 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); diff --git a/source/Ox/js/Core.js b/source/Ox/js/Core.js index f2f87fbb..7bcc60e2 100644 --- a/source/Ox/js/Core.js +++ b/source/Ox/js/Core.js @@ -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 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)/*, diff --git a/source/Ox/js/DOM.js b/source/Ox/js/DOM.js index 57b03af2..308e0cd0 100644 --- a/source/Ox/js/DOM.js +++ b/source/Ox/js/DOM.js @@ -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 The attribute @*/ removeAttr: function() { - Ox.toArray(arguments[0]).forEach(function(key) { + Ox.makeArray(arguments[0]).forEach(function(key) { this[0].removeAttribute(key); }); return this; diff --git a/source/Ox/js/Encoding.js b/source/Ox/js/Encoding.js index 6f085641..fc6e64b2 100644 --- a/source/Ox/js/Encoding.js +++ b/source/Ox/js/Encoding.js @@ -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(''); diff --git a/source/Ox/js/Function.js b/source/Ox/js/Function.js index ce1e2ed6..5ed25724 100644 --- a/source/Ox/js/Function.js +++ b/source/Ox/js/Function.js @@ -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]; }); } diff --git a/source/Ox/js/JavaScript.js b/source/Ox/js/JavaScript.js index 557b1ce5..44ef989c 100644 --- a/source/Ox/js/JavaScript.js +++ b/source/Ox/js/JavaScript.js @@ -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) { diff --git a/source/Ox/js/Request.js b/source/Ox/js/Request.js index f9b1e79f..9bdc03fb 100644 --- a/source/Ox/js/Request.js +++ b/source/Ox/js/Request.js @@ -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 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; diff --git a/source/Ox/js/Type.js b/source/Ox/js/Type.js index 2bec9120..4b93f881 100644 --- a/source/Ox/js/Type.js +++ b/source/Ox/js/Type.js @@ -7,7 +7,7 @@ Ox.checkType Throws a TypeError if a value is not of a given type type 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(); } };