如何编写一个将 map 作为输入的 util 函数迭代所有键并调用此函数和 returns 一个经过清理的键值映射?

How to write a util function that takes map as an input iterate over all the keys and calls this function and returns a sanitized key value map?

这是函数 addorUpdateAddress 而不是使用这个 sanitizeXSS 和 decrypt_key 一次又一次地编写一个 util 函数,它将 map 作为输入迭代所有键并调用此函数和 returns a清理键值映射。

addOrUpdateAddress : function (request, resolve) 
    {
        const customerKey = sanitizeXSS(decrypt_key(request.customer_key));
        const lat = sanitizeXSS(decrypt_key(request.lat));
        const lng = sanitizeXSS(decrypt_key(request.lng));
        const line1 = sanitizeXSS(decrypt_key(request.line1));
        const line2 = sanitizeXSS(decrypt_key(request.line2));
        const city = sanitizeXSS(decrypt_key(request.city));
        const pincode = sanitizeXSS(decrypt_key(request.pincode));
        const state = sanitizeXSS(decrypt_key(request.state));
       
        const contact = sanitizeXSS(decrypt_key(request.contact));
        const landmark = request.landmark?sanitizeXSS(decrypt_key(request.landmark)):null;     
     }
 sanitizeXSS: string => {
      const map = {
          '&': '&',
          '<': '&lt;',
          '>': '&gt;',
          '"': '&quot;',
          "'": '&#x27;',
          "/": '&#x2F;',
      };
      const reg = /[&<>"'/]/ig;
      return string.replace(reg, (match)=>(map[match]));
    }
function decrypt_key(message, action)
{
    var text = message;
    var encrypted = "";

    for(var i = 0; i < text.length; i++) {
    var ASCII = text[i].charCodeAt(0);
    var n = null;

    if(i % 2 == 0) {
      n = action == 'encrypt' ? ASCII + 4 : ASCII - 4;
    }

    else if(i % 2 == 1) {
      n = action == 'encrypt' ? ASCII + 3 : ASCII - 3;
    }

    var s = String.fromCharCode(n);

        encrypted += s;
    }
    return encrypted;
}

是的,你可以写这样的函数。

function sanitizeXSS(string){
      const map = {
          '&': '&amp;',
          '<': '&lt;',
          '>': '&gt;',
          '"': '&quot;',
          "'": '&#x27;',
          "/": '&#x2F;',
      };
      const reg = /[&<>"'/]/ig;
      return string.replace(reg, (match)=>(map[match]));
}

function decrypt_key(message, action)
{
    var text = message;
    var encrypted = "";

    for(var i = 0; i < text.length; i++) {
    var ASCII = text[i].charCodeAt(0);
    var n = null;

    if(i % 2 == 0) {
      n = action == 'encrypt' ? ASCII + 4 : ASCII - 4;
    }

    else if(i % 2 == 1) {
      n = action == 'encrypt' ? ASCII + 3 : ASCII - 3;
    }

    var s = String.fromCharCode(n);

        encrypted += s;
    }
    return encrypted;
}

function decryptAndSanitizeXss(obj){
  let newObj = {}
  for(const prop in obj)    {
    if(obj[prop]){
        newObj[prop] = sanitizeXSS(decrypt_key(obj[prop]));    
    }
    }
    return newObj
}

function addOrUpdateAddress(request, resolve) {
   request = decryptAndSanitizeXss(request)
}