API Formatera
Ostatnia aktualizacja
Czy to było pomocne?
Czy to było pomocne?
function formatText(input) {
if (typeof input.textToFormat == 'undefined') {
return {
formattedText: "",
cursorPosition: -1
};
}
let number_format = function(number, decimals, dec_point, thousands_sep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
let n = !isFinite(+number) ? 0 : +number, prec = !isFinite(+decimals) ? 0
: Math.abs(decimals), sep = (typeof thousands_sep === 'undefined') ? ','
: thousands_sep, dec = (typeof dec_point === 'undefined') ? '.'
: dec_point, s = '', toFixedFix = function(n, prec) {
let k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
return s.join(dec);
}
let textToFormat = new String(input.textToFormat + "");
textToFormat = textToFormat.replace(/\s+/g, '');
textToFormat = textToFormat.replace(/,/g, '.')
textToFormat = textToFormat.replace(/[^\d^\.]/g,'');
let temp = parseFloat(textToFormat);
if(isNaN(temp)){
return {
formattedText: "",
cursorPosition: -1
};
}
let resultText = number_format(temp, 2, ',', ' ');
return {
formattedText: resultText,
cursorPosition: resultText.length
};
}function formatText(input) {
let spacesInInput;
let localCursorPosition;
if(input.cursorPosition) {
localCursorPosition = input.cursorPosition;
}
if(input.textToFormat) {
if(input.textToFormat.startsWith('0')) {
localCursorPosition--;
}
spacesInInput = (input.textToFormat.match(/ /g) || []).length;
} else {
return {
formattedText: "",
cursorPosition: -1
};
}
let number_format = function(number, decimals, dec_point, thousands_sep) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
let n = !isFinite(+number) ? 0 : +number, prec = !isFinite(+decimals) ? 0
: Math.abs(decimals), sep = (typeof thousands_sep === 'undefined') ? ','
: thousands_sep, dec = (typeof dec_point === 'undefined') ? '.'
: dec_point, s = '', toFixedFix = function(n, prec) {
let k = Math.pow(10, prec);
return '' + Math.round(n * k) / k;
};
s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
}
return s.join(dec);
}
let textToFormat = new String(input.textToFormat + "");
textToFormat = textToFormat.replace(/\s+/g, '');
textToFormat = textToFormat.replace(/,/g, '.')
textToFormat = textToFormat.replace(/[^\d^\.]/g,'');
let temp = parseFloat(textToFormat);
if(isNaN(temp)){
return {
formattedText: "",
cursorPosition: -1
};
}
let resultText = number_format(temp, 2, ',', ' ');
let caretPosition = localCursorPosition ? localCursorPosition : resultText.length;
let spacesAfterParse = (resultText.match(/ /g) || []).length;
if(spacesAfterParse > spacesInInput) {
caretPosition++;
} else if(spacesAfterParse < spacesInInput) {
caretPosition--;
}
return {
formattedText: resultText,
cursorPosition: caretPosition
};
}function formatText(textToFormat, locale) {
return formattedText;
}