Source

utils/string.js

/**
 * @namespace Utils
 * @category Utils
 * @module String*/

/**
 * Remove french accents
 * @param {*} strAccents
 * @returns {String} Normalized string
 */
export function removeAccents(strAccents) {
  strAccents = strAccents.split('')
  var strAccentsOut = new Array()
  var strAccentsLen = strAccents.length
  var accents = 'ÀÁÂÃÄÅàáâãäåÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž'
  var accentsOut =
    'AAAAAAaaaaaaOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz'
  for (var y = 0; y < strAccentsLen; y++) {
    if (accents.indexOf(strAccents[y]) != -1) {
      strAccentsOut[y] = accentsOut.substr(accents.indexOf(strAccents[y]), 1)
    } else strAccentsOut[y] = strAccents[y]
  }
  strAccentsOut = strAccentsOut.join('')

  return strAccentsOut
}

export function normalizeString(input, options = {}) {
  const {
    replaceSpaces = false,
    removeParentheses = false,
    removeAccents = false,
    prefix = '',
  } = options

  const convertToLowercase =
    options.convertToLowercase === undefined ? true : options.convertToLowercase

  let normalized = input

  if (replaceSpaces) {
    normalized = normalized.replace(/ /g, '_')
  }

  if (convertToLowercase) {
    normalized = normalized.toLowerCase()
  }

  if (removeParentheses) {
    normalized = normalized.replace(/[()]/g, '')
  }

  if (removeAccents) {
    const accents = 'ÀÁÂÄÈÉÊËÌÍÎÏÒÓÔÖÙÚÛÜàáâäèéêëìíîïòóôöùúûü'
    const accentsOut = 'AAAAEEEEIIIIOOOOUUUUaaaaeeeeiiiioooouuuu'
    normalized = normalized.replace(
      new RegExp('[' + accents + ']', 'g'),
      function (c) {
        return accentsOut.charAt(accents.indexOf(c))
      }
    )
  }

  if (prefix) {
    normalized = prefix + normalized
  }

  return normalized
}