Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One #300

Closed
wants to merge 10 commits into from
Closed

One #300

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ Returns the owner window for the specified *node*. If *node* is a node, returns

Returns the value of the style property with the specified *name* for the specified *node*. If the *node* has an inline style with the specified *name*, its value is returned; otherwise, the [computed property value](https://developer.mozilla.org/en-US/docs/Web/CSS/computed_value) is returned. See also [*selection*.style](#selection_style).

<a name="one" href="#one">#</a> d3.<b>one</b>(<i>selection</i>, <i>name</i>[, <i>class</i>]) · [Source](https://github.com/d3/d3-selection/blob/master/src/selection/one.js)

A convenience utility for managing a single element of the given <i>name</i> within the given <i>selection</i>. Optionally a <i>class</i> can be specified to disambiguate between siblings of the same name.

For example, consider the following logic for managing an axis container:

```js
const xAxisG = selection
.selectAll(`g.x-axis`)
.data([null])
.join('g')
.attr('class', 'x-axis');
```

The above logic can be expressed more concisely as:

```js
const xAxisG = one(selection, 'g', 'x-axis');
```

### Modifying Elements

After selecting elements, use the selection’s transformation methods to affect document content. For example, to set the name attribute and color style of an anchor element:
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {default as local} from "./local.js";
export {default as matcher} from "./matcher.js";
export {default as namespace} from "./namespace.js";
export {default as namespaces} from "./namespaces.js";
export {default as one} from "./one.js";
export {default as pointer} from "./pointer.js";
export {default as pointers} from "./pointers.js";
export {default as select} from "./select.js";
Expand Down
12 changes: 12 additions & 0 deletions src/one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default (selection, name, className) =>
className
? selection
.selectAll(`${name}.${className}`)
.data([null])
.join(name)
.attr('class', className)
: selection
.selectAll(name)
.data([null])
.join(name);

22 changes: 22 additions & 0 deletions test/selection/one-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from "assert";
import {select, one} from "../../src/index.js";
import it from "../jsdom.js";

it("selection.one(name) manages a single element", "<div id='container'></div>", () => {
const container = document.querySelector("#container");
const s = select(container);
const div = one(s, "div");
assert.strictEqual(div._groups[0][0].tagName, "DIV");
});

it("selection.one(name, class) elements by class", "<div id='container'></div>", () => {
const container = document.querySelector("#container");
const s = select(container);
const a = one(s, "div", "a");
const b = one(s, "div", "b");
assert.strictEqual(a._groups[0][0].tagName, "DIV");
assert.strictEqual(a._groups[0][0].className, "a");
assert.strictEqual(b._groups[0][0].tagName, "DIV");
assert.strictEqual(b._groups[0][0].className, "b");
});