Two versions of an Array.make method that handles list-like data structures such as [HTMLCollection]s / [NodeList]s, [String]s or [arguments] -arrays turning them into native array objects.
This firstly implemented version creates an Array.make that will throw a [TypeError] if it got passed invalid arguments whereas the latter in that case will fail silently.
Array.make throwing a [TypeError]:
Array.make = (function () { var make, arr, str, all = (document.getElementsByTagName && document.getElementsByTagName("*")), slice = Array.prototype.slice, exposeImplementation = Object.prototype.toString, isString = (function () { var regXBaseClass = (/^\[object\s+String\]$/); return (function (obj/*:[object|value]*/) { return regXBaseClass.test(exposeImplementation.call(obj)); }); })(), isArray = (function () { var regXBaseClass = (/^\[object\s+Array\]$/); return (function (obj/*:[object|value]*/) { return regXBaseClass.test(exposeImplementation.call(obj)); }); })(); try { arr = slice.call(all); // [NodeList|HTMLCollection] test. // msie fails. arr = slice.call(arguments); // [Array:arguments] test. str = arr.join(""); if ((arr.length != 3) || (str != "Array.make")) { throw (new Error); } arr = slice.call(str); // [String] test. // opera and msie fail. if ((arr.length !== 10) || (arr[5] !== ".")) { throw (new Error); } make = (function (list) { var arr, len = ((list || isString(list)) && list.length); if ((typeof len == "number") && isFinite(len)) { // detect invalid list structures. arr = (slice.call(list) || (new Array(len))); } else { throw (new TypeError("1st argument needs to be some kind of list.")); } return (arr || list); // (arr || []); // (arr || [list]); // there might be a debate on it. }); delete isArray; } catch (err) { make = (function (list) { var len, arr = ((isString(list) && list.split("")) || (isArray(list) && slice.call(list)) || arr); // [String] and [Array] test shortcut. if (!arr) { len = ((list !== 0) && list && list.length); // prevents passing zero as an argument. if ((typeof len == "number") && isFinite(len)) { // detect invalid list structures. arr = new Array(len); var i = 0, elm; while (i < len) { elm = (/*(list.charAt && list.charAt(i)) || */(list.item && list.item(i)) || list[i]); // [String|string].charAt is not necessary anymore. if ((typeof elm != "undefined") || (i in list)) { arr[i] = elm; } ++i; } } else { throw (new TypeError("1st argument needs to be some kind of list.")); } } return (arr || list); // (arr || []); // (arr || [list]); // there might be a debate on it. }); } delete arr; delete str; delete all; return make; })("Array", ".", "make");
Array.make failing silently:
Array.make = (function () { var make, arr, str, all = (document.getElementsByTagName && document.getElementsByTagName("*")), slice = Array.prototype.slice, exposeImplementation = Object.prototype.toString, isString = (function () { var regXBaseClass = (/^\[object\s+String\]$/); return (function (obj/*:[object|value]*/) { return regXBaseClass.test(exposeImplementation.call(obj)); }); })(), isArray = (function () { var regXBaseClass = (/^\[object\s+Array\]$/); return (function (obj/*:[object|value]*/) { return regXBaseClass.test(exposeImplementation.call(obj)); }); })(); try { arr = slice.call(all); // [NodeList|HTMLCollection] test. // msie fails. arr = slice.call(arguments); // [Array:arguments] test. str = arr.join(""); if ((arr.length != 3) || (str != "Array.make")) { throw (new Error); } arr = slice.call(str); // [String] test. // opera and msie fail. if ((arr.length !== 10) || (arr[5] !== ".")) { throw (new Error); } make = (function (list) { var len = ((list || isString(list)) && list.length); return (((typeof len == "number") && isFinite(len) && (slice.call(list) || (new Array(len)))) || list); // ( ... || []); // ( ... || [list]); // there might be a debate on it. }); delete isArray; } catch (err) { make = (function (list) { var len, arr = ((isString(list) && list.split("")) || (isArray(list) && slice.call(list)) || arr); // [String] and [Array] test shortcut. if (!arr) { len = ((list !== 0) && list && list.length); // prevents passing zero as an argument. if ((typeof len == "number") && isFinite(len)) { // detect invalid list structures. arr = new Array(len); var i = 0, elm; while (i < len) { elm = (/*(list.charAt && list.charAt(i)) || */(list.item && list.item(i)) || list[i]); // [String|string].charAt is not necessary anymore. if ((typeof elm != "undefined") || (i in list)) { arr[i] = elm; } ++i; } } } return (arr || list); // (arr || []); // (arr || [list]); // there might be a debate on it. }); } delete arr; delete str; delete all; return make; })("Array", ".", "make");