rosetta-converters_dist_src_config.js

/***************************************************
 * Licensed Materials - Property of HCL
 * (c)Copyright HCL America, Inc. 2023
 ***************************************************/

/**
 * @module rosetta-converter-config
 */

const _config = {
  _rosettajs: 'rosettajs', // the object variable name where the rosetta API is defined
  _allowPassthrough: false,
  _natives: {
    _global: {
      enabled: true // true =all native js
    },
    "true": {
      enabled: true,
      notes: "@True",
      openFormula: "TRUE"
    },
    "false": {
      enabled: true,
      notes: "@False",
      openFormula: "FALSE"
    },
    "length": {
      enabled: true,
      notes: "@Length"
    }
  },
  _stripNumbers: true,
  _paramAsFieldName: true,
  _customAPIs: undefined,
  _returnFinal: false,
  _rosettaGateHost: ''
};

const nativeName = {};
for (const key in _config._natives) {  
  if (key) { nativeName[key] = key; }
}

const config = {
  nativeName,
  getNativenessNotesName,
  getNativenessOpenFormulaName,
  getRosettaJSname,
  setRosettaJSname,
  getAllowPassthrough,
  setAllowPassthrough,
  getNativeness,
  setNativeness,
  getStripNumbers,
  setStripNumbers,
  getParamAsFieldName,
  setParamAsFieldName,
  getCustomAPIDef,
  setCustomAPIDef,
  getReturnFinal,
  setReturnFinal,
  getRosettaGateHost,
  setRosettaGateHost
};

/**
 * Get rosettajs name for converter to use.
 * @returns {string} the default rosettajs object name string currently set.
*/
function getRosettaJSname() { 
  return _config._rosettajs; 
}

/**
 * Set rosettajs name for converter to use.
 * @param {string} _rosettajs the default rosettajs object name string to set.
 * @returns {string} the default rosettajs object name string currently set.
 */
function setRosettaJSname(_rosettajs) { 
  _config._rosettajs = _rosettajs;
  return getRosettaJSname();
}

/**
* Get passthorugh allowed or not in converter configuration.
* @returns {boolean} whether passthrough is allowed or not.
* @returns true = non-recognized formula will be passed through.
* @returns false = non-recognized formula will retrun error.
*/
function getAllowPassthrough() { 
  return _config._allowPassthrough; 
}

/**
* Set passthorugh allowed or not in converter configuration.
* @param {boolean} allow whether passthrough is allowed or not.
* @returns {boolean} setting updated in converter configuration.
*/
function setAllowPassthrough(allow) { 
  _config._allowPassthrough = allow;
  return getAllowPassthrough();
}

/**
* Get stripNumbers setting in converter configuration.
* @returns {boolean} whether stripNumbers is allowed or not.
* @returns true = leading 0 on the number literal will be stripped.
* @returns false = leading 0 on the number literal will not be stripped.
*/
function getStripNumbers() { 
  return _config._stripNumbers; 
}

/**
* Set stripNumbers to strip leading 0 on the number literal or not in converter configuration.
* @param {boolean} strip whether stripNumbers is allowed or not.
* @returns {boolean} setting updated in converter configuration.
*/
function setStripNumbers(strip) { 
  _config._stripNumbers = strip;
  return getStripNumbers();
}

/**
* Get paramAsFieldName setting in converter configuration.
* @returns {boolean} whether paramAsFieldName is allowed or not.
* @returns true = treat undefined parameter as field name and convert to getField.
* @returns false = treat undefined parameter as variable name.
*/
function getParamAsFieldName() { 
  return _config._paramAsFieldName;
}

/**
* Set whether parameter will be converted to getField or not.
* @param {boolean} convert whether paramAsFieldName is allowed or not.
* @returns {boolean} setting updated in converter configuration.
*/
function setParamAsFieldName(convert) {
  _config._paramAsFieldName = convert;
  return getParamAsFieldName();
}

/**
* Get custom API definition JSON array.
* @returns {array} custom API definition JSON array.
*/
function getCustomAPIDef() { 
  return _config._customAPIs;
}

/**
* Set custom API definition JSON array
* @param {array} custom custom API definition JSON array to set.
* @returns {array} custom API definition JSON array in converter configuration.
*/
function setCustomAPIDef(custom) {
  _config._customAPIs = custom;
  return getCustomAPIDef();
}

/**
* Get whether registered formula converted to native or not
* @param {string} formula the formula name to check. ('if', 'true', 'false', 'length')
* @returns {boolean} whether formula is converted to native or not. For example, if getNativeness('if') true then, formula '@If' will be converted to 'if' instead of 'rosettajs.Logical.if'.
*/
function getNativeness(formula) { 
  if(_config._natives[formula] === undefined) {
    return false;
  }
  return _config._natives._global.enabled || _config._natives[formula].enabled; 
}

/**
* Set registered formula converted to native or not
* @param {string} formula the formula name to set. ('if', 'true', 'false', 'length')
* @param {boolean} native to set whether formula is converted to native or not.
* @returns {boolean} setting updated in converter configuration.
*/
function setNativeness(formula, native) { 
  if(!(_config._natives[formula] === undefined)) {
    _config._natives[formula].enabled = native;
  }
  return getNativeness(formula);
}
export { config };

/*
return Notes formula name for nativeness
*/
function getNativenessNotesName(formula) { 
  return _config._natives[formula].notes??'';
}

/*
return OpenFormula name for nativeness
*/
function getNativenessOpenFormulaName(formula) { 
  return _config._natives[formula].openFormula??'';
}

/**
* Get return final statement or not in converter configuration.
* @returns {boolean} whether to return final statement or not.
* @returns true = final statement will return.
* @returns false = no return statement.
*/
function getReturnFinal() { 
  return _config._returnFinal; 
}

/**
* Set return final statement or not in converter configuration.
* @param {boolean} whether to return final statement or not.
* @returns {boolean} setting updated in converter configuration.
*/
function setReturnFinal(needReturn) { 
  _config._returnFinal = needReturn;
  return getReturnFinal();
}

/**
 * Get rosetta gate url for converter to use.
 * @returns {string} the rosetta host url string currently set.
*/
function getRosettaGateHost() { 
  return _config._rosettaGateHost; 
}

/**
 * Set rosetta gate url for converter to use.
 * @param {string} _rosettaGateHost rosetta gate url string to set.
 * @returns {string} the rosetta host url string currently set.
 */
function setRosettaGateHost(url) { 
  _config._rosettaGateHost = url;
  return getRosettaGateHost();
}