| "use strict";var d=Object.defineProperty;var c=(r,t,e)=>t in r?d(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e;var i=(r,t,e)=>(c(r,typeof t!="symbol"?t+"":t,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class v{constructor(){i(this,"events",new Map)}on(t,e){return this.events.has(t)?this.events.get(t).push(e):this.events.set(t,[e]),()=>this.off(t,e)}off(t,e){this.events.has(t)&&this.events.set(t,this.events.get(t).filter(s=>s!==e))}emit(t,e){this.events.has(t)&&this.events.get(t).forEach(s=>s(e)),t!=="*"&&this.events.has("*")&&this.events.get("*").forEach(s=>s(t,e))}}function l(){return new v}const m=()=>"";class n{constructor({value:t="",touched:e=!1,error:s="",test:a=m,form:o=null}={}){i(this,"form",null);i(this,"eeSync",l());i(this,"ee",l());i(this,"data",{value:"",error:"",touched:!1});i(this,"set",(t,e)=>{if(this.setData("value",t),e===!0){this.validate();return}e!==!1&&e===void 0&&this.form.options.validateOnChange&&this.validate()});i(this,"onNativeInput",t=>{this.set(t.target.value)});i(this,"onBlur",()=>{this.setTouched(!0),this.form.options.validateOnBlur&&this.validate()});this.form=o,this.test=a,this.initParams={value:t,touched:e,error:s,test:a},this.data.value=t,this.data.error=s,this.data.touched=e,o.addField(this)}notify(t,e){this.eeSync.emit(t,e),this.form.calls.add(this,t,()=>{this.ee.emit(t,e)}),this.form.calls.add(this,"global",()=>{this.ee.emit("global",[this,t,e])}),this.form.notify("global",[this,t,e])}get value(){return this.data.value}set value(t){this.set(t)}get error(){return this.data.error}set error(t){this.setError(t)}get touched(){return this.data.touched}set touched(t){this.setTouched(t)}get errorTouched(){return this.data.touched===!1?"":this.data.error}setDataPure(t,e){}setData(t,e){const s=this.data[t];if(this.form.options.checkPrevData&&s===e)return!1;const a=this.errorTouched;if(this.data[t]=e,this.notify(t,e),t==="error"){const o=s,h=e;h!==""&&o===""?(this.form._errors++,this.form._errors===1&&this.form.notify("valid",!1)):h===""&&o!==""&&(this.form._errors--,this.form._errors===0&&this.form.notify("valid",!0))}return(t==="error"||t==="touched")&&a!==this.errorTouched&&this.notify("errorTouched",this.errorTouched),!0}update(){this.form.calls.add(this,"*",()=>this.ee.emit("*")),this.form.calls.add(this.form,"*",()=>this.form.ee.emit("*"))}setError(t){this.setData("error",t)}setTouched(t){this.setData("touched",t)}async validate(){const t=await this.test(this.data.value);return this.setError(t),t}on(t,e){return this.ee.on(t,e),()=>this.ee.off(t,e)}off(t,e){return()=>this.ee.off(t,e)}reset(){this.setData("value",this.initParams.value),this.setData("touched",this.initParams.touched),this.setData("error",this.initParams.error)}}function g(){let r,t;return[new Promise((e,s)=>{r=e,t=s}),{resolve:r,reject:t}]}class F{constructor(t){i(this,"_calls",new Map);i(this,"promise",null);this.batch=t||(e=>e())}add(t,e,s){if(this._calls.has(t)||this._calls.set(t,new Map),this._calls.get(t).set(e,s),!this.promise){const[o,h]=g();this.promise=o,this.promiseConrtols=h,queueMicrotask(async()=>{await this._call(),this._calls.clear(),this.promiseConrtols.resolve(),this.promise=null})}return this.promise}_call(){return this.batch(()=>{for(const t of this._calls.values())for(const e of t.values())e()})}}class u{constructor(t={plugins:[]}){i(this,"options",{validateOnChange:!0,validateOnBlur:!0,checkPrevData:!1});i(this,"fields",{});i(this,"f",null);i(this,"onSubmit",()=>{});i(this,"calls",new F);i(this,"eeSync",l());i(this,"ee",l());i(this,"submit",async()=>{if(await this.validate())return this.onSubmit(this)});i(this,"_errors",0);i(this,"_flatFields",new Set);const e=t.plugins||[];this.f=this.fields,e.forEach(a=>a(this)),this.options={...this.options,...t.options},this.onSubmit=t.onSubmit??(()=>{}),this.initValues=t.initValues??{};for(const a in this.initValues){const o=this.initValues[a];this._getFlatFieldOrCreate(a,{value:o})}t.batch&&(this.batch=t.batch);const s=typeof t.validateSchema=="function"?t.validateSchema(this):t.validateSchema;for(const a in s){const o=s[a],h=this._getFlatFieldOrCreate(a);h.test=o}}notify(t,e){this.eeSync.emit(t,e),this.calls.add(this,t,()=>{this.ee.emit(t,e)}),t!=="global"&&this.notify("global",[this,t,e])}batch(t){t()}createField(t={}){return new n(t)}_getFlatFieldOrCreate(t,e){return this.fields[t]||(this.fields[t]=this.createField({name:t,form:this,...e})),this.fields[t]}async validate(){return(await Promise.all([...this._flatFields].map(e=>e.validate()))).every(e=>e==="")}getValid(){return this._errors===0}get valid(){return this.getValid()}reset(){for(const t of this._flatFields)t.reset()}_prepareFieldData(t){const e={};for(const s in this.fields)t(this.fields[s])!==void 0&&(e[s]=t(this.fields[s]));return e}get values(){return this._prepareFieldData(t=>t.value)}get errors(){return this._prepareFieldData(t=>t.error||void 0)}get touches(){return this._prepareFieldData(t=>t.touched||void 0)}get data(){return this._prepareFieldData(t=>({value:t.value,error:t.error,touched:t.touched}))}on(t,e){return this.ee.on(t,e),()=>this.ee.off(t,e)}off(t,e){return()=>this.ee.off(t,e)}addField(t){t.error!==""&&this._errors++,this._flatFields.add(t)}removeField(t){t.error!==""&&this._errors--,this._flatFields.delete(t)}}function f(r){return async t=>{try{return await r.validate(t),""}catch(e){return e.message}}}function p(r){const t={};for(const e in r)t[e]=f(r[e]);return t}const _=n,y=u;exports.FfwField=_;exports.FfwForm=y;exports.Field=n;exports.Form=u;exports.convertYupToTest=f;exports.prepareYup=p; |
+279
| var u = Object.defineProperty; | ||
| var f = (r, t, e) => t in r ? u(r, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : r[t] = e; | ||
| var i = (r, t, e) => (f(r, typeof t != "symbol" ? t + "" : t, e), e); | ||
| class d { | ||
| constructor() { | ||
| i(this, "events", /* @__PURE__ */ new Map()); | ||
| } | ||
| on(t, e) { | ||
| return this.events.has(t) ? this.events.get(t).push(e) : this.events.set(t, [e]), () => this.off(t, e); | ||
| } | ||
| off(t, e) { | ||
| this.events.has(t) && this.events.set( | ||
| t, | ||
| this.events.get(t).filter((s) => s !== e) | ||
| ); | ||
| } | ||
| emit(t, e) { | ||
| this.events.has(t) && this.events.get(t).forEach((s) => s(e)), t !== "*" && this.events.has("*") && this.events.get("*").forEach((s) => s(t, e)); | ||
| } | ||
| } | ||
| function l() { | ||
| return new d(); | ||
| } | ||
| const c = () => ""; | ||
| class n { | ||
| constructor({ | ||
| value: t = "", | ||
| touched: e = !1, | ||
| error: s = "", | ||
| test: a = c, | ||
| form: o = null | ||
| } = {}) { | ||
| i(this, "form", null); | ||
| i(this, "eeSync", l()); | ||
| i(this, "ee", l()); | ||
| i(this, "data", { | ||
| value: "", | ||
| error: "", | ||
| touched: !1 | ||
| }); | ||
| i(this, "set", (t, e) => { | ||
| if (this.setData("value", t), e === !0) { | ||
| this.validate(); | ||
| return; | ||
| } | ||
| e !== !1 && e === void 0 && this.form.options.validateOnChange && this.validate(); | ||
| }); | ||
| i(this, "onNativeInput", (t) => { | ||
| this.set(t.target.value); | ||
| }); | ||
| i(this, "onBlur", () => { | ||
| this.setTouched(!0), this.form.options.validateOnBlur && this.validate(); | ||
| }); | ||
| this.form = o, this.test = a, this.initParams = { | ||
| value: t, | ||
| touched: e, | ||
| error: s, | ||
| test: a | ||
| }, this.data.value = t, this.data.error = s, this.data.touched = e, o.addField(this); | ||
| } | ||
| notify(t, e) { | ||
| this.eeSync.emit(t, e), this.form.calls.add(this, t, () => { | ||
| this.ee.emit(t, e); | ||
| }), this.form.calls.add(this, "global", () => { | ||
| this.ee.emit("global", [this, t, e]); | ||
| }), this.form.notify("global", [this, t, e]); | ||
| } | ||
| get value() { | ||
| return this.data.value; | ||
| } | ||
| set value(t) { | ||
| this.set(t); | ||
| } | ||
| get error() { | ||
| return this.data.error; | ||
| } | ||
| set error(t) { | ||
| this.setError(t); | ||
| } | ||
| get touched() { | ||
| return this.data.touched; | ||
| } | ||
| set touched(t) { | ||
| this.setTouched(t); | ||
| } | ||
| get errorTouched() { | ||
| return this.data.touched === !1 ? "" : this.data.error; | ||
| } | ||
| setDataPure(t, e) { | ||
| } | ||
| setData(t, e) { | ||
| const s = this.data[t]; | ||
| if (this.form.options.checkPrevData && s === e) | ||
| return !1; | ||
| const a = this.errorTouched; | ||
| if (this.data[t] = e, this.notify(t, e), t === "error") { | ||
| const o = s, h = e; | ||
| h !== "" && o === "" ? (this.form._errors++, this.form._errors === 1 && this.form.notify("valid", !1)) : h === "" && o !== "" && (this.form._errors--, this.form._errors === 0 && this.form.notify("valid", !0)); | ||
| } | ||
| return (t === "error" || t === "touched") && a !== this.errorTouched && this.notify("errorTouched", this.errorTouched), !0; | ||
| } | ||
| update() { | ||
| this.form.calls.add(this, "*", () => this.ee.emit("*")), this.form.calls.add(this.form, "*", () => this.form.ee.emit("*")); | ||
| } | ||
| setError(t) { | ||
| this.setData("error", t); | ||
| } | ||
| setTouched(t) { | ||
| this.setData("touched", t); | ||
| } | ||
| async validate() { | ||
| const t = await this.test(this.data.value); | ||
| return this.setError(t), t; | ||
| } | ||
| on(t, e) { | ||
| return this.ee.on(t, e), () => this.ee.off(t, e); | ||
| } | ||
| off(t, e) { | ||
| return () => this.ee.off(t, e); | ||
| } | ||
| reset() { | ||
| this.setData("value", this.initParams.value), this.setData("touched", this.initParams.touched), this.setData("error", this.initParams.error); | ||
| } | ||
| } | ||
| function v() { | ||
| let r, t; | ||
| return [new Promise((e, s) => { | ||
| r = e, t = s; | ||
| }), { resolve: r, reject: t }]; | ||
| } | ||
| class m { | ||
| constructor(t) { | ||
| i(this, "_calls", /* @__PURE__ */ new Map()); | ||
| i(this, "promise", null); | ||
| this.batch = t || ((e) => e()); | ||
| } | ||
| add(t, e, s) { | ||
| if (this._calls.has(t) || this._calls.set(t, /* @__PURE__ */ new Map()), this._calls.get(t).set(e, s), !this.promise) { | ||
| const [o, h] = v(); | ||
| this.promise = o, this.promiseConrtols = h, queueMicrotask(async () => { | ||
| await this._call(), this._calls.clear(), this.promiseConrtols.resolve(), this.promise = null; | ||
| }); | ||
| } | ||
| return this.promise; | ||
| } | ||
| _call() { | ||
| return this.batch(() => { | ||
| for (const t of this._calls.values()) | ||
| for (const e of t.values()) | ||
| e(); | ||
| }); | ||
| } | ||
| } | ||
| class g { | ||
| constructor(t = { | ||
| plugins: [] | ||
| }) { | ||
| i(this, "options", { | ||
| validateOnChange: !0, | ||
| validateOnBlur: !0, | ||
| checkPrevData: !1 | ||
| }); | ||
| i(this, "fields", {}); | ||
| i(this, "f", null); | ||
| i(this, "onSubmit", () => { | ||
| }); | ||
| i(this, "calls", new m()); | ||
| i(this, "eeSync", l()); | ||
| i(this, "ee", l()); | ||
| i(this, "submit", async () => { | ||
| if (await this.validate()) | ||
| return this.onSubmit(this); | ||
| }); | ||
| i(this, "_errors", 0); | ||
| i(this, "_flatFields", /* @__PURE__ */ new Set()); | ||
| const e = t.plugins || []; | ||
| this.f = this.fields, e.forEach((a) => a(this)), this.options = { ...this.options, ...t.options }, this.onSubmit = t.onSubmit ?? (() => { | ||
| }), this.initValues = t.initValues ?? {}; | ||
| for (const a in this.initValues) { | ||
| const o = this.initValues[a]; | ||
| this._getFlatFieldOrCreate(a, { | ||
| value: o | ||
| }); | ||
| } | ||
| t.batch && (this.batch = t.batch); | ||
| const s = typeof t.validateSchema == "function" ? t.validateSchema(this) : t.validateSchema; | ||
| for (const a in s) { | ||
| const o = s[a], h = this._getFlatFieldOrCreate(a); | ||
| h.test = o; | ||
| } | ||
| } | ||
| notify(t, e) { | ||
| this.eeSync.emit(t, e), this.calls.add(this, t, () => { | ||
| this.ee.emit(t, e); | ||
| }), t !== "global" && this.notify("global", [this, t, e]); | ||
| } | ||
| batch(t) { | ||
| t(); | ||
| } | ||
| createField(t = {}) { | ||
| return new n(t); | ||
| } | ||
| _getFlatFieldOrCreate(t, e) { | ||
| return this.fields[t] || (this.fields[t] = this.createField({ name: t, form: this, ...e })), this.fields[t]; | ||
| } | ||
| async validate() { | ||
| return (await Promise.all( | ||
| [...this._flatFields].map((e) => e.validate()) | ||
| )).every((e) => e === ""); | ||
| } | ||
| getValid() { | ||
| return this._errors === 0; | ||
| } | ||
| get valid() { | ||
| return this.getValid(); | ||
| } | ||
| reset() { | ||
| for (const t of this._flatFields) | ||
| t.reset(); | ||
| } | ||
| _prepareFieldData(t) { | ||
| const e = {}; | ||
| for (const s in this.fields) | ||
| t(this.fields[s]) !== void 0 && (e[s] = t(this.fields[s])); | ||
| return e; | ||
| } | ||
| get values() { | ||
| return this._prepareFieldData((t) => t.value); | ||
| } | ||
| get errors() { | ||
| return this._prepareFieldData((t) => t.error || void 0); | ||
| } | ||
| get touches() { | ||
| return this._prepareFieldData((t) => t.touched || void 0); | ||
| } | ||
| get data() { | ||
| return this._prepareFieldData((t) => ({ | ||
| value: t.value, | ||
| error: t.error, | ||
| touched: t.touched | ||
| })); | ||
| } | ||
| on(t, e) { | ||
| return this.ee.on(t, e), () => this.ee.off(t, e); | ||
| } | ||
| off(t, e) { | ||
| return () => this.ee.off(t, e); | ||
| } | ||
| addField(t) { | ||
| t.error !== "" && this._errors++, this._flatFields.add(t); | ||
| } | ||
| removeField(t) { | ||
| t.error !== "" && this._errors--, this._flatFields.delete(t); | ||
| } | ||
| } | ||
| function _(r) { | ||
| return async (t) => { | ||
| try { | ||
| return await r.validate(t), ""; | ||
| } catch (e) { | ||
| return e.message; | ||
| } | ||
| }; | ||
| } | ||
| function F(r) { | ||
| const t = {}; | ||
| for (const e in r) | ||
| t[e] = _(r[e]); | ||
| return t; | ||
| } | ||
| const y = n, b = g; | ||
| export { | ||
| y as FfwField, | ||
| b as FfwForm, | ||
| n as Field, | ||
| g as Form, | ||
| _ as convertYupToTest, | ||
| F as prepareYup | ||
| }; |
+5
-26
| { | ||
| "name": "ffw", | ||
| "version": "1.1.1", | ||
| "description": "", | ||
| "version": "1.1.3", | ||
| "type": "module", | ||
| "main": "./dist/cjs/index.js", | ||
| "exports": { | ||
| ".": { | ||
| "development": { | ||
| "import": "./dist/esm/dev.js", | ||
| "require": "./dist/cjs/dev.js" | ||
| }, | ||
| "production": { | ||
| "import": "./dist/esm/prod.js", | ||
| "require": "./dist/cjs/prod.js" | ||
| }, | ||
| "import": "./dist/esm/prod.js", | ||
| "require": "./dist/cjs/prod.js" | ||
| }, | ||
| "./*": "./*" | ||
| "import": "./dist/ffw.js", | ||
| "require": "./dist/ffw.cjs" | ||
| }, | ||
@@ -25,15 +12,7 @@ "files": [ | ||
| ], | ||
| "author": "", | ||
| "author": "utftu", | ||
| "license": "MIT", | ||
| "devDependencies": { | ||
| "utftu": "^1.0.12" | ||
| }, | ||
| "dependencies": { | ||
| "strangelove": "^1.0.8" | ||
| }, | ||
| "scripts": { | ||
| "build2": "rollup -c ./relax.config.mjs", | ||
| "build": "rollup -c rollup.config.js", | ||
| "test": "echo \"Error: no test specified\"" | ||
| "build": "node ./build.js" | ||
| } | ||
| } |
-533
| var __create = Object.create; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __getProtoOf = Object.getPrototypeOf; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( | ||
| isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, | ||
| mod | ||
| )); | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // packages/ffw/src/index.js | ||
| var src_exports = {}; | ||
| __export(src_exports, { | ||
| FfwField: () => FfwField, | ||
| FfwForm: () => FfwForm, | ||
| Field: () => field_default, | ||
| Form: () => form_default, | ||
| prepareYup: () => yup_default | ||
| }); | ||
| module.exports = __toCommonJS(src_exports); | ||
| // node_modules/.pnpm/utftu@1.0.12/node_modules/utftu/dist/ee/esm/prod.js | ||
| function i(s) { | ||
| let e = s || /* @__PURE__ */ new Map(); | ||
| return { on(t2, f) { | ||
| e.has(t2) || e.set(t2, []), e.get(t2).push(f); | ||
| }, off(t2, f) { | ||
| e.has(t2) && e.set(t2, e.get(t2).filter((o) => o !== f)); | ||
| }, emit(t2, f) { | ||
| e.has(t2) && e.get(t2).forEach((o) => o(f)), e.has("*") && e.get("*").forEach((o) => o(t2, f)); | ||
| } }; | ||
| } | ||
| var r = i; | ||
| var u = r; | ||
| // packages/ffw/src/field/field.js | ||
| var defaultTest = () => ""; | ||
| var Field = class { | ||
| form = null; | ||
| ee = u(); | ||
| data = { | ||
| value: "", | ||
| error: "", | ||
| touched: false | ||
| }; | ||
| constructor({ | ||
| value = "", | ||
| touched = false, | ||
| error = "", | ||
| test = defaultTest, | ||
| form = null | ||
| } = {}) { | ||
| this.form = form; | ||
| this.test = defaultTest; | ||
| this.initParams = { | ||
| value, | ||
| touched, | ||
| error, | ||
| test | ||
| }; | ||
| this.test = test; | ||
| this.data.value = value; | ||
| this.data.error = error; | ||
| this.data.touched = touched; | ||
| form.addField(this); | ||
| } | ||
| get value() { | ||
| return this.data.value; | ||
| } | ||
| set value(newValue) { | ||
| this.set(newValue); | ||
| } | ||
| get error() { | ||
| return this.data.error; | ||
| } | ||
| set error(newError) { | ||
| this.setError(newError); | ||
| } | ||
| get touched() { | ||
| return this.data.touched; | ||
| } | ||
| set touched(newTouched) { | ||
| this.setTouched(newTouched); | ||
| } | ||
| get errorTouched() { | ||
| if (this.data.touched === false) { | ||
| return ""; | ||
| } | ||
| return this.data.error; | ||
| } | ||
| setData(name, newData) { | ||
| const prevData = this.data[name]; | ||
| if (prevData === newData) { | ||
| return; | ||
| } | ||
| const oldErrorTouched = this.errorTouched; | ||
| this.data[name] = newData; | ||
| this.form.calls.add(this, name, () => this.ee.emit(name, newData)); | ||
| this.form.calls.add(this.form, "*", () => this.form.ee.emit("global")); | ||
| if (name === "error") { | ||
| const oldError = prevData; | ||
| const error = newData; | ||
| if (error !== "" && oldError === "") { | ||
| this.form._errors++; | ||
| if (this.form._errors === 1) { | ||
| this.form.calls.add( | ||
| this.form, | ||
| "valid", | ||
| () => this.form.ee.emit("valid", false) | ||
| ); | ||
| } | ||
| } else if (error === "" && oldError !== "") { | ||
| this.form._errors--; | ||
| if (this.form._errors === 0) { | ||
| this.form.calls.add( | ||
| this.form, | ||
| "valid", | ||
| () => this.form.ee.emit("valid", true) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| if (name === "error" || name === "touched") { | ||
| if (oldErrorTouched === this.errorTouched) { | ||
| return; | ||
| } | ||
| this.form.calls.add( | ||
| this, | ||
| "errorTouched", | ||
| () => this.ee.emit("errorTouched", this.errorTouched) | ||
| ); | ||
| } | ||
| } | ||
| update() { | ||
| this.form.calls.add(this, "*", () => this.ee.emit("*")); | ||
| this.form.calls.add(this.form, "*", () => this.form.ee.emit("*")); | ||
| } | ||
| setError(error) { | ||
| this.setData("error", error); | ||
| } | ||
| setTouched(touched) { | ||
| this.setData("touched", touched); | ||
| } | ||
| set(value, validate) { | ||
| this.setData("value", value); | ||
| if (validate === true) { | ||
| this.validate(); | ||
| return; | ||
| } | ||
| if (validate === false) { | ||
| return; | ||
| } | ||
| if (validate === void 0 && this.form.options.validateOnChange) { | ||
| this.validate(); | ||
| } | ||
| } | ||
| async validate() { | ||
| const error = await this.test(this.data.value); | ||
| this.setError(error); | ||
| return error; | ||
| } | ||
| on(name, cb) { | ||
| this.ee.on(name, cb); | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| off(name, cb) { | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| onInput = (event) => { | ||
| this.set(event.target.value); | ||
| }; | ||
| onBlur = () => { | ||
| this.setTouched(true); | ||
| if (this.form.options.validateOnBlur) { | ||
| this.validate(); | ||
| } | ||
| }; | ||
| reset() { | ||
| this.setData("value", this.initParams.value); | ||
| this.setData("touched", this.initParams.touched); | ||
| this.setData("error", this.initParams.error); | ||
| } | ||
| }; | ||
| var field_default = Field; | ||
| // node_modules/.pnpm/utftu@1.0.12/node_modules/utftu/dist/create-controlled-promise/esm/prod.js | ||
| function m(o) { | ||
| let e, r2; | ||
| return [new Promise((i2, l) => { | ||
| e = i2, r2 = l, o && o(e, r2); | ||
| }), { resolve: e, reject: r2 }]; | ||
| } | ||
| var t = m; | ||
| var p = t; | ||
| // packages/ffw/src/delayed-calls/delayed-calls.js | ||
| var DelayedCalls = class { | ||
| _calls = /* @__PURE__ */ new Map(); | ||
| constructor(batch) { | ||
| this.batch = batch || ((cb) => cb()); | ||
| } | ||
| promise = null; | ||
| add(key1, key2, callback) { | ||
| if (!this._calls.has(key1)) { | ||
| this._calls.set(key1, /* @__PURE__ */ new Map()); | ||
| } | ||
| const level1 = this._calls.get(key1); | ||
| level1.set(key2, callback); | ||
| if (!this.promise) { | ||
| const [promise, promiseControls] = p(); | ||
| this.promise = promise; | ||
| this.promiseConrtols = promiseControls; | ||
| queueMicrotask(async () => { | ||
| await this._call(); | ||
| this._calls.clear(); | ||
| this.promiseConrtols.resolve(); | ||
| this.promise = null; | ||
| }); | ||
| } | ||
| return this.promise; | ||
| } | ||
| _call() { | ||
| return this.batch(() => { | ||
| for (const level1 of this._calls.values()) { | ||
| for (const cb of level1.values()) { | ||
| cb(); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
| var delayed_calls_default = DelayedCalls; | ||
| // packages/ffw/src/transform-structure/get-type.js | ||
| function getType(data) { | ||
| if (!data) { | ||
| return "unknown"; | ||
| } | ||
| if (Array.isArray(data)) { | ||
| return "array"; | ||
| } | ||
| if (data.__proto__ === Object.prototype) { | ||
| return "object"; | ||
| } | ||
| return "unknown"; | ||
| } | ||
| var get_type_default = getType; | ||
| // packages/ffw/src/transform-structure/iterate.js | ||
| iterate.EXIT = Symbol("exit"); | ||
| function iterate(structure, type, cb) { | ||
| if (type === "array") { | ||
| for (let i2 = 0; i2 < structure.length; i2++) { | ||
| const elem = structure[i2]; | ||
| const cbResult = cb(elem, i2); | ||
| if (cbResult === iterate.EXIT) { | ||
| return iterate.EXIT; | ||
| } | ||
| } | ||
| } else if (type === "object") { | ||
| for (let key in structure) { | ||
| const cbResult = cb(structure[key], key); | ||
| if (cbResult === iterate.EXIT) { | ||
| return iterate.EXIT; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| var iterate_default = iterate; | ||
| // packages/ffw/src/transform-structure/find.js | ||
| function find(structure, checkTarget) { | ||
| const type = get_type_default(structure); | ||
| if (checkTarget(structure) === true) { | ||
| return true; | ||
| } | ||
| if (type === "unknown") { | ||
| return false; | ||
| } | ||
| const iterateResult = iterate_default(structure, type, (elem) => { | ||
| if (find(elem, checkTarget)) { | ||
| return iterate_default.EXIT; | ||
| } | ||
| }); | ||
| if (iterateResult === iterate_default.EXIT) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| var find_default = find; | ||
| // packages/ffw/src/transform-structure/creators.js | ||
| var creators = { | ||
| array: () => [], | ||
| object: () => ({}) | ||
| }; | ||
| var creators_default = creators; | ||
| // packages/ffw/src/transform-structure/transform.js | ||
| transform.PASS = Symbol("pass"); | ||
| function transform(structure, cb, checkTarget) { | ||
| const type = get_type_default(structure); | ||
| if (checkTarget(structure, type)) { | ||
| return cb(structure); | ||
| } | ||
| if (type === "unknown") { | ||
| return structure; | ||
| } | ||
| const newData = creators_default[type](); | ||
| iterate_default(structure, type, (data, i2) => { | ||
| const transformResult = transform(data, cb, checkTarget); | ||
| if (transformResult === transform.PASS) { | ||
| return; | ||
| } | ||
| newData[i2] = transformResult; | ||
| }); | ||
| return newData; | ||
| } | ||
| var transform_default = transform; | ||
| // packages/ffw/src/form/form.js | ||
| transformFields.PASS = transform_default.PASS; | ||
| function transformFields(fields, getProperty) { | ||
| return transform_default( | ||
| fields, | ||
| (field) => { | ||
| const newValue = getProperty(field); | ||
| if (newValue === transformFields.PASS) { | ||
| return transform_default.PASS; | ||
| } | ||
| return transformFields(newValue, getProperty); | ||
| }, | ||
| (target) => target instanceof field_default | ||
| ); | ||
| } | ||
| var Form = class { | ||
| options = { | ||
| validateOnChange: true, | ||
| validateOnBlur: true, | ||
| validateOnMount: false, | ||
| checkPrevData: true | ||
| }; | ||
| fields = {}; | ||
| f = null; | ||
| validateSchema = {}; | ||
| onSubmit = () => { | ||
| }; | ||
| initValues = {}; | ||
| calls = new delayed_calls_default(); | ||
| ee = u(); | ||
| batch(cb) { | ||
| cb(); | ||
| } | ||
| createField(props = {}) { | ||
| return new field_default(props); | ||
| } | ||
| _getFlatFieldOrCreate(name, props) { | ||
| if (!this.fields[name]) { | ||
| this.fields[name] = this.createField({ name, form: this, ...props }); | ||
| } | ||
| return this.fields[name]; | ||
| } | ||
| constructor(props = {}) { | ||
| this.f = this.fields; | ||
| this.options = { ...this.options, ...props.options }; | ||
| this.onSubmit = props.onSubmit ?? (() => { | ||
| }); | ||
| this.initValues = props.initValues ?? {}; | ||
| for (const fieldName in this.initValues) { | ||
| const defaultValue = this.initValues[fieldName]; | ||
| this._getFlatFieldOrCreate(fieldName, { | ||
| value: defaultValue | ||
| }); | ||
| } | ||
| if (props.batch) { | ||
| this.batch = props.batch; | ||
| } | ||
| const validateObj = typeof props.validateSchema === "function" ? props.validateSchema(this) : props.validateSchema; | ||
| for (const fieldName in validateObj) { | ||
| const test = validateObj[fieldName]; | ||
| const field = this._getFlatFieldOrCreate(fieldName); | ||
| field.test = test; | ||
| } | ||
| if (this.options.validateOnMount) { | ||
| this.validate(); | ||
| } | ||
| } | ||
| async validate() { | ||
| const errors = await Promise.all( | ||
| [...this._flatFields].map((field) => field.validate()) | ||
| ); | ||
| return errors.every((error) => error === ""); | ||
| } | ||
| getValid() { | ||
| if (this._errors === 0) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| get valid() { | ||
| return this.getValid(); | ||
| } | ||
| submit = async () => { | ||
| const formValid = await this.validate(); | ||
| if (formValid) { | ||
| return this.onSubmit(this); | ||
| } | ||
| }; | ||
| reset() { | ||
| for (const field of this._flatFields) { | ||
| field.reset(); | ||
| } | ||
| } | ||
| getData() { | ||
| function rec(data) { | ||
| return transformFields(data, (field) => { | ||
| return { | ||
| error: field.error, | ||
| touched: field.touched, | ||
| value: rec(field.value) | ||
| }; | ||
| }); | ||
| } | ||
| return rec(this.fields); | ||
| } | ||
| getValues() { | ||
| return transformFields(this.fields, (field) => field.value); | ||
| } | ||
| getErrors() { | ||
| return transformFields(this.fields, (field) => { | ||
| if (find_default(field.value, (data) => data instanceof field_default)) { | ||
| return field.value; | ||
| } else { | ||
| if (field.error !== "") { | ||
| return field.error; | ||
| } | ||
| return transformFields.PASS; | ||
| } | ||
| }); | ||
| } | ||
| getTouches() { | ||
| return transformFields(this.fields, (field) => { | ||
| if (find_default(field.value, (data) => data instanceof field_default)) { | ||
| return field.value; | ||
| } else { | ||
| if (field.touched === true) { | ||
| return field.touched; | ||
| } | ||
| return transformFields.PASS; | ||
| } | ||
| }); | ||
| } | ||
| get values() { | ||
| return this.getValues(); | ||
| } | ||
| get errors() { | ||
| return this.getErrors(); | ||
| } | ||
| get touches() { | ||
| return this.getTouches(); | ||
| } | ||
| on(name, cb) { | ||
| this.ee.on(name, cb); | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| off(name, cb) { | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| _errors = 0; | ||
| _flatFields = /* @__PURE__ */ new Set(); | ||
| addField(field) { | ||
| if (field.error !== "") { | ||
| this._errors++; | ||
| } | ||
| this._flatFields.add(field); | ||
| } | ||
| removeField(field) { | ||
| if (field.error !== "") { | ||
| this._errors--; | ||
| } | ||
| this._flatFields.delete(field); | ||
| } | ||
| }; | ||
| var form_default = Form; | ||
| // packages/ffw/src/validators/yup.js | ||
| var yup = __toESM(require("yup"), 1); | ||
| function yupToTest(yupSchema) { | ||
| return async (value) => { | ||
| try { | ||
| await yupSchema.validate(value); | ||
| return ""; | ||
| } catch (error) { | ||
| return error.message; | ||
| } | ||
| }; | ||
| } | ||
| function prepareYup(structure) { | ||
| return transform_default( | ||
| structure, | ||
| (yupSchema) => { | ||
| return yupToTest(yupSchema); | ||
| }, | ||
| (value) => value instanceof yup.BaseSchema | ||
| ); | ||
| } | ||
| var yup_default = prepareYup; | ||
| // packages/ffw/src/index.js | ||
| var FfwField = field_default; | ||
| var FfwForm = form_default; | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| FfwField, | ||
| FfwForm, | ||
| Field, | ||
| Form, | ||
| prepareYup | ||
| }); |
| 'use strict'; | ||
| if (process.env.NODE_ENV === 'development') { | ||
| module.exports = require('./dev.js'); | ||
| } else { | ||
| module.exports = require('./prod.js'); | ||
| } |
| { | ||
| "type": "commonjs" | ||
| } |
| var A=Object.create;var d=Object.defineProperty;var C=Object.getOwnPropertyDescriptor;var D=Object.getOwnPropertyNames;var I=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty;var X=(r,t)=>{for(var e in t)d(r,e,{get:t[e],enumerable:!0})},y=(r,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of D(t))!R.call(r,i)&&i!==e&&d(r,i,{get:()=>t[i],enumerable:!(s=C(t,i))||s.enumerable});return r};var j=(r,t,e)=>(e=r!=null?A(I(r)):{},y(t||!r||!r.__esModule?d(e,"default",{value:r,enumerable:!0}):e,r)),M=r=>y(d({},"__esModule",{value:!0}),r);var L={};X(L,{FfwField:()=>J,FfwForm:()=>K,Field:()=>n,Form:()=>T,prepareYup:()=>O});module.exports=M(L);function B(r){let t=r||new Map;return{on(e,s){t.has(e)||t.set(e,[]),t.get(e).push(s)},off(e,s){t.has(e)&&t.set(e,t.get(e).filter(i=>i!==s))},emit(e,s){t.has(e)&&t.get(e).forEach(i=>i(s)),t.has("*")&&t.get("*").forEach(i=>i(e,s))}}}var k=B,c=k;var b=()=>"",v=class{form=null;ee=c();data={value:"",error:"",touched:!1};constructor({value:t="",touched:e=!1,error:s="",test:i=b,form:o=null}={}){this.form=o,this.test=b,this.initParams={value:t,touched:e,error:s,test:i},this.test=i,this.data.value=t,this.data.error=s,this.data.touched=e,o.addField(this)}get value(){return this.data.value}set value(t){this.set(t)}get error(){return this.data.error}set error(t){this.setError(t)}get touched(){return this.data.touched}set touched(t){this.setTouched(t)}get errorTouched(){return this.data.touched===!1?"":this.data.error}setData(t,e){let s=this.data[t];if(s===e)return;let i=this.errorTouched;if(this.data[t]=e,this.form.calls.add(this,t,()=>this.ee.emit(t,e)),this.form.calls.add(this.form,"*",()=>this.form.ee.emit("global")),t==="error"){let o=s,l=e;l!==""&&o===""?(this.form._errors++,this.form._errors===1&&this.form.calls.add(this.form,"valid",()=>this.form.ee.emit("valid",!1))):l===""&&o!==""&&(this.form._errors--,this.form._errors===0&&this.form.calls.add(this.form,"valid",()=>this.form.ee.emit("valid",!0)))}if(t==="error"||t==="touched"){if(i===this.errorTouched)return;this.form.calls.add(this,"errorTouched",()=>this.ee.emit("errorTouched",this.errorTouched))}}update(){this.form.calls.add(this,"*",()=>this.ee.emit("*")),this.form.calls.add(this.form,"*",()=>this.form.ee.emit("*"))}setError(t){this.setData("error",t)}setTouched(t){this.setData("touched",t)}set(t,e){if(this.setData("value",t),e===!0){this.validate();return}e!==!1&&e===void 0&&this.form.options.validateOnChange&&this.validate()}async validate(){let t=await this.test(this.data.value);return this.setError(t),t}on(t,e){return this.ee.on(t,e),()=>this.ee.off(t,e)}off(t,e){return()=>this.ee.off(t,e)}onInput=t=>{this.set(t.target.value)};onBlur=()=>{this.setTouched(!0),this.form.options.validateOnBlur&&this.validate()};reset(){this.setData("value",this.initParams.value),this.setData("touched",this.initParams.touched),this.setData("error",this.initParams.error)}},n=v;function N(r){let t,e;return[new Promise((s,i)=>{t=s,e=i,r&&r(t,e)}),{resolve:t,reject:e}]}var Y=N,w=Y;var g=class{_calls=new Map;constructor(t){this.batch=t||(e=>e())}promise=null;add(t,e,s){if(this._calls.has(t)||this._calls.set(t,new Map),this._calls.get(t).set(e,s),!this.promise){let[o,l]=w();this.promise=o,this.promiseConrtols=l,queueMicrotask(async()=>{await this._call(),this._calls.clear(),this.promiseConrtols.resolve(),this.promise=null})}return this.promise}_call(){return this.batch(()=>{for(let t of this._calls.values())for(let e of t.values())e()})}},E=g;function q(r){return r?Array.isArray(r)?"array":r.__proto__===Object.prototype?"object":"unknown":"unknown"}var m=q;u.EXIT=Symbol("exit");function u(r,t,e){if(t==="array")for(let s=0;s<r.length;s++){let i=r[s];if(e(i,s)===u.EXIT)return u.EXIT}else if(t==="object"){for(let s in r)if(e(r[s],s)===u.EXIT)return u.EXIT}}var h=u;function x(r,t){let e=m(r);return t(r)===!0?!0:e==="unknown"?!1:h(r,e,i=>{if(x(i,t))return h.EXIT})===h.EXIT}var S=x;var z={array:()=>[],object:()=>({})},P=z;p.PASS=Symbol("pass");function p(r,t,e){let s=m(r);if(e(r,s))return t(r);if(s==="unknown")return r;let i=P[s]();return h(r,s,(o,l)=>{let F=p(o,t,e);F!==p.PASS&&(i[l]=F)}),i}var f=p;a.PASS=f.PASS;function a(r,t){return f(r,e=>{let s=t(e);return s===a.PASS?f.PASS:a(s,t)},e=>e instanceof n)}var _=class{options={validateOnChange:!0,validateOnBlur:!0,validateOnMount:!1,checkPrevData:!0};fields={};f=null;validateSchema={};onSubmit=()=>{};initValues={};calls=new E;ee=c();batch(t){t()}createField(t={}){return new n(t)}_getFlatFieldOrCreate(t,e){return this.fields[t]||(this.fields[t]=this.createField({name:t,form:this,...e})),this.fields[t]}constructor(t={}){this.f=this.fields,this.options={...this.options,...t.options},this.onSubmit=t.onSubmit??(()=>{}),this.initValues=t.initValues??{};for(let s in this.initValues){let i=this.initValues[s];this._getFlatFieldOrCreate(s,{value:i})}t.batch&&(this.batch=t.batch);let e=typeof t.validateSchema=="function"?t.validateSchema(this):t.validateSchema;for(let s in e){let i=e[s],o=this._getFlatFieldOrCreate(s);o.test=i}this.options.validateOnMount&&this.validate()}async validate(){return(await Promise.all([...this._flatFields].map(e=>e.validate()))).every(e=>e==="")}getValid(){return this._errors===0}get valid(){return this.getValid()}submit=async()=>{if(await this.validate())return this.onSubmit(this)};reset(){for(let t of this._flatFields)t.reset()}getData(){function t(e){return a(e,s=>({error:s.error,touched:s.touched,value:t(s.value)}))}return t(this.fields)}getValues(){return a(this.fields,t=>t.value)}getErrors(){return a(this.fields,t=>S(t.value,e=>e instanceof n)?t.value:t.error!==""?t.error:a.PASS)}getTouches(){return a(this.fields,t=>S(t.value,e=>e instanceof n)?t.value:t.touched===!0?t.touched:a.PASS)}get values(){return this.getValues()}get errors(){return this.getErrors()}get touches(){return this.getTouches()}on(t,e){return this.ee.on(t,e),()=>this.ee.off(t,e)}off(t,e){return()=>this.ee.off(t,e)}_errors=0;_flatFields=new Set;addField(t){t.error!==""&&this._errors++,this._flatFields.add(t)}removeField(t){t.error!==""&&this._errors--,this._flatFields.delete(t)}},T=_;var V=j(require("yup"),1);function G(r){return async t=>{try{return await r.validate(t),""}catch(e){return e.message}}}function H(r){return f(r,t=>G(t),t=>t instanceof V.BaseSchema)}var O=H;var J=n,K=T;0&&(module.exports={FfwField,FfwForm,Field,Form,prepareYup}); |
-497
| // node_modules/.pnpm/utftu@1.0.12/node_modules/utftu/dist/ee/esm/prod.js | ||
| function i(s) { | ||
| let e = s || /* @__PURE__ */ new Map(); | ||
| return { on(t2, f) { | ||
| e.has(t2) || e.set(t2, []), e.get(t2).push(f); | ||
| }, off(t2, f) { | ||
| e.has(t2) && e.set(t2, e.get(t2).filter((o) => o !== f)); | ||
| }, emit(t2, f) { | ||
| e.has(t2) && e.get(t2).forEach((o) => o(f)), e.has("*") && e.get("*").forEach((o) => o(t2, f)); | ||
| } }; | ||
| } | ||
| var r = i; | ||
| var u = r; | ||
| // packages/ffw/src/field/field.js | ||
| var defaultTest = () => ""; | ||
| var Field = class { | ||
| form = null; | ||
| ee = u(); | ||
| data = { | ||
| value: "", | ||
| error: "", | ||
| touched: false | ||
| }; | ||
| constructor({ | ||
| value = "", | ||
| touched = false, | ||
| error = "", | ||
| test = defaultTest, | ||
| form = null | ||
| } = {}) { | ||
| this.form = form; | ||
| this.test = defaultTest; | ||
| this.initParams = { | ||
| value, | ||
| touched, | ||
| error, | ||
| test | ||
| }; | ||
| this.test = test; | ||
| this.data.value = value; | ||
| this.data.error = error; | ||
| this.data.touched = touched; | ||
| form.addField(this); | ||
| } | ||
| get value() { | ||
| return this.data.value; | ||
| } | ||
| set value(newValue) { | ||
| this.set(newValue); | ||
| } | ||
| get error() { | ||
| return this.data.error; | ||
| } | ||
| set error(newError) { | ||
| this.setError(newError); | ||
| } | ||
| get touched() { | ||
| return this.data.touched; | ||
| } | ||
| set touched(newTouched) { | ||
| this.setTouched(newTouched); | ||
| } | ||
| get errorTouched() { | ||
| if (this.data.touched === false) { | ||
| return ""; | ||
| } | ||
| return this.data.error; | ||
| } | ||
| setData(name, newData) { | ||
| const prevData = this.data[name]; | ||
| if (prevData === newData) { | ||
| return; | ||
| } | ||
| const oldErrorTouched = this.errorTouched; | ||
| this.data[name] = newData; | ||
| this.form.calls.add(this, name, () => this.ee.emit(name, newData)); | ||
| this.form.calls.add(this.form, "*", () => this.form.ee.emit("global")); | ||
| if (name === "error") { | ||
| const oldError = prevData; | ||
| const error = newData; | ||
| if (error !== "" && oldError === "") { | ||
| this.form._errors++; | ||
| if (this.form._errors === 1) { | ||
| this.form.calls.add( | ||
| this.form, | ||
| "valid", | ||
| () => this.form.ee.emit("valid", false) | ||
| ); | ||
| } | ||
| } else if (error === "" && oldError !== "") { | ||
| this.form._errors--; | ||
| if (this.form._errors === 0) { | ||
| this.form.calls.add( | ||
| this.form, | ||
| "valid", | ||
| () => this.form.ee.emit("valid", true) | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| if (name === "error" || name === "touched") { | ||
| if (oldErrorTouched === this.errorTouched) { | ||
| return; | ||
| } | ||
| this.form.calls.add( | ||
| this, | ||
| "errorTouched", | ||
| () => this.ee.emit("errorTouched", this.errorTouched) | ||
| ); | ||
| } | ||
| } | ||
| update() { | ||
| this.form.calls.add(this, "*", () => this.ee.emit("*")); | ||
| this.form.calls.add(this.form, "*", () => this.form.ee.emit("*")); | ||
| } | ||
| setError(error) { | ||
| this.setData("error", error); | ||
| } | ||
| setTouched(touched) { | ||
| this.setData("touched", touched); | ||
| } | ||
| set(value, validate) { | ||
| this.setData("value", value); | ||
| if (validate === true) { | ||
| this.validate(); | ||
| return; | ||
| } | ||
| if (validate === false) { | ||
| return; | ||
| } | ||
| if (validate === void 0 && this.form.options.validateOnChange) { | ||
| this.validate(); | ||
| } | ||
| } | ||
| async validate() { | ||
| const error = await this.test(this.data.value); | ||
| this.setError(error); | ||
| return error; | ||
| } | ||
| on(name, cb) { | ||
| this.ee.on(name, cb); | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| off(name, cb) { | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| onInput = (event) => { | ||
| this.set(event.target.value); | ||
| }; | ||
| onBlur = () => { | ||
| this.setTouched(true); | ||
| if (this.form.options.validateOnBlur) { | ||
| this.validate(); | ||
| } | ||
| }; | ||
| reset() { | ||
| this.setData("value", this.initParams.value); | ||
| this.setData("touched", this.initParams.touched); | ||
| this.setData("error", this.initParams.error); | ||
| } | ||
| }; | ||
| var field_default = Field; | ||
| // node_modules/.pnpm/utftu@1.0.12/node_modules/utftu/dist/create-controlled-promise/esm/prod.js | ||
| function m(o) { | ||
| let e, r2; | ||
| return [new Promise((i2, l) => { | ||
| e = i2, r2 = l, o && o(e, r2); | ||
| }), { resolve: e, reject: r2 }]; | ||
| } | ||
| var t = m; | ||
| var p = t; | ||
| // packages/ffw/src/delayed-calls/delayed-calls.js | ||
| var DelayedCalls = class { | ||
| _calls = /* @__PURE__ */ new Map(); | ||
| constructor(batch) { | ||
| this.batch = batch || ((cb) => cb()); | ||
| } | ||
| promise = null; | ||
| add(key1, key2, callback) { | ||
| if (!this._calls.has(key1)) { | ||
| this._calls.set(key1, /* @__PURE__ */ new Map()); | ||
| } | ||
| const level1 = this._calls.get(key1); | ||
| level1.set(key2, callback); | ||
| if (!this.promise) { | ||
| const [promise, promiseControls] = p(); | ||
| this.promise = promise; | ||
| this.promiseConrtols = promiseControls; | ||
| queueMicrotask(async () => { | ||
| await this._call(); | ||
| this._calls.clear(); | ||
| this.promiseConrtols.resolve(); | ||
| this.promise = null; | ||
| }); | ||
| } | ||
| return this.promise; | ||
| } | ||
| _call() { | ||
| return this.batch(() => { | ||
| for (const level1 of this._calls.values()) { | ||
| for (const cb of level1.values()) { | ||
| cb(); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| }; | ||
| var delayed_calls_default = DelayedCalls; | ||
| // packages/ffw/src/transform-structure/get-type.js | ||
| function getType(data) { | ||
| if (!data) { | ||
| return "unknown"; | ||
| } | ||
| if (Array.isArray(data)) { | ||
| return "array"; | ||
| } | ||
| if (data.__proto__ === Object.prototype) { | ||
| return "object"; | ||
| } | ||
| return "unknown"; | ||
| } | ||
| var get_type_default = getType; | ||
| // packages/ffw/src/transform-structure/iterate.js | ||
| iterate.EXIT = Symbol("exit"); | ||
| function iterate(structure, type, cb) { | ||
| if (type === "array") { | ||
| for (let i2 = 0; i2 < structure.length; i2++) { | ||
| const elem = structure[i2]; | ||
| const cbResult = cb(elem, i2); | ||
| if (cbResult === iterate.EXIT) { | ||
| return iterate.EXIT; | ||
| } | ||
| } | ||
| } else if (type === "object") { | ||
| for (let key in structure) { | ||
| const cbResult = cb(structure[key], key); | ||
| if (cbResult === iterate.EXIT) { | ||
| return iterate.EXIT; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| var iterate_default = iterate; | ||
| // packages/ffw/src/transform-structure/find.js | ||
| function find(structure, checkTarget) { | ||
| const type = get_type_default(structure); | ||
| if (checkTarget(structure) === true) { | ||
| return true; | ||
| } | ||
| if (type === "unknown") { | ||
| return false; | ||
| } | ||
| const iterateResult = iterate_default(structure, type, (elem) => { | ||
| if (find(elem, checkTarget)) { | ||
| return iterate_default.EXIT; | ||
| } | ||
| }); | ||
| if (iterateResult === iterate_default.EXIT) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| var find_default = find; | ||
| // packages/ffw/src/transform-structure/creators.js | ||
| var creators = { | ||
| array: () => [], | ||
| object: () => ({}) | ||
| }; | ||
| var creators_default = creators; | ||
| // packages/ffw/src/transform-structure/transform.js | ||
| transform.PASS = Symbol("pass"); | ||
| function transform(structure, cb, checkTarget) { | ||
| const type = get_type_default(structure); | ||
| if (checkTarget(structure, type)) { | ||
| return cb(structure); | ||
| } | ||
| if (type === "unknown") { | ||
| return structure; | ||
| } | ||
| const newData = creators_default[type](); | ||
| iterate_default(structure, type, (data, i2) => { | ||
| const transformResult = transform(data, cb, checkTarget); | ||
| if (transformResult === transform.PASS) { | ||
| return; | ||
| } | ||
| newData[i2] = transformResult; | ||
| }); | ||
| return newData; | ||
| } | ||
| var transform_default = transform; | ||
| // packages/ffw/src/form/form.js | ||
| transformFields.PASS = transform_default.PASS; | ||
| function transformFields(fields, getProperty) { | ||
| return transform_default( | ||
| fields, | ||
| (field) => { | ||
| const newValue = getProperty(field); | ||
| if (newValue === transformFields.PASS) { | ||
| return transform_default.PASS; | ||
| } | ||
| return transformFields(newValue, getProperty); | ||
| }, | ||
| (target) => target instanceof field_default | ||
| ); | ||
| } | ||
| var Form = class { | ||
| options = { | ||
| validateOnChange: true, | ||
| validateOnBlur: true, | ||
| validateOnMount: false, | ||
| checkPrevData: true | ||
| }; | ||
| fields = {}; | ||
| f = null; | ||
| validateSchema = {}; | ||
| onSubmit = () => { | ||
| }; | ||
| initValues = {}; | ||
| calls = new delayed_calls_default(); | ||
| ee = u(); | ||
| batch(cb) { | ||
| cb(); | ||
| } | ||
| createField(props = {}) { | ||
| return new field_default(props); | ||
| } | ||
| _getFlatFieldOrCreate(name, props) { | ||
| if (!this.fields[name]) { | ||
| this.fields[name] = this.createField({ name, form: this, ...props }); | ||
| } | ||
| return this.fields[name]; | ||
| } | ||
| constructor(props = {}) { | ||
| this.f = this.fields; | ||
| this.options = { ...this.options, ...props.options }; | ||
| this.onSubmit = props.onSubmit ?? (() => { | ||
| }); | ||
| this.initValues = props.initValues ?? {}; | ||
| for (const fieldName in this.initValues) { | ||
| const defaultValue = this.initValues[fieldName]; | ||
| this._getFlatFieldOrCreate(fieldName, { | ||
| value: defaultValue | ||
| }); | ||
| } | ||
| if (props.batch) { | ||
| this.batch = props.batch; | ||
| } | ||
| const validateObj = typeof props.validateSchema === "function" ? props.validateSchema(this) : props.validateSchema; | ||
| for (const fieldName in validateObj) { | ||
| const test = validateObj[fieldName]; | ||
| const field = this._getFlatFieldOrCreate(fieldName); | ||
| field.test = test; | ||
| } | ||
| if (this.options.validateOnMount) { | ||
| this.validate(); | ||
| } | ||
| } | ||
| async validate() { | ||
| const errors = await Promise.all( | ||
| [...this._flatFields].map((field) => field.validate()) | ||
| ); | ||
| return errors.every((error) => error === ""); | ||
| } | ||
| getValid() { | ||
| if (this._errors === 0) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| get valid() { | ||
| return this.getValid(); | ||
| } | ||
| submit = async () => { | ||
| const formValid = await this.validate(); | ||
| if (formValid) { | ||
| return this.onSubmit(this); | ||
| } | ||
| }; | ||
| reset() { | ||
| for (const field of this._flatFields) { | ||
| field.reset(); | ||
| } | ||
| } | ||
| getData() { | ||
| function rec(data) { | ||
| return transformFields(data, (field) => { | ||
| return { | ||
| error: field.error, | ||
| touched: field.touched, | ||
| value: rec(field.value) | ||
| }; | ||
| }); | ||
| } | ||
| return rec(this.fields); | ||
| } | ||
| getValues() { | ||
| return transformFields(this.fields, (field) => field.value); | ||
| } | ||
| getErrors() { | ||
| return transformFields(this.fields, (field) => { | ||
| if (find_default(field.value, (data) => data instanceof field_default)) { | ||
| return field.value; | ||
| } else { | ||
| if (field.error !== "") { | ||
| return field.error; | ||
| } | ||
| return transformFields.PASS; | ||
| } | ||
| }); | ||
| } | ||
| getTouches() { | ||
| return transformFields(this.fields, (field) => { | ||
| if (find_default(field.value, (data) => data instanceof field_default)) { | ||
| return field.value; | ||
| } else { | ||
| if (field.touched === true) { | ||
| return field.touched; | ||
| } | ||
| return transformFields.PASS; | ||
| } | ||
| }); | ||
| } | ||
| get values() { | ||
| return this.getValues(); | ||
| } | ||
| get errors() { | ||
| return this.getErrors(); | ||
| } | ||
| get touches() { | ||
| return this.getTouches(); | ||
| } | ||
| on(name, cb) { | ||
| this.ee.on(name, cb); | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| off(name, cb) { | ||
| return () => this.ee.off(name, cb); | ||
| } | ||
| _errors = 0; | ||
| _flatFields = /* @__PURE__ */ new Set(); | ||
| addField(field) { | ||
| if (field.error !== "") { | ||
| this._errors++; | ||
| } | ||
| this._flatFields.add(field); | ||
| } | ||
| removeField(field) { | ||
| if (field.error !== "") { | ||
| this._errors--; | ||
| } | ||
| this._flatFields.delete(field); | ||
| } | ||
| }; | ||
| var form_default = Form; | ||
| // packages/ffw/src/validators/yup.js | ||
| import * as yup from "yup"; | ||
| function yupToTest(yupSchema) { | ||
| return async (value) => { | ||
| try { | ||
| await yupSchema.validate(value); | ||
| return ""; | ||
| } catch (error) { | ||
| return error.message; | ||
| } | ||
| }; | ||
| } | ||
| function prepareYup(structure) { | ||
| return transform_default( | ||
| structure, | ||
| (yupSchema) => { | ||
| return yupToTest(yupSchema); | ||
| }, | ||
| (value) => value instanceof yup.BaseSchema | ||
| ); | ||
| } | ||
| var yup_default = prepareYup; | ||
| // packages/ffw/src/index.js | ||
| var FfwField = field_default; | ||
| var FfwForm = form_default; | ||
| export { | ||
| FfwField, | ||
| FfwForm, | ||
| field_default as Field, | ||
| form_default as Form, | ||
| yup_default as prepareYup | ||
| }; |
| function P(s){let t=s||new Map;return{on(e,r){t.has(e)||t.set(e,[]),t.get(e).push(r)},off(e,r){t.has(e)&&t.set(e,t.get(e).filter(i=>i!==r))},emit(e,r){t.has(e)&&t.get(e).forEach(i=>i(r)),t.has("*")&&t.get("*").forEach(i=>i(e,r))}}}var V=P,d=V;var T=()=>"",p=class{form=null;ee=d();data={value:"",error:"",touched:!1};constructor({value:t="",touched:e=!1,error:r="",test:i=T,form:o=null}={}){this.form=o,this.test=T,this.initParams={value:t,touched:e,error:r,test:i},this.test=i,this.data.value=t,this.data.error=r,this.data.touched=e,o.addField(this)}get value(){return this.data.value}set value(t){this.set(t)}get error(){return this.data.error}set error(t){this.setError(t)}get touched(){return this.data.touched}set touched(t){this.setTouched(t)}get errorTouched(){return this.data.touched===!1?"":this.data.error}setData(t,e){let r=this.data[t];if(r===e)return;let i=this.errorTouched;if(this.data[t]=e,this.form.calls.add(this,t,()=>this.ee.emit(t,e)),this.form.calls.add(this.form,"*",()=>this.form.ee.emit("global")),t==="error"){let o=r,l=e;l!==""&&o===""?(this.form._errors++,this.form._errors===1&&this.form.calls.add(this.form,"valid",()=>this.form.ee.emit("valid",!1))):l===""&&o!==""&&(this.form._errors--,this.form._errors===0&&this.form.calls.add(this.form,"valid",()=>this.form.ee.emit("valid",!0)))}if(t==="error"||t==="touched"){if(i===this.errorTouched)return;this.form.calls.add(this,"errorTouched",()=>this.ee.emit("errorTouched",this.errorTouched))}}update(){this.form.calls.add(this,"*",()=>this.ee.emit("*")),this.form.calls.add(this.form,"*",()=>this.form.ee.emit("*"))}setError(t){this.setData("error",t)}setTouched(t){this.setData("touched",t)}set(t,e){if(this.setData("value",t),e===!0){this.validate();return}e!==!1&&e===void 0&&this.form.options.validateOnChange&&this.validate()}async validate(){let t=await this.test(this.data.value);return this.setError(t),t}on(t,e){return this.ee.on(t,e),()=>this.ee.off(t,e)}off(t,e){return()=>this.ee.off(t,e)}onInput=t=>{this.set(t.target.value)};onBlur=()=>{this.setTouched(!0),this.form.options.validateOnBlur&&this.validate()};reset(){this.setData("value",this.initParams.value),this.setData("touched",this.initParams.touched),this.setData("error",this.initParams.error)}},n=p;function O(s){let t,e;return[new Promise((r,i)=>{t=r,e=i,s&&s(t,e)}),{resolve:t,reject:e}]}var A=O,F=A;var v=class{_calls=new Map;constructor(t){this.batch=t||(e=>e())}promise=null;add(t,e,r){if(this._calls.has(t)||this._calls.set(t,new Map),this._calls.get(t).set(e,r),!this.promise){let[o,l]=F();this.promise=o,this.promiseConrtols=l,queueMicrotask(async()=>{await this._call(),this._calls.clear(),this.promiseConrtols.resolve(),this.promise=null})}return this.promise}_call(){return this.batch(()=>{for(let t of this._calls.values())for(let e of t.values())e()})}},y=v;function C(s){return s?Array.isArray(s)?"array":s.__proto__===Object.prototype?"object":"unknown":"unknown"}var c=C;u.EXIT=Symbol("exit");function u(s,t,e){if(t==="array")for(let r=0;r<s.length;r++){let i=s[r];if(e(i,r)===u.EXIT)return u.EXIT}else if(t==="object"){for(let r in s)if(e(s[r],r)===u.EXIT)return u.EXIT}}var h=u;function b(s,t){let e=c(s);return t(s)===!0?!0:e==="unknown"?!1:h(s,e,i=>{if(b(i,t))return h.EXIT})===h.EXIT}var g=b;var D={array:()=>[],object:()=>({})},w=D;m.PASS=Symbol("pass");function m(s,t,e){let r=c(s);if(e(s,r))return t(s);if(r==="unknown")return s;let i=w[r]();return h(s,r,(o,l)=>{let _=m(o,t,e);_!==m.PASS&&(i[l]=_)}),i}var f=m;a.PASS=f.PASS;function a(s,t){return f(s,e=>{let r=t(e);return r===a.PASS?f.PASS:a(r,t)},e=>e instanceof n)}var S=class{options={validateOnChange:!0,validateOnBlur:!0,validateOnMount:!1,checkPrevData:!0};fields={};f=null;validateSchema={};onSubmit=()=>{};initValues={};calls=new y;ee=d();batch(t){t()}createField(t={}){return new n(t)}_getFlatFieldOrCreate(t,e){return this.fields[t]||(this.fields[t]=this.createField({name:t,form:this,...e})),this.fields[t]}constructor(t={}){this.f=this.fields,this.options={...this.options,...t.options},this.onSubmit=t.onSubmit??(()=>{}),this.initValues=t.initValues??{};for(let r in this.initValues){let i=this.initValues[r];this._getFlatFieldOrCreate(r,{value:i})}t.batch&&(this.batch=t.batch);let e=typeof t.validateSchema=="function"?t.validateSchema(this):t.validateSchema;for(let r in e){let i=e[r],o=this._getFlatFieldOrCreate(r);o.test=i}this.options.validateOnMount&&this.validate()}async validate(){return(await Promise.all([...this._flatFields].map(e=>e.validate()))).every(e=>e==="")}getValid(){return this._errors===0}get valid(){return this.getValid()}submit=async()=>{if(await this.validate())return this.onSubmit(this)};reset(){for(let t of this._flatFields)t.reset()}getData(){function t(e){return a(e,r=>({error:r.error,touched:r.touched,value:t(r.value)}))}return t(this.fields)}getValues(){return a(this.fields,t=>t.value)}getErrors(){return a(this.fields,t=>g(t.value,e=>e instanceof n)?t.value:t.error!==""?t.error:a.PASS)}getTouches(){return a(this.fields,t=>g(t.value,e=>e instanceof n)?t.value:t.touched===!0?t.touched:a.PASS)}get values(){return this.getValues()}get errors(){return this.getErrors()}get touches(){return this.getTouches()}on(t,e){return this.ee.on(t,e),()=>this.ee.off(t,e)}off(t,e){return()=>this.ee.off(t,e)}_errors=0;_flatFields=new Set;addField(t){t.error!==""&&this._errors++,this._flatFields.add(t)}removeField(t){t.error!==""&&this._errors--,this._flatFields.delete(t)}},E=S;import*as x from"yup";function I(s){return async t=>{try{return await s.validate(t),""}catch(e){return e.message}}}function R(s){return f(s,t=>I(t),t=>t instanceof x.BaseSchema)}var X=R;var ut=n,ht=E;export{ut as FfwField,ht as FfwForm,n as Field,E as Form,X as prepareYup}; |
| declare class DelayedCalls { | ||
| constructor(batch: () => void); | ||
| changes: Record<string, () => void>; | ||
| promise: Promise<void>; | ||
| addCall(name: string, callback: () => void): Promise<void>; | ||
| handleChanges(): void; | ||
| } | ||
| export default DelayedCalls; |
| import type ee from 'utftu/ee'; | ||
| import Form from './form'; | ||
| declare class Field<TValue, TForm extends Form<Field<TValue, TForm>>> { | ||
| constructor(props: { | ||
| name: string; | ||
| value: TValue; | ||
| error: string; | ||
| touched: boolean; | ||
| form: TForm; | ||
| }); | ||
| name: string; | ||
| form: TForm; | ||
| data: { | ||
| value: TValue; | ||
| error: string; | ||
| touched: boolean; | ||
| [name: string | number | symbol]: any; | ||
| }; | ||
| ee: typeof ee<string> | ||
| emitter: Emitter<{ | ||
| value: TValue; | ||
| error: string; | ||
| touched: boolean; | ||
| [name: string | number | symbol]: any; | ||
| }>; | ||
| get value(): TValue; | ||
| set value(value: TValue); | ||
| get error(): string; | ||
| set error(error: string); | ||
| get touched(): boolean; | ||
| set touched(touched: boolean); | ||
| setData(name: string, newData: TValue): void; | ||
| setError(error: string): void; | ||
| setTouched(touched: boolean): void; | ||
| set(value: TValue, validate?: boolean): void; | ||
| validate(): Promise<boolean>; | ||
| subscribe(name: string, listener: (newData: TValue) => void): void; | ||
| unsubscribe(name: string, listener: (newData: TValue) => void): void; | ||
| onChange(event: {target: {value: string}}): void; | ||
| onBlur(): void; | ||
| } | ||
| export default Field; |
| import type DelayedCalls from './delayd-calls'; | ||
| import type Field from './field'; | ||
| import type {BaseSchema} from 'yup'; | ||
| declare class Form<TField extends Field<any, Form<TField>>> { | ||
| constructor(props: { | ||
| validateSchema?: BaseSchema; | ||
| onSubmit?: (form: Form<TField>) => void; | ||
| initValues?: Record<string, any>; | ||
| initData?: Record<string, any>; | ||
| batch?: (cb: () => void) => void; | ||
| createField: (name: string) => TField; | ||
| options: { | ||
| validateOnChange: boolean; | ||
| validateOnBlur: boolean; | ||
| validateOnMount: boolean; | ||
| checkPrevData: boolean; | ||
| }; | ||
| }); | ||
| options: { | ||
| validateOnChange: boolean; | ||
| validateOnBlur: boolean; | ||
| validateOnMount: boolean; | ||
| checkPrevData: boolean; | ||
| }; | ||
| _fields: Record<string, TField>; | ||
| fields: this['_fields']; | ||
| f: this['_fields']; | ||
| calls: DelayedCalls; | ||
| validateSchema: any; | ||
| addField<TValue>(name: string, field: TField): void; | ||
| createField<TValue>(name: string): TField; | ||
| getField<TName extends keyof this['_fields']>( | ||
| name: TName | ||
| ): this['_fields'][TName]; | ||
| onSubmit(form: this): void; | ||
| batch(): void; | ||
| getFields(): this['_fields']; | ||
| iterateFields(cb: (field: TField) => void): void; | ||
| addGlobalListener( | ||
| listener: (field: TField, dataName: string, data: any) => void | ||
| ): void; | ||
| reset(): void; | ||
| setValue( | ||
| name: keyof this['_fields'], | ||
| value: keyof this['_fields'][typeof name] | ||
| ): void; | ||
| setTouched(name: keyof this['_fields'] | string, touched: boolean): void; | ||
| setError(name: keyof this['_fields'] | string, error: string): void; | ||
| setValues(values: Record<string, any>): void; | ||
| setErrors(errors: Record<string, string>): void; | ||
| setTouches(touches: Record<string, boolean>): void; | ||
| getValues(): Record< | ||
| keyof this['_fields'], | ||
| {[Property in keyof this['_fields']]: this['_fields'][Property]['value']} | ||
| >; | ||
| getErrors(): Record<keyof this['_fields'], string>; | ||
| getTouches(): Record<keyof this['_fields'], boolean>; | ||
| get values(): Record<keyof this['_fields'], ReturnType<this['getValues']>>; | ||
| get errors(): Record<keyof this['_fields'], string>; | ||
| get touches(): Record<keyof this['_fields'], boolean>; | ||
| get valid(): boolean; | ||
| } | ||
| export default Form; | ||
| declare class Box<Type> { | ||
| contents: Type; | ||
| } |
| import type Field from './field'; | ||
| import type Form from './form'; | ||
| type createFormProxy<TField extends Field = Field> = () => Form<TField>; | ||
| export {Field, Form, Field as FfwField, Form as FfwForm}; |
-21
| MIT License | ||
| Copyright (c) 2021 utftufutukgyftryidytftuv | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
0
-100%0
-100%1
-50%3
-25%1
-50%13287
-69.39%3
-75%301
-74.47%2
100%- Removed
- Removed