Skip to content

Commit

Permalink
added ability to call methods by name in the chain, fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 5, 2014
1 parent 0091bcd commit 34634b6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
12 changes: 8 additions & 4 deletions README.md
Expand Up @@ -35,7 +35,7 @@ var line = d3.svg.line()
```

This is very common D3 callback code. Here is the same code with callbacks refactored
to use *d3-helpers*
with *d3-helpers*

```js
var line = d3.svg.line()
Expand Down Expand Up @@ -63,20 +63,21 @@ augmented by other tiny functions. First the *d3h* function itself

Returns a function that can chain property access and function composition.

`d3h('propertyName', fnToApply, 'anotherPropertyName', orAnotherFn, ...);`
`d3h('propertyName', fnToApply, 'method name to call', 'anotherPropertyName', orAnotherFn, ...);`

```js
var foo = {
getName: function() { return this.name; },
name: 'foo'
};
function concatSelf(x) { return x + x; }
function add2(x) { return x + 2; }
var f = d3h('name', concatSelf, 'length', add2);
var f = d3h('getName', concatSelf, 'length', add2);
f(foo) // returns 8

// f is the same as
function (obj) {
return add2(concatSelf(obj.name).length);
return add2(concatSelf(obj.getName()).length);
}
```

Expand All @@ -88,6 +89,9 @@ Use on `d` argument:
.(d3h.d('length', xScale));
```

When calling a method on the object, `this` is bound to the
object, and it is passed itself as first argument.

### d3h.i

Same chaining as `d3h.d` but operates on the second argument, usually the index
Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "d3-helpers",
"main": "index.js",
"version": "0.2.0",
"version": "0.2.1",
"homepage": "https://github.com/bahmutov/d3-helpers",
"license": "MIT",
"ignore": [
Expand Down
6 changes: 5 additions & 1 deletion index.js
Expand Up @@ -9,7 +9,11 @@
return function (d) {
fns.forEach(function (fn) {
if (typeof fn === 'string') {
d = d[fn];
if (typeof d[fn] === 'function') {
d = d[fn].call(d, d);
} else {
d = d[fn];
}
} else if (typeof fn === 'function') {
d = fn(d);
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "d3-helpers",
"description": "Little utility D3 functions",
"version": "0.2.0",
"version": "0.2.1",
"author": "Gleb Bahmutov <gleb.bahmutov@gmail.com>",
"bugs": {
"url": "https://github.com/bahmutov/d3-helpers/issues"
Expand Down
19 changes: 19 additions & 0 deletions test/helpers.spec.js
Expand Up @@ -35,6 +35,25 @@ describe('d3h d3-helpers', function () {
}
expect(explicit(foo)).to.equal(8);
});

it('can call functions', function () {
var data = {
name: function () {
return 'foo';
}
};
expect(d3h('name')(data)).to.equal('foo');
});

it('method, get property, execute function', function () {
var data = {
self: function () {
return this;
},
age: 10
};
expect(d3h('self', 'age', add2)(data)).to.equal(12);
});
});

it('is a collection of functions', function () {
Expand Down

0 comments on commit 34634b6

Please sign in to comment.