swiss-ssn
Version:
Small utility for validating and generating Swiss national identification number.
1 lines • 1.6 kB
JavaScript
(function(global,factory){if(typeof define==="function"&&define.amd){define(["exports"],factory)}else if(typeof exports!=="undefined"){factory(exports)}else{var mod={exports:{}};factory(mod.exports);global.swissSsn=mod.exports}})(typeof globalThis!=="undefined"?globalThis:typeof self!=="undefined"?self:this,function(_exports){"use strict";Object.defineProperty(_exports,"__esModule",{value:true});_exports.default=void 0;class SwissSSN{static#COUNTRY_CODE=[7,5,6];static#MAX_VALUE=10;static#SSN_LENGTH=13;static validateSSN(ssn){if(!ssn){return false}const parsedSSN=this.#parse(ssn);if(parsedSSN.length!==this.#SSN_LENGTH){return false}const checkDigit=this.#getCheckDigit(parsedSSN);return parseInt(parsedSSN[12],10)===checkDigit}static generateSSN(){const randomNumbers=Array.from({length:9},()=>Math.floor(Math.random()*this.#MAX_VALUE));const ssnWithoutCheckDigit=[...this.#COUNTRY_CODE,...randomNumbers];const checkDigit=this.#getCheckDigit(ssnWithoutCheckDigit);const unformattedSSN=[...ssnWithoutCheckDigit,checkDigit];return this.#ssnFormatter(unformattedSSN)}static#getCheckDigit(ssn){const total=[...ssn].slice(0,12).reduce((sum,digit,index)=>{const multiplier=index%2===0?1:3;return sum+parseInt(digit,10)*multiplier},0);return total%10===0?0:Math.ceil(total/10)*10-total}static#ssnFormatter(ssn){const ssnString=ssn.map(String);return`756.${ssnString.slice(3,7).join("")}.${ssnString.slice(7,11).join("")}.${ssnString.slice(11,13).join("")}`}static#parse(ssn){return ssn.replace(/\D/g,"")}}if(typeof window!=="undefined"){window.SwissSSN=SwissSSN}var _default=_exports.default=SwissSSN});