From d8dd0afe3b2ac11715ebc242c2f6e33c57366b3c Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Fri, 6 Apr 2018 17:23:53 +0300 Subject: [PATCH 1/9] Disallow parse a long string --- src/number.js | 4 ++++ tests/number-tests.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/number.js b/src/number.js index f367dd7..a463cbb 100644 --- a/src/number.js +++ b/src/number.js @@ -83,6 +83,10 @@ numberLocalization.inject({ return format.parser(text); } + if(text.length > 15) { + return NaN; + } + text = this._normalizeNumber(text, format); return parseFloat(text); diff --git a/tests/number-tests.js b/tests/number-tests.js index 73ca3d3..b704233 100644 --- a/tests/number-tests.js +++ b/tests/number-tests.js @@ -169,6 +169,10 @@ locales.forEach(function(localeId) { assert.equal(numberLocalization.parse('!437', { parser: function(text) { return Number(text.substr(1)); } }), 437); }); + QUnit.test('parse long string', function(assert) { + assert.ok(isNaN(numberLocalization.parse('1111111111111111111111111111111111111'))); + }); + QUnit.module('currency', { beforeEach: function() { locale('en'); From 4734f623840a3d6e60a207cf70410284f7d07f81 Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Mon, 9 Apr 2018 17:02:05 +0300 Subject: [PATCH 2/9] Use DevExtreme parser for numbers --- src/number.js | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/src/number.js b/src/number.js index a463cbb..26cf350 100644 --- a/src/number.js +++ b/src/number.js @@ -74,40 +74,6 @@ numberLocalization.inject({ return this.callBase.apply(this, arguments); }, - parse: function(text, format) { - if(!text) { - return; - } - - if(format && format.parser) { - return format.parser(text); - } - - if(text.length > 15) { - return NaN; - } - - text = this._normalizeNumber(text, format); - - return parseFloat(text); - }, - _normalizeNumber: function(text, format) { - var isExponentialRegexp = /^[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)+$/, - legitDecimalSeparator = '.'; - - if(this.convertDigits) { - text = this.convertDigits(text, true); - } - - if(isExponentialRegexp.test(text)) { - return text; - } - - var decimalSeparator = this._getDecimalSeparator(format); - var cleanUpRegexp = new RegExp('[^0-9\-\\' + decimalSeparator + ']', 'g'); - - return text.replace(cleanUpRegexp, '').replace(decimalSeparator, legitDecimalSeparator); - }, _getDecimalSeparator: function(format) { return getFormatter(format)(0.1)[1]; }, From bdd4b08c2dba3f5b479f6881a9f58ebcd76c88cb Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Tue, 10 Apr 2018 10:20:21 +0300 Subject: [PATCH 3/9] Remove unusable method --- src/number.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/number.js b/src/number.js index 26cf350..754c076 100644 --- a/src/number.js +++ b/src/number.js @@ -74,9 +74,6 @@ numberLocalization.inject({ return this.callBase.apply(this, arguments); }, - _getDecimalSeparator: function(format) { - return getFormatter(format)(0.1)[1]; - }, _getCurrencySymbolInfo: function(currency) { var formatter = getCurrencyFormatter(currency); return this._extractCurrencySymbolInfo(formatter.format(0)); From 98f02be54f0425795de4006ae235e81a96609b90 Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Tue, 10 Apr 2018 10:50:45 +0300 Subject: [PATCH 4/9] Return parser for DevExtreme version < 17.2.8 --- src/number.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/number.js b/src/number.js index 754c076..3c23697 100644 --- a/src/number.js +++ b/src/number.js @@ -2,6 +2,7 @@ var objectAssign = require('object-assign'); var dxConfig = require('devextreme/core/config'); var locale = require('devextreme/localization').locale; var numberLocalization = require('devextreme/localization').number; +var dxVersion = require('devextreme/core/version'); var currencyOptionsCache = {}, detectCurrencySymbolRegex = /([^\s0]+)?(\s*)0*[.,]*0*(\s*)([^\s0]+)?/, @@ -74,6 +75,48 @@ numberLocalization.inject({ return this.callBase.apply(this, arguments); }, + + parse: function(text, format) { + if(dxVersion >= '17.2.8') { + return this.callBase.apply(this, arguments); + } + if(!text) { + return; + } + + if(format && format.parser) { + return format.parser(text); + } + + text = this._normalizeNumber(text, format); + + if(text.length > 15) { + return NaN; + } + + return parseFloat(text); + }, + _normalizeNumber: function(text, format) { + var isExponentialRegexp = /^[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)+$/, + legitDecimalSeparator = '.'; + + if(this.convertDigits) { + text = this.convertDigits(text, true); + } + + if(isExponentialRegexp.test(text)) { + return text; + } + + var decimalSeparator = this._getDecimalSeparator(format); + var cleanUpRegexp = new RegExp('[^0-9\-\\' + decimalSeparator + ']', 'g'); + + return text.replace(cleanUpRegexp, '').replace(decimalSeparator, legitDecimalSeparator); + }, + _getDecimalSeparator: function(format) { + return getFormatter(format)(0.1)[1]; + }, + _getCurrencySymbolInfo: function(currency) { var formatter = getCurrencyFormatter(currency); return this._extractCurrencySymbolInfo(formatter.format(0)); From 048ccd22c8cd3d4a104dfbaf5d021e67074ed33d Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Tue, 10 Apr 2018 10:53:57 +0300 Subject: [PATCH 5/9] Remove unnecessary empty lines --- src/number.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/number.js b/src/number.js index 3c23697..42dca5f 100644 --- a/src/number.js +++ b/src/number.js @@ -75,7 +75,6 @@ numberLocalization.inject({ return this.callBase.apply(this, arguments); }, - parse: function(text, format) { if(dxVersion >= '17.2.8') { return this.callBase.apply(this, arguments); @@ -116,7 +115,6 @@ numberLocalization.inject({ _getDecimalSeparator: function(format) { return getFormatter(format)(0.1)[1]; }, - _getCurrencySymbolInfo: function(currency) { var formatter = getCurrencyFormatter(currency); return this._extractCurrencySymbolInfo(formatter.format(0)); From c739641277fdc94fd1ce66ce38c354cbb7d8cc37 Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Tue, 10 Apr 2018 10:57:32 +0300 Subject: [PATCH 6/9] Remove unnecessary whitespaces --- src/number.js | 52 +++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/number.js b/src/number.js index 42dca5f..8ae8366 100644 --- a/src/number.js +++ b/src/number.js @@ -75,45 +75,45 @@ numberLocalization.inject({ return this.callBase.apply(this, arguments); }, - parse: function(text, format) { + parse: function(text, format) { if(dxVersion >= '17.2.8') { return this.callBase.apply(this, arguments); } - if(!text) { - return; - } + if(!text) { + return; + } - if(format && format.parser) { - return format.parser(text); - } + if(format && format.parser) { + return format.parser(text); + } - text = this._normalizeNumber(text, format); + text = this._normalizeNumber(text, format); if(text.length > 15) { return NaN; } - return parseFloat(text); - }, - _normalizeNumber: function(text, format) { - var isExponentialRegexp = /^[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)+$/, - legitDecimalSeparator = '.'; - - if(this.convertDigits) { - text = this.convertDigits(text, true); - } + return parseFloat(text); + }, + _normalizeNumber: function(text, format) { + var isExponentialRegexp = /^[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)+$/, + legitDecimalSeparator = '.'; - if(isExponentialRegexp.test(text)) { - return text; - } + if(this.convertDigits) { + text = this.convertDigits(text, true); + } + + if(isExponentialRegexp.test(text)) { + return text; + } - var decimalSeparator = this._getDecimalSeparator(format); - var cleanUpRegexp = new RegExp('[^0-9\-\\' + decimalSeparator + ']', 'g'); + var decimalSeparator = this._getDecimalSeparator(format); + var cleanUpRegexp = new RegExp('[^0-9\-\\' + decimalSeparator + ']', 'g'); - return text.replace(cleanUpRegexp, '').replace(decimalSeparator, legitDecimalSeparator); - }, - _getDecimalSeparator: function(format) { - return getFormatter(format)(0.1)[1]; + return text.replace(cleanUpRegexp, '').replace(decimalSeparator, legitDecimalSeparator); + }, + _getDecimalSeparator: function(format) { + return getFormatter(format)(0.1)[1]; }, _getCurrencySymbolInfo: function(currency) { var formatter = getCurrencyFormatter(currency); From b7e61ed80d0c2aa065781e675ed2ae1e57f8688f Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Tue, 10 Apr 2018 10:58:37 +0300 Subject: [PATCH 7/9] Remove unnecessary whitespaces --- src/number.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/number.js b/src/number.js index 8ae8366..24f5dbf 100644 --- a/src/number.js +++ b/src/number.js @@ -86,7 +86,7 @@ numberLocalization.inject({ if(format && format.parser) { return format.parser(text); } - + text = this._normalizeNumber(text, format); if(text.length > 15) { @@ -98,7 +98,7 @@ numberLocalization.inject({ _normalizeNumber: function(text, format) { var isExponentialRegexp = /^[-+]?[0-9]*.?[0-9]+([eE][-+]?[0-9]+)+$/, legitDecimalSeparator = '.'; - + if(this.convertDigits) { text = this.convertDigits(text, true); } From ebe1a392497f0cea11d520f231f6204552a27936 Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Tue, 10 Apr 2018 12:21:55 +0300 Subject: [PATCH 8/9] Create util for version comparison --- src/date.js | 5 +++-- src/number.js | 3 ++- src/utils.js | 20 ++++++++++++++++++++ tests/date-tests.js | 11 ++++++----- tests/number-tests.js | 3 ++- tests/utils-tests.js | 15 +++++++++++++++ 6 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 src/utils.js create mode 100644 tests/utils-tests.js diff --git a/src/date.js b/src/date.js index 9f11068..6ee5d99 100644 --- a/src/date.js +++ b/src/date.js @@ -3,6 +3,7 @@ var locale = require('devextreme/localization').locale; var dateLocalization = require('devextreme/localization').date; var firstDayOfWeekData = require('../locale-data/first-day-of-week-data'); var dxVersion = require('devextreme/core/version'); +var compareVersions = require('./utils').compareVersions; var SYMBOLS_TO_REMOVE_REGEX = /[\u200E\u200F]/g; @@ -203,13 +204,13 @@ dateLocalization.inject({ parse: function(dateString, format) { var SIMPLE_FORMATS = ['shortdate', 'shorttime', 'shortdateshorttime', 'longtime']; - if(dxVersion < '17.2.4' && dateString && typeof format === 'string' && SIMPLE_FORMATS.indexOf(format.toLowerCase()) > -1) { + if(compareVersions(dxVersion, '17.2.4') === -1 && dateString && typeof format === 'string' && SIMPLE_FORMATS.indexOf(format.toLowerCase()) > -1) { return this._parseDateBySimpleFormat(dateString, format.toLowerCase()); } var formatter; - if(dxVersion >= '17.2.4' && format && !format.parser && typeof dateString === 'string') { + if(compareVersions(dxVersion, '17.2.4') >= 0 && format && !format.parser && typeof dateString === 'string') { dateString = normalizeMonth(dateString); formatter = function(date) { return normalizeMonth(dateLocalization.format(date, format)); diff --git a/src/number.js b/src/number.js index 24f5dbf..a7d482e 100644 --- a/src/number.js +++ b/src/number.js @@ -3,6 +3,7 @@ var dxConfig = require('devextreme/core/config'); var locale = require('devextreme/localization').locale; var numberLocalization = require('devextreme/localization').number; var dxVersion = require('devextreme/core/version'); +var compareVersions = require('./utils').compareVersions; var currencyOptionsCache = {}, detectCurrencySymbolRegex = /([^\s0]+)?(\s*)0*[.,]*0*(\s*)([^\s0]+)?/, @@ -76,7 +77,7 @@ numberLocalization.inject({ return this.callBase.apply(this, arguments); }, parse: function(text, format) { - if(dxVersion >= '17.2.8') { + if(compareVersions(dxVersion, '17.2.8') >= 0) { return this.callBase.apply(this, arguments); } if(!text) { diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..02b8cfe --- /dev/null +++ b/src/utils.js @@ -0,0 +1,20 @@ +var compareVersions = function(x, y) { + x = x.split('.'); + y = y.split('.'); + + var length = Math.max(x.length, y.length); + + for(var i = 0; i < length; i++) { + var xItem = parseInt(x[i] || 0, 10), + yItem = parseInt(y[i] || 0, 10); + + if(xItem < yItem) { + return -1; + } + if(xItem > yItem) { + return 1; + } + } + return 0; +}; +exports.compareVersions = compareVersions; diff --git a/tests/date-tests.js b/tests/date-tests.js index 6ae7536..fd4f4e2 100644 --- a/tests/date-tests.js +++ b/tests/date-tests.js @@ -2,6 +2,7 @@ var QUnit = require('qunitjs'); var locale = require('devextreme/localization').locale; var dateLocalization = require('devextreme/localization/date'); var dxVersion = require('devextreme/core/version'); +var compareVersions = require('../src/utils').compareVersions; require('../src/date'); @@ -12,11 +13,11 @@ if(Intl.__disableRegExpRestore) { var SYMBOLS_TO_REMOVE_REGEX = /[\u200E\u200F]/g; var locales = [ 'de', 'en', 'ja', 'ru' ]; -if(dxVersion >= '17.2.3') { +if(compareVersions(dxVersion, '17.2.3') >= 0) { Array.prototype.push.apply(locales, [ 'zh', 'ar', 'hr' ]); } -if(dxVersion >= '17.2.4') { +if(compareVersions(dxVersion, '17.2.4') >= 0) { Array.prototype.push.apply(locales, [ 'el', 'ca' ]); } @@ -269,7 +270,7 @@ locales.forEach(function(localeId) { { format: 'longtime', date: new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(), 12, 59, 59) }, ]; - if(dxVersion >= '17.2.3') { + if(compareVersions(dxVersion, '17.2.3') >= 0) { Array.prototype.push.apply(testData, [ { format: 'longDate', date: new Date(2016, 10, 17) }, { format: 'longDate', date: new Date(2016, 11, 31) }, @@ -297,12 +298,12 @@ locales.forEach(function(localeId) { var date = config.date; // https://github.com/DevExpress/DevExtreme-Intl/issues/33 - if(dxVersion < '17.2.4' && localeId.substr(0, 2) === 'zh' && format === 'month') { + if(compareVersions(dxVersion, '17.2.4') === -1 && localeId.substr(0, 2) === 'zh' && format === 'month') { return; } // https://github.com/DevExpress/DevExtreme-Intl/issues/34 - if(dxVersion < '17.2.4' && localeId.substr(0, 2) === 'ar' && (format === 'longDate' || format === 'longDateLongTime')) { + if(compareVersions(dxVersion, '17.2.4') === -1 && localeId.substr(0, 2) === 'ar' && (format === 'longDate' || format === 'longDateLongTime')) { return; } diff --git a/tests/number-tests.js b/tests/number-tests.js index b704233..ad6d204 100644 --- a/tests/number-tests.js +++ b/tests/number-tests.js @@ -2,11 +2,12 @@ var QUnit = require('qunitjs'); var locale = require('devextreme/localization').locale; var numberLocalization = require('devextreme/localization/number'); var dxVersion = require('devextreme/core/version'); +var compareVersions = require('../src/utils').compareVersions; require('../src/number'); var locales = [ 'de', 'en', 'ja', 'ru' ]; -if(dxVersion >= '17.2.3') { +if(compareVersions(dxVersion, '17.2.3') >= 0) { Array.prototype.push.apply(locales, [ 'ar' ]); } locales.forEach(function(localeId) { diff --git a/tests/utils-tests.js b/tests/utils-tests.js new file mode 100644 index 0000000..0785df6 --- /dev/null +++ b/tests/utils-tests.js @@ -0,0 +1,15 @@ +var QUnit = require('qunitjs'); +var compareVersions = require('../src/utils').compareVersions; + +QUnit.module('compareVersions'); + +QUnit.test('base', function(assert) { + assert.equal(compareVersions('17.2.5', '17.2.4'), 1); + assert.equal(compareVersions('17.2.5', '17.2.5'), 0); + assert.equal(compareVersions('17.2.5', '17.2.6'), -1); + + assert.equal(compareVersions('17.2.5', '17.2'), 1); + assert.equal(compareVersions('17.2', '17.2.5'), -1); + + assert.equal(compareVersions('17.2.10', '17.2.5'), 1); +}); From 4044cd2f83b695f3308e1e2593321179537db774 Mon Sep 17 00:00:00 2001 From: "zhernovkova.anna" Date: Tue, 10 Apr 2018 12:59:08 +0300 Subject: [PATCH 9/9] Use DevExtreme util to versions comparison --- src/date.js | 2 +- src/number.js | 2 +- src/utils.js | 20 -------------------- tests/date-tests.js | 2 +- tests/number-tests.js | 2 +- tests/utils-tests.js | 15 --------------- 6 files changed, 4 insertions(+), 39 deletions(-) delete mode 100644 src/utils.js delete mode 100644 tests/utils-tests.js diff --git a/src/date.js b/src/date.js index 6ee5d99..238b868 100644 --- a/src/date.js +++ b/src/date.js @@ -3,7 +3,7 @@ var locale = require('devextreme/localization').locale; var dateLocalization = require('devextreme/localization').date; var firstDayOfWeekData = require('../locale-data/first-day-of-week-data'); var dxVersion = require('devextreme/core/version'); -var compareVersions = require('./utils').compareVersions; +var compareVersions = require('devextreme/core/utils/version').compare; var SYMBOLS_TO_REMOVE_REGEX = /[\u200E\u200F]/g; diff --git a/src/number.js b/src/number.js index a7d482e..eed2f1c 100644 --- a/src/number.js +++ b/src/number.js @@ -3,7 +3,7 @@ var dxConfig = require('devextreme/core/config'); var locale = require('devextreme/localization').locale; var numberLocalization = require('devextreme/localization').number; var dxVersion = require('devextreme/core/version'); -var compareVersions = require('./utils').compareVersions; +var compareVersions = require('devextreme/core/utils/version').compare; var currencyOptionsCache = {}, detectCurrencySymbolRegex = /([^\s0]+)?(\s*)0*[.,]*0*(\s*)([^\s0]+)?/, diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index 02b8cfe..0000000 --- a/src/utils.js +++ /dev/null @@ -1,20 +0,0 @@ -var compareVersions = function(x, y) { - x = x.split('.'); - y = y.split('.'); - - var length = Math.max(x.length, y.length); - - for(var i = 0; i < length; i++) { - var xItem = parseInt(x[i] || 0, 10), - yItem = parseInt(y[i] || 0, 10); - - if(xItem < yItem) { - return -1; - } - if(xItem > yItem) { - return 1; - } - } - return 0; -}; -exports.compareVersions = compareVersions; diff --git a/tests/date-tests.js b/tests/date-tests.js index fd4f4e2..0f16979 100644 --- a/tests/date-tests.js +++ b/tests/date-tests.js @@ -2,7 +2,7 @@ var QUnit = require('qunitjs'); var locale = require('devextreme/localization').locale; var dateLocalization = require('devextreme/localization/date'); var dxVersion = require('devextreme/core/version'); -var compareVersions = require('../src/utils').compareVersions; +var compareVersions = require('devextreme/core/utils/version').compare; require('../src/date'); diff --git a/tests/number-tests.js b/tests/number-tests.js index ad6d204..ef18e4e 100644 --- a/tests/number-tests.js +++ b/tests/number-tests.js @@ -2,7 +2,7 @@ var QUnit = require('qunitjs'); var locale = require('devextreme/localization').locale; var numberLocalization = require('devextreme/localization/number'); var dxVersion = require('devextreme/core/version'); -var compareVersions = require('../src/utils').compareVersions; +var compareVersions = require('devextreme/core/utils/version').compare; require('../src/number'); diff --git a/tests/utils-tests.js b/tests/utils-tests.js deleted file mode 100644 index 0785df6..0000000 --- a/tests/utils-tests.js +++ /dev/null @@ -1,15 +0,0 @@ -var QUnit = require('qunitjs'); -var compareVersions = require('../src/utils').compareVersions; - -QUnit.module('compareVersions'); - -QUnit.test('base', function(assert) { - assert.equal(compareVersions('17.2.5', '17.2.4'), 1); - assert.equal(compareVersions('17.2.5', '17.2.5'), 0); - assert.equal(compareVersions('17.2.5', '17.2.6'), -1); - - assert.equal(compareVersions('17.2.5', '17.2'), 1); - assert.equal(compareVersions('17.2', '17.2.5'), -1); - - assert.equal(compareVersions('17.2.10', '17.2.5'), 1); -});