Comparing version 2.0.0 to 2.1.0
31
index.js
@@ -0,1 +1,3 @@ | ||
var getAtPath = require('get-at-path'); | ||
function Accessor() { | ||
@@ -23,4 +25,5 @@ var cachedAccessors = { | ||
property = prop; | ||
} | ||
else { | ||
} else if (typeof prop === 'object' && typeof prop.path === 'string') { | ||
property = prop.path.split('/'); | ||
} else { | ||
property = '' + prop; | ||
@@ -38,16 +41,24 @@ } | ||
function createAccessor(property, defaultValue) { | ||
return function accessProperty(d) { | ||
return accessProperty; | ||
function accessProperty(d) { | ||
if (typeof d === 'object') { | ||
var value = d[property]; | ||
var value = getPropFromObject(d, property); | ||
if (value === undefined) { | ||
return defaultValue; | ||
} else { | ||
return value; | ||
} | ||
else { | ||
return d[property]; | ||
} | ||
} | ||
else { | ||
} else { | ||
return defaultValue; | ||
} | ||
}; | ||
} | ||
function getPropFromObject(d, prop) { | ||
if (typeof prop === 'string') { | ||
return d[prop]; | ||
} else if (Array.isArray(prop)) { | ||
return getAtPath(d, prop); | ||
} | ||
} | ||
} | ||
@@ -54,0 +65,0 @@ } |
{ | ||
"name": "accessor", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Provides accessor functions for convenience in D3 programming", | ||
@@ -25,5 +25,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tape": "^3.0.3" | ||
"tape": "^4.10.1" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"get-at-path": "^1.0.1" | ||
} | ||
} |
@@ -25,2 +25,8 @@ accessor | ||
If you want to write accessors that traverse a path in an object to get properties, do this: | ||
thingElements.text(accessor({ path: 'data/meta/label')); | ||
That will make it look for an object named `data`, then look for an object named `meta` in that, then look for a property named `label` within that. If you need to traverse arrays you can use array indexes in the path, e.g. `data/list/3/label`. | ||
Installation | ||
@@ -27,0 +33,0 @@ ------------ |
Sorry, the diff of this file is not supported yet
6649
107
70
1
6
+ Addedget-at-path@^1.0.1
+ Addedget-at-path@1.0.1(transitive)