adaptform 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.cjs.js +1 -0
  2. package/package.json +13 -12
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class r{_value;defaultValue;_error=null;isRequired;isValid;validators;constructor(e,t={}){this._value=e??null,this.defaultValue=e??null,this.isValid=e===null?null:!0,this.isRequired=t.isRequired??!0,this.validators=t.validators??[]}get value(){return this._value}set value(e){this._value=this.normalizeValue(e),this.checkValid()}get error(){return this._error??""}set error(e){this._error=e}set __valid_error(e){const[t="",i=""]=(this.error||"").split(" | ");e?t||(this.error=i?`${e} | ${this.error}`:e):this.error=i||null}reset(){this._value=this.defaultValue??null,this._error=null,this.isValid=this.defaultValue===null?null:!0}normalizeValue(e){return e}fieldIsEmpty(e){return e==null||e===""?!0:typeof e=="object"&&Object.keys(e).length===0}get isEmpty(){return this.fieldIsEmpty(this._value)}fieldIsZero(e){return e===0||e==="0"}get isZero(){return this.fieldIsZero(this._value)}checkValid(){if(this.isEmpty&&!this.isZero)return this.isValid=!this.isRequired,this.isValid;this.isValid=!0;for(const e of this.validators){const t=e(this._value);if(t)return this._error=t,this.isValid=!1,!1}return this._error=null,this.isValid}}class m extends r{minLength;maxLength;regex;example;mask;returnWithMask;constructor(e="",t={}){super(e,t),this.minLength=t.minLength??null,this.maxLength=t.maxLength??null,this.regex=t.regex instanceof RegExp?t.regex:new RegExp(t.regex||".*"),this.example=t.example??"Enter a valid string",this.mask=t.mask??null,this.returnWithMask=t.returnWithMask??!1,this.validators.push(this.minLengthValidator.bind(this)),this.validators.push(this.maxLengthValidator.bind(this)),this.validators.push(this.regexValidator.bind(this))}normalizeValue(e){return this.fieldIsEmpty(e)?"":String(e)}get valueClear(){if(this.returnWithMask)return this._value===""?null:this._value;if(!this.mask)return this._value||null;const e=this.mask.split(/_/g).join("");return(this._value||"").replace(new RegExp(`[${e}]`,"g"),"")}get value(){return this._value}set value(e){if(!this.mask||!e){super.value=e;return}const t=this.mask.replace(/_/g,""),i=e.replace(new RegExp(`[${t}]`,"g"),"");if(!i){super.value="";return}let s=0;const l=this.mask.replace(/_/g,()=>i[s++]??"_"),a=l.indexOf("_");super.value=a===-1?l:l.slice(0,a)}minLengthValidator(e){return this.minLength===null||!e?null:e.length<this.minLength?`Minimum ${this.minLength} characters`:null}maxLengthValidator(e){return this.maxLength===null||!e?null:e.length>this.maxLength?`Maximum ${this.maxLength} characters`:null}regexValidator(e){return e?this.regex.test(e)?null:this.example?`Example: ${this.example}`:"Invalid format":null}}class d extends r{min;max;example;constructor(e=null,t={}){super(e,t),this.min=t.min??null,this.max=t.max??null,this.example=t.example??"Enter a valid number",this.validators.push(this.typeValidator.bind(this)),this.validators.push(this.minValidator.bind(this)),this.validators.push(this.maxValidator.bind(this))}normalizeValue(e){if(this.fieldIsEmpty(e))return null;if(typeof e=="number")return isNaN(e)?null:e;if(typeof e=="string"){const t=e.trim();if(t==="")return null;const i=Number(t);return isNaN(i)?null:i}return null}get valueClear(){return this._value}typeValidator(e){return e===null?this.isRequired?`Example: ${this.example}`:null:typeof e!="number"||isNaN(e)?this.example?`Example: ${this.example}`:"Invalid number":null}minValidator(e){return e===null||this.min===null?null:e<this.min?`Minimum value: ${this.min}`:null}maxValidator(e){return e===null||this.max===null?null:e>this.max?`Maximum value: ${this.max}`:null}}class o extends r{mode;min;max;example;constructor(e=null,t={}){const i=e instanceof Date?e:e?new Date(e):null;super(i,t),this.mode=t.mode??"datetime",this.min=t.min?new Date(t.min):null,this.max=t.max?new Date(t.max):null,this.example=t.example??(this.mode==="date"?"YYYY-MM-DD":"YYYY-MM-DD HH:mm:ss"),this.validators.push(this.typeValidator.bind(this)),this.validators.push(this.minValidator.bind(this)),this.validators.push(this.maxValidator.bind(this))}normalizeValue(e){if(this.fieldIsEmpty(e))return null;if(e instanceof Date)return isNaN(e.getTime())?null:e;if(typeof e=="string"||typeof e=="number"){const t=new Date(e);return isNaN(t.getTime())?null:t}return null}get valueClear(){if(!this._value)return null;const e=l=>String(l).padStart(2,"0"),t=this._value.getFullYear(),i=e(this._value.getMonth()+1),s=e(this._value.getDate());return this.mode==="date"?`${t}-${i}-${s}`:this._value.toISOString()}typeValidator(e){return e===null?this.isRequired?`Example: ${this.example}`:null:!(e instanceof Date)||isNaN(e.getTime())?this.example?`Example: ${this.example}`:"Invalid date format":null}minValidator(e){return!e||!this.min?null:e<this.min?`Date must be after ${this.formatDate(this.min)}`:null}maxValidator(e){return!e||!this.max?null:e>this.max?`Date must be before ${this.formatDate(this.max)}`:null}formatDate(e){const t=u=>String(u).padStart(2,"0"),i=e.getFullYear(),s=t(e.getMonth()+1),l=t(e.getDate()),a=t(e.getHours()),h=t(e.getMinutes());return this.mode==="date"?`${i}-${s}-${l}`:`${i}-${s}-${l} ${a}:${h}`}}class c{_errors={};get declaredFields(){return Object.keys(this).filter(e=>this[e]instanceof r)}checkValid(){let e=!0;for(const t of this.declaredFields){const i=this[t];i instanceof r&&(i.checkValid(),(i.isValid===!1||i.isValid===null&&i.isRequired)&&(e=!1))}return e}get valueObject(){const e={};for(const t of this.declaredFields){const i=this[t];e[t]=i instanceof r?i.valueClear:i}return e}get valueArray(){return this.declaredFields.map(e=>{const t=this[e];return t instanceof r?t.valueClear:t})}get errors(){return this._errors}set errors(e){for(const t in e){const i=this[t];i instanceof r?i.error=e[t]:this._errors[t]=e[t]}}set fields(e){for(const t in e){if(!this.declaredFields.includes(t))throw new Error(`Field "${t}" is not declared in form "${this.constructor.name}"`);const i=this[t],s=e[t];i instanceof r?i.value=s:this[t]=s}}setFieldValue(e,t){const i=this[e];i instanceof r?i.value=t:this[e]=t}getFieldValue(e){const t=this[e];return t instanceof r?t.valueClear:t}reset(){for(const e of this.declaredFields){const t=this[e];t instanceof r&&t.reset()}for(const e of Object.keys(this._errors))delete this._errors[e];return this}}exports.DateTimeField=o;exports.Form=c;exports.FormField=r;exports.NumberField=d;exports.StringField=m;
package/package.json CHANGED
@@ -1,29 +1,30 @@
1
1
  {
2
2
  "name": "adaptform",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Library of adaptive forms of validation",
5
- "main": "index.js",
6
- "module": "./dist/index.mjs",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs.js",
7
+ "module": "./dist/index.es.js",
7
8
  "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.es.js",
12
+ "require": "./dist/index.cjs.js"
13
+ }
14
+ },
8
15
  "files": [
9
16
  "dist/**/*"
10
17
  ],
11
18
  "scripts": {
19
+ "dev": "vite",
12
20
  "build": "vite build",
13
21
  "prepublishOnly": "npm run build"
14
22
  },
15
- "keywords": [
16
- "form",
17
- "validation",
18
- "typescript"
19
- ],
23
+ "keywords": ["form", "validation", "typescript"],
20
24
  "author": "ArhiMEN",
21
25
  "license": "MIT",
22
- "type": "module",
23
- "dependencies": {
24
- "vite": "^7.1.1"
25
- },
26
26
  "devDependencies": {
27
+ "vite": "^7.1.1",
27
28
  "vite-plugin-dts": "^4.0.0",
28
29
  "typescript": "^5.2.2"
29
30
  }