@lacussoft/cpf-val
Version:
Utility to validate CPF (Brazilian Individual's Taxpayer ID)
21 lines (20 loc) • 7.9 kB
JavaScript
/**
* Lacus Solutions :: cpf-val v3.0.0
*
* @author Julio L. Muller.
* @license MIT - 2021-2026
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).cpfVal=e()}(this,function(){"use strict";var t=function extendStatics(e,r){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},t(e,r)};function __extends$1(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function __(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(__.prototype=r.prototype,new __)}
/**
* Lacus Solutions :: utils v1.0.0
*
* @author Julio L. Muller.
* @license MIT - 2026
*/
function describeType(t){if(!Array.isArray(t))return"number"==typeof t?isNaN(t)?"NaN":isFinite(t)?Number.isInteger(t)?"integer number":"float number":"Infinity":null===t?"null":typeof t;if(0===t.length)return"Array (empty)";var e=new Set(t.map(function(t){return typeof t})),r=Array.from(e);return 1===r.length?"".concat(r[0],"[]"):"(".concat(r.join(" | "),")[]")}
/**
* Lacus Solutions :: cpf-dv v1.0.0
*
* @author Julio L. Muller.
* @license MIT - 2026
*/"function"==typeof SuppressedError&&SuppressedError;var extendStatics=function(t,e){return extendStatics=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])},extendStatics(t,e)};function __extends(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function __(){this.constructor=t}extendStatics(t,e),t.prototype=null===e?Object.create(e):(__.prototype=e.prototype,new __)}function __spreadArray(t,e,r){if(r||2===arguments.length)for(var n,i=0,o=e.length;i<o;i++)!n&&i in e||(n||(n=Array.prototype.slice.call(e,0,i)),n[i]=e[i]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var e=function(t){function CpfCheckDigitsInputTypeError(e,r){var n=describeType(e);return t.call(this,e,n,r,"CPF input must be of type ".concat(r,". Got ").concat(n,"."))||this}return __extends(CpfCheckDigitsInputTypeError,t),CpfCheckDigitsInputTypeError}(function(t){function CpfCheckDigitsTypeError(e,r,n,i){var o=this.constructor,c=t.call(this,i)||this;return Object.setPrototypeOf(c,o.prototype),c.name=c.constructor.name,c.actualInput=e,c.actualType=r,c.expectedType=n,c}return __extends(CpfCheckDigitsTypeError,t),CpfCheckDigitsTypeError}(TypeError)),r=function(t){function CpfCheckDigitsException(e){var r=this.constructor,n=t.call(this,e)||this;return Object.setPrototypeOf(n,r.prototype),n.name=n.constructor.name,n}return __extends(CpfCheckDigitsException,t),CpfCheckDigitsException}(Error),n=function(t){function CpfCheckDigitsInputLengthException(e,r,n,i){var o=this,c="string"==typeof e?'"'.concat(e,'"'):JSON.stringify(e),p=e===r?"".concat(r.length):"".concat(r.length,' in "').concat(r,'"');return(o=t.call(this,"CPF input ".concat(c," does not contain ").concat(n," to ").concat(i," digits. Got ").concat(p,"."))||this).actualInput=e,o.evaluatedInput=r,o.minExpectedLength=n,o.maxExpectedLength=i,o}return __extends(CpfCheckDigitsInputLengthException,t),CpfCheckDigitsInputLengthException}(r),i=function(t){function CpfCheckDigitsInputInvalidException(e,r){var n=this,i="string"==typeof e?'"'.concat(e,'"'):JSON.stringify(e);return(n=t.call(this,"CPF input ".concat(i," is invalid. ").concat(r))||this).actualInput=e,n.reason=r,n}return __extends(CpfCheckDigitsInputInvalidException,t),CpfCheckDigitsInputInvalidException}(r),o=function(){function CpfCheckDigits(t){var r;if(this._cachedFirstDigit=void 0,this._cachedSecondDigit=void 0,"string"==typeof t)r=this._handleStringInput(t);else{if(!Array.isArray(t))throw new e(t,"string or string[]");r=this._handleArrayInput(t)}this._validateLength(r,t),this._validateNonRepeatedDigits(r,t),this._cpfDigits=r.slice(0,9)}return Object.defineProperty(CpfCheckDigits.prototype,"first",{get:function(){if(void 0===this._cachedFirstDigit){var t=__spreadArray([],this._cpfDigits,!0);this._cachedFirstDigit=this._calculate(t)}return this._cachedFirstDigit.toString()},enumerable:!1,configurable:!0}),Object.defineProperty(CpfCheckDigits.prototype,"second",{get:function(){if(void 0===this._cachedSecondDigit){var t=__spreadArray(__spreadArray([],this._cpfDigits,!0),[Number(this.first)],!1);this._cachedSecondDigit=this._calculate(t)}return this._cachedSecondDigit.toString()},enumerable:!1,configurable:!0}),Object.defineProperty(CpfCheckDigits.prototype,"both",{get:function(){return this.first+this.second},enumerable:!1,configurable:!0}),Object.defineProperty(CpfCheckDigits.prototype,"cpf",{get:function(){return __spreadArray(__spreadArray([],this._cpfDigits,!0),[this.both],!1).join("")},enumerable:!1,configurable:!0}),CpfCheckDigits.prototype._handleStringInput=function(t){return t.replace(/\D/g,"").split("").map(Number)},CpfCheckDigits.prototype._handleArrayInput=function(t){if(0===t.length)return[];if(!t.every(function(t){return"string"==typeof t}))throw new e(t,"string or string[]");return this._handleStringInput(t.join(""))},CpfCheckDigits.prototype._validateLength=function(t,e){var r=t.length;if(r<9||r>11)throw new n(e,t.join(""),9,11)},CpfCheckDigits.prototype._validateNonRepeatedDigits=function(t,e){var r=t.slice(0,9);if(1===new Set(r).size)throw new i(e,"Repeated digits are not considered valid.")},CpfCheckDigits.prototype._calculate=function(t){for(var e=t.length+1,r=0,n=0,i=t;n<i.length;n++){r+=i[n]*e,e-=1}var o=11-r%11;return o>9?0:o},CpfCheckDigits}();Object.freeze(o);var c=function(t){function CpfValidatorTypeError(e,r,n,i){var o=this.constructor,c=t.call(this,i)||this;return Object.setPrototypeOf(c,o.prototype),c.name=c.constructor.name,c.actualInput=e,c.actualType=r,c.expectedType=n,c}return __extends$1(CpfValidatorTypeError,t),CpfValidatorTypeError}(TypeError),p=function(t){function CpfValidatorInputTypeError(e,r){var n=describeType(e);return t.call(this,e,n,r,"CPF input must be of type ".concat(r,". Got ").concat(n,"."))||this}return __extends$1(CpfValidatorInputTypeError,t),CpfValidatorInputTypeError}(c),a=function(t){function CpfValidatorException(e){var r=this.constructor,n=t.call(this,e)||this;return Object.setPrototypeOf(n,r.prototype),n.name=n.constructor.name,n}return __extends$1(CpfValidatorException,t),CpfValidatorException}(Error),s=function(){function CpfValidator(){}return CpfValidator.prototype.isValid=function(t){var e=this._toStringInput(t).replace(/\D/g,"");if(11!==e.length)return!1;try{return e===new o(e).cpf}catch(t){return!1}},CpfValidator.prototype._toStringInput=function(t){if("string"==typeof t)return t;if(Array.isArray(t)){for(var e=0,r=t;e<r.length;e++){if("string"!=typeof r[e])throw new p(t,"string or string[]")}return t.join("")}throw new p(t,"string or string[]")},CpfValidator}();function cpfVal$1(t){return(new s).isValid(t)}Object.freeze(s);var f=cpfVal$1,u=function __rest(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(n=Object.getOwnPropertySymbols(t);i<n.length;i++)e.indexOf(n[i])<0&&Object.prototype.propertyIsEnumerable.call(t,n[i])&&(r[n[i]]=t[n[i]])}return r}(Object.freeze({__proto__:null,CPF_LENGTH:11,CpfValidator:s,CpfValidatorException:a,CpfValidatorInputTypeError:p,CpfValidatorTypeError:c,cpfVal:cpfVal$1,default:cpfVal$1}),["default","cpfVal"]),l=Object.assign(function cpfVal(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return f.apply(void 0,t)},u);return l});