Skip to content

Commit

Permalink
Merge pull request #33 from bilalfastnu/regex-validations
Browse files Browse the repository at this point in the history
Add regex validations
  • Loading branch information
vigan-abd authored Nov 6, 2024
2 parents e3dce27 + 9d89e29 commit a17a181
Show file tree
Hide file tree
Showing 7 changed files with 336 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#1.16.0
- feat: regex validations

#1.15.0
- feat: snakeCase
- feat: promiseResolveCb
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ Currently supported utils:
- `sum` - calculate sum of array items
- `sumBy` - calculate sum of array items using iteratee function or string shortcut
- `uniqBy` - get unique values of array by the iteratee function or property path
- `validateInput` - validates the input based on the regex format options: `NUMBER | EMAIL | PATH | NAME | NAME_WITH_DIGITS | INPUT | ADDRESS | PHONE_CODE | PHONE | IMAGE | FILE | FILENAME | PASSWORD`
- `without` - creates an array of values from the first argument excluding all given arguments
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ export function snakeCase (str: string): string
export function sum (values: Array<string>): number
export function sumBy (values: Array, iteratee: Function | string): number
export function uniqBy (array: Array, iteratee: Function | string): Array
export function validateInput ( input: string, format: string ): Boolean
export function without (array: Array, ...values: Array): Array
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const snakeCase = require('./src/snakeCase')
const sum = require('./src/sum')
const sumBy = require('./src/sumBy')
const uniqBy = require('./src/uniqBy')
const validateInput = require('./src/validateInput')
const without = require('./src/without')

module.exports = {
Expand Down Expand Up @@ -75,5 +76,6 @@ module.exports = {
sum,
sumBy,
uniqBy,
validateInput,
without
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bitfinexcom/lib-js-util-base",
"version": "1.15.0",
"version": "1.16.0",
"description": "general utils",
"main": "index.js",
"scripts": {
Expand Down
51 changes: 51 additions & 0 deletions src/validateInput.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable no-misleading-character-class */
'use strict'
// https://v8.dev/features/regexp-v-flag
// https://mathiasbynens.be/notes/es-unicode-property-escapes
// https://www.regular-expressions.info/unicode.html

const CHARACTERS = '\\p{L}\\p{M}'
const INPUT_BASE_REG_EXP = new RegExp(`-0-9.,;:<>?/\n${CHARACTERS}'_ &()/+@%`, 'u')
const DIGITS = '0-9'

const NUMBER = /^-?\d*[\.]?\d+$/
const EMAIL = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i
const PATH = /([^/]*)\/*$/
const NAME = new RegExp(`^[-${CHARACTERS}' &()/]+$`, 'u')
const NAME_WITH_DIGITS = new RegExp(`^[-${CHARACTERS}${DIGITS}' &()/]+$`, 'u')
const INPUT = new RegExp(`^[${INPUT_BASE_REG_EXP.source}]+$`, 'u')
const ADDRESS = new RegExp(`^[${INPUT_BASE_REG_EXP.source}#]+$`, 'u')
const PHONE_CODE = /^(\+?\d{1,3}|\d{1,4})$/
const PHONE = /^\+?(\d{1,3}|\d{1,4})?[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/
const IMAGE = /^data:image\/[A-Za-z+]+;base64,[A-Za-z0-9+/=]+$/
const FILE = /^data:(image|application|video)\/[A-Za-z0-9+]+;base64,[A-Za-z0-9+/=]+$/
const FILENAME = new RegExp(`^[-${DIGITS}.${CHARACTERS} ()_]+$`, 'u')
const PASSWORD = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]).{8,}$/

/**
* @param {string} input
* @param {'NAME'|'EMAIL'|..} format
* @returns {boolean}
* @throws {Error}
*/
const validateInput = (input, format) => {
switch (format) {
case 'NAME': return NAME.test(input)
case 'NAME_WITH_DIGITS': return NAME_WITH_DIGITS.test(input)
case 'NUMBER': return NUMBER.test(input)
case 'EMAIL': return EMAIL.test(input)
case 'PATH': return PATH.test(input)
case 'INPUT': return INPUT.test(input)
case 'ADDRESS': return ADDRESS.test(input)
case 'PHONE': return PHONE.test(input)
case 'PHONE_CODE': return PHONE_CODE.test(input)
case 'IMAGE': return IMAGE.test(input)
case 'FILE': return FILE.test(input)
case 'FILENAME': return FILENAME.test(input)
case 'PASSWORD': return PASSWORD.test(input)

default: throw new Error('ERR_FORMAT_NOT_SUPPORTED')
}
}

module.exports = validateInput
Loading

0 comments on commit a17a181

Please sign in to comment.