UNPKG

@lacussoft/cnpj-gen

Version:

Utility to generate CNPJ (Brazilian Business Tax ID)

22 lines (21 loc) 12.4 kB
/** * Lacus Solutions :: cnpj-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).cnpjGen=e()}(this,function(){"use strict";var t=function extendStatics(e,n){return t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},t(e,n)};function __extends$1(e,n){if("function"!=typeof n&&null!==n)throw new TypeError("Class extends value "+String(n)+" is not a constructor or null");function __(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(__.prototype=n.prototype,new __)}var e=function __assign(){return e=Object.assign||function __assign(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var i in e=arguments[n])Object.prototype.hasOwnProperty.call(e,i)&&(t[i]=e[i]);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})),n=Array.from(e);return 1===n.length?"".concat(n[0],"[]"):"(".concat(n.join(" | "),")[]")}"function"==typeof SuppressedError&&SuppressedError;var n="0123456789",r="ABCDEFGHIJKLMNOPQRSTUVWXYZ",i=n+r; /** * Lacus Solutions :: cnpj-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 n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},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,n){if(n||2===arguments.length)for(var r,i=0,o=e.length;i<o;i++)!r&&i in e||(r||(r=Array.prototype.slice.call(e,0,i)),r[i]=e[i]);return t.concat(r||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var o=function(t){function CnpjCheckDigitsInputTypeError(e,n){var r=describeType(e);return t.call(this,e,r,n,"CNPJ input must be of type ".concat(n,". Got ").concat(r,"."))||this}return __extends(CnpjCheckDigitsInputTypeError,t),CnpjCheckDigitsInputTypeError}(function(t){function CnpjCheckDigitsTypeError(e,n,r,i){var o=this.constructor,p=t.call(this,i)||this;return Object.setPrototypeOf(p,o.prototype),p.name=p.constructor.name,p.actualInput=e,p.actualType=n,p.expectedType=r,p}return __extends(CnpjCheckDigitsTypeError,t),CnpjCheckDigitsTypeError}(TypeError)),p=function(t){function CnpjCheckDigitsException(e){var n=this.constructor,r=t.call(this,e)||this;return Object.setPrototypeOf(r,n.prototype),r.name=r.constructor.name,r}return __extends(CnpjCheckDigitsException,t),CnpjCheckDigitsException}(Error),a=function(t){function CnpjCheckDigitsInputLengthException(e,n,r,i){var o=this,p="string"==typeof e?'"'.concat(e,'"'):JSON.stringify(e),a=e===n?"".concat(n.length):"".concat(n.length,' in "').concat(n,'"');return(o=t.call(this,"CNPJ input ".concat(p," does not contain ").concat(r," to ").concat(i," digits. Got ").concat(a,"."))||this).actualInput=e,o.evaluatedInput=n,o.minExpectedLength=r,o.maxExpectedLength=i,o}return __extends(CnpjCheckDigitsInputLengthException,t),CnpjCheckDigitsInputLengthException}(p),c=function(t){function CnpjCheckDigitsInputInvalidException(e,n){var r=this,i="string"==typeof e?'"'.concat(e,'"'):JSON.stringify(e);return(r=t.call(this,"CNPJ input ".concat(i," is invalid. ").concat(n))||this).actualInput=e,r.reason=n,r}return __extends(CnpjCheckDigitsInputInvalidException,t),CnpjCheckDigitsInputInvalidException}(p),s="0".repeat(8),u="0".repeat(4),l="0".charCodeAt(0),f=function(){function CnpjCheckDigits(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 o(t,"string or string[]");e=this._handleArrayInput(t)}this._validateLength(e,t),this._validateBaseId(e,t),this._validateBranchId(e,t),this._validateNonRepeatedDigits(e,t),this._cnpjChars=e.slice(0,12)}return Object.defineProperty(CnpjCheckDigits.prototype,"first",{get:function(){if(void 0===this._cachedFirstDigit){var t=__spreadArray([],this._cnpjChars,!0);this._cachedFirstDigit=this._calculate(t)}return this._cachedFirstDigit.toString()},enumerable:!1,configurable:!0}),Object.defineProperty(CnpjCheckDigits.prototype,"second",{get:function(){if(void 0===this._cachedSecondDigit){var t=__spreadArray(__spreadArray([],this._cnpjChars,!0),[this.first],!1);this._cachedSecondDigit=this._calculate(t)}return this._cachedSecondDigit.toString()},enumerable:!1,configurable:!0}),Object.defineProperty(CnpjCheckDigits.prototype,"both",{get:function(){return this.first+this.second},enumerable:!1,configurable:!0}),Object.defineProperty(CnpjCheckDigits.prototype,"cnpj",{get:function(){return __spreadArray(__spreadArray([],this._cnpjChars,!0),[this.both],!1).join("")},enumerable:!1,configurable:!0}),CnpjCheckDigits.prototype._handleStringInput=function(t){return t.replace(/[^0-9A-Z]/gi,"").toUpperCase().split("")},CnpjCheckDigits.prototype._handleArrayInput=function(t){if(0===t.length)return[];if(!t.every(function(t){return"string"==typeof t}))throw new o(t,"string or string[]");return this._handleStringInput(t.join(""))},CnpjCheckDigits.prototype._validateLength=function(t,e){var n=t.length;if(n<12||n>14)throw new a(e,t.join(""),12,14)},CnpjCheckDigits.prototype._validateBaseId=function(t,e){if(t.slice(0,8).join("")===s)throw new c(e,'Base ID "'.concat(s,'" is not eligible.'))},CnpjCheckDigits.prototype._validateBranchId=function(t,e){if(t.slice(8,12).join("")===u)throw new c(e,'Branch ID "'.concat(u,'" is not eligible.'))},CnpjCheckDigits.prototype._validateNonRepeatedDigits=function(t,e){var n,r=t.slice(0,12);if(1===new Set(r).size&&/^\d$/.test(null!==(n=r[0])&&void 0!==n?n:""))throw new c(e,"Repeated digits are not considered valid.")},CnpjCheckDigits.prototype._calculate=function(t){for(var e=2,n=0,r=t.length-1;r>=0;r--){n+=(t[r].charCodeAt(0)-l)*e,e=9===e?2:e+1}var i=n%11;return i<2?0:11-i},CnpjCheckDigits}();Object.freeze(f);var h=function(t){function CnpjGeneratorTypeError(e,n,r,i){var o=this.constructor,p=t.call(this,i)||this;return Object.setPrototypeOf(p,o.prototype),p.name=p.constructor.name,p.actualInput=e,p.actualType=n,p.expectedType=r,p}return __extends$1(CnpjGeneratorTypeError,t),CnpjGeneratorTypeError}(TypeError),d=function(t){function CnpjGeneratorOptionsTypeError(e,n,r){var i=this,o=describeType(n);return(i=t.call(this,n,o,r,'CNPJ generator option "'.concat(e,'" must be of type ').concat(r,". Got ").concat(o,"."))||this).optionName=e,i}return __extends$1(CnpjGeneratorOptionsTypeError,t),CnpjGeneratorOptionsTypeError}(h),y=function(t){function CnpjGeneratorException(e){var n=this.constructor,r=t.call(this,e)||this;return Object.setPrototypeOf(r,n.prototype),r.name=r.constructor.name,r}return __extends$1(CnpjGeneratorException,t),CnpjGeneratorException}(Error),g=function(t){function CnpjGeneratorOptionPrefixInvalidException(e,n){var r=t.call(this,'CNPJ generator option "prefix" with value "'.concat(e,'" is invalid. ').concat(n))||this;return r.actualInput=e,r.reason=n,r}return __extends$1(CnpjGeneratorOptionPrefixInvalidException,t),CnpjGeneratorOptionPrefixInvalidException}(y),_=function(t){function CnpjGeneratorOptionTypeInvalidException(e,n){var r=t.call(this,'CNPJ generator option "type" accepts only the following values: "'.concat(n.join('", "'),'". Got "').concat(e,'".'))||this;return r.actualInput=e,r.expectedValues=n,r}return __extends$1(CnpjGeneratorOptionTypeInvalidException,t),CnpjGeneratorOptionTypeInvalidException}(y),j="0".repeat(8),C="0".repeat(4),v=["alphabetic","alphanumeric","numeric"],O=function(){function CnpjGeneratorOptions(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];this._options={},this.format=null==t?void 0:t.format,this.prefix=null==t?void 0:t.prefix,this.type=null==t?void 0:t.type;for(var r=0,i=e;r<i.length;r++){var o=i[r];this.set(o)}}return Object.defineProperty(CnpjGeneratorOptions.prototype,"all",{get:function get(){var t=e({},this._options);return Object.freeze(t)},enumerable:!1,configurable:!0}),Object.defineProperty(CnpjGeneratorOptions.prototype,"format",{get:function get(){return this._options.format},set:function set(t){var e=null!=t?t:CnpjGeneratorOptions.DEFAULT_FORMAT;e=Boolean(e),this._options.format=e},enumerable:!1,configurable:!0}),Object.defineProperty(CnpjGeneratorOptions.prototype,"prefix",{get:function get(){return this._options.prefix},set:function set(t){var e=null!=t?t:CnpjGeneratorOptions.DEFAULT_PREFIX;if("string"!=typeof e)throw new d("prefix",e,"string");e=(e=(e=e.replace(/[^0-9A-Z]/gi,"")).toUpperCase()).slice(0,12),this._validatePrefixBaseId(e),this._validatePrefixBranchId(e),this._validatePrefixNonRepeatedDigits(e),this._options.prefix=e},enumerable:!1,configurable:!0}),Object.defineProperty(CnpjGeneratorOptions.prototype,"type",{get:function get(){return this._options.type},set:function set(t){var e=null!=t?t:CnpjGeneratorOptions.DEFAULT_TYPE;if("string"!=typeof e)throw new d("type",e,"string");if(!v.includes(e))throw new _(e,v);this._options.type=e},enumerable:!1,configurable:!0}),CnpjGeneratorOptions.prototype.set=function(t){var e,n,r;return this.format=null!==(e=t.format)&&void 0!==e?e:this.format,this.prefix=null!==(n=t.prefix)&&void 0!==n?n:this.prefix,this.type=null!==(r=t.type)&&void 0!==r?r:this.type,this},CnpjGeneratorOptions.prototype._validatePrefixBaseId=function(t){if(!(t.length<8)&&t.slice(0,8)===j)throw new g(t,"Zeroed base ID is not eligible.")},CnpjGeneratorOptions.prototype._validatePrefixBranchId=function(t){if(!(t.length<12)&&t.slice(8,12)===C)throw new g(t,"Zeroed branch ID is not eligible.")},CnpjGeneratorOptions.prototype._validatePrefixNonRepeatedDigits=function(t){var e;if(!(t.length<12)){var n=t.slice(0,12);if(1===new Set(n).size&&/^\d$/.test(null!==(e=n[0])&&void 0!==e?e:""))throw new g(t,"Repeated digits are not considered valid.")}},CnpjGeneratorOptions.DEFAULT_FORMAT=!1,CnpjGeneratorOptions.DEFAULT_PREFIX="",CnpjGeneratorOptions.DEFAULT_TYPE="alphanumeric",CnpjGeneratorOptions}();Object.freeze(O);var b=function(){function CnpjGenerator(t){this._options=t instanceof O?t:new O(t)}return Object.defineProperty(CnpjGenerator.prototype,"options",{get:function get(){return this._options},enumerable:!1,configurable:!0}),CnpjGenerator.prototype.generate=function(t){var e=t?new O(this._options,t):this._options,o=function generateRandomSequence(t,e){var o=[],p=i;for("numeric"===e?p=n:"alphabetic"===e&&(p=r);o.length<t;){var a=Math.random()*p.length,c=p[Math.floor(a)];o.push(c)}return o.join("")}(12-e.prefix.length,e.type),a=e.prefix+o;try{a=new f(a).cnpj}catch(e){if(e instanceof p)return this.generate(t);throw e}return e.format&&(a=a.slice(0,2)+"."+a.slice(2,5)+"."+a.slice(5,8)+"/"+a.slice(8,12)+"-"+a.slice(12,14)),a},CnpjGenerator}();function cnpjGen$1(t){return new b(t).generate()}Object.freeze(b);var x=cnpjGen$1,E=function __rest(t,e){var n={};for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&e.indexOf(r)<0&&(n[r]=t[r]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var i=0;for(r=Object.getOwnPropertySymbols(t);i<r.length;i++)e.indexOf(r[i])<0&&Object.prototype.propertyIsEnumerable.call(t,r[i])&&(n[r[i]]=t[r[i]])}return n}(Object.freeze({__proto__:null,CNPJ_LENGTH:14,CNPJ_PREFIX_MAX_LENGTH:12,CnpjGenerator:b,CnpjGeneratorException:y,CnpjGeneratorOptionPrefixInvalidException:g,CnpjGeneratorOptionTypeInvalidException:_,CnpjGeneratorOptions:O,CnpjGeneratorOptionsTypeError:d,CnpjGeneratorTypeError:h,cnpjGen:cnpjGen$1,default:cnpjGen$1}),["default","cnpjGen"]),G=Object.assign(function cnpjGen(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return x.apply(void 0,t)},E);return G});