From: Sven Schöling Date: Fri, 13 Oct 2017 11:41:04 +0000 (+0200) Subject: kivi.js: parse_amount und parse_date quirks X-Git-Tag: release-3.5.4~703 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=02438e2c4347d13d9b3bd66be21712a08366ef46;p=kivitendo-erp.git kivi.js: parse_amount und parse_date quirks - Parsing von 1,2,4,6,8-stelligen Daten ohne Separator - Parsing von yyyy-mm-dd Format ohne Separator - Parsing von 0-Präfixen bei Zahlen --- diff --git a/js/kivi.js b/js/kivi.js index 210020a60..4215a2e4a 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -32,11 +32,49 @@ namespace("kivi", function(ns) { ns.parse_date = function(date) { var parts = date.replace(/\s+/g, "").split(ns._date_format.sep); - date = new Date( - ((parts[ ns._date_format.y ] || 0) * 1) || (new Date()).getFullYear(), - (parts[ ns._date_format.m ] || 0) * 1 - 1, // Months are 0-based. - (parts[ ns._date_format.d ] || 0) * 1 - ); + var today = new Date(); + + // without separator? + // assume fixed pattern, and extract parts again + if (parts.length == 1) { + date = parts[0]; + parts = date.match(/../g); + if (date.length == 8) { + parts[ns._date_format.y] += parts.splice(ns._date_format.y + 1, 1) + } + else + if (date.length == 6 || date.length == 4) { + } + else + if (date.length == 1 || date.length == 2) { + parts = [] + parts[ ns._date_format.y ] = today.getFullYear(); + parts[ ns._date_format.m ] = today.getMonth() + 1; + parts[ ns._date_format.d ] = date; + } + else { + return undefined; + } + } + + if (parts.length == 3) { + var year = +parts[ ns._date_format.y ] || 0 * 1 || (new Date()).getFullYear(); + if (year < 100) { + year += year > 70 ? 1900 : 2000; + } + date = new Date( + year, + (parts[ ns._date_format.m ] || 0) * 1 - 1, // Months are 0-based. + (parts[ ns._date_format.d ] || 0) * 1 + ); + } else if (parts.length == 2) { + date = new Date( + (new Date()).getFullYear(), + (parts[ (ns._date_format.m > ns._date_format.d) * 1 ] || 0) * 1 - 1, // Months are 0-based. + (parts[ (ns._date_format.d > ns._date_format.m) * 1 ] || 0) * 1 + ); + } else + return undefined; return isNaN(date.getTime()) ? undefined : date; }; @@ -59,12 +97,14 @@ namespace("kivi", function(ns) { if (ns._number_format.decimalSep == ',') amount = amount.replace(/\./g, "").replace(/,/g, "."); - amount = amount.replace(/[\',]/g, "") + amount = amount.replace(/[\',]/g, ""); // Make sure no code wich is not a math expression ends up in eval(). if (!amount.match(/^[0-9 ()\-+*/.]*$/)) return 0; + amount = amount.replace(/^0+/, ''); + /* jshint -W061 */ try { return eval(amount); diff --git a/js/t/kivi/parse_format_date.js b/js/t/kivi/parse_format_date.js index b488b1ed6..8a675d7cd 100644 --- a/js/t/kivi/parse_format_date.js +++ b/js/t/kivi/parse_format_date.js @@ -12,7 +12,17 @@ QUnit.test("kivi.parse_date function for German date style with dots", function( assert.deepEqual(kivi.parse_date("25.12."), new Date((new Date).getFullYear(), 11, 25)); assert.deepEqual(kivi.parse_date("25.12"), new Date((new Date).getFullYear(), 11, 25)); + assert.deepEqual(kivi.parse_date("2512"), new Date((new Date).getFullYear(), 11, 25)); + assert.deepEqual(kivi.parse_date("25122015"), new Date(2015, 11, 25)); + assert.deepEqual(kivi.parse_date("251215"), new Date(2015, 11, 25)); + assert.deepEqual(kivi.parse_date("25"), new Date((new Date).getFullYear(), (new Date).getMonth(), 25)); + assert.deepEqual(kivi.parse_date("1"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); + assert.deepEqual(kivi.parse_date("01"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); + assert.deepEqual(kivi.parse_date("Totally Invalid!"), undefined); + assert.deepEqual(kivi.parse_date(":"), undefined); + assert.deepEqual(kivi.parse_date("::"), undefined); + assert.deepEqual(kivi.parse_date(""), undefined); }); QUnit.test("kivi.parse_date function for German date style with slashes", function( assert ) { @@ -100,5 +110,12 @@ QUnit.test("kivi.format_date function for ISO date style", function( assert ) { assert.deepEqual(kivi.format_date(new Date(2008, 1, 29)), "2008-02-29"); assert.deepEqual(kivi.format_date(new Date(2014, 11, 11)), "2014-12-11"); + assert.deepEqual(kivi.parse_date("1225"), new Date((new Date).getFullYear(), 11, 25)); + assert.deepEqual(kivi.parse_date("20151225"), new Date(2015, 11, 25)); + assert.deepEqual(kivi.parse_date("151225"), new Date(2015, 11, 25)); + assert.deepEqual(kivi.parse_date("25"), new Date((new Date).getFullYear(), (new Date).getMonth(), 25)); + assert.deepEqual(kivi.parse_date("1"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); + assert.deepEqual(kivi.parse_date("01"), new Date((new Date).getFullYear(), (new Date).getMonth(), 1)); + assert.deepEqual(kivi.format_date(new Date(undefined, undefined, undefined)), undefined); });