Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new option 'numeralDecimalPadding'. #707

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 68 additions & 13 deletions dist/cleave-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ return /******/ (function(modules) { // webpackBootstrap
pps.numeralDecimalScale,
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
pps.numeralDecimalPadding,
pps.stripLeadingZeroes,
pps.prefix,
pps.signBeforePrefix,
Expand Down Expand Up @@ -326,7 +327,7 @@ return /******/ (function(modules) { // webpackBootstrap
if (pps.numeral) {
// Do not show prefix when noImmediatePrefix is specified
// This mostly because we need to show user the native input placeholder
if (pps.prefix && pps.noImmediatePrefix && value.length === 0) {
if (pps.prefix && pps.noImmediatePrefix && value.length === 0 && !pps.numeralDecimalPadding) {
pps.result = '';
} else {
pps.result = pps.numeralFormatter.format(value);
Expand Down Expand Up @@ -432,7 +433,7 @@ return /******/ (function(modules) { // webpackBootstrap
var oldValue = owner.element.value;
var newValue = pps.result;

endPos = Util.getNextCursorPosition(endPos, oldValue, newValue, pps.delimiter, pps.delimiters);
endPos = Util.getNextCursorPosition(endPos, oldValue, newValue, pps);

// fix Android browser type="text" input field
// cursor not jumping issue
Expand Down Expand Up @@ -625,6 +626,7 @@ return /******/ (function(modules) { // webpackBootstrap
numeralDecimalScale,
numeralThousandsGroupStyle,
numeralPositiveOnly,
numeralDecimalPadding,
stripLeadingZeroes,
prefix,
signBeforePrefix,
Expand All @@ -637,6 +639,7 @@ return /******/ (function(modules) { // webpackBootstrap
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
owner.numeralDecimalPadding = numeralDecimalPadding !== false;
owner.stripLeadingZeroes = stripLeadingZeroes !== false;
owner.prefix = (prefix || prefix === '') ? prefix : '';
owner.signBeforePrefix = !!signBeforePrefix;
Expand Down Expand Up @@ -730,6 +733,17 @@ return /******/ (function(modules) { // webpackBootstrap
break;
}

if (owner.numeralDecimalPadding) {
if (owner.numeralDecimalScale > 0) {
if (partInteger.toString() === '') {
partInteger = '0';
}
partDecimal = String((partDecimal === '' ? owner.numeralDecimalMark : partDecimal)).padEnd(
owner.numeralDecimalScale + owner.numeralDecimalMark.length,
'0');
}
}

if (owner.tailPrefix) {
return partSign + partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '') + owner.prefix;
}
Expand Down Expand Up @@ -1380,24 +1394,64 @@ return /******/ (function(modules) { // webpackBootstrap
return new RegExp(delimiter.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'), 'g');
},

getNextCursorPosition: function (prevPos, oldValue, newValue, delimiter, delimiters) {
// If cursor was at the end of value, just place it back.
// Because new value could contain additional chars.
if (oldValue.length === prevPos) {
return newValue.length;
}
getNextCursorPosition: function (prevPos, oldValue, newValue, pps) {
// If cursor was at the end of value, just place it back.
// Because new value could contain additional chars.
if (oldValue.length === prevPos && !pps.numeralDecimalPadding) {
return newValue.length;
}

return prevPos + this.getPositionOffset(prevPos, oldValue, newValue, delimiter ,delimiters);
return prevPos + this.getPositionOffset(prevPos, oldValue, newValue, pps);
},

getPositionOffset: function (prevPos, oldValue, newValue, delimiter, delimiters) {
getPositionOffset: function (prevPos, oldValue, newValue, pps) {
var oldRawValue, newRawValue, lengthOffset;

oldRawValue = this.stripDelimiters(oldValue.slice(0, prevPos), delimiter, delimiters);
newRawValue = this.stripDelimiters(newValue.slice(0, prevPos), delimiter, delimiters);
oldRawValue = this.stripDelimiters(oldValue.slice(0, prevPos), pps.delimiter, pps.delimiters);
newRawValue = this.stripDelimiters(newValue.slice(0, prevPos), pps.delimiter, pps.delimiters);
lengthOffset = oldRawValue.length - newRawValue.length;

return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
if (pps.numeral && pps.numeralDecimalPadding && pps.numeralDecimalScale > 0) {
// in prefix
if (pps.prefix) {
if (!pps.tailPrefix) {
if (prevPos <= pps.prefixLength) {
return pps.prefixLength;
}
}
}
const decimalMarkPos = newValue.indexOf(pps.numeralDecimalMark);
if (oldValue === '') {
return -prevPos + decimalMarkPos;
}
if (oldValue.charAt(prevPos) === pps.numeralDecimalMark) {
if (prevPos > 0 && oldValue.charAt(prevPos - 1) === pps.numeralDecimalMark) {
return -prevPos + decimalMarkPos + 1;
}
else {
return -prevPos + decimalMarkPos;
}
}
// on the left of decimal mark
if (prevPos < decimalMarkPos) {
return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
}
if (prevPos == decimalMarkPos) {
return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
}
else {
// on the right of decimal mark
if (prevPos <= decimalMarkPos + pps.numeralDecimalScale + 1) {
return 0;
}
else {
return -1;
}
}
}
else {
return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
}
},

stripDelimiters: function (value, delimiter, delimiters) {
Expand Down Expand Up @@ -1668,6 +1722,7 @@ return /******/ (function(modules) { // webpackBootstrap
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
target.numeralPositiveOnly = !!opts.numeralPositiveOnly;
target.numeralDecimalPadding = opts.numeralDecimalPadding !== false;
target.stripLeadingZeroes = opts.stripLeadingZeroes !== false;
target.signBeforePrefix = !!opts.signBeforePrefix;
target.tailPrefix = !!opts.tailPrefix;
Expand Down
5 changes: 3 additions & 2 deletions dist/cleave-angular.min.js

Large diffs are not rendered by default.

81 changes: 68 additions & 13 deletions dist/cleave-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var NumeralFormatter = function (numeralDecimalMark,
numeralDecimalScale,
numeralThousandsGroupStyle,
numeralPositiveOnly,
numeralDecimalPadding,
stripLeadingZeroes,
prefix,
signBeforePrefix,
Expand All @@ -17,6 +18,7 @@ var NumeralFormatter = function (numeralDecimalMark,
owner.numeralDecimalScale = numeralDecimalScale >= 0 ? numeralDecimalScale : 2;
owner.numeralThousandsGroupStyle = numeralThousandsGroupStyle || NumeralFormatter.groupStyle.thousand;
owner.numeralPositiveOnly = !!numeralPositiveOnly;
owner.numeralDecimalPadding = numeralDecimalPadding !== false;
owner.stripLeadingZeroes = stripLeadingZeroes !== false;
owner.prefix = (prefix || prefix === '') ? prefix : '';
owner.signBeforePrefix = !!signBeforePrefix;
Expand Down Expand Up @@ -110,6 +112,17 @@ NumeralFormatter.prototype = {
break;
}

if (owner.numeralDecimalPadding) {
if (owner.numeralDecimalScale > 0) {
if (partInteger.toString() === '') {
partInteger = '0';
}
partDecimal = String((partDecimal === '' ? owner.numeralDecimalMark : partDecimal)).padEnd(
owner.numeralDecimalScale + owner.numeralDecimalMark.length,
'0');
}
}

if (owner.tailPrefix) {
return partSign + partInteger.toString() + (owner.numeralDecimalScale > 0 ? partDecimal.toString() : '') + owner.prefix;
}
Expand Down Expand Up @@ -725,24 +738,64 @@ var Util = {
return new RegExp(delimiter.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'), 'g');
},

getNextCursorPosition: function (prevPos, oldValue, newValue, delimiter, delimiters) {
// If cursor was at the end of value, just place it back.
// Because new value could contain additional chars.
if (oldValue.length === prevPos) {
return newValue.length;
}
getNextCursorPosition: function (prevPos, oldValue, newValue, pps) {
// If cursor was at the end of value, just place it back.
// Because new value could contain additional chars.
if (oldValue.length === prevPos && !pps.numeralDecimalPadding) {
return newValue.length;
}

return prevPos + this.getPositionOffset(prevPos, oldValue, newValue, delimiter ,delimiters);
return prevPos + this.getPositionOffset(prevPos, oldValue, newValue, pps);
},

getPositionOffset: function (prevPos, oldValue, newValue, delimiter, delimiters) {
getPositionOffset: function (prevPos, oldValue, newValue, pps) {
var oldRawValue, newRawValue, lengthOffset;

oldRawValue = this.stripDelimiters(oldValue.slice(0, prevPos), delimiter, delimiters);
newRawValue = this.stripDelimiters(newValue.slice(0, prevPos), delimiter, delimiters);
oldRawValue = this.stripDelimiters(oldValue.slice(0, prevPos), pps.delimiter, pps.delimiters);
newRawValue = this.stripDelimiters(newValue.slice(0, prevPos), pps.delimiter, pps.delimiters);
lengthOffset = oldRawValue.length - newRawValue.length;

return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
if (pps.numeral && pps.numeralDecimalPadding && pps.numeralDecimalScale > 0) {
// in prefix
if (pps.prefix) {
if (!pps.tailPrefix) {
if (prevPos <= pps.prefixLength) {
return pps.prefixLength;
}
}
}
const decimalMarkPos = newValue.indexOf(pps.numeralDecimalMark);
if (oldValue === '') {
return -prevPos + decimalMarkPos;
}
if (oldValue.charAt(prevPos) === pps.numeralDecimalMark) {
if (prevPos > 0 && oldValue.charAt(prevPos - 1) === pps.numeralDecimalMark) {
return -prevPos + decimalMarkPos + 1;
}
else {
return -prevPos + decimalMarkPos;
}
}
// on the left of decimal mark
if (prevPos < decimalMarkPos) {
return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
}
if (prevPos == decimalMarkPos) {
return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
}
else {
// on the right of decimal mark
if (prevPos <= decimalMarkPos + pps.numeralDecimalScale + 1) {
return 0;
}
else {
return -1;
}
}
}
else {
return (lengthOffset !== 0) ? (lengthOffset / Math.abs(lengthOffset)) : 0;
}
},

stripDelimiters: function (value, delimiter, delimiters) {
Expand Down Expand Up @@ -1006,6 +1059,7 @@ var DefaultProperties = {
target.numeralDecimalMark = opts.numeralDecimalMark || '.';
target.numeralThousandsGroupStyle = opts.numeralThousandsGroupStyle || 'thousand';
target.numeralPositiveOnly = !!opts.numeralPositiveOnly;
target.numeralDecimalPadding = opts.numeralDecimalPadding !== false;
target.stripLeadingZeroes = opts.stripLeadingZeroes !== false;
target.signBeforePrefix = !!opts.signBeforePrefix;
target.tailPrefix = !!opts.tailPrefix;
Expand Down Expand Up @@ -1169,6 +1223,7 @@ Cleave.prototype = {
pps.numeralDecimalScale,
pps.numeralThousandsGroupStyle,
pps.numeralPositiveOnly,
pps.numeralDecimalPadding,
pps.stripLeadingZeroes,
pps.prefix,
pps.signBeforePrefix,
Expand Down Expand Up @@ -1326,7 +1381,7 @@ Cleave.prototype = {
if (pps.numeral) {
// Do not show prefix when noImmediatePrefix is specified
// This mostly because we need to show user the native input placeholder
if (pps.prefix && pps.noImmediatePrefix && value.length === 0) {
if (pps.prefix && pps.noImmediatePrefix && value.length === 0 && !pps.numeralDecimalPadding) {
pps.result = '';
} else {
pps.result = pps.numeralFormatter.format(value);
Expand Down Expand Up @@ -1432,7 +1487,7 @@ Cleave.prototype = {
var oldValue = owner.element.value;
var newValue = pps.result;

endPos = Util.getNextCursorPosition(endPos, oldValue, newValue, pps.delimiter, pps.delimiters);
endPos = Util.getNextCursorPosition(endPos, oldValue, newValue, pps);

// fix Android browser type="text" input field
// cursor not jumping issue
Expand Down
2 changes: 1 addition & 1 deletion dist/cleave-esm.min.js

Large diffs are not rendered by default.

Loading