javascript 智利DNI验证器并获取JSON {run:Number / String,adv:Number / String,dv:Number / String,valid:Boolean

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 智利DNI验证器并获取JSON {run:Number / String,adv:Number / String,dv:Number / String,valid:Boolean相关的知识,希望对你有一定的参考价值。

function DNI_CL({ run, string }){
  
  if(run !== '' && (typeof run === "string" || typeof run === "number")){
    
    function format(n){
      if (typeof n === 'string') {
        // Remove '.' if exist
        n = (n.includes('.')) ? n.split('.').join('') : n;
        // Remove '-' if exist
        n = (n.includes('-')) ? n.split('-').join('') : n;
      }
      return n;
    }

    function DV(n){
      // Initialize multiples and total
      let m = 2, sum = 0;
      // Reverse string
      n = n.split('').reverse().join('');
      // Algorithm to get the DV
      n.split('').map((i) => {
        // Multiply digit by multiples [2,7]
        sum += (Number(i) * m);
        // Add one to mutiple variable
        m = (m < 7) ? m+1 : 2;
      });
      // Get remainder of the division and after get subtraction
      const dv = (11 - (sum % 11));
      return (dv === 10) ? 'K' : (dv === 11) ? 0 : dv ;
    }

    function formatter({run, dv}){
      // Get the first numbers
      const a1 = run.slice(0, run.length - 6);
      // Get the last 6 numbrs
      const a2 = run.slice(run.length - 6, run.length).match(/.{1,3}/g).join('.');
      // Return in string or number format
      return (string) ? ((dv) ? `${a1}.${a2}-${DV(run)}` : `${a1}.${a2}`) : ((dv && typeof DV(run) === "number") ? Number(`${run}${DV(run)}`) : Number(run));
    }

    run = format(run);
    // Get DV
    let dv = run.toString().split('')[run.toString().split('').length - 1];
    // If is number convert or set to uppercase
    dv = (Number(dv)) ? Number(dv) : dv.toUpperCase();
    // Format and remove the last number
    run = run.toString().slice(0, -1);
    // Return JSON format
    return {
      run: formatter({run, dv: true}),
      adv: formatter({run}),
      dv: (string) ? DV(run).toString() : DV(run),
      valid: (DV(run).toString() === dv.toString())
    }
  } else {
    return { run: 0, adv: 0, dv: 0, valid: false };
  }
}
function DNI_CL({run,string}){if(run!==''&&(typeof run==="string"||typeof run==="number")){function format(n){if(typeof n==='string'){n=(n.includes('.'))?n.split('.').join(''):n;n=(n.includes('-'))?n.split('-').join(''):n}
return n}
function DV(n){let m=2,sum=0;n=n.split('').reverse().join('');n.split('').map((i)=>{sum+=(Number(i)*m);m=(m<7)?m+1:2});const dv=(11-(sum%11));return(dv===10)?'K':(dv===11)?0:dv}
function formatter({run,dv}){const a1=run.slice(0,run.length-6);const a2=run.slice(run.length-6,run.length).match(/.{1,3}/g).join('.');return(string)?((dv)?`${a1}.${a2}-${DV(run)}`:`${a1}.${a2}`):((dv&&typeof DV(run)==="number")?Number(`${run}${DV(run)}`):Number(run))}
run=format(run);let dv=run.toString().split('')[run.toString().split('').length-1];dv=(Number(dv))?Number(dv):dv.toUpperCase();run=run.toString().slice(0,-1);return{run:formatter({run,dv:!0}),adv:formatter({run}),dv:(string)?DV(run).toString():DV(run),valid:(DV(run).toString()===dv.toString())}}else{return{run:0,adv:0,dv:0,valid:!1}}}

以上是关于javascript 智利DNI验证器并获取JSON {run:Number / String,adv:Number / String,dv:Number / String,valid:Boolean的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 有效的Nif,Cif,Dni

javascript Validador DNI,CIF y NIE

以 JSON 格式获取 Twitter 订阅源,无需身份验证

python Validar DNI,NIF,NIE

虹科Panorama为智利林业提供用于轨道交通的坚固型天线解决方案

智利惊魂记