init
This commit is contained in:
43
convertData.js
Normal file
43
convertData.js
Normal file
@ -0,0 +1,43 @@
|
||||
const config = require('./conf/transformConfig.json');
|
||||
const functionsConfig = require('./conf/functionsConfig.json');
|
||||
const loadedFunctions = {};
|
||||
|
||||
Object.keys(functionsConfig).forEach(funcName => {
|
||||
loadedFunctions[funcName] = require(functionsConfig[funcName]);
|
||||
});
|
||||
|
||||
function resolveField(source, fieldConfig) {
|
||||
let result = null;
|
||||
|
||||
if (typeof fieldConfig === 'string' || typeof fieldConfig === 'boolean' || typeof fieldConfig === 'number') {
|
||||
return fieldConfig;
|
||||
}
|
||||
|
||||
if (fieldConfig.from && source.hasOwnProperty(fieldConfig.from)) {
|
||||
result = source[fieldConfig.from];
|
||||
}
|
||||
|
||||
if (result === null && fieldConfig.fallback) {
|
||||
result = source[fieldConfig.fallback];
|
||||
}
|
||||
|
||||
if (fieldConfig.template) {
|
||||
result = fieldConfig.template.replace(/\$\{(.*?)\}/g, (match, key) => source[key] || '');
|
||||
}
|
||||
|
||||
if (fieldConfig.func && typeof loadedFunctions[fieldConfig.func] === 'function') {
|
||||
result = loadedFunctions[fieldConfig.func](fieldConfig.params || {});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function convertToGiteaFormat(originalData) {
|
||||
const result = {};
|
||||
Object.keys(config.fields).forEach((key) => {
|
||||
result[key] = resolveField(originalData, config.fields[key]);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
module.exports = { convertToGiteaFormat };
|
Reference in New Issue
Block a user