Skip to content

Commit

Permalink
feat(validator): add some new functions to validate
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammadhonarvar authored and AliMD committed Apr 25, 2023
1 parent b19b0f8 commit aea3ec1
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions core/validator/src/validator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isNumber} from '@alwatr/math';
import {UnicodeDigits, isNumber, type UnicodeLangKeys} from '@alwatr/math';

import type {JsonSchema} from './type.js';
import type {Stringifyable, StringifyableRecord} from '@alwatr/type';
Expand Down Expand Up @@ -140,9 +140,37 @@ export function validator<T extends StringifyableRecord>(
return targetObject as T;
}

/**
* Removes extra spaces and translates digits of the its input
*/
export const sanitizeString = (str: string, replaceNumberToLang: UnicodeLangKeys = 'fa'): string => {
if (!str) return '';
const unicodeDigits = new UnicodeDigits(replaceNumberToLang, 'all');
str = unicodeDigits.translate(str);
return str.replace(/\s/g, '');
};

const phoneJungRegExp = /^98|^0098|^00989|^0|^\+98/;
/**
* Coverts an input to a valid and sanitized phone number
*/
export const sanitizePhoneNumber = (input?: string | number | null): number | null => {
if (input == null) return null;

if (typeof input === 'number') input = input + '';
input = input.replace(/ /g, '');
return +('98' + +input);
input = sanitizeString(input);
input = '98' + input.replace(phoneJungRegExp, '');

return +input;
};

const validPhoneRegExp = /\+989(9[0-9]|0[1-2]|1[0-9]|3[0-9]|2[0-1])-?[0-9]{3}-?[0-9]{4}$/;
/**
* Checks an input is a valid phone number or not
*/
export const validatePhone = (phone: string, skipSanitize = false): boolean => {
if (!skipSanitize) {
phone = sanitizePhoneNumber(phone) + '';
}
return validPhoneRegExp.test(phone);
};

0 comments on commit aea3ec1

Please sign in to comment.