UNPKG

@gsretail.com/gui-core

Version:

A skeleton to create your own React component library using Rollup, TypeScript, Sass and Storybook

2 lines (1 loc) 5.78 kB
import{escapeRegExp as t,DIRECTION as s}from"../core/utils.js";import e from"../core/change-details.js";import a from"./base.js";import r from"../core/holder.js";class i extends a{static UNMASKED_RADIX=".";static EMPTY_VALUES=[...a.EMPTY_VALUES,0];static DEFAULTS={mask:Number,radix:",",thousandsSeparator:"",mapToRadix:[i.UNMASKED_RADIX],min:Number.MIN_SAFE_INTEGER,max:Number.MAX_SAFE_INTEGER,scale:2,normalizeZeros:!0,padFractionalZeros:!1,parse:Number,format:t=>t.toLocaleString("en-US",{useGrouping:!1,maximumFractionDigits:20})};constructor(t){super({...i.DEFAULTS,...t})}updateOptions(t){super.updateOptions(t)}_update(t){super._update(t),this._updateRegExps()}_updateRegExps(){const s="^"+(this.allowNegative?"[+|\\-]?":""),e=(this.scale?`(${t(this.radix)}\\d{0,${this.scale}})?`:"")+"$";this._numberRegExp=new RegExp(s+"\\d*"+e),this._mapToRadixRegExp=new RegExp(`[${this.mapToRadix.map(t).join("")}]`,"g"),this._thousandsSeparatorRegExp=new RegExp(t(this.thousandsSeparator),"g")}_removeThousandsSeparators(t){return t.replace(this._thousandsSeparatorRegExp,"")}_insertThousandsSeparators(t){const s=t.split(this.radix);return s[0]=s[0].replace(/\B(?=(\d{3})+(?!\d))/g,this.thousandsSeparator),s.join(this.radix)}doPrepareChar(t,s){void 0===s&&(s={});const[e,a]=super.doPrepareChar(this._removeThousandsSeparators(this.scale&&this.mapToRadix.length&&(s.input&&s.raw||!s.input&&!s.raw)?t.replace(this._mapToRadixRegExp,this.radix):t),s);return t&&!e&&(a.skip=!0),!e||this.allowPositive||this.value||"-"===e||a.aggregate(this._appendChar("-")),[e,a]}_separatorsCount(t,s){void 0===s&&(s=!1);let e=0;for(let a=0;a<t;++a)this._value.indexOf(this.thousandsSeparator,a)===a&&(++e,s&&(t+=this.thousandsSeparator.length));return e}_separatorsCountFromSlice(t){return void 0===t&&(t=this._value),this._separatorsCount(this._removeThousandsSeparators(t).length,!0)}extractInput(t,s,e){return void 0===t&&(t=0),void 0===s&&(s=this.displayValue.length),[t,s]=this._adjustRangeWithSeparators(t,s),this._removeThousandsSeparators(super.extractInput(t,s,e))}_appendCharRaw(t,s){if(void 0===s&&(s={}),!this.thousandsSeparator)return super._appendCharRaw(t,s);const e=s.tail&&s._beforeTailState?s._beforeTailState._value:this._value,a=this._separatorsCountFromSlice(e);this._value=this._removeThousandsSeparators(this.value);const r=super._appendCharRaw(t,s);this._value=this._insertThousandsSeparators(this._value);const i=s.tail&&s._beforeTailState?s._beforeTailState._value:this._value,o=this._separatorsCountFromSlice(i);return r.tailShift+=(o-a)*this.thousandsSeparator.length,r.skip=!r.rawInserted&&t===this.thousandsSeparator,r}_findSeparatorAround(t){if(this.thousandsSeparator){const s=t-this.thousandsSeparator.length+1,e=this.value.indexOf(this.thousandsSeparator,s);if(e<=t)return e}return-1}_adjustRangeWithSeparators(t,s){const e=this._findSeparatorAround(t);e>=0&&(t=e);const a=this._findSeparatorAround(s);return a>=0&&(s=a+this.thousandsSeparator.length),[t,s]}remove(t,s){void 0===t&&(t=0),void 0===s&&(s=this.displayValue.length),[t,s]=this._adjustRangeWithSeparators(t,s);const a=this.value.slice(0,t),r=this.value.slice(s),i=this._separatorsCount(a.length);this._value=this._insertThousandsSeparators(this._removeThousandsSeparators(a+r));const o=this._separatorsCountFromSlice(a);return new e({tailShift:(o-i)*this.thousandsSeparator.length})}nearestInputPos(t,e){if(!this.thousandsSeparator)return t;switch(e){case s.NONE:case s.LEFT:case s.FORCE_LEFT:{const a=this._findSeparatorAround(t-1);if(a>=0){const r=a+this.thousandsSeparator.length;if(t<r||this.value.length<=r||e===s.FORCE_LEFT)return a}break}case s.RIGHT:case s.FORCE_RIGHT:{const s=this._findSeparatorAround(t);if(s>=0)return s+this.thousandsSeparator.length}}return t}doValidate(t){let s=Boolean(this._removeThousandsSeparators(this.value).match(this._numberRegExp));if(s){const t=this.number;s=s&&!isNaN(t)&&(null==this.min||this.min>=0||this.min<=this.number)&&(null==this.max||this.max<=0||this.number<=this.max)}return s&&super.doValidate(t)}doCommit(){if(this.value){const t=this.number;let s=t;null!=this.min&&(s=Math.max(s,this.min)),null!=this.max&&(s=Math.min(s,this.max)),s!==t&&(this.unmaskedValue=this.format(s,this));let e=this.value;this.normalizeZeros&&(e=this._normalizeZeros(e)),this.padFractionalZeros&&this.scale>0&&(e=this._padFractionalZeros(e)),this._value=e}super.doCommit()}_normalizeZeros(t){const s=this._removeThousandsSeparators(t).split(this.radix);return s[0]=s[0].replace(/^(\D*)(0*)(\d*)/,((t,s,e,a)=>s+a)),t.length&&!/\d$/.test(s[0])&&(s[0]=s[0]+"0"),s.length>1&&(s[1]=s[1].replace(/0*$/,""),s[1].length||(s.length=1)),this._insertThousandsSeparators(s.join(this.radix))}_padFractionalZeros(t){if(!t)return t;const s=t.split(this.radix);return s.length<2&&s.push(""),s[1]=s[1].padEnd(this.scale,"0"),s.join(this.radix)}doSkipInvalid(t,s,e){void 0===s&&(s={});const a=0===this.scale&&t!==this.thousandsSeparator&&(t===this.radix||t===i.UNMASKED_RADIX||this.mapToRadix.includes(t));return super.doSkipInvalid(t,s,e)&&!a}get unmaskedValue(){return this._removeThousandsSeparators(this._normalizeZeros(this.value)).replace(this.radix,i.UNMASKED_RADIX)}set unmaskedValue(t){super.unmaskedValue=t}get typedValue(){return this.parse(this.unmaskedValue,this)}set typedValue(t){this.rawInputValue=this.format(t,this).replace(i.UNMASKED_RADIX,this.radix)}get number(){return this.typedValue}set number(t){this.typedValue=t}get allowNegative(){return null!=this.min&&this.min<0||null!=this.max&&this.max<0}get allowPositive(){return null!=this.min&&this.min>0||null!=this.max&&this.max>0}typedValueEquals(t){return(super.typedValueEquals(t)||i.EMPTY_VALUES.includes(t)&&i.EMPTY_VALUES.includes(this.typedValue))&&!(0===t&&""===this.value)}}r.MaskedNumber=i;export{i as default};