data-shaper
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -5,2 +5,4 @@ 'use strict'; | ||
var merge = require('lodash.merge'); | ||
var hashFetchDataCall = require('./lib/helpers').hashFetchDataCall; | ||
var resolveValue = require('./lib/resolve-value'); | ||
@@ -61,3 +63,3 @@ var resolveFragment = require('./lib/resolve-fragment'); | ||
if (shaperOptions.memoize) { | ||
shaperOptions.fetchData = async.memoize(shaperOptions.fetchData); | ||
shaperOptions.fetchData = async.memoize(shaperOptions.fetchData, hashFetchDataCall); | ||
} | ||
@@ -64,0 +66,0 @@ |
@@ -30,5 +30,20 @@ 'use strict'; | ||
/** | ||
* Hash a fetchData function call for memoization. This approach is | ||
* extremely naïve and depends on the id and reference combination | ||
* to always resolve to the same item. This however should always | ||
* be the case for fetchData functions passed to the data shaper. | ||
* | ||
* @param {int} id | ||
* @param {string} reference | ||
* @return {string} hash of function call | ||
*/ | ||
function hashFetchDataCall(id, reference) { | ||
return reference + '::' + id; | ||
} | ||
module.exports = { | ||
splitReference: splitReference, | ||
getPartOfReference: getPartOfReference | ||
getPartOfReference: getPartOfReference, | ||
hashFetchDataCall: hashFetchDataCall | ||
}; |
@@ -53,5 +53,5 @@ 'use strict'; | ||
// Resolve next level | ||
resolveValue( | ||
options.resolveValue( | ||
data, | ||
getPartOfReference(reference, 1), | ||
reference.substr(dotPosition + 1), | ||
options, | ||
@@ -58,0 +58,0 @@ callback |
{ | ||
"name": "data-shaper", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Utility for building meaningful data shapes from normalized, related data", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -30,4 +30,7 @@ # Data shaper | ||
var companyShape = { | ||
id: 'id', | ||
name: 'name' | ||
collectionName: 'companies', | ||
shape: { | ||
id: 'id', | ||
name: 'name' | ||
} | ||
}; | ||
@@ -58,4 +61,4 @@ | ||
{ | ||
persons: { '1' : { id: 1, name: 'Kristoffer', employer: { employers: 1 } } }, | ||
employers: { '2' : { id: 2, name: 'VG' } } | ||
persons: { '1' : { id: 1, name: 'Kristoffer', company: { companies: 2 } } }, | ||
companies: { '2' : { id: 2, name: 'VG' } } | ||
} | ||
@@ -62,0 +65,0 @@ ``` |
@@ -64,3 +64,3 @@ 'use strict'; | ||
dataShaper( | ||
[{id: 1, name: 'Fred', zipId: 1234}], | ||
[{ id: 1, name: 'Fred', zipId: 1234 }], | ||
duplicateShape, | ||
@@ -67,0 +67,0 @@ { fetchData: fetchCompanyData }, |
@@ -36,2 +36,12 @@ 'use strict'; | ||
}); | ||
describe('#hashFetchDataCall', function() { | ||
it('disregards the callback parameter when hashing', function(done) { | ||
var hash1 = helpers.hashFetchDataCall(1337, 'foobarId', function foo() {}); | ||
var hash2 = helpers.hashFetchDataCall(1337, 'foobarId', function bar() {}); | ||
assert.strictEqual(hash1, hash2); | ||
done(); | ||
}); | ||
}); | ||
}); |
'use strict'; | ||
var assert = require('assert'); | ||
var merge = require('lodash.merge'); | ||
var resolveValue = require('../lib/resolve-value'); | ||
@@ -8,2 +11,6 @@ var mockError = require('./mock/error'); | ||
var defaultOptions = { | ||
resolveValue: resolveValue, | ||
}; | ||
describe('Resolve value', function() { | ||
@@ -13,3 +20,3 @@ var data = { id: 1, lastName: 'Flintstone', companyId: 2}; | ||
it('takes local values off the data object', function(done) { | ||
resolveValue(data, 'lastName', {}, function(err, res) { | ||
resolveValue(data, 'lastName', defaultOptions, function(err, res) { | ||
assert(!err); | ||
@@ -22,11 +29,23 @@ assert.equal(res, data.lastName); | ||
it('resolves dot notated references', function(done) { | ||
var companyData = { id: 2, name: 'VG' }; | ||
var customData = { | ||
companyId: { '2': { id: 2, name: 'VG', municipalId: 1 } }, | ||
municipalId: { '1' : { id: 1, name: 'Oslo', countryId: 4 }}, | ||
countryId: { '4' : { id: 4, name: 'Norway' }} | ||
}; | ||
// Data fetcher that responds to id and reference params and returns | ||
// mock data for a few different collections | ||
var customFetchData = function(id, reference, callback) { | ||
process.nextTick(function() { | ||
callback(null, customData[reference][String(id)]); | ||
}); | ||
} | ||
resolveValue( | ||
data, | ||
'companyId.name', | ||
{ fetchData: fetchData(companyData) }, | ||
function(err, res) { | ||
'companyId.municipalId.countryId.name', | ||
merge({}, defaultOptions, { fetchData: customFetchData }), | ||
function(err, value) { | ||
assert(!err); | ||
assert.equal(res, companyData.name); | ||
assert.equal(value, customData.countryId['4'].name); | ||
done(); | ||
@@ -43,3 +62,3 @@ } | ||
'companyId.name', | ||
{ fetchData: mockError(errorText) }, | ||
merge({}, defaultOptions, { fetchData: mockError(errorText) }), | ||
function(err) { | ||
@@ -53,3 +72,3 @@ assert.equal(err, errorText); | ||
it('returns null if null is passed as data', function(done) { | ||
resolveValue(null, 'foobar', {}, function(err, res) { | ||
resolveValue(null, 'foobar', defaultOptions, function(err, res) { | ||
assert(!err); | ||
@@ -56,0 +75,0 @@ assert.equal(res, null); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33227
759
140