Skip to content

Commit

Permalink
Merge pull request #48 from DevExpress/long_string
Browse files Browse the repository at this point in the history
Disallow parse a long string
  • Loading branch information
anna-zhernovkova authored Apr 10, 2018
2 parents f8145ed + 4044cd2 commit aae4627
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('devextreme/core/utils/version').compare;

var SYMBOLS_TO_REMOVE_REGEX = /[\u200E\u200F]/g;

Expand Down Expand Up @@ -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));
Expand Down
9 changes: 9 additions & 0 deletions src/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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 compareVersions = require('devextreme/core/utils/version').compare;

var currencyOptionsCache = {},
detectCurrencySymbolRegex = /([^\s0]+)?(\s*)0*[.,]*0*(\s*)([^\s0]+)?/,
Expand Down Expand Up @@ -75,6 +77,9 @@ numberLocalization.inject({
return this.callBase.apply(this, arguments);
},
parse: function(text, format) {
if(compareVersions(dxVersion, '17.2.8') >= 0) {
return this.callBase.apply(this, arguments);
}
if(!text) {
return;
}
Expand All @@ -85,6 +90,10 @@ numberLocalization.inject({

text = this._normalizeNumber(text, format);

if(text.length > 15) {
return NaN;
}

return parseFloat(text);
},
_normalizeNumber: function(text, format) {
Expand Down
11 changes: 6 additions & 5 deletions tests/date-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('devextreme/core/utils/version').compare;

require('../src/date');

Expand All @@ -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' ]);
}

Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 6 additions & 1 deletion tests/number-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('devextreme/core/utils/version').compare;

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) {
Expand Down Expand Up @@ -169,6 +170,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');
Expand Down

0 comments on commit aae4627

Please sign in to comment.