d3plus-common
Version:
Common functions and methods used across D3plus modules.
20 lines • 576 B
JavaScript
/**
@function accessor
@desc Wraps an object key in a simple accessor function.
@param {String} key The key to be returned from each Object passed to the function.
@param {*} [def] A default value to be returned if the key is not present.
@example <caption>this</caption>
accessor("id");
@example <caption>returns this</caption>
function(d) {
return d["id"];
}
*/
export default function (key, def) {
if (def === undefined) return function (d) {
return d[key];
};
return function (d) {
return d[key] === undefined ? def : d[key];
};
}