const getWithFirstUpper = (s) => s[0].toUpperCase() + s.slice(1); export const getEntryHTML = (id, { type }) => { const typeClass = getWithFirstUpper(type); switch (true) { case type === 'boolean': return ``; case type === 'array': return ` `; case type === 'email': return ``; case type === 'number': return ``; default: return ``; } }; export const getEntryErrors = (id, { type }) => { const typeClass = getWithFirstUpper(type); return `{#each $errors${typeClass}${id} as errorCode}
{errorCode} rule validation error
{/each}` }; export const getEntryJS = (id, params) => { const { type, ...rest } = params; const typeClass = getWithFirstUpper(type); let paramsObject = `type: '${type}'`; Object.keys(rest).forEach(ruleKey => { const paramValue = ruleKey === 'match' ? `/${rest[ruleKey]}/` : (rest[ruleKey] === '' ? "''" : JSON.stringify(rest[ruleKey])); paramsObject += `,\n ${ruleKey}: ${paramValue}`; }); return ``; }; export const getFormCode = ({ validateOnEvents, clearErrorsOnEvents, listenInputEvents, presence, trim, includeAllEntries }) => { const options = []; if (listenInputEvents !== 0 && (!validateOnEvents.change || validateOnEvents.input || validateOnEvents.blur)) { let code = ''; code += `validateOnEvents: { `; if (validateOnEvents.change) code += `change: true, `; if (validateOnEvents.input) code += `input: true, `; if (validateOnEvents.blur) code += `blur: true, `; code += `}`; options.push(code); } if (!clearErrorsOnEvents.reset || (listenInputEvents !== 0 && clearErrorsOnEvents.focus)) { let code = ''; code += `clearErrorsOnEvents: { `; if (clearErrorsOnEvents.reset) code += `reset: true, `; if (clearErrorsOnEvents.focus && listenInputEvents !== 0) code += `focus: true, `; code += `}`; options.push(code); } if (listenInputEvents !== 2) { options.push(`listenInputEvents: ` + listenInputEvents); } if (presence === 'required') { options.push(`presence: 'required'`); } else if (presence === 'optional') { options.push(`presence: 'optional'`); } if (trim) { options.push(`trim: true`); } if (includeAllEntries) { options.push(`includeAllEntries: true`); } const js = [ `` ].join('\n'); const html = [ `` ].join('\n'); return js + '\n\n' + html; };