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

Remove bower #1

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
bower_components/
.DS_Store
6 changes: 6 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ <h4>Note: The <code>reverse</code> option is meant to be used with the <code>for
</script>
<pre class="brush: js">$("#demo14").maskMoney({ formatOnBlur: true, reverse: true, prefix: '$', selectAllOnFocus: true, precision: 4, allowEmpty: true });</pre>

<input type="text" id="demo15" value="" />
<script type="text/javascript">
$("#demo15").maskMoney({prefix:'R$ ', allowNegative: true, thousands:'.', precision: 0, affixesStay: true});
</script>
<pre class="brush: js">$("#demo15").maskMoney({prefix:'R$ ', allowNegative: true, thousands:'.', precision: 0, affixesStay: true});</pre>

</body>
<script type="text/javascript">
SyntaxHighlighter.all()
Expand Down
64 changes: 37 additions & 27 deletions src/jquery.maskMoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
allowEmpty: false,
bringCaretAtEndOnFocus: true
},
methods = {
methods = {
destroy: function () {
$(this).unbind(".maskMoney");

Expand Down Expand Up @@ -53,35 +53,45 @@
return this.map(function () {
var value = ($(this).val() || "0"),
isNegative = value.indexOf("-") !== -1,
decimalPart;
// get the last position of the array that is a number(coercion makes "" to be evaluated as false)
$(value.split(/\D/).reverse()).each(function (index, element) {
if (element) {
decimalPart = element;
return false;
decimalPart,
settings = $(this).data("settings") || defaultOptions;

// if precision is > 0, we apply the adjusts for precision,
if( settings.precision > 0 ){
// get the last position of the array that is a number(coercion makes "" to be evaluated as false)
$(value.split(/\D/).reverse()).each(function (index, element) {
if (element) {
decimalPart = element;
return false;
}
});
value = value.replace(/\D/g, "");
value = value.replace(new RegExp(decimalPart + "$"), "." + decimalPart);
if (isNegative) {
value = "-" + value;
}
});
value = value.replace(/\D/g, "");
value = value.replace(new RegExp(decimalPart + "$"), "." + decimalPart);
if (isNegative) {
value = "-" + value;
}
else{
// if <= 0, replace the . of the float value
value = value.replace(/\D/g, "");
}

return parseFloat(value);
});
},

unmaskedWithOptions: function () {
unmaskedWithOptions: function () {
return this.map(function () {
var value = ($(this).val() || "0"),
settings = $(this).data("settings") || defaultOptions,
regExp = new RegExp((settings.thousandsForUnmasked || settings.thousands), "g");
settings = $(this).data("settings") || defaultOptions,
regExp = new RegExp((settings.thousandsForUnmasked || settings.thousands), "g");
value = value.replace(regExp, "");
return parseFloat(value);
});
},

init: function (parameters) {
// the default options should not be shared with others
// the default options should not be shared with others
parameters = $.extend($.extend({}, defaultOptions), parameters);

return this.each(function () {
Expand Down Expand Up @@ -203,18 +213,18 @@
return;
}
var isNumber = !isNaN(value);
var decimalPointIndex = isNumber? value.indexOf("."): value.indexOf(settings.decimal);
var decimalPointIndex = isNumber? value.indexOf("."): value.indexOf(settings.decimal);
if (settings.precision > 0) {
if(decimalPointIndex < 0){
value += settings.decimal + new Array(settings.precision + 1).join(0);
}
else {
// If the following decimal part dosen't have enough length against the precision, it needs to be filled with zeros.
var integerPart = value.slice(0, decimalPointIndex),
decimalPart = value.slice(decimalPointIndex + 1);
value = integerPart + settings.decimal + decimalPart +
new Array((settings.precision + 1) - decimalPart.length).join(0);
}
if(decimalPointIndex < 0){
value += settings.decimal + new Array(settings.precision + 1).join(0);
}
else {
// If the following decimal part dosen't have enough length against the precision, it needs to be filled with zeros.
var integerPart = value.slice(0, decimalPointIndex),
decimalPart = value.slice(decimalPointIndex + 1);
value = integerPart + settings.decimal + decimalPart +
new Array((settings.precision + 1) - decimalPart.length).join(0);
}
} else if (decimalPointIndex > 0) {
// if the precision is 0, discard the decimal part
value = value.slice(0, decimalPointIndex);
Expand Down