Comparing version 1.0.5 to 1.0.6
{ | ||
"name": "manyfest", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "JSON Object Manifest for Data Description and Parsing", | ||
@@ -5,0 +5,0 @@ "main": "source/Manyfest.js", |
@@ -110,4 +110,30 @@ /** | ||
} | ||
mergeAddressMappings(pManyfestSchemaDescriptorsDestination, pManyfestSchemaDescriptorsSource) | ||
{ | ||
if ((typeof(pManyfestSchemaDescriptorsSource) != 'object') || (typeof(pManyfestSchemaDescriptorsDestination) != 'object')) | ||
{ | ||
this.logError(`Attempted to merge two schema descriptors but both were not objects.`); | ||
return false; | ||
} | ||
let tmpSource = JSON.parse(JSON.stringify(pManyfestSchemaDescriptorsSource)); | ||
let tmpNewManyfestSchemaDescriptors = JSON.parse(JSON.stringify(pManyfestSchemaDescriptorsDestination)); | ||
// The first passed-in set of descriptors takes precedence. | ||
let tmpDescriptorAddresses = Object.keys(tmpSource); | ||
tmpDescriptorAddresses.forEach( | ||
(pDescriptorAddress) => | ||
{ | ||
if (!tmpNewManyfestSchemaDescriptors.hasOwnProperty(pDescriptorAddress)) | ||
{ | ||
tmpNewManyfestSchemaDescriptors[pDescriptorAddress] = tmpSource[pDescriptorAddress]; | ||
} | ||
}); | ||
return tmpNewManyfestSchemaDescriptors; | ||
} | ||
} | ||
module.exports = ManyfestSchemaManipulation; |
@@ -74,2 +74,15 @@ /** | ||
clone() | ||
{ | ||
// Make a copy of the options in-place | ||
let tmpNewOptions = JSON.parse(JSON.stringify(this.options)); | ||
let tmpNewManyfest = new Manyfest(this.getManifest(), this.logInfo, this.logError, tmpNewOptions); | ||
// Import the hash translations | ||
tmpNewManyfest.hashTranslations.addTranslation(this.hashTranslations.translationTable); | ||
return tmpNewManyfest; | ||
} | ||
// Deserialize a Manifest from a string | ||
@@ -129,2 +142,3 @@ deserialize(pManifestString) | ||
// Serialize the Manifest to a string | ||
// TODO: Should this also serialize the translation table? | ||
serialize() | ||
@@ -137,3 +151,3 @@ { | ||
{ | ||
let tmpManifest = ( | ||
return ( | ||
{ | ||
@@ -140,0 +154,0 @@ Scope: this.scope, |
@@ -90,2 +90,112 @@ /** | ||
); | ||
test | ||
( | ||
'We should be able to merge properties safely.', | ||
(fTestComplete)=> | ||
{ | ||
let tmpSchemaDescriptors = ( | ||
{ | ||
"a": { "Hash": "a", "Type": "Number" }, | ||
"b": { "Hash": "b", "Type": "Number" } | ||
}); | ||
let tmpSchemaDescriptorsToMerge = ( | ||
{ | ||
"c": { "Hash": "c" }, | ||
"d": { "Hash": "d" }, | ||
"e": { "Hash": "e" }, | ||
"a": { "Hash": "ARBUCKLE", "Type": "Number" } | ||
}); | ||
Expect(tmpSchemaDescriptors.a.Hash).to.equal('a'); | ||
let _Manyfest = new libManyfest(); | ||
// Now remap the schema (in-place) | ||
let tmpNewSchemaDescriptors = _Manyfest.schemaManipulations.mergeAddressMappings(tmpSchemaDescriptors, tmpSchemaDescriptorsToMerge); | ||
// The schema should be safe | ||
Expect(tmpNewSchemaDescriptors.a.Hash).to.equal('a'); | ||
// And a new schema should have been created with the alterations | ||
Expect(tmpNewSchemaDescriptors.b.Hash).to.equal('b'); | ||
Expect(tmpNewSchemaDescriptors.c.Hash).to.equal('c'); | ||
fTestComplete(); | ||
} | ||
); | ||
test | ||
( | ||
'Cloning should work.', | ||
(fTestComplete)=> | ||
{ | ||
let tmpSchemaDescriptors = ( | ||
{ | ||
"a": { "Hash": "a", "Type": "Number" }, | ||
"b": { "Hash": "b", "Type": "Number" } | ||
}); | ||
let tmpTranslationTable = ( | ||
{ | ||
"a": "CarrotCost", | ||
"b": "AppleCost" | ||
}); | ||
let _Manyfest = new libManyfest({ Scope:'Archive.org', Descriptors: {'metadata.creator': {Name:'Creator', Hash:'Creator'}}}); | ||
// Property not schema, accessed by hash: | ||
let tmpCreator = _Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Creator'); | ||
Expect(tmpCreator).to.equal('General Mills'); | ||
let _ClonedManyfest = _Manyfest.clone(); | ||
Expect(_ClonedManyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Creator')).to.equal('General Mills'); | ||
fTestComplete(); | ||
} | ||
); | ||
test | ||
( | ||
'Cloning should take into account translation.', | ||
(fTestComplete)=> | ||
{ | ||
let tmpSchemaDescriptors = ( | ||
{ | ||
"a": { "Hash": "a", "Type": "Number" }, | ||
"b": { "Hash": "b", "Type": "Number" } | ||
}); | ||
let tmpTranslationTable = ( | ||
{ | ||
"a": "CarrotCost", | ||
"b": "AppleCost" | ||
}); | ||
let _Manyfest = new libManyfest({ Scope:'Archive.org', Descriptors: {'metadata.creator': {Name:'Creator', Hash:'Creator'}}}); | ||
// Property not schema, accessed by hash: | ||
let tmpCreator = _Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Creator'); | ||
Expect(tmpCreator).to.equal('General Mills'); | ||
// Create a translation between "Creator" and "Director" as well as "Author" | ||
_Manyfest.hashTranslations.addTranslation({"Director":"Creator", "Author":"Creator", "Songwriter":"Creator"}); | ||
Expect(tmpCreator).to.equal('General Mills'); | ||
// Director should also work | ||
Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Director')).to.equal('General Mills'); | ||
// And Author! | ||
Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Author')).to.equal('General Mills'); | ||
// Now remove Director | ||
_Manyfest.hashTranslations.clearTranslations(); | ||
Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Author')).to.equal(undefined); | ||
Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Director')).to.equal(undefined); | ||
Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Songwriter')).to.equal(undefined); | ||
Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Creator')).to.equal('General Mills'); | ||
let _ClonedManyfest = _Manyfest.clone(); | ||
Expect(_ClonedManyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Author')).to.equal(undefined); | ||
Expect(_ClonedManyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Director')).to.equal(undefined); | ||
Expect(_ClonedManyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Songwriter')).to.equal(undefined); | ||
Expect(_ClonedManyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Creator')).to.equal('General Mills'); | ||
// New translations should not affect the old manyfest | ||
_ClonedManyfest.hashTranslations.addTranslation({"Director":"Creator", "Author":"Creator", "Songwriter":"Creator"}); | ||
Expect(_ClonedManyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Director')).to.equal('General Mills'); | ||
Expect(_Manyfest.getValueByHash(_SampleDataArchiveOrgFrankenberry, 'Director')).to.equal(undefined); | ||
fTestComplete(); | ||
} | ||
); | ||
} | ||
@@ -92,0 +202,0 @@ ); |
222482
3489