@jdlien/validator-utils 1.2.7 → 2.0.0
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.
- package/README.md +5 -18
- package/dist/validator-utils.d.ts +20 -28
- package/dist/validator-utils.js +1 -1
- package/package.json +18 -11
package/README.md
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
5
|
-
This package is a library of utility functions that can
|
|
6
|
-
strings
|
|
5
|
+
This package is a lightweight (<9KB or <4KB zipped) library of utility functions that can validate and sanitize
|
|
6
|
+
dates, times, strings, numbers, and more. This is especially useful in forms. This package is the sole dependency for the [@jdlien/validator package](https://github.com/jdlien/validator).
|
|
7
7
|
|
|
8
8
|
This package was separated from Validator so that it could be used in other projects without
|
|
9
9
|
pulling in the entire Validator package if you only need some of its validation and parsing functions without the form validation and error message functionality.
|
|
@@ -12,10 +12,6 @@ pulling in the entire Validator package if you only need some of its validation
|
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
14
|
npm install @jdlien/validator-utils
|
|
15
|
-
|
|
16
|
-
# or
|
|
17
|
-
|
|
18
|
-
yarn add @jdlien/validator-utils
|
|
19
15
|
```
|
|
20
16
|
|
|
21
17
|
## Utility Functions
|
|
@@ -24,9 +20,9 @@ Validator includes several utility functions that may be useful in your own code
|
|
|
24
20
|
If you wish to use these, you may import the functions directly from the module as an object that contains all the functions:
|
|
25
21
|
|
|
26
22
|
```javascript
|
|
27
|
-
import * as validatorUtils from '@jdlien/validator'
|
|
23
|
+
import * as validatorUtils from '@jdlien/validator-utils'
|
|
28
24
|
// you could assign the functions you need to more convenient variables
|
|
29
|
-
const {
|
|
25
|
+
const { parseDate, formatDateTime } = validatorUtils
|
|
30
26
|
```
|
|
31
27
|
|
|
32
28
|
Here is a list of the utility functions:
|
|
@@ -66,14 +62,5 @@ Here is a list of the utility functions:
|
|
|
66
62
|
Install dev dependencies:
|
|
67
63
|
|
|
68
64
|
```bash
|
|
69
|
-
|
|
65
|
+
pnpm install
|
|
70
66
|
```
|
|
71
|
-
|
|
72
|
-
When running Vite, you may get an error like
|
|
73
|
-
|
|
74
|
-
```
|
|
75
|
-
Module did not self-register: '...\node_modules\canvas\build\Release\canvas.node'
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
If that happens, you
|
|
79
|
-
need to install the canvas module manually: `bash npm rebuild canvas --update-binary `
|
|
@@ -1,48 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @format
|
|
2
|
+
* Validator Utils - Lightweight validation and parsing utilities
|
|
3
|
+
* Used by the @jdlien/validator package
|
|
5
4
|
*/
|
|
6
|
-
type DateParts = {
|
|
7
|
-
year: number;
|
|
8
|
-
month: number;
|
|
9
|
-
day: number;
|
|
10
|
-
};
|
|
11
|
-
export declare function isFormControl(el: any): boolean;
|
|
12
|
-
interface ValidationResult {
|
|
13
|
-
valid: boolean;
|
|
14
|
-
error?: boolean;
|
|
15
|
-
messages: string[];
|
|
16
|
-
}
|
|
17
|
-
export declare function isType(el: HTMLInputElement | HTMLTextAreaElement, types: string | string[]): boolean;
|
|
18
|
-
export declare function momentToFPFormat(format: string): string;
|
|
19
|
-
export declare function monthToNumber(str: string | number): number;
|
|
20
|
-
export declare function yearToFull(year: number | string): number;
|
|
21
|
-
export declare function parseDate(value: string | Date): Date;
|
|
22
|
-
export declare function guessDatePart(num: number, knownMeanings?: (string | null)[]): string[];
|
|
23
|
-
export declare function guessDateParts(str: string): DateParts;
|
|
24
5
|
export declare function parseTime(value: string): {
|
|
25
6
|
hour: number;
|
|
26
7
|
minute: number;
|
|
27
8
|
second: number;
|
|
28
9
|
} | null;
|
|
29
|
-
export declare function
|
|
10
|
+
export declare function isTime(value: string): boolean;
|
|
11
|
+
export declare function isMeridiem(token: string): boolean;
|
|
12
|
+
export declare function yearToFull(y: number | string): number;
|
|
13
|
+
export declare function parseDate(value: string | Date): Date;
|
|
30
14
|
export declare function parseDateTime(value: string | Date): Date | null;
|
|
31
15
|
export declare function formatDateTime(date: Date | string, format?: string): string;
|
|
32
|
-
export declare function
|
|
33
|
-
export declare function parseDateTimeToString(value: string | Date, format?: string): string;
|
|
16
|
+
export declare function momentToFPFormat(format: string): string;
|
|
34
17
|
export declare function isDate(value: string | Date): boolean;
|
|
35
18
|
export declare function isDateTime(value: string | Date): boolean;
|
|
19
|
+
export declare function parseDateToString(value: string | Date, format?: string): string;
|
|
20
|
+
export declare function parseDateTimeToString(value: string | Date, format?: string): string;
|
|
21
|
+
export declare function parseTimeToString(value: string, format?: string): string;
|
|
22
|
+
export declare function monthToNumber(str: string | number): number;
|
|
36
23
|
export declare function isDateInRange(date: Date, range: string): boolean;
|
|
37
|
-
export declare function
|
|
38
|
-
export declare function
|
|
24
|
+
export declare function isFormControl(el: any): boolean;
|
|
25
|
+
export declare function isType(el: HTMLInputElement | HTMLTextAreaElement, types: string | string[]): boolean;
|
|
39
26
|
export declare function isEmail(value: string): boolean;
|
|
40
27
|
export declare function parseNANPTel(value: string): string;
|
|
41
28
|
export declare function isNANPTel(value: string): boolean;
|
|
42
29
|
export declare function parseInteger(value: string): string;
|
|
43
|
-
export declare function isNumber(value: string): boolean;
|
|
44
|
-
export declare function parseNumber(value: string): string;
|
|
45
30
|
export declare function isInteger(value: string): boolean;
|
|
31
|
+
export declare function parseNumber(value: string): string;
|
|
32
|
+
export declare function isNumber(value: string): boolean;
|
|
46
33
|
export declare function parseUrl(value: string): string;
|
|
47
34
|
export declare function isUrl(value: string): boolean;
|
|
48
35
|
export declare function parseZip(value: string): string;
|
|
@@ -51,6 +38,11 @@ export declare function parsePostalCA(value: string): string;
|
|
|
51
38
|
export declare function isPostalCA(value: string): boolean;
|
|
52
39
|
export declare function isColor(value: string): boolean;
|
|
53
40
|
export declare function parseColor(value: string): string;
|
|
41
|
+
interface ValidationResult {
|
|
42
|
+
valid: boolean;
|
|
43
|
+
error?: boolean;
|
|
44
|
+
messages: string[];
|
|
45
|
+
}
|
|
54
46
|
export declare function normalizeValidationResult(res: boolean | string | {
|
|
55
47
|
valid: boolean;
|
|
56
48
|
message?: string;
|
package/dist/validator-utils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(i=typeof globalThis<"u"?globalThis:i||self,f(i.validatorUtils={}))})(this,function(i){"use strict";function f(e){return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement}function $(e,n){typeof n=="string"&&(n=[n]);const t=e.dataset.type||"",r=e.type;return!!(n.includes(t)||n.includes(r))}function k(e){return e.replace(/YYYY/g,"Y").replace(/YY/g,"y").replace(/MMMM/g,"F").replace(/MMM/g,"{3}").replace(/MM/g,"{2}").replace(/M/g,"n").replace(/DD/g,"{5}").replace(/D/g,"j").replace(/dddd/g,"l").replace(/ddd/g,"D").replace(/dd/g,"D").replace(/d/g,"w").replace(/HH/g,"{6}").replace(/H/g,"G").replace(/hh/g,"h").replace(/mm/g,"i").replace(/m/g,"i").replace(/ss/g,"S").replace(/s/g,"s").replace(/A/gi,"K").replace(/\{3\}/g,"M").replace(/\{2\}/g,"m").replace(/\{5\}/g,"d").replace(/\{6\}/g,"H")}function M(e){const n=parseInt(e);if(typeof e=="number"||!isNaN(n))return n-1;const t=new Date(`1 ${e} 2000`).getMonth();if(!isNaN(t))return t;const r={ja:0,en:0,fe:1,fé:1,ap:3,ab:3,av:3,mai:4,juin:5,juil:6,au:7,ag:7,ao:7,se:8,o:9,n:10,d:11};for(const l in r)if(e.toLowerCase().startsWith(l))return r[l];throw new Error("Invalid month name: "+e)}function w(e){return typeof e=="string"&&(e=parseInt(e.replace(/\D/g,""))),e>99?e:e<(new Date().getFullYear()+20)%100?e+2e3:e+1900}function m(e){if(e instanceof Date)return e;e=e.trim().toLowerCase();let n=0,t=0,r=0,l=0,s=0,a=0;const d=new RegExp(/\d{1,2}\:\d\d(?:\:\d\ds?)?\s?(?:[a|p]m?)?/gi);if(d.test(e)){const u=e.match(d)[0];e=e.replace(u,"").trim();const g=p(u);if(g!==null&&({hour:l,minute:s,second:a}=g),e.length<=2){const b=new Date;return new Date(b.getFullYear(),b.getMonth(),b.getDate(),l,s,a)}}const c=/(^|\b)(mo|tu|we|th|fr|sa|su|lu|mard|mer|jeu|ve|dom)[\w]*\.?/gi;e=e.replace(c,"").trim();const o=new Date(new Date().setHours(0,0,0,0));if(/(now|today)/.test(e))return o;if(e.includes("tomorrow"))return new Date(o.setDate(o.getDate()+1));e.length===8&&(e=e.replace(/(\d\d\d\d)(\d\d)(\d\d)/,"$1-$2-$3")),e.length===6&&(e=e.replace(/(\d\d)(\d\d)(\d\d)/,w(e.slice(0,2))+"-$2-$3"));try{({year:n,month:t,day:r}=Y(e))}catch{return new Date("")}return new Date(n,t-1,r,l,s,a)}function A(e,n=[null,null,null]){const t=r=>r.filter(l=>!n.includes(l));return e===0||e>31?t(["y"]):e>12?t(["d","y"]):e>=1&&e<=12?t(["m","d","y"]):[]}function Y(e){const n=e.split(/[\s-/:.,]+/).filter(s=>s!=="");if(n.length<3){if(e.match(/\d{4}/)!==null)throw new Error("Invalid Date");n.unshift(String(new Date().getFullYear()))}const t={year:0,month:0,day:0};function r(s,a){s==="year"?t.year=w(a):t[s]=a}let l=0;for(;!(t.year&&t.month&&t.day);){e:for(const s of n){if(l++,/^[a-zA-Zé]+$/.test(s)){t.month||r("month",M(s)+1);continue}if(/^'\d\d$/.test(s)||/^\d{3,5}$/.test(s)){t.year||r("year",parseInt(s.replace(/'/,"")));continue}const a=parseInt(s);if(isNaN(a))throw console.error(`not date because ${s} isNaN`),new Error("Invalid Date");const d=A(a,[t.year?"y":null,t.month?"m":null,t.day?"d":null]);if(d.length==1){if(d[0]==="m"&&!t.month){r("month",a);continue e}if(d[0]==="d"&&!t.day){r("day",a);continue e}if(d[0]==="y"&&!t.year){r("year",a);continue e}}l>3&&(!t.month&&d.includes("m")?r("month",a):!t.day&&d.includes("d")&&r("day",a))}if(l>6)throw new Error("Invalid Date")}if(t.year&&t.month&&t.day)return t;throw new Error("Invalid Date")}function p(e){if(e=e.trim().toLowerCase(),e==="now"){const o=new Date;return{hour:o.getHours(),minute:o.getMinutes(),second:o.getSeconds()}}if(e==="noon")return{hour:12,minute:0,second:0};const n=e.match(/(\d{3,4})/);if(n){const o=n[1].length,u=n[1].slice(0,o==3?1:2),g=n[1].slice(-2);e=e.replace(n[1],u+":"+g)}const t=new RegExp(/^(\d{1,2})(?::(\d{1,2}))?\s*(?:(a|p)\.?m?\.?)?$/i);if(t.test(e)){const o=e.match(t);if(o===null)return null;e=o[1]+":"+(o[2]||"00")+(o[3]||"")}const r=new RegExp(/^(\d{1,2}):(\d{1,2})(?::(\d{1,2}))?\s*(?:(a|p)m?)?$/i);if(!r.test(e))return null;const l=e.match(r);if(l===null)return null;const s=parseInt(l[1]),a=parseInt(l[2]),d=l[3]?parseInt(l[3]):0,c=l[4];return isNaN(s)||isNaN(a)||isNaN(d)?null:c==="p"&&s<12?{hour:s+12,minute:a,second:d}:c==="a"&&s===12?{hour:0,minute:a,second:d}:s<0||s>23||a<0||a>59||d<0||d>59?null:{hour:s,minute:a,second:d}}function E(e,n="h:mm A"){const t=p(e);if(t){const r=new Date;return r.setHours(t.hour),r.setMinutes(t.minute),r.setSeconds(t.second),r.setMilliseconds(0),h(r,n)}return""}function D(e){if(e instanceof Date)return e;if(e.trim().length<3)return null;e=e.replace(/(\d)T(\d)/,"$1 $2");let n=e.split(/[\s,]+/).filter(c=>c!==""),t="",r="",l="";n.forEach((c,o)=>{const u=c.match(/^(\d{1,4})([apAP]\.?[mM]?\.?)/);u?r=u[0]:(c.includes(":")||c==="now"||c==="noon")&&(r=c),R(c)&&(l=c,!r&&o>0&&S(n[o-1])&&(r=n[o-1]))}),r?n=n.filter(c=>c!==r&&c!==l):[t,n]=C(n);const s=r?`${r} ${l}`:t?n.join(" "):"",a=p(s)||{hour:0,minute:0,second:0},d=m(t||n.join(" ").trim()||"today");return!d||isNaN(d.getTime())?null:new Date(d.getFullYear(),d.getMonth(),d.getDate(),a.hour,a.minute,a.second)}function C(e){for(let n=3;n>0;n--){const t=e.slice(0,n).join(" ");if(N(t))return[t,e.slice(n)]}return["",e]}function h(e,n="YYYY-MM-DD"){if(e=m(e),isNaN(e.getTime()))return"";const t={y:e.getFullYear(),M:e.getMonth(),D:e.getDate(),W:e.getDay(),H:e.getHours(),m:e.getMinutes(),s:e.getSeconds(),ms:e.getMilliseconds()},r=(o,u=2)=>(o+"").padStart(u,"0"),l=()=>t.H%12||12,s=o=>o<12?"AM":"PM",a=o=>"January|February|March|April|May|June|July|August|September|October|November|December".split("|")[o];function d(o,u=0){const g="Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday".split("|");return u?g[o].slice(0,u):g[o]}const c={YY:String(t.y).slice(-2),YYYY:t.y,M:t.M+1,MM:r(t.M+1),MMMM:a(t.M),MMM:a(t.M).slice(0,3),D:String(t.D),DD:r(t.D),d:String(t.W),dd:d(t.W,2),ddd:d(t.W,3),dddd:d(t.W),H:String(t.H),HH:r(t.H),h:l(),hh:r(l()),A:s(t.H),a:s(t.H).toLowerCase(),m:String(t.m),mm:r(t.m),s:String(t.s),ss:r(t.s),SSS:r(t.ms,3)};return n.replace(/\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g,(o,u)=>u||c[o])}function x(e,n){const t=m(e);return isNaN(t.getTime())?"":((!n||n.length===0)&&(n="YYYY-MMM-DD"),h(t,n))}function H(e,n){const t=D(e);return t===null||isNaN(t.getTime())?"":((!n||n.length===0)&&(n="YYYY-MMM-DD h:mm A"),h(t,n))}function N(e){if(typeof e!="string"&&!(e instanceof Date))return!1;let n=m(e);return n==null?!1:!isNaN(n.getTime())}function P(e){if(typeof e!="string"&&!(e instanceof Date))return!1;let n=D(e);return n==null?!1:!isNaN(n.getTime())}function I(e,n){return!(n==="past"&&e>new Date||n==="future"&&e.getTime()<new Date().setHours(0,0,0,0))}function R(e){const n=e.toLowerCase().replace(/[.\s]/g,"");return["am","pm","a","p"].includes(n)}function S(e){let n=p(e);return n===null?!1:!isNaN(n.hour)&&!isNaN(n.minute)&&!isNaN(n.second)}function z(e){if(e.length>255||!new RegExp(/^.+@.+\.[a-zA-Z0-9]{2,}$/).test(e))return!1;let t="";return t+="^([a-zA-Z0-9!#$%'*+/=?^_`{|}~-]+",t+="(?:\\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*",t+="|",t+='"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*"',t+=")@(",t+="(",t+="(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\\.)+",t+="[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?",t+=")",t+=")$",new RegExp(t).test(e)}function Z(e){return e=e.replace(/^[^2-90]+/g,""),e=e.replace(/(\d\d\d).*?(\d\d\d).*?(\d\d\d\d)(.*)/,"$1-$2-$3$4"),e}function F(e){return/^\d\d\d-\d\d\d-\d\d\d\d$/.test(e)}function L(e){return e.replace(/[^0-9]/g,"")}function j(e){return/^\-?\d*\.?\d*$/.test(e)}function q(e){return e.replace(/[^\-0-9.]/g,"").replace(/(^-)|(-)/g,(n,t)=>t?"-":"").replace(/(\..*)\./g,"$1")}function W(e){return/^\-?\d*$/.test(e)}function U(e){return e=e.trim(),new RegExp("^(?:[a-z+]+:)?//","i").test(e)?e:"https://"+e}function J(e){return new RegExp("^(?:[-a-z+]+:)?//","i").test(e)}function V(e){return e=e.replace(/[^0-9]/g,"").replace(/(.{5})(.*)/,"$1-$2").trim(),e.length===6&&(e=e.replace(/-/,"")),e}function G(e){return new RegExp(/^\d{5}(-\d{4})?$/).test(e)}function K(e){return e=e.toUpperCase().replace(/[^A-Z0-9]/g,"").replace(/(.{3})\s*(.*)/,"$1 $2").trim(),e}function O(e){return new RegExp(/^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/).test(e)}function B(e){return["transparent","currentColor"].includes(e)?!0:typeof e!="string"||!e.trim()?!1:typeof CSS=="object"&&typeof CSS.supports=="function"?CSS.supports("color",e):X(e)}function X(e){const n=new RegExp(/^rgba?\(\s*(\d{1,3}%?,\s*){2}\d{1,3}%?\s*(?:,\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),t=new RegExp(/^hsla?\(\s*\d+(deg|grad|rad|turn)?,\s*\d{1,3}%,\s*\s*\d{1,3}%(?:,\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),r=new RegExp(/^rgba?\(\s*(\d{1,3}%?\s+){2}\d{1,3}%?\s*(?:\s*\/\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),l=new RegExp(/^hsla?\(\s*\d+(deg|grad|rad|turn)?\s+\d{1,3}%\s+\s*\d{1,3}%(?:\s*\/\s*(\.\d+|0+(\.\d+)?|1(\.0+)?|0|1\.0|\d{1,2}(\.\d*)?%|100%))?\s*\)$/),s=new RegExp(/^#([0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);let a="aliceblue|antiquewhite|aqua|aquamarine|azure|beige|bisque|black|blanchedalmond|blue|blueviolet|brown|burlywood|cadetblue|chartreuse|chocolate|coral|cornflowerblue|cornsilk|crimson|cyan|darkblue|darkcyan|darkgoldenrod|darkgray|darkgreen|darkgrey|darkkhaki|darkmagenta|darkolivegreen|darkorange|darkorchid|darkred|darksalmon|darkseagreen|darkslateblue|darkslategray|darkslategrey|darkturquoise|darkviolet|deeppink|deepskyblue|dimgray|dimgrey|dodgerblue|firebrick|floralwhite|forestgreen|fuchsia|gainsboro|ghostwhite|gold|goldenrod|gray|green|greenyellow|grey|honeydew|hotpink|indianred|indigo|ivory|khaki|lavender|lavenderblush|lawngreen|lemonchiffon|lightblue|lightcoral|lightcyan|lightgoldenrodyellow|lightgray|lightgreen|lightgrey|lightpink|lightsalmon|lightseagreen|lightskyblue|lightslategray|lightslategrey|lightsteelblue|lightyellow|lime|limegreen|linen|magenta|maroon|mediumaquamarine|mediumblue|mediumorchid|mediumpurple|mediumseagreen|mediumslateblue|mediumspringgreen|mediumturquoise|mediumvioletred|midnightblue|mintcream|mistyrose|moccasin|navajowhite|navy|oldlace|olive|olivedrab|orange|orangered|orchid|palegoldenrod|palegreen|paleturquoise|palevioletred|papayawhip|peachpuff|peru|pink|plum|powderblue|purple|rebeccapurple|red|rosybrown|royalblue|saddlebrown|salmon|sandybrown|seagreen|seashell|sienna|silver|skyblue|slateblue|slategray|slategrey|snow|springgreen|steelblue|tan|teal|thistle|tomato|turquoise|violet|wheat|white|whitesmoke|yellow|yellowgreen";const d=new RegExp(`^(${a})$`,"i");return n.test(e)||t.test(e)||r.test(e)||l.test(e)||s.test(e)||d.test(e)}let y=null;const T=new Map;function _(e){if(e=e.trim().toLowerCase(),["transparent","currentcolor"].includes(e))return e;if(T.has(e))return T.get(e);y===null&&(y=document.createElement("canvas"),y.willReadFrequently=!0);let n=y.getContext("2d");if(!n)throw new Error("Can't get context from colorCanvas");n.fillStyle=e,n.fillRect(0,0,1,1);let t=n.getImageData(0,0,1,1).data,r="#"+("000000"+(t[0]<<16|t[1]<<8|t[2]).toString(16)).slice(-6);return T.set(e,r),r}function Q(e){let n={valid:!1,error:!1,messages:[]};return typeof e=="boolean"?{valid:e,error:!1,messages:[]}:typeof e=="string"?{valid:!1,error:!1,messages:[e]}:(typeof e.valid=="boolean"&&(n.valid=e.valid),typeof e.message=="string"&&(n.messages=[e.message]),typeof e.messages=="string"&&(n.messages=[e.messages]),Array.isArray(e.messages)&&(n.messages=e.messages),e.error===!0&&(n.error=!0),n)}i.formatDateTime=h,i.isColor=B,i.isDate=N,i.isDateInRange=I,i.isDateTime=P,i.isEmail=z,i.isFormControl=f,i.isInteger=W,i.isMeridiem=R,i.isNANPTel=F,i.isNumber=j,i.isPostalCA=O,i.isTime=S,i.isType=$,i.isUrl=J,i.isZip=G,i.momentToFPFormat=k,i.monthToNumber=M,i.normalizeValidationResult=Q,i.parseColor=_,i.parseDate=m,i.parseDateTime=D,i.parseDateTimeToString=H,i.parseDateToString=x,i.parseInteger=L,i.parseNANPTel=Z,i.parseNumber=q,i.parsePostalCA=K,i.parseTime=p,i.parseTimeToString=E,i.parseUrl=U,i.parseZip=V,i.yearToFull=w,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
|
1
|
+
(function(i,T){typeof exports=="object"&&typeof module<"u"?T(exports):typeof define=="function"&&define.amd?define(["exports"],T):(i=typeof globalThis<"u"?globalThis:i||self,T(i.validatorUtils={}))})(this,(function(i){"use strict";const T=/^(?:[a-z+]+:)?\/\//i,C=/^(?:[-a-z+]+:)?\/\//i,H=/^\d{5}(-\d{4})?$/,R=/^[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ] ?[0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]$/,F=/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}$/,P=/^\d{3}-\d{3}-\d{4}$/,_=/^-?\d*$/,L=/^-?\d*\.?\d*$/;function h(e){let t=e.trim().toLowerCase();if(t==="now"){const o=new Date;return{hour:o.getHours(),minute:o.getMinutes(),second:o.getSeconds()}}if(t==="noon")return{hour:12,minute:0,second:0};t=t.replace(/\s+/g,"").replace(/\.+$/g,"");const r=t.replace(/^(\d{1,2})(\d{2})([ap]?m?\.?)$/i,"$1:$2$3").match(/^(\d{1,2})(?::(\d{1,2}))?(?::(\d{1,2}))?\s*([ap])?\.?m?\.?$/i);if(!r)return null;let l=+r[1],f=+(r[2]||0),m=+(r[3]||0);const s=r[4]?.toLowerCase();return s==="p"&&l<12&&(l+=12),s==="a"&&l===12&&(l=0),l>23||f>59||m>59?null:{hour:l,minute:f,second:m}}function I(e){return h(e)!==null}function Z(e){return/^[ap]\.?m?\.?$/i.test(e.replace(/\s/g,""))}const z="jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec",b=new RegExp(`(${z})[a-z]*`,"i");function O(e){return new Date(`1 ${e} 2000`).getMonth()}function D(e){if(typeof e=="string"&&(e=parseInt(e.replace(/\D/g,""))),e>99)return e;const t=(new Date().getFullYear()+20)%100;return e+(e<t?2e3:1900)}function Y(e){if(e instanceof Date)return e;let t=e.trim().toLowerCase();const n=new Date(new Date().setHours(0,0,0,0));if(/^(now|today)$/.test(t))return n;if(t==="tomorrow")return new Date(n.setDate(n.getDate()+1));t=t.replace(/\b(mon|tue|wed|thu|fri|sat|sun|lun|mar(?:di|tes)|mer|jeu|ven|sam|dim|dom)[a-z]*\.?\b/gi,"").trim();let r=0,l=0,f=0;const m=t.match(/(\d{1,2}:\d{2}(?::\d{2})?\s*[ap]?\.?m?\.?)/i);if(m){const M=h(m[1]);if(M&&({hour:r,minute:l,second:f}=M),t=t.replace(m[0],"").trim(),!t||t.length<=2){const d=new Date;return new Date(d.getFullYear(),d.getMonth(),d.getDate(),r,l,f)}}if(/^\d{8}$/.test(t))return new Date(+t.slice(0,4),+t.slice(4,6)-1,+t.slice(6,8),r,l,f);if(/^\d{6}$/.test(t))return new Date(D(+t.slice(0,2)),+t.slice(2,4)-1,+t.slice(4,6),r,l,f);const s=t.match(b);let o=-1,a=0,c=0;s&&(o=O(s[1]),t=t.replace(s[0]," ").trim());const p=t.match(/'(\d{2})\b/);p&&(a=D(+p[1]),t=t.replace(p[0]," ").trim());const u=t.match(/\d+/g)?.map(Number)||[];if(o>=0)u.length>=2?u[0]>99?(a=u[0],c=u[1]):u[1]>99?(a=u[1],c=u[0]):u[0]>31?(c=u[1],a=D(u[0])):u[1]>31?(c=u[0],a=D(u[1])):(c=u[0],a=a||D(u[1])):u.length===1&&(c=u[0],a=a||new Date().getFullYear());else if(u.length>=3){const[M,d,g]=u;M>31?(a=M,o=d-1,c=g):M>12&&g>12?(c=M,o=d-1,a=g>31?g:D(g)):g>31||g>12?(o=M-1,c=d,a=g>31?g:D(g)):(d>12,o=M-1,c=d,a=D(g))}else u.length===2&&(o=u[0]-1,c=u[1],a=a||new Date().getFullYear());return a&&o>=0&&c&&c>=1&&c<=31?new Date(a>99?a:D(a),o,c,r,l,f):new Date("")}function N(e){if(e instanceof Date)return e;let t=e.trim();if(t.length<3)return null;if(t=t.replace(/(\d)T(\d)/i,"$1 $2"),t=t.replace(/(^|[\sT])(\d{1,2})\.(\d{1,2})(?:\.(\d{1,2}))?(?=\s*[ap]\.?m?\.?\b|(?:\s|$))/gi,(s,o,a,c,p)=>`${o}${p?`${a}:${c}:${p}`:`${a}:${c}`}`),/^now$/i.test(t)){const s=new Date;return s.setMilliseconds(0),s}if(/^noon$/i.test(t)){const s=new Date;return new Date(s.getFullYear(),s.getMonth(),s.getDate(),12,0,0)}let n=null,r=t;const l=[/(\d{1,2}:\d{1,2}(?::\d{2})?)\s*([ap]\.?m?\.?)?/i,/\b(\d{1,2})\s*([ap]\.?m?\.?)\b/i,/\b(\d{3,4})([ap])m?\b/i];for(const s of l){const o=t.match(s);if(o){const a=h(o[0]);if(a){n=a,r=t.replace(o[0]," ").replace(/[\s.]+$/g,"").replace(/\s+/g," ").trim();break}}}if(!n){const s=r.match(/^(\d{4}[\-\/\.\s]\d{1,2}[\-\/\.\s]\d{1,2}|\d{8})\s+(\d{1,6})(\s*[ap]\.?m?\.?)?(?:\s*(?:z|utc|gmt|[+-]\d{2}:?\d{2})\b)?$/i);if(s){const o=s[2],a=(s[3]||"").replace(/\s+/g,"");let c=o+a;o.length===6&&(c=`${o.slice(0,2)}:${o.slice(2,4)}:${o.slice(4,6)}${a}`);const p=h(c);p&&(n=p,r=s[1])}}if(!n){const s=r.match(/^(.+?)\s+(\d{1,2})(\s*[ap]\.?m?\.?)?$/i);if(s){const o=s[2]+(s[3]||""),a=h(o);a&&b.test(s[1])&&(n=a,r=s[1])}}if(n&&(!r||/^,?\s*$/.test(r))){const s=new Date;return new Date(s.getFullYear(),s.getMonth(),s.getDate(),n.hour,n.minute,n.second)}const f=Y(r);if(isNaN(f.getTime()))return null;const m=n||{hour:0,minute:0,second:0};return new Date(f.getFullYear(),f.getMonth(),f.getDate(),m.hour,m.minute,m.second)}function $(e,t="YYYY-MM-DD"){if(typeof e=="string"&&(e=Y(e)),isNaN(e.getTime()))return"";const n=e.getFullYear(),r=e.getMonth(),l=e.getDate(),f=e.getDay(),m=e.getHours(),s=e.getMinutes(),o=e.getSeconds(),a=e.getMilliseconds(),c=(A,y=2)=>String(A).padStart(y,"0"),p=m%12||12,u=m<12?"AM":"PM",M=["January","February","March","April","May","June","July","August","September","October","November","December"],d=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],g={YYYY:n,YY:String(n).slice(-2),MMMM:M[r],MMM:M[r].slice(0,3),MM:c(r+1),M:r+1,DD:c(l),D:l,dddd:d[f],ddd:d[f].slice(0,3),dd:d[f].slice(0,2),d:f,HH:c(m),H:m,hh:c(p),h:p,mm:c(s),m:s,ss:c(o),s:o,SSS:c(a,3),A:u,a:u.toLowerCase()};return t.replace(/\[([^\]]+)]|YYYY|YY|MMMM|MMM|MM|M|DD|D|dddd|ddd|dd|d|HH|H|hh|h|mm|m|ss|s|SSS|A|a/g,(A,y)=>y??String(g[A]))}const j={YYYY:"Y",YY:"y",MMMM:"F",MMM:"M",MM:"m",M:"n",DD:"d",D:"j",dddd:"l",ddd:"D",dd:"D",d:"w",HH:"H",H:"G",hh:"h",mm:"i",m:"i",ss:"S",s:"s",A:"K",a:"K"},U=/YYYY|YY|MMMM|MMM|MM|M|DD|D|dddd|ddd|dd|d|HH|H|hh|mm|m|ss|s|A|a/g;function J(e){return e.replace(U,t=>j[t])}function K(e){return typeof e!="string"&&!(e instanceof Date)?!1:!isNaN(Y(e).getTime())}function V(e){if(typeof e!="string"&&!(e instanceof Date))return!1;const t=N(e);return t!==null&&!isNaN(t.getTime())}function G(e,t="YYYY-MMM-DD"){const n=Y(e);return isNaN(n.getTime())?"":$(n,t)}function W(e,t="YYYY-MMM-DD h:mm A"){const n=N(e);return n&&!isNaN(n.getTime())?$(n,t):""}function B(e,t="h:mm A"){const n=h(e);if(!n)return"";const r=new Date;return r.setHours(n.hour,n.minute,n.second,0),$(r,t)}const E={ja:0,en:0,fe:1,fé:1,ap:3,ab:3,av:3,mai:4,juin:5,juil:6,au:7,ag:7,ao:7,se:8,o:9,n:10,d:11};function k(e){if(typeof e=="number")return e-1;const t=parseInt(e);if(!isNaN(t))return t-1;const n=new Date(`1 ${e} 2000`).getMonth();if(!isNaN(n))return n;const r=e.toLowerCase();for(const l in E)if(r.startsWith(l))return E[l];throw new Error("Invalid month name: "+e)}function X(e,t){return!(t==="past"&&e>new Date||t==="future"&&e.getTime()<new Date().setHours(0,0,0,0))}function q(e){return e instanceof HTMLInputElement||e instanceof HTMLSelectElement||e instanceof HTMLTextAreaElement}function Q(e,t){return typeof t=="string"&&(t=[t]),t.includes(e.dataset.type||"")||t.includes(e.type)}function v(e){return e.length<=255&&F.test(e)}function x(e){return e.replace(/^[^2-90]+/g,"").replace(/(\d\d\d).*?(\d\d\d).*?(\d\d\d\d)(.*)/,"$1-$2-$3$4")}function ee(e){return P.test(e)}function te(e){return e.replace(/[^0-9]/g,"")}function ne(e){return _.test(e)}function ie(e){const t=e.replace(/[^\-0-9.]/g,"");let n="",r=!1,l=!1;for(let f=0;f<t.length;f+=1){const m=t[f];if(m==="-"){!l&&n.length===0&&(n+="-",l=!0);continue}if(m==="."){r||(n+=".",r=!0);continue}n+=m}return n}function re(e){return L.test(e)}function se(e){return e=e.trim(),T.test(e)?e:"https://"+e}function ae(e){return C.test(e)}function oe(e){return e=e.replace(/[^0-9]/g,"").replace(/(.{5})(.*)/,"$1-$2").trim(),e.length===6?e.replace(/-/,""):e}function ce(e){return H.test(e)}function ue(e){return e.toUpperCase().replace(/[^A-Z0-9]/g,"").replace(/(.{3})\s*(.*)/,"$1 $2").trim()}function le(e){return R.test(e)}function fe(e){return["transparent","currentColor"].includes(e)?!0:!e.trim()||typeof CSS>"u"||!CSS.supports?!1:CSS.supports("color",e)}let w=null;const S=new Map;function me(e){if(e=e.trim().toLowerCase(),["transparent","currentcolor"].includes(e))return e;if(S.has(e))return S.get(e);w||(w=document.createElement("canvas"),w.willReadFrequently=!0);const t=w.getContext("2d");t.fillStyle=e,t.fillRect(0,0,1,1);const n=t.getImageData(0,0,1,1).data,r="#"+("000000"+(n[0]<<16|n[1]<<8|n[2]).toString(16)).slice(-6);return S.set(e,r),r}function de(e){if(typeof e=="boolean")return{valid:e,error:!1,messages:[]};if(typeof e=="string")return{valid:!1,error:!1,messages:[e]};const t={valid:e.valid,error:e.error??!1,messages:[]};return typeof e.message=="string"?t.messages=[e.message]:typeof e.messages=="string"?t.messages=[e.messages]:Array.isArray(e.messages)&&(t.messages=e.messages),t}i.formatDateTime=$,i.isColor=fe,i.isDate=K,i.isDateInRange=X,i.isDateTime=V,i.isEmail=v,i.isFormControl=q,i.isInteger=ne,i.isMeridiem=Z,i.isNANPTel=ee,i.isNumber=re,i.isPostalCA=le,i.isTime=I,i.isType=Q,i.isUrl=ae,i.isZip=ce,i.momentToFPFormat=J,i.monthToNumber=k,i.normalizeValidationResult=de,i.parseColor=me,i.parseDate=Y,i.parseDateTime=N,i.parseDateTimeToString=W,i.parseDateToString=G,i.parseInteger=te,i.parseNANPTel=x,i.parseNumber=ie,i.parsePostalCA=ue,i.parseTime=h,i.parseTimeToString=B,i.parseUrl=se,i.parseZip=oe,i.yearToFull=D,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jdlien/validator-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"packageManager": "pnpm@10.11.0",
|
|
5
6
|
"module": "dist/validator-utils.js",
|
|
6
7
|
"main": "dist/validator-utils.js",
|
|
7
8
|
"types": "dist/validator-utils.d.ts",
|
|
@@ -12,6 +13,7 @@
|
|
|
12
13
|
"scripts": {
|
|
13
14
|
"dev": "vite",
|
|
14
15
|
"build": "vite build && tsc --emitDeclarationOnly",
|
|
16
|
+
"size:wire": "node scripts/measure-wire-size.mjs",
|
|
15
17
|
"preview": "vite preview",
|
|
16
18
|
"test": "vitest",
|
|
17
19
|
"coverage": "vitest --coverage"
|
|
@@ -38,16 +40,21 @@
|
|
|
38
40
|
},
|
|
39
41
|
"homepage": "https://github.com/jdlien/validator-utils#readme",
|
|
40
42
|
"devDependencies": {
|
|
41
|
-
"@types/jsdom": "^
|
|
42
|
-
"@vitest/coverage-
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"jsdom": "^22.1.0",
|
|
43
|
+
"@types/jsdom": "^27.0.0",
|
|
44
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
45
|
+
"canvas": "^3.2.1",
|
|
46
|
+
"jsdom": "^27.4.0",
|
|
46
47
|
"jsdom-global": "^3.0.2",
|
|
47
|
-
"prettier": "^3.
|
|
48
|
-
"typescript": "^5.
|
|
49
|
-
"vite": "^
|
|
50
|
-
"vitest": "^0.
|
|
48
|
+
"prettier": "^3.8.1",
|
|
49
|
+
"typescript": "^5.9.3",
|
|
50
|
+
"vite": "^7.3.1",
|
|
51
|
+
"vitest": "^4.0.18"
|
|
51
52
|
},
|
|
52
|
-
"sideEffects": false
|
|
53
|
+
"sideEffects": false,
|
|
54
|
+
"pnpm": {
|
|
55
|
+
"onlyBuiltDependencies": [
|
|
56
|
+
"canvas",
|
|
57
|
+
"esbuild"
|
|
58
|
+
]
|
|
59
|
+
}
|
|
53
60
|
}
|