Formateo de numeros en JS
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
@push('page_scripts')
|
||||
<script>
|
||||
Intl.NumberFormat.prototype.parse = function(valueString) {
|
||||
const format = new Intl.NumberFormat(this.resolvedOptions().locale);
|
||||
const parts = format.formatToParts(-12345.6);
|
||||
const numerals = Array.from({ length: 10 }).map((_, i) => format.format(i));
|
||||
const index = new Map(numerals.map((d, i) => [d, i]));
|
||||
_minusSign = new RegExp(`[${parts.find(d => d.type === 'minusSign').value}]`);
|
||||
_group = new RegExp(`[${parts.find(d => d.type === 'group').value}]`, 'g');
|
||||
_decimal = new RegExp(`[${parts.find(d => d.type === 'decimal').value}]`);
|
||||
_numeral = new RegExp(`[${numerals.join('')}]`, 'g');
|
||||
_index = d => index.get(d);
|
||||
|
||||
const DIRECTION_MARK = /\u061c|\u200e/g
|
||||
return +(
|
||||
valueString.trim()
|
||||
.replace(DIRECTION_MARK, '')
|
||||
.replace(_group, '')
|
||||
.replace(_decimal, '.')
|
||||
.replace(_numeral, _index)
|
||||
.replace(_minusSign, '-')
|
||||
)
|
||||
}
|
||||
Intl.NumberFormat.prototype.isLocale = function(stringValue) {
|
||||
const format = new Intl.NumberFormat(this.resolvedOptions().locale);
|
||||
const parts = format.formatToParts(-12345.6);
|
||||
const group = parts.find(d => d.type === 'group').value;
|
||||
const decimal = parts.find(d => d.type === 'decimal').value;
|
||||
|
||||
if (stringValue.includes(group)) {
|
||||
if (stringValue.includes(decimal)) {
|
||||
return stringValue.indexOf(group) < stringValue.indexOf(decimal)
|
||||
}
|
||||
if (stringValue.split(group).map(d => d.length).filter(d => d > 3).length > 0) {
|
||||
return false
|
||||
}
|
||||
return stringValue.split(group).length > 2;
|
||||
}
|
||||
if (stringValue.includes(decimal)) {
|
||||
return stringValue.split(decimal).length <= 2;
|
||||
}
|
||||
return false
|
||||
}
|
||||
</script>
|
||||
@endpush
|
Reference in New Issue
Block a user