UNPKG

@lacussoft/cpf-fmt

Version:

Utility to format CPF (Brazilian Individual's Taxpayer ID)

15 lines (14 loc) 11.2 kB
/** * Lacus Solutions :: cpf-fmt 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).cpfFmt=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(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 o in e=arguments[n])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})),n=Array.from(e);return 1===n.length?"".concat(n[0],"[]"):"(".concat(n.join(" | "),")[]")}"function"==typeof SuppressedError&&SuppressedError;var n=function(t){function CpfFormatterTypeError(e,n,r,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=n,p.expectedType=r,p}return __extends(CpfFormatterTypeError,t),CpfFormatterTypeError}(TypeError),r=function(t){function CpfFormatterInputTypeError(e,n){var r=describeType(e);return t.call(this,e,r,n,"CPF input must be of type ".concat(n,". Got ").concat(r,"."))||this}return __extends(CpfFormatterInputTypeError,t),CpfFormatterInputTypeError}(n),o=function(t){function CpfFormatterOptionsTypeError(e,n,r){var o=this,i=describeType(n);return(o=t.call(this,n,i,r,'CPF formatting option "'.concat(e,'" must be of type ').concat(r,". Got ").concat(i,"."))||this).optionName=e,o}return __extends(CpfFormatterOptionsTypeError,t),CpfFormatterOptionsTypeError}(n),i=function(t){function CpfFormatterException(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(CpfFormatterException,t),CpfFormatterException}(Error),p=function(t){function CpfFormatterOptionsHiddenRangeInvalidException(e,n,r,o){var i=t.call(this,'CPF formatting option "'.concat(e,'" must be an integer between ').concat(r," and ").concat(o,". Got ").concat(n,"."))||this;return i.optionName=e,i.actualInput=n,i.minExpectedValue=r,i.maxExpectedValue=o,i}return __extends(CpfFormatterOptionsHiddenRangeInvalidException,t),CpfFormatterOptionsHiddenRangeInvalidException}(i),a=function(t){function CpfFormatterOptionsForbiddenKeyCharacterException(e,n,r){var o=t.call(this,'Value "'.concat(n,'" for CPF formatting option "').concat(e,'" contains disallowed characters ("').concat(r.join('", "'),'").'))||this;return o.optionName=e,o.actualInput=n,o.forbiddenCharacters=r,o}return __extends(CpfFormatterOptionsForbiddenKeyCharacterException,t),CpfFormatterOptionsForbiddenKeyCharacterException}(i),s=function(t){function CpfFormatterInputLengthException(e,n,r){var o=this,i="string"==typeof e?'"'.concat(e,'"'):JSON.stringify(e),p=e===n?"".concat(n.length):"".concat(n.length,' in "').concat(n,'"');return(o=t.call(this,"CPF input ".concat(i," does not contain ").concat(r," digits. Got ").concat(p,"."))||this).actualInput=e,o.evaluatedInput=n,o.expectedLength=r,o}return __extends(CpfFormatterInputLengthException,t),CpfFormatterInputLengthException}(i),c=function(){function CpfFormatterOptions(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];this._options={},this.hidden=null==t?void 0:t.hidden,this.hiddenKey=null==t?void 0:t.hiddenKey,this.dotKey=null==t?void 0:t.dotKey,this.dashKey=null==t?void 0:t.dashKey,this.escape=null==t?void 0:t.escape,this.encode=null==t?void 0:t.encode,this.onFail=null==t?void 0:t.onFail,this.setHiddenRange(null==t?void 0:t.hiddenStart,null==t?void 0:t.hiddenEnd);for(var r=0,o=e;r<o.length;r++){var i=o[r];this.set(i)}}return Object.defineProperty(CpfFormatterOptions.prototype,"all",{get:function get(){var t=e({},this._options);return Object.freeze(t)},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"hidden",{get:function get(){return this._options.hidden},set:function set(t){var e=null!=t?t:CpfFormatterOptions.DEFAULT_HIDDEN;e=Boolean(e),this._options.hidden=e},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"hiddenKey",{get:function get(){return this._options.hiddenKey},set:function set(t){var e=null!=t?t:CpfFormatterOptions.DEFAULT_HIDDEN_KEY;if("string"!=typeof e)throw new o("hiddenKey",e,"string");this._assertNoDisallowedKeyCharacters("hiddenKey",e),this._options.hiddenKey=e},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"hiddenStart",{get:function get(){return this._options.hiddenStart},set:function set(t){this.setHiddenRange(t,this._options.hiddenEnd)},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"hiddenEnd",{get:function get(){return this._options.hiddenEnd},set:function set(t){this.setHiddenRange(this._options.hiddenStart,t)},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"dotKey",{get:function get(){return this._options.dotKey},set:function set(t){var e=null!=t?t:CpfFormatterOptions.DEFAULT_DOT_KEY;if("string"!=typeof e)throw new o("dotKey",e,"string");this._assertNoDisallowedKeyCharacters("dotKey",e),this._options.dotKey=e},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"dashKey",{get:function get(){return this._options.dashKey},set:function set(t){var e=null!=t?t:CpfFormatterOptions.DEFAULT_DASH_KEY;if("string"!=typeof e)throw new o("dashKey",e,"string");this._assertNoDisallowedKeyCharacters("dashKey",e),this._options.dashKey=e},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"escape",{get:function get(){return this._options.escape},set:function set(t){var e=null!=t?t:CpfFormatterOptions.DEFAULT_ESCAPE;e=Boolean(e),this._options.escape=e},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"encode",{get:function get(){return this._options.encode},set:function set(t){var e=null!=t?t:CpfFormatterOptions.DEFAULT_ENCODE;e=Boolean(e),this._options.encode=e},enumerable:!1,configurable:!0}),Object.defineProperty(CpfFormatterOptions.prototype,"onFail",{get:function get(){return this._options.onFail},set:function set(t){var e=null!=t?t:CpfFormatterOptions.DEFAULT_ON_FAIL;if("function"!=typeof e)throw new o("onFail",t,"function");this._options.onFail=e},enumerable:!1,configurable:!0}),CpfFormatterOptions.prototype.setHiddenRange=function(t,e){var n,r=null!=t?t:CpfFormatterOptions.DEFAULT_HIDDEN_START,i=null!=e?e:CpfFormatterOptions.DEFAULT_HIDDEN_END;if("number"!=typeof r||!Number.isInteger(r))throw new o("hiddenStart",r,"integer");if("number"!=typeof i||!Number.isInteger(i))throw new o("hiddenEnd",i,"integer");if(r<0||r>10)throw new p("hiddenStart",r,0,10);if(i<0||i>10)throw new p("hiddenEnd",i,0,10);return r>i&&(r=(n=[i,r])[0],i=n[1]),this._options.hiddenStart=r,this._options.hiddenEnd=i,this},CpfFormatterOptions.prototype.set=function(t){var e,n,r,o,i,p,a,s,c;return this.hidden=null!==(e=t.hidden)&&void 0!==e?e:this.hidden,this.hiddenKey=null!==(n=t.hiddenKey)&&void 0!==n?n:this.hiddenKey,this.dotKey=null!==(r=t.dotKey)&&void 0!==r?r:this.dotKey,this.dashKey=null!==(o=t.dashKey)&&void 0!==o?o:this.dashKey,this.escape=null!==(i=t.escape)&&void 0!==i?i:this.escape,this.encode=null!==(p=t.encode)&&void 0!==p?p:this.encode,this.onFail=null!==(a=t.onFail)&&void 0!==a?a:this.onFail,this.setHiddenRange(null!==(s=t.hiddenStart)&&void 0!==s?s:this.hiddenStart,null!==(c=t.hiddenEnd)&&void 0!==c?c:this.hiddenEnd),this},CpfFormatterOptions.prototype._assertNoDisallowedKeyCharacters=function(t,e){var n=CpfFormatterOptions.DISALLOWED_KEY_CHARACTERS;if(n.some(function(t){return e.includes(t)}))throw new a(t,e,n)},CpfFormatterOptions.DEFAULT_HIDDEN=!1,CpfFormatterOptions.DEFAULT_HIDDEN_KEY="*",CpfFormatterOptions.DEFAULT_HIDDEN_START=3,CpfFormatterOptions.DEFAULT_HIDDEN_END=10,CpfFormatterOptions.DEFAULT_DOT_KEY=".",CpfFormatterOptions.DEFAULT_DASH_KEY="-",CpfFormatterOptions.DEFAULT_ESCAPE=!1,CpfFormatterOptions.DEFAULT_ENCODE=!1,CpfFormatterOptions.DEFAULT_ON_FAIL=function DEFAULT_ON_FAIL(){return""},CpfFormatterOptions.DISALLOWED_KEY_CHARACTERS=Object.freeze(["å","ë","ï","ö"]),CpfFormatterOptions}();Object.freeze(c);var f=c.DISALLOWED_KEY_CHARACTERS[0],d=function(){function CpfFormatter(t){this._options=t instanceof c?t:new c(t)}return Object.defineProperty(CpfFormatter.prototype,"options",{get:function get(){return this._options},enumerable:!1,configurable:!0}),CpfFormatter.prototype.format=function(t,e){var n=this._toStringInput(t),r=e?new c(this._options,e):this._options,o=n.replace(/\D/g,"");if(11!==o.length){var i=new s(t,o,11);return r.onFail(t,i)}if(r.hidden){var p=o.slice(0,r.hiddenStart),a=o.slice(r.hiddenEnd+1),d=r.hiddenEnd-r.hiddenStart+1;o=p+f.repeat(d)+a}return o=(o=o.slice(0,3)+r.dotKey+o.slice(3,6)+r.dotKey+o.slice(6,9)+r.dashKey+o.slice(9,11)).replace(new RegExp(f,"g"),r.hiddenKey),r.escape&&(o=function escapeHTML(t){return t.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;")}(o)),r.encode&&(o=encodeURIComponent(o)),o},CpfFormatter.prototype._toStringInput=function(t){if("string"==typeof t)return t;if(Array.isArray(t)){for(var e=0,n=t;e<n.length;e++){if("string"!=typeof n[e])throw new r(t,"string or string[]")}return t.join("")}throw new r(t,"string or string[]")},CpfFormatter}();function cpfFmt$1(t,e){return new d(e).format(t)}Object.freeze(d);var u=cpfFmt$1,l=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 o=0;for(r=Object.getOwnPropertySymbols(t);o<r.length;o++)e.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(t,r[o])&&(n[r[o]]=t[r[o]])}return n}(Object.freeze({__proto__:null,CPF_LENGTH:11,CpfFormatter:d,CpfFormatterException:i,CpfFormatterInputLengthException:s,CpfFormatterInputTypeError:r,CpfFormatterOptions:c,CpfFormatterOptionsForbiddenKeyCharacterException:a,CpfFormatterOptionsHiddenRangeInvalidException:p,CpfFormatterOptionsTypeError:o,CpfFormatterTypeError:n,cpfFmt:cpfFmt$1,default:cpfFmt$1}),["default","cpfFmt"]),h=Object.assign(function cpfFmt(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return u.apply(void 0,t)},l);return h});