Skip to content

Commit

Permalink
feat($d3-ext) Select by string or d3.selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
sym3tri authored and Ed Rooth committed Jan 17, 2013
1 parent c668947 commit 2ee75f4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/d3-ext/util.js
@@ -0,0 +1,23 @@
/**
* @fileOverview
* Utility functions related to d3 selections.
*/
define(
function() {
'use strict';

return {

/**
* Same as d3.select but allows a d3.selection as input too.
* @param {d3.selection|string} selection
*/
select: function(selection) {
return (selection instanceof d3.selection) ?
selection :
d3.select(selection);
}

};

});
32 changes: 32 additions & 0 deletions test/unit/d3-ext/util.spec.js
@@ -0,0 +1,32 @@
define([
'd3-ext/util'
],
function(selection) {
'use strict';

describe('d3-ext.selection', function() {

beforeEach(function() {
});

it('selects with a text selector', function() {
var fixture = jasmine.htmlFixture(),
sel = selection.select('#html-fixture');
expect(sel.node()).toBe(fixture.node());
});

it('selects with a d3 selection', function() {
var fixture = jasmine.htmlFixture(),
sel = selection.select(d3.select('#html-fixture'));
expect(sel.node()).toBe(fixture.node());
});

it('selects with an DOM node', function() {
var fixture = jasmine.htmlFixture(),
sel = selection.select(fixture.node());
expect(sel.node()).toBe(fixture.node());
});

});

});

0 comments on commit 2ee75f4

Please sign in to comment.