Peer-reviewed code snippets that anyone can edit
follow refactory on twitter
blog
feedback
A wiki for useful code snippets
Bugs? Suggestions?
38-107-191-111
create/login
options
RECENT
STUBS/REQUESTS
STARRED
ACTIVITY
ADD
VIEW
EDIT
HISTORY
FORK
Array.make - transforms list-like data structures into native arrays
javascript
array-generic
htmlcollection
nodelist
string
Your work won't be attributed to you
because you aren't logged in.
Login using OpenID or an existing username, or create a username
(no email required) before posting.
Languages
Comma separated. Like
ruby, rails
or
java, swing
Keywords
Comma separated. Like
file, network
Mark as stub
Snippet
Wrap code in
[code=
language
][/code]
- Use
WikiText markup
outside of [code] tags
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]}}: [code=javascript]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");[/code] {{Array.make}} failing silently: [code=javascript]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");[/code]
Log message
Human?
public snippets
This is a community-maintained collection of reusable code snippets.
Contribute something
without logging in, or improve existing contributions. All code is dedicated to the public domain unless otherwise specified.
stats
/
top contributers