Truncate values

The goal posts are moving quite a bit, can you confirm that these are all the desired results?

/* match 840-869 */
847.467 4        → 847.4 Pass
850.767 456      → 850.7 Pass
869.999 3        → 869.9 Pass
861.39 CU        → CU 861.3 Pass
863.456 CL       → CL 863.4 Pass
861.444 05 AR    → AR 861.4 Pass
869.347 5 BR     → BR 869.3 Pass

/* match 801-807 */
802.556 667567   → 802 Pass
805.875 4        → 805 Pass

/* match 839.31 or 839.36 */
839.315 6        → 839.31 Pass
839.366 87       → 839.36 Pass
111.aaa 1        → 11.aaa Pass

/* match 823 or 843 numbers */
823.556 3 IE     → 823.9 Pass
823.358 44 AU    → 823.9 Pass
823.45 ZA        → 823.9 Pass
823.234 6 NZ     → 823.9 Pass
823.34 KE        → 823.9 Pass
823.35 ZW        → 823.9 Pass
823.347 3 LK     → 823.9 Pass
843.914 45653    → 843.9 Pass
843.356 3 BE     → 843.9 Pass
843.567 6 CA     → 843.9 Pass
843.246 8554 CH  → 843.9 Pass
843.345 6 CI     → 843.9 Pass

If so, you should be able to copy and paste this into your Autofill.

const trunc = (num, places = 0) => {
  if (places <= 0) {
    return String(Math.trunc(num.split(' ')[0]))
  }

  const regex = new RegExp(`\\d+\\.\\d{1,${places}}`)
  const match = num.match(regex)

  return (match !== null) ? match[0] : null
}

const truncGroups = [
  // match 823 or 843 numbers
  // truncate to number with a decimal of .9
  {
    groupRx: /\b823\b|\b843\b/,
    truncate: (code) => code.substring(0, 3) + ".9"
  },
  
  // match 840-869
  // truncate to 1 decimal place, with optional letters
  // Note: this will match 843 numbers too. Hence the order!
  {
    groupRx: /\b8[4-6][0-9]\b/, 
    truncate: (code) => {
      const [num, letters] = code.split(/ ([A-Z]{2})/)
      const truncNumber = trunc(num, 1)

      return (letters)
        ? `${letters} ${truncNumber}`
        : `${truncNumber}`
    }
  },
  
  // match 801-807
  // truncate to an integer
  {
    groupRx: /\b80[1-7]\b/,
    truncate: (code) => trunc(code),
  },
  
  // match 839.31 or 839.36
  // truncate to 2 decimal places
  {
    groupRx: /839\.3[16]/,
    truncate: (code) => trunc(code, 2)
  }
]

const formatCode = (code) => {
  for (const { groupRx, truncate } of truncGroups) {
    if (groupRx.test(code)) {
      return truncate(code)
    }
  }
  return code
}

// Update value of <input name='collocazione'>
const collocazione = document.querySelector('#collocazione');

if (collocazione) {
  collocazione.value = formatCode(collocazione.value);
}

Following Paul’s tests: (Note: not to be copied and pasted into Autofill)

const tests = [
  // group matching 8[4-6][0-9]
  { code: '847.467 4', expected: '847.4' },
  { code: '850.767 456', expected: '850.7' },
  { code: '869.999 3', expected: '869.9' },
  { code: '861.39 CU', expected: 'CU 861.3' },
  { code: '863.456 CL', expected: 'CL 863.4' },
  { code: '861.444 05 AR', expected: 'AR 861.4' },
  { code: '869.347 5 BR', expected: 'BR 869.3' },

  // group matching 80[1-7]
  { code: '802.556 667567', expected: '802' },
  { code: '805.875 4', expected: '805' },

  // group matching 839.3[1,6]
  { code: '839.315 6', expected: '839.31' },
  { code: '839.366 87', expected: '839.36' },
  { code: '111.aaa', expected: '111.aaa' },
  
  // group matching 823|843
  { code: "823.556 3 IE", expected: "823.9" },
  { code: "823.358 44 AU", expected: "823.9" },
  { code: "823.45 ZA", expected: "823.9" },
  { code: "823.234 6 NZ", expected: "823.9" },
  { code: "823.34 KE", expected: "823.9" },
  { code: "823.35 ZW", expected: "823.9" },
  { code: "823.347 3 LK", expected: "823.9" },
  { code: "843.914 45653", expected: "843.9" },
  { code: "843.356 3 BE", expected: "843.9" },
  { code: "843.567 6 CA", expected: "843.9" },
  { code: "843.246 8554 CH", expected: "843.9" },
  { code: "843.345 6 CI", expected: "843.9" }
]

tests.forEach(({ code, expected }) => {
  const converted = formatCode(code)
  console.log(
    code, 
    converted, 
    converted === expected ? 'Pass' : 'Fail'
  )
})