Skip to content

Commit

Permalink
Merge pull request #2758 from SergioCrisostomo/Array.from
Browse files Browse the repository at this point in the history
Use Array.convert instead of Array.from, add compat layer for Array.from
  • Loading branch information
SergioCrisostomo committed Dec 8, 2015
2 parents 61efb25 + d33af9b commit 8ce77d1
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Docs/Core/Core.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ This method has been deprecated. Please use [Number.random](/core/Types/Number#N
Function: $splat {#Deprecated-Functions:splat}
-------------------------

This method has been deprecated. Please use [Array.from](/core/Types/Array#Array:Array-from) instead.
However `$splat` does *not* transform Array-like objects such as NodeList or FileList in arrays, `Array.from` does.
This method has been deprecated. Please use [Array.convert](/core/Types/Array#Array:Array:convert) instead.
However `$splat` does *not* transform Array-like objects such as NodeList or FileList in arrays, `Array.convert` does.


Function: $time {#Deprecated-Functions:time}
Expand Down
31 changes: 22 additions & 9 deletions Docs/Types/Array.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,14 @@ Returns a copy of the passed array.

This is an array-specific equivalent of *$unlink* from MooTools 1.2.



Function: Array.from {#Array:Array-from}
Function: Array.convert {#Array:Array:convert}
----------------------------------

Converts the argument passed in to an array if it is defined and not already an array.

### Syntax:

var splatted = Array.from(obj);
var splatted = Array.convert(obj);

### Arguments:

Expand All @@ -103,16 +101,14 @@ Converts the argument passed in to an array if it is defined and not already an

### Example:

Array.from('hello'); // returns ['hello'].
Array.from(['a', 'b', 'c']); // returns ['a', 'b', 'c'].
Array.convert('hello'); // returns ['hello'].
Array.convert(['a', 'b', 'c']); // returns ['a', 'b', 'c'].

### Notes:

This is equivalent to *$splat* from MooTools 1.2, with the exception of Array-like Objects such as NodeList or FileList which `Array.from` does transform in
This is equivalent to *$splat* from MooTools 1.2, with the exception of Array-like Objects such as NodeList or FileList which `Array.convert` does transform in
Arrays and `$splat` not.



Array method: each {#Array:each}
---------------------------------

Expand Down Expand Up @@ -778,11 +774,28 @@ Converts an RGB color value to hexadecimal. Input array must be in one of the fo
- [String:rgbToHex][]


Deprecated Functions {#Deprecated-Functions}
============================================

Function: Array.from {#Deprecated-Functions:Array:Array:from}
----------------------------------

This method has been deprecated in MooTools 1.6, please use *[Array:convert][]* instead.
For backwards compatibility you can use the _compat layer_ that still uses the old implementation, overriding the Native ES6 implementation.
Please use the no compat version instead, unless you know why you have to use the _compat layer_.

### See Also:

- [MDN Array:from][]



[Array:convert]: /core/Types/Array/#Array:convert
[Function:bind]: /core/Types/Function/#Function:bind
[String:hexToRgb]: /core/Types/String/#String:hexToRgb
[String:rgbToHex]: /core/Types/String/#String:rgbToHex
[MDN Array]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array
[MDN Array:from]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from
[MDN Array:every]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/every
[MDN Array:filter]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/filter
[MDN Array:indexOf]: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
Expand Down
6 changes: 3 additions & 3 deletions Source/Browser/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,11 @@ if (this.attachEvent && !this.addEventListener){
}

// IE fails on collections and <select>.options (refers to <select>)
var arrayFrom = Array.from;
var arrayFrom = Array.convert;
try {
arrayFrom(document.html.childNodes);
} catch (e){
Array.from = function(item){
Array.convert = function(item){
if (typeof item != 'string' && Type.isEnumerable(item) && typeOf(item) != 'array'){
var i = item.length, array = new Array(i);
while (i--) array[i] = item[i];
Expand All @@ -220,7 +220,7 @@ try {
['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift', 'concat', 'join', 'slice'].each(function(name){
var method = prototype[name];
Array[name] = function(item){
return method.apply(Array.from(item), slice.call(arguments, 1));
return method.apply(Array.convert(item), slice.call(arguments, 1));
};
});
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Class/Class.Extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ this.Events = new Class({
type = removeOn(type);
var events = this.$events[type];
if (!events) return this;
args = Array.from(args);
args = Array.convert(args);
events.each(function(fn){
if (delay) fn.delay(delay, this, args);
else fn.apply(this, args);
Expand Down
2 changes: 1 addition & 1 deletion Source/Class/Class.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Class.Mutators = {
},

Implements: function(items){
Array.from(items).each(function(item){
Array.convert(items).each(function(item){
var instance = new item;
for (var key in instance) implement.call(this, key, instance[key], true);
}, this);
Expand Down
16 changes: 10 additions & 6 deletions Source/Core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,20 @@ Function.prototype.implement = function(key, value){

var slice = Array.prototype.slice;

Array.convert = function(item){
if (item == null) return [];
return (Type.isEnumerable(item) && typeof item != 'string') ? (typeOf(item) == 'array') ? item : slice.call(item) : [item];
};

Function.from = function(item){
return (typeOf(item) == 'function') ? item : function(){
return item;
};
};

Array.from = function(item){
if (item == null) return [];
return (Type.isEnumerable(item) && typeof item != 'string') ? (typeOf(item) == 'array') ? item : slice.call(item) : [item];
};
/*<1.5compat>*/
Array.from = Array.convert;
/*</1.5compat>*/

Number.from = function(item){
var number = parseFloat(item);
Expand Down Expand Up @@ -479,7 +483,7 @@ Array.type = function(item){
};

this.$A = function(item){
return Array.from(item).slice();
return Array.convert(item).slice();
};

this.$arguments = function(i){
Expand Down Expand Up @@ -526,7 +530,7 @@ this.$merge = function(){
this.$lambda = Function.from;
this.$mixin = Object.merge;
this.$random = Number.random;
this.$splat = Array.from;
this.$splat = Array.convert;
this.$time = Date.now;

this.$type = function(object){
Expand Down
2 changes: 1 addition & 1 deletion Source/Element/Element.Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Element.Properties.events = {set: function(events){
fireEvent: function(type, args, delay){
var events = this.retrieve('events');
if (!events || !events[type]) return this;
args = Array.from(args);
args = Array.convert(args);

events[type].keys.each(function(fn){
if (delay) fn.delay(delay, this, args);
Expand Down
2 changes: 1 addition & 1 deletion Source/Element/Element.Style.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Element.implement({
property = camelCase(property == 'float' ? floatName : property);
if (typeOf(value) != 'string'){
var map = (Element.Styles[property] || '@').split(' ');
value = Array.from(value).map(function(val, i){
value = Array.convert(value).map(function(val, i){
if (!map[i]) return '';
return (typeOf(val) == 'number') ? map[i].replace('@', Math.round(val)) : val;
}).join(' ');
Expand Down
12 changes: 6 additions & 6 deletions Source/Element/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ Element.implement({
},

getProperties: function(){
var args = Array.from(arguments);
var args = Array.convert(arguments);
return args.map(this.getProperty, this).associate(args);
},

Expand Down Expand Up @@ -853,7 +853,7 @@ Element.implement({

getSelected: function(){
this.selectedIndex; // Safari 3.2.1
return new Elements(Array.from(this.options).filter(function(option){
return new Elements(Array.convert(this.options).filter(function(option){
return option.selected;
}));
},
Expand All @@ -869,7 +869,7 @@ Element.implement({
return document.id(opt).get('value');
}) : ((type == 'radio' || type == 'checkbox') && !el.checked) ? null : el.get('value');

Array.from(value).each(function(val){
Array.convert(value).each(function(val){
if (typeof val != 'undefined') queryString.push(encodeURIComponent(el.name) + '=' + encodeURIComponent(val));
});
});
Expand Down Expand Up @@ -938,7 +938,7 @@ Element.implement({
},

empty: function(){
Array.from(this.childNodes).each(Element.dispose);
Array.convert(this.childNodes).each(Element.dispose);
return this;
},

Expand All @@ -951,8 +951,8 @@ Element.implement({
var clone = this.cloneNode(contents), ce = [clone], te = [this], i;

if (contents){
ce.append(Array.from(clone.getElementsByTagName('*')));
te.append(Array.from(this.getElementsByTagName('*')));
ce.append(Array.convert(clone.getElementsByTagName('*')));
te.append(Array.convert(this.getElementsByTagName('*')));
}

for (i = ce.length; i--;){
Expand Down
4 changes: 2 additions & 2 deletions Source/Fx/Fx.CSS.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Fx.CSS = new Class({
//prepares the base from/to object

prepare: function(element, property, values){
values = Array.from(values);
values = Array.convert(values);
var from = values[0], to = values[1];
if (to == null){
to = from;
Expand Down Expand Up @@ -53,7 +53,7 @@ Fx.CSS = new Class({

parse: function(value){
value = Function.from(value)();
value = (typeof value == 'string') ? value.split(' ') : Array.from(value);
value = (typeof value == 'string') ? value.split(' ') : Array.convert(value);
return value.map(function(val){
val = String(val);
var found = false;
Expand Down
2 changes: 1 addition & 1 deletion Source/Fx/Fx.Transitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Fx.implement({
});

Fx.Transition = function(transition, params){
params = Array.from(params);
params = Array.convert(params);
var easeIn = function(pos){
return transition(pos, params);
};
Expand Down
2 changes: 1 addition & 1 deletion Source/Types/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Array.implement({
Array.alias('extend', 'append');

var $pick = this.$pick = function(){
return Array.from(arguments).pick();
return Array.convert(arguments).pick();
};

//</1.2compat>
12 changes: 6 additions & 6 deletions Source/Types/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Function.implement({

attempt: function(args, bind){
try {
return this.apply(bind, Array.from(args));
return this.apply(bind, Array.convert(args));
} catch (e){}

return null;
Expand Down Expand Up @@ -60,7 +60,7 @@ Function.implement({

pass: function(args, bind){
var self = this;
if (args != null) args = Array.from(args);
if (args != null) args = Array.convert(args);
return function(){
return self.apply(bind, args || arguments);
};
Expand All @@ -87,7 +87,7 @@ Function.implement({
options = options || {};
return function(event){
var args = options.arguments;
args = (args != null) ? Array.from(args) : Array.slice(arguments, (options.event) ? 1 : 0);
args = (args != null) ? Array.convert(args) : Array.slice(arguments, (options.event) ? 1 : 0);
if (options.event) args.unshift(event || window.event);
var returns = function(){
return self.apply(options.bind || null, args);
Expand All @@ -101,22 +101,22 @@ Function.implement({

bind: function(bind, args){
var self = this;
if (args != null) args = Array.from(args);
if (args != null) args = Array.convert(args);
return function(){
return self.apply(bind, args || arguments);
};
},

bindWithEvent: function(bind, args){
var self = this;
if (args != null) args = Array.from(args);
if (args != null) args = Array.convert(args);
return function(event){
return self.apply(bind, (args == null) ? arguments : [event].concat(args));
};
},

run: function(args, bind){
return this.apply(bind, Array.from(args));
return this.apply(bind, Array.convert(args));
}

});
Expand Down
2 changes: 1 addition & 1 deletion Source/Types/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var methods = {};

math.each(function(name){
if (!Number[name]) methods[name] = function(){
return Math[name].apply(null, [this].concat(Array.from(arguments)));
return Math[name].apply(null, [this].concat(Array.convert(arguments)));
};
});

Expand Down
Loading

0 comments on commit 8ce77d1

Please sign in to comment.