journeyapps
Advanced tools
Comparing version 0.2.9 to 0.2.10
@@ -119,2 +119,22 @@ | ||
// Merge hashes of hashes (no non-hash values) | ||
// Sample: | ||
// deepMerge({a: {}}, {b: {}}) => {a: {}, b: {}} | ||
// deepMerge({a: {b: {c: {}}}, d: {}}, {a: {e: {}}}) => {a: {b: {c: {}}, e: {}}, d: {}} | ||
function deepMerge(a, b) { | ||
if((typeof a != 'object') || (typeof b != 'object')) { | ||
throw new Error('Parameters must be objects only'); | ||
} | ||
Object.keys(b).forEach(function(key) { | ||
if(!(key in a)) { | ||
// There are no actual "values" here, except for more nested hashes. | ||
// The presence of keys is the important part. | ||
a[key] = {}; | ||
} | ||
deepMerge(a[key], b[key]); | ||
}); | ||
return a; | ||
} | ||
function extract(type, expression, into, depth) { | ||
@@ -127,3 +147,5 @@ var dot = expression.indexOf('.'); | ||
} else { | ||
into[expression] = objectType.displayFormat.extractRelationshipStructure(objectType, depth+1); | ||
var b = {}; | ||
b[expression] = objectType.displayFormat.extractRelationshipStructure(objectType, depth + 1); | ||
deepMerge(into, b); | ||
} | ||
@@ -139,3 +161,5 @@ } else { | ||
} else { | ||
into[head] = {}; | ||
if(!(head in into)) { | ||
into[head] = {}; | ||
} | ||
extract(child, tail, into[head]); | ||
@@ -149,3 +173,3 @@ } | ||
// This will recursively evaluate format strings where required. | ||
FormatString.prototype.extractRelationshipStructure = function(type, depth) { | ||
FormatString.prototype.extractRelationshipStructure = function(type, depth, into) { | ||
if(depth == null) { | ||
@@ -157,3 +181,3 @@ depth = 0; | ||
var result = {}; | ||
var result = into || {}; | ||
@@ -246,3 +270,4 @@ var tokens = this.tokens; | ||
compile: compile, | ||
formatString: formatString | ||
formatString: formatString, | ||
_deepMerge: deepMerge // Exposed for tests only | ||
}; |
{ | ||
"name": "journeyapps", | ||
"version": "0.2.9", | ||
"version": "0.2.10", | ||
"description": "Journey JS library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -123,2 +123,14 @@ | ||
it('should handle repeats', function() { | ||
companyType.displayFormat = new evaluator.FormatString("{name}"); | ||
var fs = new evaluator.FormatString('{company.category.name} {company.category} {company}'); | ||
expect(fs.extractRelationshipStructure(personType)).toEqual({company: {category: {}}}); | ||
}); | ||
it('should handle more repeats', function() { | ||
companyType.displayFormat = new evaluator.FormatString("{name}"); | ||
var fs = new evaluator.FormatString('{company.category.name} {company.category} {company.name}'); | ||
expect(fs.extractRelationshipStructure(personType)).toEqual({company: {category: {}}}); | ||
}); | ||
it('should handle bad input', function() { | ||
@@ -129,2 +141,8 @@ var fs = new evaluator.FormatString('{something.else} {company.what.is.this}'); | ||
it('should merge results', function() { | ||
companyType.displayFormat = new evaluator.FormatString("{name}"); | ||
var fs = new evaluator.FormatString('{company.category.name} {company.category} {company}'); | ||
expect(fs.extractRelationshipStructure(personType, undefined, {company: {test: {}}})).toEqual({company: {test: {}, category: {}}}); | ||
}); | ||
it('should handle recursive display names', function() { | ||
@@ -155,2 +173,7 @@ var theSchema = new schema.Schema(); | ||
it('should deepMerge', function() { | ||
var deepMerge = evaluator._deepMerge; | ||
expect(deepMerge({a: {}}, {b: {}})).toEqual({a: {}, b: {}}); | ||
expect(deepMerge({a: {b: {c: {}}}, d: {}}, {a: {e: {}}})).toEqual({a: {b: {c: {}}, e: {}}, d: {}}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
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
1110670
23316