d3-helpers
Little utility D3 functions
Example
D3 code before
// x and y are scale functionsvar line = d3svg x { return ; } y { return ; };
This is very common D3 callback code. Here is the same code with callbacks refactored with d3-helpers
var line = d3svg x y;
Notice several benefits:
- The
.x
callback is easier to read from left to right: grab property date, then call d3h.newDate function on it, then call x function. No more inside out composition flow as inx(new Date(d.date))
- Allowing only property names and functions to apply makes the author's intention clear.
+d.y
is always ambiguous: did the author forget to add something or was this the intention? Writingd3h('y', Number, ...)
makes it explicit. - By eliminating writing each callback function, we eliminate potential sources of errors. In addition, since every function passed as argument is external, they becoming testable.
Install
Node:
npm install d3-helpers --save
var d3h = require('d3-helpers');
Browser:
bower install d3-helpers
<script src="bower_components/d3-helpers/index.js"></script>
// attaches as window.d3h object
Api
// index
d3h (alias d3h.d)
d3h.i
d3h.noop
d3h.undef
d3h.pass (alias d3.datum)
d3h.property
d3h.yes / no
d3h.index
d3h.value
d3h.newDate
d3h.hermit
d3-helpers is a well-tested function augmented by other tiny functions. First the d3h function itself
d3h = d3h.d
Returns a function that can chain property access and function composition.
d3h('propertyName', fnToApply, 'method name to call', 'anotherPropertyName', orAnotherFn, ...);
var foo = { return thisname; } name: 'foo'; { return x + x; } { return x + 2; }var f = ; // returns 8 // f is the same as { return ;}
Use on d
argument:
x; // d3h.d is an alias d3h;
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
yd3h// same asy { return ;};
If you want to return the index element, you need to execute the function to create the callback
yd3h// same asy { return i;};
d3h.noop
Same as function () {}
d3h.undef
Same as function () { return; }
if you need a function that
always returns undefined.
d3h.pass = d3.datum
Same as function (d) { return d; }
You can pass a function as argument, then it works as a wrapped function for any value passed next. For example to scale by function triple we can replace
function triple(x) { return 3 * x; }
.attr('left', function (d) { return triple(d); })
// with
.attr('left', d3h.pass(triple));
d3h.property
{ return { return objname; };}// if passed function(s) as additional arguments { return { return ; };}
Logically, read this from left to right. First grab named property, then apply function f, then apply to the result g, etc.
Useful to extract a property and convert type, for example for D3 selections
textd3hwidthd3htextd3h
or scale property from datum
var x = d3time y = d3scale;var line = d3svg x { return ; } y { return ; };// same using d3-helpersvar line = d3svg xd3h yd3h
d3h.yes / no
d3h.yes function always returns true
,
d3h.no function always returns false
.
d3h.index
Same as function (d, i) { return i; }
d3h.value
Same as
{ return { return val; };}
Value could be anything: number, string, undefined, even another function.
d3h.newDate
Same as function (d) { return new Date(d); }
to get around
the new
keyword requirement in JavaScript for Dates. Useful
for constructing Date instances inside d3h.property
var x = d3time;var line = d3svg x { return ; }// same using d3-helpersvar line = d3svg xd3h
d3h.hermit
Wraps a function that should not receive any arguments. Returned function just calls the original without any arguments.
{ if argumentslength throw 'I cannot handle arguments'; return 42;}d3h100; // 42
Related
I have another tiny library with single exported function called functional-pipeline. It is very similar to d3h function for building a left to right pipelines, but has better debug mode.
Small print
Author: Gleb Bahmutov © 2014
License: MIT - do anything with the code, but don't blame me if it does not work.
Spread the word: tweet, star on github, etc.
Support: if you find any problems with this module, email / tweet / open issue on Github
MIT License
Copyright (c) 2014 Gleb Bahmutov
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.