@lacussoft/cpf-gen
Version:
Utility to generate CPF (Brazilian Individual's Taxpayer ID)
22 lines (21 loc) • 10.5 kB
JavaScript
/**
* Lacus Solutions :: cpf-gen 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).cpfGen=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 __)}var e=function __assign(){return e=Object.assign||function __assign(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t},e.apply(this,arguments)};
/**
* 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(" | "),")[]")}"function"==typeof SuppressedError&&SuppressedError;var r="0123456789";
/**
* Lacus Solutions :: cpf-dv v1.0.0
*
* @author Julio L. Muller.
* @license MIT - 2026
*/
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,o=0,i=e.length;o<i;o++)!n&&o in e||(n||(n=Array.prototype.slice.call(e,0,o)),n[o]=e[o]);return t.concat(n||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var n=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,o){var i=this.constructor,p=t.call(this,o)||this;return Object.setPrototypeOf(p,i.prototype),p.name=p.constructor.name,p.actualInput=e,p.actualType=r,p.expectedType=n,p}return __extends(CpfCheckDigitsTypeError,t),CpfCheckDigitsTypeError}(TypeError)),o=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),i=function(t){function CpfCheckDigitsInputLengthException(e,r,n,o){var i=this,p="string"==typeof e?'"'.concat(e,'"'):JSON.stringify(e),c=e===r?"".concat(r.length):"".concat(r.length,' in "').concat(r,'"');return(i=t.call(this,"CPF input ".concat(p," does not contain ").concat(n," to ").concat(o," digits. Got ").concat(c,"."))||this).actualInput=e,i.evaluatedInput=r,i.minExpectedLength=n,i.maxExpectedLength=o,i}return __extends(CpfCheckDigitsInputLengthException,t),CpfCheckDigitsInputLengthException}(o),p=function(t){function CpfCheckDigitsInputInvalidException(e,r){var n=this,o="string"==typeof e?'"'.concat(e,'"'):JSON.stringify(e);return(n=t.call(this,"CPF input ".concat(o," is invalid. ").concat(r))||this).actualInput=e,n.reason=r,n}return __extends(CpfCheckDigitsInputInvalidException,t),CpfCheckDigitsInputInvalidException}(o),c=function(){function CpfCheckDigits(t){var e;if(this._cachedFirstDigit=void 0,this._cachedSecondDigit=void 0,"string"==typeof t)e=this._handleStringInput(t);else{if(!Array.isArray(t))throw new n(t,"string or string[]");e=this._handleArrayInput(t)}this._validateLength(e,t),this._validateNonRepeatedDigits(e,t),this._cpfDigits=e.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 n(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 i(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 p(e,"Repeated digits are not considered valid.")},CpfCheckDigits.prototype._calculate=function(t){for(var e=t.length+1,r=0,n=0,o=t;n<o.length;n++){r+=o[n]*e,e-=1}var i=11-r%11;return i>9?0:i},CpfCheckDigits}();Object.freeze(c);var a=function(t){function CpfGeneratorTypeError(e,r,n,o){var i=this.constructor,p=t.call(this,o)||this;return Object.setPrototypeOf(p,i.prototype),p.name=p.constructor.name,p.actualInput=e,p.actualType=r,p.expectedType=n,p}return __extends$1(CpfGeneratorTypeError,t),CpfGeneratorTypeError}(TypeError),s=function(t){function CpfGeneratorOptionsTypeError(e,r,n){var o=this,i=describeType(r);return(o=t.call(this,r,i,n,'CPF generator option "'.concat(e,'" must be of type ').concat(n,". Got ").concat(i,"."))||this).optionName=e,o}return __extends$1(CpfGeneratorOptionsTypeError,t),CpfGeneratorOptionsTypeError}(a),f=function(t){function CpfGeneratorException(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(CpfGeneratorException,t),CpfGeneratorException}(Error),u=function(t){function CpfGeneratorOptionPrefixInvalidException(e,r){var n=t.call(this,'CPF generator option "prefix" with value "'.concat(e,'" is invalid. ').concat(r))||this;return n.actualInput=e,n.reason=r,n}return __extends$1(CpfGeneratorOptionPrefixInvalidException,t),CpfGeneratorOptionPrefixInvalidException}(f),l="0".repeat(9),h=function(){function CpfGeneratorOptions(t){for(var e=[],r=1;r<arguments.length;r++)e[r-1]=arguments[r];this._options={},this.format=null==t?void 0:t.format,this.prefix=null==t?void 0:t.prefix;for(var n=0,o=e;n<o.length;n++){var i=o[n];this.set(i)}}return Object.defineProperty(CpfGeneratorOptions.prototype,"all",{get:function get(){var t=e({},this._options);return Object.freeze(t)},enumerable:!1,configurable:!0}),Object.defineProperty(CpfGeneratorOptions.prototype,"format",{get:function get(){return this._options.format},set:function set(t){var e=null!=t?t:CpfGeneratorOptions.DEFAULT_FORMAT;e=Boolean(e),this._options.format=e},enumerable:!1,configurable:!0}),Object.defineProperty(CpfGeneratorOptions.prototype,"prefix",{get:function get(){return this._options.prefix},set:function set(t){var e=null!=t?t:CpfGeneratorOptions.DEFAULT_PREFIX;if("string"!=typeof e)throw new s("prefix",e,"string");e=(e=e.replace(/\D/g,"")).slice(0,9),this._validatePrefixBaseId(e),this._validatePrefixNonRepeatedDigits(e),this._options.prefix=e},enumerable:!1,configurable:!0}),CpfGeneratorOptions.prototype.set=function(t){var e,r;return this.format=null!==(e=t.format)&&void 0!==e?e:this.format,this.prefix=null!==(r=t.prefix)&&void 0!==r?r:this.prefix,this},CpfGeneratorOptions.prototype._validatePrefixBaseId=function(t){if(!(t.length<9)&&t.slice(0,9)===l)throw new u(t,"Zeroed base ID is not eligible.")},CpfGeneratorOptions.prototype._validatePrefixNonRepeatedDigits=function(t){if(!(t.length<9)){var e=t.slice(0,9);if(1===new Set(e).size)throw new u(t,"Repeated digits are not considered valid.")}},CpfGeneratorOptions.DEFAULT_FORMAT=!1,CpfGeneratorOptions.DEFAULT_PREFIX="",CpfGeneratorOptions}();Object.freeze(h);var g=function(){function CpfGenerator(t){this._options=t instanceof h?t:new h(t)}return Object.defineProperty(CpfGenerator.prototype,"options",{get:function get(){return this._options},enumerable:!1,configurable:!0}),CpfGenerator.prototype.generate=function(t){var e=t?new h(this._options,t):this._options,n=function generateRandomSequence(t,e){var n,o=[];for(n=r;o.length<t;){var i=10*Math.random(),p=n[Math.floor(i)];o.push(p)}return o.join("")}(9-e.prefix.length),i=e.prefix+n;try{i=new c(i).cpf}catch(e){if(e instanceof o)return this.generate(t);throw e}return e.format&&(i=i.slice(0,3)+"."+i.slice(3,6)+"."+i.slice(6,9)+"-"+i.slice(9,11)),i},CpfGenerator}();function cpfGen$1(t){return new g(t).generate()}Object.freeze(g);var y=cpfGen$1,d=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 o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(r[n[o]]=t[n[o]])}return r}(Object.freeze({__proto__:null,CPF_LENGTH:11,CPF_PREFIX_MAX_LENGTH:9,CpfGenerator:g,CpfGeneratorException:f,CpfGeneratorOptionPrefixInvalidException:u,CpfGeneratorOptions:h,CpfGeneratorOptionsTypeError:s,CpfGeneratorTypeError:a,cpfGen:cpfGen$1,default:cpfGen$1}),["default","cpfGen"]),_=Object.assign(function cpfGen(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return y.apply(void 0,t)},d);return _});