UNPKG

@zhsz/cool-design-crud

Version:

130 lines (129 loc) 3.06 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); const index = require("./index.js"); const format = { number(value) { return value ? index.isArray(value) ? value.map(Number) : Number(value) : value; }, string(value) { return value ? index.isArray(value) ? value.map(String) : String(value) : value; }, split(value) { if (index.isString(value)) { return value.split(",").filter(Boolean); } else if (index.isArray(value)) { return value; } else { return []; } }, join(value) { return index.isArray(value) ? value.join(",") : value; }, boolean(value) { return Boolean(value); }, booleanNumber(value) { return value ? 1 : 0; }, datetimeRange(value, { form, method, prop }) { const key = prop.charAt(0).toUpperCase() + prop.slice(1); const start = `start${key}`; const end = `end${key}`; if (method === "bind") { return [form[start], form[end]]; } else { const [startTime, endTime] = value || []; form[start] = startTime; form[end] = endTime; return void 0; } }, splitJoin(value, { method }) { if (method === "bind") { return index.isString(value) ? value.split(",").filter(Boolean) : value; } else { return index.isArray(value) ? value.join(",") : value; } }, json(value, { method }) { if (method === "bind") { try { return JSON.parse(value); } catch (e) { return {}; } } else { return JSON.stringify(value); } }, empty(value) { if (index.isString(value)) { return value === "" ? void 0 : value; } return value; } }; function init({ value, form, prop }) { form[prop] = value; } function parse(method, { value, hook: pipe, form, prop, schema }) { init({ value, method, form, prop }); if (!pipe) { if (schema) schema._default = value; return false; } let pipes = []; if (index.isString(pipe)) { if (format[pipe]) { pipes = [pipe]; } else { console.error(`Hook[${pipe}] is not found`); } } else if (index.isArray(pipe)) { pipes = pipe; } else if (index.isObject(pipe)) { pipes = index.isArray(pipe[method]) ? pipe[method] : [pipe[method]]; } else if (index.isFunction(pipe)) { pipes = [pipe]; } else { console.error(`Hook error`); } let v = value; pipes.forEach((e) => { let f = null; if (index.isString(e)) { f = format[e]; } else if (index.isFunction(e)) { f = e; } if (f) { v = f(v, { method, form, prop }); } }); if (prop) { form[prop] = v; } if (method === "bind" && schema) { schema._default = v; } } const formHook = { bind(data) { parse("bind", data); }, submit(data) { parse("submit", data); } }; function registerFormHook(name, fn) { format[name] = fn; } exports.default = formHook; exports.format = format; exports.registerFormHook = registerFormHook;