private775

JS: IsValidImei

Jan 15th, 2021 (edited)
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const imeiRx = /^\d{15}$/;
  2. const IsValidImei = (imei) => {
  3.     if (!imeiRx.test(imei)) {
  4.         return false;
  5.     }
  6.     const mod10 = imei.split('')
  7.         .map((x) => parseInt(x.toString()))
  8.         .map((x, i) => i % 2 == 0 ? x : 2 * x)
  9.         .map(x => x >= 10 ? x - 9 : x)
  10.         .reduce((s, x) => s + x, 0)
  11.         % 10;
  12.     return (mod10 === 0);
  13. }
  14.  
Add Comment
Please, Sign In to add comment