Comparing version 2.1.0 to 3.0.0
84
index.js
var getAtPath = require('get-at-path'); | ||
function Accessor() { | ||
var cachedAccessors = { | ||
identity: identity | ||
}; | ||
return accessor; | ||
function accessor(prop, defaultValue) { | ||
var property = 'id'; | ||
function accessor(prop, defaultValue) { | ||
var property = 'id'; | ||
var cachedAccessor; | ||
if (defaultValue === undefined) { | ||
// Never try to cache accessors that have default values. | ||
cachedAccessor = cachedAccessors[prop]; | ||
if (prop) { | ||
if (prop === 'identity') { | ||
return identity; | ||
} | ||
if (cachedAccessor) { | ||
return cachedAccessor; | ||
if (typeof prop === 'string') { | ||
property = prop; | ||
} else if (typeof prop === 'object' && typeof prop.path === 'string') { | ||
property = prop.path.split('/'); | ||
} else { | ||
property = '' + prop; | ||
} | ||
} | ||
if (prop) { | ||
if (typeof prop === 'string') { | ||
property = prop; | ||
} else if (typeof prop === 'object' && typeof prop.path === 'string') { | ||
property = prop.path.split('/'); | ||
} else { | ||
property = '' + prop; | ||
} | ||
} | ||
var accessProperty = createAccessor(property, defaultValue); | ||
return accessProperty; | ||
} | ||
var accessProperty = createAccessor(property, defaultValue); | ||
if (defaultValue !== undefined) { | ||
cachedAccessors[property] = defaultValue; | ||
} | ||
return accessProperty; | ||
} | ||
function createAccessor(property, defaultValue) { | ||
return accessProperty; | ||
function createAccessor(property, defaultValue) { | ||
return accessProperty; | ||
function accessProperty(d) { | ||
if (typeof d === 'object') { | ||
var value = getPropFromObject(d, property); | ||
if (value === undefined) { | ||
return defaultValue; | ||
} else { | ||
return value; | ||
} | ||
function accessProperty(d) { | ||
if (typeof d === 'object') { | ||
var value = getPropFromObject(d, property); | ||
if (value === undefined) { | ||
return defaultValue; | ||
} else { | ||
return defaultValue; | ||
return value; | ||
} | ||
} else { | ||
return defaultValue; | ||
} | ||
} | ||
function getPropFromObject(d, prop) { | ||
if (typeof prop === 'string') { | ||
return d[prop]; | ||
} else if (Array.isArray(prop)) { | ||
return getAtPath(d, prop); | ||
} | ||
function getPropFromObject(d, prop) { | ||
if (typeof prop === 'string') { | ||
return d[prop]; | ||
} else if (Array.isArray(prop)) { | ||
return getAtPath(d, prop); | ||
} | ||
@@ -65,8 +49,6 @@ } | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
module.exports = Accessor; | ||
} | ||
function identity(x) { | ||
return x; | ||
} | ||
module.exports = accessor; |
{ | ||
"name": "accessor", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Provides accessor functions for convenience in D3 programming", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -39,23 +39,14 @@ accessor | ||
If you're using Browserify: | ||
var accessor = require('accessor'); | ||
var Accessor = require('accessor'); | ||
Accessor takes an argument, `property`, and returns a function that takes an object and returns that `object.property`. If you provide no argument `property` will default to `id`. | ||
Otherwise: | ||
<script src="node_modules/accessor/index.js"> | ||
Then, you'll have the `Accessor` constructor. It will return to you an instance of accessor that takes an argument, `property`, and returns a function that takes an object and returns that `object.property`. If you provide no argument `property` will default to `id`. | ||
e.g. | ||
var Accessor = require('accessor'); | ||
var createAccessor = Accessor(); | ||
rootSelectAll('.leaf').data(things, createAccessor('foo')); | ||
var accessor = require('accessor'); | ||
rootSelectAll('.leaf').data(things, accessor('foo')); | ||
accessor instances cache the functions they create. So if you call `createAccessor('foo')` twice, you'll always get back the same accessor function. | ||
You can specify a second argument, a default value. The accessor will return the default value if the property you're accessing is undefined on the object. (It will never cache accessors that have a default value, BTW.) | ||
If you just want the identity function (x => x), you can use `createAccessor('identity')`. An inline `x => x` definition may actually be fine for your case; `createAccessor('identity')` just creates fewer copies of that function than that. | ||
If you just want the identity function (x => x), you can use `accessor('identity')`. An inline `x => x` definition may actually be fine for your case; `accessor('identity')` just creates fewer copies of that function than that. | ||
@@ -62,0 +53,0 @@ Tests |
var test = require('tape'); | ||
var Accessor = require('../index'); | ||
var accessor = require('../index'); | ||
@@ -15,3 +15,2 @@ test('Basic test', function basicTest(t) { | ||
var accessor = Accessor(); | ||
var getId = accessor(); | ||
@@ -18,0 +17,0 @@ var getOtherStuff = accessor('otherstuff'); |
var test = require('tape'); | ||
var Accessor = require('../index'); | ||
var accessor = require('../index'); | ||
@@ -19,3 +19,2 @@ test('Path test', pathTest); | ||
var accessor = Accessor(); | ||
var getNestedId = accessor({ path: 'data/id' }); | ||
@@ -22,0 +21,0 @@ var getPropObjectInArray = accessor({ path: 'otherstuff/2/number' }); |
5676
89
61