Skip to content

Commit

Permalink
Split d3_array into d3_arrayNodes and d3_arrayArguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasondavies committed Feb 18, 2011
1 parent fe5fdb3 commit e6bfd66
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 31 deletions.
40 changes: 12 additions & 28 deletions src/core/array.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
function d3_array(psuedoarray) {
var d3_arrayArguments = d3_arraySlice, // conversion for arguments
d3_arrayNodes = d3_arraySlice; // conversion for NodeLists

function d3_arraySlow(psuedoarray) {
var i = -1, n = psuedoarray.length, array = [];
while (++i < n) array.push(psuedoarray[i]);
return array;
}

function d3_arraySlice(psuedoarray) {
return Array.prototype.slice.call(psuedoarray);
}

// Adapted from Sizzle.js:
//
// Perform a simple check to determine if the browser is capable of
// converting a NodeList to an array using builtin methods.
// Also verifies that the returned array holds DOM nodes
// (which is not the case in the Blackberry browser)
try {
Array.prototype.slice.call(document.documentElement.childNodes, 0)[0].nodeType;
// Provide a fallback method if it does not work
d3_arrayNodes(document.documentElement.childNodes)[0].nodeType;
} catch(e) {
d3_array = function(array) {
var i = 0,
ret = [];

if (toString.call(array) === "[object Array]") {
Array.prototype.push.apply(ret, array);
} else {
if (typeof array.length === "number") {
for (var l = array.length; i < l; i++) {
ret.push(array[i]);
}
} else {
for (; array[i]; i++) {
ret.push(array[i]);
}
}
}
return ret;
};
d3_arrayNodes = d3_arraySlow;
}
2 changes: 1 addition & 1 deletion src/core/call.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function d3_call(callback, var_args) {
var_args = d3_array(arguments);
var_args = d3_arrayArguments(arguments);
var_args[0] = this;
callback.apply(this, var_args);
return this;
Expand Down
4 changes: 2 additions & 2 deletions src/core/selection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var d3_select = function(s, n) { return n.querySelector(s); },
d3_selectAll = function(s, n) { return d3_array(n.querySelectorAll(s)); };
d3_selectAll = function(s, n) { return d3_arrayNodes(n.querySelectorAll(s)); };

// Use Sizzle, if available.
if (typeof Sizzle == "function") {
Expand All @@ -20,7 +20,7 @@ d3.select = function(selector) {
d3.selectAll = function(selector) {
return typeof selector == "string"
? d3_root.selectAll(selector)
: d3_selection([d3_array(selector)]); // assume node[]
: d3_selection([d3_arrayNodes(selector)]); // assume node[]
};

function d3_selection(groups) {
Expand Down

0 comments on commit e6bfd66

Please sign in to comment.