%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/emergentqj/actioncivile/pre-auth/.4cc3a2e1590a4463a5f392ddcc26929e/static/js/utils/
Upload File :
Create Path :
Current File : /home/emergentqj/actioncivile/pre-auth/.4cc3a2e1590a4463a5f392ddcc26929e/static/js/utils/normalize.js

import appConfig from "config/countryData";

const leavePhoneNumberCharactersOnly = (string) => string.replace(/[^0-9-.+]|\s/g, "");

const leaveUsernameCharactersOnly = (string) => string.replace(/[^a-zA-Z0-9-.+@]|\s/g, "");

const containsOnlyNumbers = (string) => /^\d+$/.test(string);

const addPrefix = (string, prefix) => (
  // Don't prefix an empty string,
  // it can become everything
  (string !== ""
  // If it starts with an +, it is already prefixed,
  // if there are letters, then it's not a phone number
  && containsOnlyNumbers(string))
    ? prefix + string
    : string
);

// Ok, not all, only the countries Easypark works
const getAllCountryPrefixes = () => {
  const { countryData } = appConfig;
  return Object.keys(countryData)
    .map((countryCode) => countryData[countryCode].code)
    .filter((prefix) => prefix != null);
};

const removeLeadingZeros = (string) => string.replace(/^0+/, "");

// Prefix +323 with phone number 02323233
// is never / nowhere +32302323233, but +3232323233
const removeLeadingZerosIfPrefixed = (phoneNumber) => {
  const allCountryPrefixes = getAllCountryPrefixes();

  const prefixWentWrong = allCountryPrefixes.find((prefix) => (
    prefix !== ""
    && phoneNumber.includes(`${prefix}0`)
  ));

  if (prefixWentWrong) {
    const withoutPrefix = phoneNumber.replace(prefixWentWrong, "");
    return prefixWentWrong + removeLeadingZeros(withoutPrefix);
  }

  return phoneNumber;
};

export const normalizePhoneNumber = (value, prefix) => {
  let phoneNumber = leavePhoneNumberCharactersOnly(value);
  phoneNumber = addPrefix(phoneNumber, prefix);
  phoneNumber = removeLeadingZerosIfPrefixed(phoneNumber);
  return phoneNumber;
};

export const normalizeUserName = (value) => {
  let phoneNumber = leaveUsernameCharactersOnly(value);
  phoneNumber = removeLeadingZerosIfPrefixed(phoneNumber);
  return phoneNumber;
};

Zerion Mini Shell 1.0