Skip to content

Commit

Permalink
fixes some issues and closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelvanrijn committed Apr 1, 2016
1 parent 7cd7553 commit a3a6c54
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 30 deletions.
2 changes: 1 addition & 1 deletion grunt.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = function(grunt) {
},
watch: {
files: '<config:lint.files>',
tasks: 'lint qunit'
tasks: 'lint'
},
jshint: {
options: {
Expand Down
20 changes: 4 additions & 16 deletions libs/jquery/jquery.js

Large diffs are not rendered by default.

33 changes: 20 additions & 13 deletions src/jquery.numeric_input.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

// bind the keypress event
_instance.$elem.keypress(function( e ) {
console.log(e)
if( _instance.preventDefaultForKeyCode(e.which) === true) {
e.preventDefault();
}
var newValue = _instance.getNewValueForKeyCode( e.which, _instance.$elem.val() );
var newValue = _instance.getNewValueForKeyCode( e.which, _instance.$elem.val(), this.selectionStart );
if( newValue !== false) {
_instance.$elem.val( newValue );
_instance.options.callback.call(_instance, newValue);
Expand Down Expand Up @@ -84,17 +85,22 @@
}
},

getNewValueForKeyCode: function( keyCode, currentValue ) {
getNewValueForKeyCode: function( keyCode, currentValue, position=null ) {
// if a comma or a dot is pressed...
if( keyCode === 44 || keyCode === 46 || keyCode === 188 || keyCode === 190 ) {
// and we do not have a options.decimal present...
if( currentValue.indexOf( this.options.decimal ) === -1 ) {
// append leading zero if currentValue is empty and leadingZeroCheck is active
if( $.trim(currentValue) === '' && this.options.leadingZeroCheck ) {
currentValue = '0';
position += 1
}
// append the options.decimal instead of the dot or comma
return currentValue + this.options.decimal;
if(position === null) {
position = currentValue.length
}

// append the options.decimal instead of the dot or comma on the correct position
return [currentValue.slice(0, position), this.options.decimal, currentValue.slice(position)].join('');
}
}
// prepend the minus
Expand All @@ -107,41 +113,42 @@
},

parseValue: function( value ) {
var seperatorKey = '||SEP||';
var minusWasStripped = false;
var result = value.replace(/[A-Za-z$]/g, '');

if(result.length === 0 && this.options.allowEmpty) {
return '';
}

result = result.replace( ',', seperatorKey ).replace( '.', seperatorKey );

// strip minus and prepend later
if( result.indexOf('-') !== -1 ) {
result = result.replace( '-', '' );
minusWasStripped = true;
}
if ( result.indexOf('.') !== -1 || result.indexOf(',') !== -1 ) {
result = result.replace( '.', this.options.decimal );
result = result.replace( ',', this.options.decimal );
}
if ( result.indexOf( this.options.decimal ) === 0 ) {
if ( result.indexOf( seperatorKey ) === 0 ) {
result = '0' + result;
}
if ( minusWasStripped === true && this.options.allowNegative === true) {
result = '-' + result;
}
if ( this.options.numberOfDecimals !== null ) {
var decimals = result.split( this.options.decimal )[1];
var decimals = result.split( seperatorKey )[1];
if( decimals !== undefined ) {
if( decimals.length > this.options.numberOfDecimals ) {
result = Number(Number(String('0.' + decimals)).toFixed(this.options.numberOfDecimals)) + Math.floor(Number(result));
result = Number(Number(String('0.' + decimals)).toFixed(this.options.numberOfDecimals)) + Math.floor(Number(result.replace(seperatorKey, '.')))
}
if( decimals.length < this.options.numberOfDecimals ) {
result += new Array( this.options.numberOfDecimals - decimals.length + 1 ).join('0');
}
}
result = String(Number(result.replace(this.options.decimal, '.')).toFixed(this.options.numberOfDecimals)).replace('.', this.options.decimal);
var num = Number(String(result).replace(seperatorKey, '.')).toFixed(this.options.numberOfDecimals);
result = String(num).replace('.', seperatorKey);
}
return result;

return result.split( seperatorKey ).join( this.options.decimal );
}
};

Expand Down
54 changes: 54 additions & 0 deletions test/jquery.numeric_input_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,4 +286,58 @@
equal(this.second_target.val(), '-1,0', 'should parse the value to 1,0');
});

module('Allow empty values', {
setup: function() {
$('#qunit-fixture')
.append('<input type="text" id="numeric" />');
this.target = $('#qunit-fixture #numeric');
this.numeric_input = this.target.numeric_input({
allowEmpty: true
}).data('numeric_input');
},
teardown: function() {
$('#qunit-fixture').empty();
this.target = undefined;
this.numeric_input = undefined;
}
});

test('should prepend the minus char if allowed', function() {
equal(this.numeric_input.parseValue(''), '');
equal(this.numeric_input.parseValue(''), '');
});

module('Number of decimals', {
setup: function() {
$('#qunit-fixture')
.append('<input type="text" id="numeric" />');
this.target = $('#qunit-fixture #numeric');
this.numeric_input = this.target.numeric_input({
numberOfDecimals: 3,
leadingZeroCheck: true
}).data('numeric_input');
},
teardown: function() {
$('#qunit-fixture').empty();
this.target = undefined;
this.numeric_input = undefined;
}
});

test('Numbers of decimals to return', function() {
equal(this.numeric_input.parseValue('10'), '10,000');
equal(this.numeric_input.parseValue('10.1'), '10,100');
equal(this.numeric_input.parseValue('10.12'), '10,120');
equal(this.numeric_input.parseValue('10.123'), '10,123');
equal(this.numeric_input.parseValue('10.1235'), '10,123');
});

test('Should place the decimal at the correct position', function() {
equal(this.numeric_input.getNewValueForKeyCode(44, ''), '0,');
equal(this.numeric_input.getNewValueForKeyCode(44, '2'), '2,');
equal(this.numeric_input.getNewValueForKeyCode(44, '22', 0), ',22');
equal(this.numeric_input.getNewValueForKeyCode(44, '22', 1), '2,2');
equal(this.numeric_input.getNewValueForKeyCode(44, '22', 10), '22,');
});

}(jQuery));

0 comments on commit a3a6c54

Please sign in to comment.