-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Number field do no longer work with decimals (#108)
* Support both dot and comma as decimal separator * Rollback generalization of number pattern. use parseNumber function in ractive * Minor fix
- Loading branch information
1 parent
6cb6b4e
commit 5a78790
Showing
9 changed files
with
78 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { | ||
module.exports = parseNumber; | ||
} | ||
|
||
var numberRegexp = new RegExp('^(?:((?:\\d{1,3}(?:\\.\\d{3})+|\\d+)(?:,\\d{1,})?)|((?:\\d{1,3}(?:,\\d{3})+|\\d+)(?:\\.\\d{1,})?))$'); | ||
var dotRegexp = /\./g; | ||
var commaRegexp = /,/g; | ||
|
||
/** | ||
* Create float number from number, given by string with decimal comma/dot | ||
* @param {string} number | ||
* @return {float|null} | ||
*/ | ||
function parseNumber(number) { | ||
if (typeof number === 'undefined' || number === null) return null; | ||
|
||
number = number.toString(); | ||
var match = number.match(numberRegexp); | ||
if (!match) return null; | ||
|
||
var isDecimalComma = typeof match[1] !== 'undefined'; | ||
|
||
number = isDecimalComma ? | ||
number.replace(dotRegexp, '').replace(',', '.') : | ||
number.replace(commaRegexp, ''); | ||
|
||
return parseFloat(number); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
describe("test parseNumber function", function() { | ||
var parseNumber = require('../../js/lib/parse-number'); | ||
|
||
var data = [ | ||
{number: 0, expected: 0, note: '0 should be converted to 0.0'}, | ||
{number: '0', expected: 0, note: 'string 0 should be converted to 0.0'}, | ||
{number: 1, expected: 1.0, note: '1 should be converted to 1.0'}, | ||
{number: 134, expected: 134.0, note: '134 should be converted to 134.0'}, | ||
{number: 1.34, expected: 1.34, note: '1.34 should be converted to 1.34'}, | ||
{number: '1,34', expected: 1.34, note: '1,34 should be converted to 1.34'}, | ||
{number: '12,503,348.09', expected: 12503348.09, note: '12,503,348.09 should be converted to 12503348.09'}, | ||
{number: '12.503.348,09', expected: 12503348.09, note: '12.503.348,09 should be converted to 12503348.09'}, | ||
{number: '12503348.09', expected: 12503348.09, note: '12503348.09 should be converted to 12503348.09'}, | ||
{number: '12503348,09', expected: 12503348.09, note: '12503348,09 should be converted to 12503348.09'}, | ||
{number: '12,503.348.09', expected: null, note: '12,503.348.09 should be converted to null'}, | ||
{number: '12,503.348,09', expected: null, note: '12,503.348,09 should be converted to null'}, | ||
{number: 'a12', expected: null, note: 'a12 should be converted to null'}, | ||
{number: undefined, expected: null, note: 'undefined should be converted to null'}, | ||
{number: null, expected: null, note: 'null should be converted to null'} | ||
]; | ||
|
||
for (var i = 0; i < data.length; i++) { | ||
var given = data[i]; | ||
|
||
testDataItem(given); | ||
} | ||
|
||
function testDataItem(given) { | ||
it(given.note, function() { | ||
var result = parseNumber(given.number); | ||
|
||
expect(result).toBe(given.expected); | ||
}); | ||
} | ||
}); |