@edirect/form-engine
Version:
Achieve form logic reusage with forms expressed in json format.
134 lines (133 loc) • 4.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.handler = void 0;
var utils = _interopRequireWildcard(require("../../utils/index.js"));
function _getRequireWildcardCache(nodeInterop) {
if (typeof WeakMap !== "function") return null;
var cacheBabelInterop = new WeakMap();
var cacheNodeInterop = new WeakMap();
return (_getRequireWildcardCache = function (nodeInterop) {
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
})(nodeInterop);
}
function _interopRequireWildcard(obj, nodeInterop) {
if (!nodeInterop && obj && obj.__esModule) {
return obj;
}
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
return {
default: obj
};
}
var cache = _getRequireWildcardCache(nodeInterop);
if (cache && cache.has(obj)) {
return cache.get(obj);
}
var newObj = {};
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
for (var key in obj) {
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
if (desc && (desc.get || desc.set)) {
Object.defineProperty(newObj, key, desc);
} else {
newObj[key] = obj[key];
}
}
}
newObj.default = obj;
if (cache) {
cache.set(obj, newObj);
}
return newObj;
}
const template = {
BEGIN: '${',
END: '}',
DEFAULT_SPLITTER: '||'
};
const hasTemplateString = value => {
return /\$\{(.*)\}.*/g.test(value);
};
const handler = ({
form
}) => {
var _a;
const replaceTemplateString = targetString => {
targetString = targetString.toString();
const lastIndex = targetString.lastIndexOf(template.BEGIN);
if (lastIndex === -1) return targetString;
const substringTemplate = targetString.substring(lastIndex + template.BEGIN.length, targetString.length);
let match = substringTemplate.substring(0, substringTemplate.indexOf(template.END));
const originalMatchLength = match.length;
let defaultValue = '';
const parts = match.split(template.DEFAULT_SPLITTER);
if (parts.length > 1) {
match = parts[0];
defaultValue = utils.object.getValueByPath(form.scope.getGlobalScope(), parts[1]) || parts[1];
}
const scopedTemplateValue = utils.object.getValueByPath(form.scope.getGlobalScope(), match);
let value = typeof scopedTemplateValue === 'undefined' ? defaultValue : scopedTemplateValue;
// If we are interested in other data than string and its the only data we have in the template, we just return it
if (typeof value !== 'string' && lastIndex === 0) {
return value;
}
// Otherwise, we just stringify it and ussume the client will use as string
try {
value = JSON.stringify(value).replace(/^"(.*)"$/, '$1');
// eslint-disable-next-line no-empty
} catch (e) {}
const scopedString = targetString.substring(0, lastIndex) + value + targetString.substring(lastIndex + originalMatchLength + template.BEGIN.length + template.END.length, targetString.length);
return replaceTemplateString(scopedString);
};
const inObject = (obj, recursionLevel = 0) => {
if (typeof obj === 'string') {
return replaceTemplateString(obj);
}
if (typeof obj === 'number') {
return obj;
}
const object = Object.assign({}, obj);
return Object.keys(object).reduce((acc, key) => {
if (object[key] === null) {
return acc;
}
if (Array.isArray(object[key])) {
return Object.assign(Object.assign({}, acc), {
[key]: object[key].map(inObject)
});
}
if (typeof object[key] === 'object') {
return Object.assign(Object.assign({}, acc), {
[key]: Object.assign({}, inObject(Object.assign({}, object[key]), recursionLevel + 1))
});
}
if (typeof object[key] !== 'string') {
return Object.assign(Object.assign({}, acc), {
[key]: object[key]
});
}
if (!hasTemplateString(object[key])) {
return Object.assign(Object.assign({}, acc), {
[key]: object[key]
});
}
let value = replaceTemplateString(object[key]);
try {
if (utils.string.hasJsonStructure(value)) {
value = JSON.parse(value);
}
// eslint-disable-next-line no-empty
} catch (e) {}
return Object.assign(Object.assign({}, acc), {
[key]: value
});
}, {});
};
form.scopedSchema = Object.assign(Object.assign({}, form.schema), inObject({
visibilityConditions: (_a = form === null || form === void 0 ? void 0 : form.schema) === null || _a === void 0 ? void 0 : _a.visibilityConditions
}));
};
exports.handler = handler;