UNPKG

rgex

Version:

A powerful, chainable regex builder platform with comprehensive validation utilities

2 lines (1 loc) 14.4 kB
var V={EMAIL:"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$",URL:"^https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&//=]*)$",PHONE:"^\\+?[1-9]\\d{4,14}$",DATE:"^\\d{4}-\\d{2}-\\d{2}$",TIME:"^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$",INTEGER:"^-?\\d+$",DECIMAL:"^-?\\d+(\\.\\d+)?$",POSITIVE_INTEGER:"^[1-9]\\d*$",POSITIVE_DECIMAL:"^([1-9]\\d*|0)(\\.\\d+)?$",UUID:"^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$",MONGO_ID:"^[0-9a-fA-F]{24}$",IPV4:"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$",IPV6:"^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$",DOMAIN:"^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?.)+[a-zA-Z]{2,}$",MAC_ADDRESS:"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$",HEX_COLOR:"^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$",SLUG:"^[a-z0-9]+(?:-[a-z0-9]+)*$",USERNAME:"^[a-zA-Z0-9_]{3,20}$",FILE_EXTENSION:"^.*\\.[a-zA-Z0-9]{1,10}$",IMAGE_FILE:"^.*\\.(jpg|jpeg|png|gif|bmp|svg|webp|ico)$",DOCUMENT_FILE:"^.*\\.(pdf|doc|docx|txt|rtf|odt)$",TIMESTAMP:"^[0-9]{10,13}$",ISO_DATETIME:"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?Z?$",CREDIT_CARD:"^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3[0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$",VISA_CARD:"^4[0-9]{12}(?:[0-9]{3})?$",MASTERCARD:"^5[1-5][0-9]{14}$",AMEX:"^3[47][0-9]{13}$",PASSWORD:"^.{6,}$",STRONG_PASSWORD:`^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?]).{8,}$`,DIGITS_ONLY:"^\\d+$",LETTERS_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8,10}$",YOUTUBE_VIDEO_ID:"^[a-zA-Z0-9_-]{11}$",AWS_S3_BUCKET:"^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$",DOCKER_IMAGE:"^[a-z0-9]+(?:[._-][a-z0-9]+)*(?:\\/[a-z0-9]+(?:[._-][a-z0-9]+)*)*(?::[a-zA-Z0-9._-]+)?$",K8S_RESOURCE_NAME:"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",NPM_PACKAGE:"^(?:@[a-z0-9-*~][a-z0-9-*._~]*\\/)?[a-z0-9-~][a-z0-9-._~]*$",E164_PHONE:"^\\+[1-9]\\d{1,14}$",LINKEDIN_PROFILE:"^https:\\/\\/(www\\.)?linkedin\\.com\\/in\\/[a-zA-Z0-9-]+\\/?$",GOOGLE_DRIVE_ID:"^[a-zA-Z0-9-_]{25,}$",FIREBASE_PROJECT_ID:"^[a-z0-9-]{6,30}$",AZURE_RESOURCE_GROUP:"^[a-zA-Z0-9._()-]{1,90}$"},A={ipv6:{type:"custom",pattern:V.IPV6,description:"IPv6 address",examples:["2001:0db8:85a3:0000:0000:8a2e:0370:7334","::1"]},ipv4:{type:"custom",pattern:V.IPV4,description:"IPv4 address",examples:["192.168.1.1","127.0.0.1"]},macaddress:{type:"custom",pattern:V.MAC_ADDRESS,description:"MAC address",examples:["00:1B:44:11:3A:B7","00-1B-44-11-3A-B7"]},domain:{type:"custom",pattern:V.DOMAIN,description:"Domain name",examples:["example.com","subdomain.example.org"]},timestamp:{type:"custom",pattern:V.TIMESTAMP,description:"Unix timestamp",examples:["1640995200","1640995200000"]},datetime:{type:"custom",pattern:V.ISO_DATETIME,description:"ISO datetime format",examples:["2023-12-25T10:30:00Z","2023-12-25T10:30:00.123Z"]},time:{type:"time",pattern:V.TIME,description:"Time in HH:MM or HH:MM:SS format",examples:["14:30","09:15:30"]},ukpostcode:{type:"custom",pattern:V.UK_POSTCODE,description:"UK postcode",examples:["SW1A 1AA","M1 1AA","B33 8TH"]},germanpostcode:{type:"custom",pattern:V.GERMAN_POSTCODE,description:"German postcode",examples:["10115","80331","20095"]},frenchpostcode:{type:"custom",pattern:V.FRENCH_POSTCODE,description:"French postcode",examples:["75001","69001","13001"]},zipcode:{type:"custom",pattern:V.US_ZIP_CODE,description:"US ZIP code",examples:["12345","12345-6789"]},postalcode:{type:"custom",pattern:V.CANADIAN_POSTAL_CODE,description:"Canadian postal code",examples:["K1A 0A6","M5V 3L9"]},email:{type:"email",pattern:V.EMAIL,description:"Valid email address",examples:["user@example.com","test.email+tag@domain.co.uk"]},phone:{type:"phone",pattern:V.PHONE,description:"Phone number with optional country code",examples:["+1234567890","1234567890"]},url:{type:"url",pattern:V.URL,description:"HTTP or HTTPS URL",examples:["https://example.com","http://test.org/path"]},date:{type:"date",pattern:V.DATE,description:"Date in YYYY-MM-DD format",examples:["2023-12-25","2024-01-01"]},number:{type:"number",pattern:V.DECIMAL,description:"Integer or decimal number",examples:["123","-45.67","0.5"]},uuid:{type:"custom",pattern:V.UUID,description:"UUID (Universally Unique Identifier)",examples:["123e4567-e89b-12d3-a456-426614174000"]},guid:{type:"custom",pattern:V.UUID,description:"GUID (Globally Unique Identifier)",examples:["123e4567-e89b-12d3-a456-426614174000"]},hexcolor:{type:"custom",pattern:V.HEX_COLOR,description:"Hexadecimal color code",examples:["#ff0000","#f00"]},letters:{type:"text",pattern:V.LETTERS_ONLY,description:"Letters only (alphabetic characters)",examples:["abc","Hello","World"]},text:{type:"text",pattern:V.TEXT,description:"General text with letters, numbers, spaces, and common punctuation",examples:["hello world","Hello, World!","This is text."]},alphanumeric:{type:"text",pattern:V.ALPHANUMERIC,description:"Alphanumeric characters only (letters and numbers)",examples:["abc123","Test123","user1"]},password:{type:"custom",pattern:V.PASSWORD,description:"Basic password (minimum 6 characters)",examples:["password123","mypass"]},creditcard:{type:"custom",pattern:V.CREDIT_CARD,description:"Credit card number",examples:["4111111111111111","5555555555554444"]},visa:{type:"custom",pattern:V.VISA_CARD,description:"Visa credit card number",examples:["4111111111111111","4012888888881881"]},mastercard:{type:"custom",pattern:V.MASTERCARD,description:"Mastercard credit card number",examples:["5555555555554444","5105105105105100"]},username:{type:"text",pattern:V.USERNAME,description:"Username (3-20 characters, letters, numbers, underscores)",examples:["user123","john_doe","admin"]},ssn:{type:"custom",pattern:V.SSN,description:"US Social Security Number",examples:["123-45-6789","987-65-4321"]},fileextension:{type:"custom",pattern:V.FILE_EXTENSION,description:"File with extension",examples:["document.pdf","image.jpg","script.js"]},imagefile:{type:"custom",pattern:V.IMAGE_FILE,description:"Image file",examples:["photo.jpg","logo.png","icon.svg"]},documentfile:{type:"custom",pattern:V.DOCUMENT_FILE,description:"Document file",examples:["report.pdf","letter.doc","notes.txt"]}};var Q=["password","123456","qwerty","admin","login","welcome","dragon","princess","letmein","monkey","sunshine","master","shadow","football","baseball","superman","michael","ninja","mustang","computer"],U=`!@#$%^&*()_+\\-=\\[\\]{};':"\\\\|,.<>\\/?`,Z="[^\\w\\s]",$="[^\\x00-\\x7F]";var B={name:"RGex",version:"1.0.0",description:"A powerful, chainable regex builder platform",author:"duongnguyen321",website:"https://duongnguyen321.github.io/rgex/",repository:"https://github.com/duongnguyen321/rgex",defaults:{passwordValidation:{minLength:8,maxLength:128,hasNumber:!0,hasSpecial:!0,hasUpperChar:!0,hasLowerChar:!0,hasSymbol:!1,hasUnicode:!1,noSequential:!1,noRepeating:!1,noCommonWords:!1,customPattern:void 0},humanTextConfidence:{high:0.8,medium:0.6,low:0.4},flags:{global:!1,ignoreCase:!1,multiline:!1,dotAll:!1,unicode:!1,sticky:!1}},features:{humanTextParsing:!0,advancedPasswordValidation:!0,prebuiltPatterns:!0,builderPattern:!0,validationMethods:!0},messages:{errors:{invalidPattern:"Invalid regex pattern provided",unknownHumanText:"Could not understand the text description",invalidCustomPattern:"Invalid custom pattern in password validation",validationFailed:"Validation failed for the provided input"},success:{patternExtracted:"Successfully extracted pattern from human text",validationRulesExtracted:"Successfully extracted validation rules",passwordAnalyzed:"Password analysis completed"}}};function Y(v,y={}){let f={...B.defaults.passwordValidation,...y},b={length:M(v,f.minLength,f.maxLength),hasNumber:O(v,f.hasNumber),hasSpecial:W(v,f.hasSpecial),hasUpperChar:l(v,f.hasUpperChar),hasLowerChar:H(v,f.hasLowerChar),hasSymbol:j(v,f.hasSymbol),hasUnicode:k(v,f.hasUnicode),noSequential:i(v,f.noSequential),noRepeating:g(v,f.noRepeating),noCommonWords:L(v,f.noCommonWords),customPattern:n(v,f.customPattern)},x=[],z=0;for(let[q,I]of Object.entries(b)){if(I.required&&!I.passed)x.push(I.message);if(I.passed)z++}let F=G(v,b,z),J=P(F,v.length,z);return{error:x.length>0?{message:"Password does not meet requirements",requirements:x}:null,pass:b,score:F,strength:J}}function M(v,y,f){let b=v.length,x=b>=y&&(f===void 0||b<=f),z="";if(f!==void 0)z=`Password must be between ${y} and ${f} characters`;else z=`Password must be at least ${y} characters`;return{passed:x,required:!0,message:z}}function O(v,y){return{passed:/\d/.test(v),required:y,message:"Password must contain at least one number"}}function W(v,y){return{passed:new RegExp(`[${U}]`).test(v),required:y,message:"Password must contain at least one special character"}}function l(v,y){return{passed:/[A-Z]/.test(v),required:y,message:"Password must contain at least one uppercase letter"}}function H(v,y){return{passed:/[a-z]/.test(v),required:y,message:"Password must contain at least one lowercase letter"}}function j(v,y){return{passed:new RegExp(Z).test(v),required:y,message:"Password must contain at least one symbol"}}function k(v,y){return{passed:new RegExp($).test(v),required:y,message:"Password must contain at least one unicode character"}}function i(v,y){if(!y)return{passed:!0,required:!1,message:""};return{passed:!C(v),required:y,message:"Password must not contain sequential characters (e.g., 123, abc)"}}function g(v,y){if(!y)return{passed:!0,required:!1,message:""};return{passed:!D(v),required:y,message:"Password must not contain repeating characters (e.g., aaa, 111)"}}function L(v,y){if(!y)return{passed:!0,required:!1,message:""};return{passed:!K(v),required:y,message:"Password must not contain common words or patterns"}}function n(v,y){if(!y)return{passed:!0,required:!1,message:""};try{return{passed:(typeof y==="string"?new RegExp(y):y).test(v),required:!0,message:"Password must match the custom pattern"}}catch(f){return{passed:!1,required:!0,message:"Invalid custom pattern provided"}}}function C(v,y=3){for(let f=0;f<=v.length-y;f++){let b=v.slice(f,f+y);if(/^\d+$/.test(b)){let x=!0;for(let z=1;z<b.length;z++){let F=b[z-1],J=b[z];if(parseInt(J,10)!==parseInt(F,10)+1){x=!1;break}}if(x)return!0}if(/^[a-zA-Z]+$/.test(b)){let x=!0;for(let z=1;z<b.length;z++)if(b.charCodeAt(z)!==b.charCodeAt(z-1)+1){x=!1;break}if(x)return!0}}return!1}function D(v,y=3){for(let f=0;f<=v.length-y;f++){let b=v.slice(f,f+y);if(b.split("").every((x)=>x===b[0]))return!0}return!1}function K(v){let y=v.toLowerCase();return Q.some((f)=>y.includes(f.toLowerCase()))}function G(v,y,f){let b=0,x=v.length;if(x>=12)b+=30;else if(x>=10)b+=25;else if(x>=8)b+=20;else if(x>=6)b+=15;else if(x>=4)b+=10;else b+=5;if(y.hasLowerChar.passed)b+=5;if(y.hasUpperChar.passed)b+=5;if(y.hasNumber.passed)b+=10;if(y.hasSpecial.passed)b+=15;if(y.hasSymbol.passed)b+=5;if(y.noSequential.passed)b+=10;if(y.noRepeating.passed)b+=10;if(y.noCommonWords.passed)b+=10;if(y.customPattern.passed&&y.customPattern.required)b+=5;if(K(v))b-=20;if(x<=3)b-=30;else if(x<6)b-=10;return Math.max(0,Math.min(100,b))}function P(v,y,f){if(v<20)return"very-weak";if(v<40)return"weak";if(v<60)return"fair";if(v<80)return"good";if(v>=80){if(y>=12&&f>=6)return"very-strong";return"strong"}return"fair"}function u(v,y={}){let f=Y(v,y),b=[];if(f.error)b.push(...f.error.requirements);if(f.strength==="very-weak"||f.strength==="weak")b.push("Consider using a longer password (12+ characters)","Mix uppercase and lowercase letters","Include numbers and special characters","Avoid common words and patterns");else if(f.strength==="fair")b.push("Add more character variety for better security","Consider increasing the length further");return b.slice(0,5)}function X(v=12,y={}){let f={lowercase:"abcdefghijklmnopqrstuvwxyz",uppercase:"ABCDEFGHIJKLMNOPQRSTUVWXYZ",numbers:"0123456789",special:"!@#$%^&*()_+-=[]{}|;:,.<>?",symbols:'~`!"#$%^&*()_+-=[]{}\\|;:\'",.<>?/'},b="",x="";if(y.hasLowerChar!==!1)b+=f.lowercase,x+=f.lowercase[Math.floor(Math.random()*f.lowercase.length)];if(y.hasUpperChar!==!1)b+=f.uppercase,x+=f.uppercase[Math.floor(Math.random()*f.uppercase.length)];if(y.hasNumber!==!1)b+=f.numbers,x+=f.numbers[Math.floor(Math.random()*f.numbers.length)];if(y.hasSpecial!==!1)b+=f.special,x+=f.special[Math.floor(Math.random()*f.special.length)];if(y.hasSymbol)b+=f.symbols,x+=f.symbols[Math.floor(Math.random()*f.symbols.length)];while(x.length<v)x+=b[Math.floor(Math.random()*b.length)];return x.split("").sort(()=>Math.random()-0.5).join("")}export{Y as validatePassword,C as hasSequentialChars,D as hasRepeatingChars,K as hasCommonWords,u as getPasswordSuggestions,X as generateStrongPassword};