manyfest
Version:
JSON Object Manifest for Data Description and Parsing
139 lines (126 loc) • 4.48 kB
JavaScript
/**
* @author <steven@velozo.com>
*/
let libSimpleLog = require('./Manyfest-LogToConsole.js');
/**
* Hash Translation
*
* This is a very simple translation table for hashes, which allows the same schema to resolve
* differently based on a loaded translation table.
*
* This is to prevent the requirement for mutating schemas over and over again when we want to
* reuse the structure but look up data elements by different addresses.
*
* One side-effect of this is that a translation table can "override" the built-in hashes, since
* this is always used to resolve hashes before any of the functionCallByHash(pHash, ...) perform
* their lookups by hash.
*
* @class ManyfestHashTranslation
*/
class ManyfestHashTranslation
{
/**
* @param {function} [pInfoLog] - (optional) A logging function for info messages
* @param {function} [pErrorLog] - (optional) A logging function for error messages
*/
constructor(pInfoLog, pErrorLog)
{
// Wire in logging
this.logInfo = (typeof(pInfoLog) === 'function') ? pInfoLog : libSimpleLog;
this.logError = (typeof(pErrorLog) === 'function') ? pErrorLog : libSimpleLog;
this.translationTable = {};
}
/**
* @return {number} The number of translations in the table
*/
translationCount()
{
return Object.keys(this.translationTable).length;
}
/**
* @param {object} pTranslation - An object containing source:destination hash pairs to add to the translation table
*/
addTranslation(pTranslation)
{
// This adds a translation in the form of:
// { "SourceHash": "DestinationHash", "SecondSourceHash":"SecondDestinationHash" }
if (typeof(pTranslation) != 'object')
{
this.logError(`Hash translation addTranslation expected a translation be type object but was passed in ${typeof(pTranslation)}`);
return false;
}
let tmpTranslationSources = Object.keys(pTranslation)
tmpTranslationSources.forEach(
(pTranslationSource) =>
{
if (typeof(pTranslation[pTranslationSource]) != 'string')
{
this.logError(`Hash translation addTranslation expected a translation destination hash for [${pTranslationSource}] to be a string but the referrant was a ${typeof(pTranslation[pTranslationSource])}`);
}
else
{
this.translationTable[pTranslationSource] = pTranslation[pTranslationSource];
}
});
}
/**
* @param {string} pTranslationHash - The source hash to remove from the translation table
*/
removeTranslationHash(pTranslationHash)
{
delete this.translationTable[pTranslationHash];
}
/**
* This removes translations.
* If passed a string, just removes the single one.
* If passed an object, it does all the source keys.
*
* @param {string|object} pTranslation - Either a source hash string to remove, or an object containing source:destination hash pairs to remove
*
* @return {boolean} True if the removal was successful, false otherwise
*/
removeTranslation(pTranslation)
{
if (typeof(pTranslation) == 'string')
{
this.removeTranslationHash(pTranslation);
return true;
}
else if (typeof(pTranslation) == 'object')
{
let tmpTranslationSources = Object.keys(pTranslation)
tmpTranslationSources.forEach(
(pTranslationSource) =>
{
this.removeTranslation(pTranslationSource);
});
return true;
}
else
{
this.logError(`Hash translation removeTranslation expected either a string or an object but the passed-in translation was type ${typeof(pTranslation)}`);
return false;
}
}
clearTranslations()
{
this.translationTable = {};
}
/**
* @param {string} pTranslation - The source hash to translate
*
* @return {string} The translated hash, or the original if no translation exists
*/
translate(pTranslation)
{
if (pTranslation in this.translationTable)
{
return this.translationTable[pTranslation];
}
else
{
return pTranslation;
}
}
}
module.exports = ManyfestHashTranslation;