Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6acdad62

Von Moritz Bunkus vor mehr als 9 Jahren hinzugefügt

  • ID 6acdad62e7a4e11c8929e7ab00ce1bac511ed883
  • Vorgänger d5fee534
  • Nachfolger f8ec31c6

kivi.js: format/round/parse_amount, format/parse_date

Unterschiede anzeigen:

js/kivi.js
1 1
namespace("kivi", function(ns) {
2 2
  ns._locale = {};
3
  ns._date_format   = {
4
    sep: '.',
5
    y:   2,
6
    m:   1,
7
    d:   0
8
  };
9
  ns._number_format = {
10
    decimalSep:  ',',
11
    thousandSep: '.'
12
  };
13

  
14
  ns.setup_formats = function(params) {
15
    var res = (params.dates || "").match(/^([ymd]+)([^a-z])([ymd]+)[^a-z]([ymd]+)$/);
16
    if (res) {
17
      ns._date_format                      = { sep: res[2] };
18
      ns._date_format[res[1].substr(0, 1)] = 0;
19
      ns._date_format[res[3].substr(0, 1)] = 1;
20
      ns._date_format[res[4].substr(0, 1)] = 2;
21
    }
22

  
23
    res = (params.numbers || "").match(/^\d*([^\d]?)\d+([^\d])\d+$/);
24
    if (res)
25
      ns._number_format = {
26
        decimalSep:  res[2],
27
        thousandSep: res[1]
28
      };
29
  };
30

  
31
  ns.parse_date = function(date) {
32
    var parts = date.replace(/\s+/g, "").split(ns._date_format.sep);
33
    date     = new Date(
34
      ((parts[ ns._date_format.y ] || 0) * 1) || (new Date).getFullYear(),
35
       (parts[ ns._date_format.m ] || 0) * 1 - 1, // Months are 0-based.
36
       (parts[ ns._date_format.d ] || 0) * 1
37
    );
38

  
39
    return isNaN(date.getTime()) ? undefined : date;
40
  };
41

  
42
  ns.format_date = function(date) {
43
    if (isNaN(date.getTime()))
44
      return undefined;
45

  
46
    var parts = [ "", "", "" ]
47
    parts[ ns._date_format.y ] = date.getFullYear();
48
    parts[ ns._date_format.m ] = (date.getMonth() <  9 ? "0" : "") + (date.getMonth() + 1); // Months are 0-based, but days are 1-based.
49
    parts[ ns._date_format.d ] = (date.getDate()  < 10 ? "0" : "") + date.getDate();
50
    return parts.join(ns._date_format.sep);
51
  };
52

  
53
  ns.parse_amount = function(amount) {
54
    if ((amount == undefined) || (amount == ''))
55
      return 0;
56

  
57
    if (ns._number_format.decimalSep == ',')
58
      amount = amount.replace(/\./g, "").replace(/,/g, ".");
59

  
60
    amount = amount.replace(/[\',]/g, "")
61

  
62
    return eval(amount);
63
  };
64

  
65
  ns.round_amount = function(amount, places) {
66
    var neg  = amount >= 0 ? 1 : -1;
67
    var mult = Math.pow(10, places + 1);
68
    var temp = Math.abs(amount) * mult;
69
    var diff = Math.abs(1 - temp + Math.floor(temp));
70
    temp     = Math.floor(temp) + (diff <= 0.00001 ? 1 : 0);
71
    var dec  = temp % 10;
72
    temp    += dec >= 5 ? 10 - dec: dec * -1;
73

  
74
    return neg * temp / mult;
75
  };
76

  
77
  ns.format_amount = function(amount, places) {
78
    amount = amount || 0;
79

  
80
    if ((places != undefined) && (places >= 0))
81
      amount = ns.round_amount(amount, Math.abs(places));
82

  
83
    var parts = ("" + Math.abs(amount)).split(/\./);
84
    var intg  = parts[0];
85
    var dec   = parts.length > 1 ? parts[1] : "";
86
    var sign  = amount  < 0      ? "-"      : "";
87

  
88
    if (places != undefined) {
89
      while (dec.length < Math.abs(places))
90
        dec += "0";
91

  
92
      if ((places > 0) && (dec.length > Math.abs(places)))
93
        dec = d.substr(0, places);
94
    }
95

  
96
    if ((ns._number_format.thousandSep != "") && (intg.length > 3)) {
97
      var len   = ((intg.length + 2) % 3) + 1,
98
          start = len,
99
          res   = intg.substr(0, len);
100
      while (start < intg.length) {
101
        res   += ns._number_format.thousandSep + intg.substr(start, 3);
102
        start += 3;
103
      }
104

  
105
      intg = res;
106
    }
107

  
108
    var sep = (places != 0) && (dec != "") ? ns._number_format.decimalSep : "";
109

  
110
    return sign + intg + sep + dec;
111
  };
3 112

  
4 113
  ns.t8 = function(text, params) {
5 114
    var text = ns._locale[text] || text;
js/t/kivi/format_amount.js
1
QUnit.test("kivi.format_amount function German number style with thousand separator", function( assert ) {
2
  kivi.setup_formats({ numbers: '1.000,00' });
3

  
4
  assert.equal(kivi.format_amount('1e1', 2), '10,00', 'format 1e1');
5
  assert.equal(kivi.format_amount(1000, 2), '1.000,00', 'format 1000');
6
  assert.equal(kivi.format_amount(1000.1234, 2), '1.000,12', 'format 1000.1234');
7
  assert.equal(kivi.format_amount(1000000000.1234, 2), '1.000.000.000,12', 'format 1000000000.1234');
8
  assert.equal(kivi.format_amount(-1000000000.1234, 2), '-1.000.000.000,12', 'format -1000000000.1234');
9
});
10

  
11
QUnit.test("kivi.format_amount function German number style without thousand separator", function( assert ) {
12
  kivi.setup_formats({ numbers: '1000,00' });
13

  
14
  assert.equal(kivi.format_amount('1e1', 2), '10,00', 'format 1e1');
15
  assert.equal(kivi.format_amount(1000, 2), '1000,00', 'format 1000');
16
  assert.equal(kivi.format_amount(1000.1234, 2), '1000,12', 'format 1000.1234');
17
  assert.equal(kivi.format_amount(1000000000.1234, 2), '1000000000,12', 'format 1000000000.1234');
18
  assert.equal(kivi.format_amount(-1000000000.1234, 2), '-1000000000,12', 'format -1000000000.1234');
19
});
20

  
21
QUnit.test("kivi.format_amount function English number style with thousand separator", function( assert ) {
22
  kivi.setup_formats({ numbers: '1,000.00' });
23

  
24
  assert.equal(kivi.format_amount('1e1', 2), '10.00', 'format 1e1');
25
  assert.equal(kivi.format_amount(1000, 2), '1,000.00', 'format 1000');
26
  assert.equal(kivi.format_amount(1000.1234, 2), '1,000.12', 'format 1000.1234');
27
  assert.equal(kivi.format_amount(1000000000.1234, 2), '1,000,000,000.12', 'format 1000000000.1234');
28
  assert.equal(kivi.format_amount(-1000000000.1234, 2), '-1,000,000,000.12', 'format -1000000000.1234');
29
});
30

  
31
QUnit.test("kivi.format_amount function English number style without thousand separator", function( assert ) {
32
  kivi.setup_formats({ numbers: '1000.00' });
33

  
34
  assert.equal(kivi.format_amount('1e1', 2), '10.00', 'format 1e1');
35
  assert.equal(kivi.format_amount(1000, 2), '1000.00', 'format 1000');
36
  assert.equal(kivi.format_amount(1000.1234, 2), '1000.12', 'format 1000.1234');
37
  assert.equal(kivi.format_amount(1000000000.1234, 2), '1000000000.12', 'format 1000000000.1234');
38
  assert.equal(kivi.format_amount(-1000000000.1234, 2), '-1000000000.12', 'format -1000000000.1234');
39
});
40

  
41
QUnit.test("kivi.format_amount function negative places", function( assert ) {
42
  kivi.setup_formats({ numbers: '1000.00' });
43

  
44
  assert.equal(kivi.format_amount(1.00045, -2), '1.00045', 'negative places');
45
  assert.equal(kivi.format_amount(1.00045, -5), '1.00045', 'negative places 2');
46
  assert.equal(kivi.format_amount(1, -2), '1.00', 'negative places 3');
47
});
48

  
49
QUnit.test("kivi.format_amount function bugs and edge cases", function( assert ) {
50
  kivi.setup_formats({ numbers: '1.000,00' });
51

  
52
  assert.equal(kivi.format_amount(0.00005), '0,00005', 'messing with small numbers and no precision');
53
  assert.equal(kivi.format_amount(undefined), '0', 'undefined');
54
  assert.equal(kivi.format_amount(''), '0', 'empty string');
55
  assert.equal(kivi.format_amount(undefined, 2), '0,00', 'undefined with precision');
56
  assert.equal(kivi.format_amount('', 2), '0,00', 'empty string with prcesion');
57

  
58
  assert.equal(kivi.format_amount(0.545, 0), '1', 'rounding up with precision 0');
59
  assert.equal(kivi.format_amount(-0.545, 0), '-1', 'neg rounding up with precision 0');
60

  
61
  assert.equal(kivi.format_amount(1.00), '1', 'autotrim to 0 places');
62

  
63
  assert.equal(kivi.format_amount(10), '10', 'autotrim does not harm integers');
64
  assert.equal(kivi.format_amount(10, 2), '10,00' , 'autotrim does not harm integers 2');
65
  assert.equal(kivi.format_amount(10, -2), '10,00' , 'autotrim does not harm integers 3');
66
  assert.equal(kivi.format_amount(10, 0), '10', 'autotrim does not harm integers 4');
67

  
68
  assert.equal(kivi.format_amount(0, 0), '0' , 'trivial zero');
69
});
js/t/kivi/parse_amount.js
1
QUnit.test("kivi.parse_amount function German number style with thousand separator", function( assert ) {
2
  kivi.setup_formats({ numbers: '1.000,00' });
3

  
4
  assert.equal(kivi.parse_amount('10,00'), 10, '10,00');
5
  assert.equal(kivi.parse_amount('10,'), 10, '10,');
6
  assert.equal(kivi.parse_amount('1010,00'), 1010, '1010,00');
7
  assert.equal(kivi.parse_amount('1010,'), 1010, '1010,');
8
  assert.equal(kivi.parse_amount('1.010,00'), 1010, '1.010,00');
9
  assert.equal(kivi.parse_amount('1.010,'), 1010, '1.010,');
10
  assert.equal(kivi.parse_amount('9.080.070.060.050.040.030.020.010,00'), 9080070060050040030020010, '9.080.070.060.050.040.030.020.010,00');
11
  assert.equal(kivi.parse_amount('9.080.070.060.050.040.030.020.010,'), 9080070060050040030020010, '9.080.070.060.050.040.030.020.010,');
12

  
13
  assert.equal(kivi.parse_amount('10,98'), 10.98, '10,98');
14
  assert.equal(kivi.parse_amount('1010,98'), 1010.98, '1010,98');
15
  assert.equal(kivi.parse_amount('1.010,98'), 1010.98, '1.010,98');
16

  
17
  assert.equal(kivi.parse_amount('10,987654321'), 10.987654321, '10,987654321');
18
  assert.equal(kivi.parse_amount('1010,987654321'), 1010.987654321, '1010,987654321');
19
  assert.equal(kivi.parse_amount('1.010,987654321'), 1010.987654321, '1.010,987654321');
20
});
21

  
22
QUnit.test("kivi.parse_amount function German number style without thousand separator", function( assert ) {
23
  kivi.setup_formats({ numbers: '1000,00' });
24

  
25
  assert.equal(kivi.parse_amount('10,00'), 10, '10,00');
26
  assert.equal(kivi.parse_amount('10,'), 10, '10,');
27
  assert.equal(kivi.parse_amount('1010,00'), 1010, '1010,00');
28
  assert.equal(kivi.parse_amount('1010,'), 1010, '1010,');
29
  assert.equal(kivi.parse_amount('1.010,00'), 1010, '1.010,00');
30
  assert.equal(kivi.parse_amount('1.010,'), 1010, '1.010,');
31
  assert.equal(kivi.parse_amount('9.080.070.060.050.040.030.020.010,00'), 9080070060050040030020010, '9.080.070.060.050.040.030.020.010,00');
32
  assert.equal(kivi.parse_amount('9.080.070.060.050.040.030.020.010,'), 9080070060050040030020010, '9.080.070.060.050.040.030.020.010,');
33

  
34
  assert.equal(kivi.parse_amount('10,98'), 10.98, '10,98');
35
  assert.equal(kivi.parse_amount('1010,98'), 1010.98, '1010,98');
36
  assert.equal(kivi.parse_amount('1.010,98'), 1010.98, '1.010,98');
37

  
38
  assert.equal(kivi.parse_amount('10,987654321'), 10.987654321, '10,987654321');
39
  assert.equal(kivi.parse_amount('1010,987654321'), 1010.987654321, '1010,987654321');
40
  assert.equal(kivi.parse_amount('1.010,987654321'), 1010.987654321, '1.010,987654321');
41
});
42

  
43
QUnit.test("kivi.parse_amount function English number style with thousand separator", function( assert ) {
44
  kivi.setup_formats({ numbers: '1,000.00' });
45

  
46
  assert.equal(kivi.parse_amount('10.00'), 10, '10.00');
47
  assert.equal(kivi.parse_amount('10.'), 10, '10.');
48
  assert.equal(kivi.parse_amount('1010.00'), 1010, '1010.00');
49
  assert.equal(kivi.parse_amount('1010.'), 1010, '1010.');
50
  assert.equal(kivi.parse_amount('1,010.00'), 1010, '1,010.00');
51
  assert.equal(kivi.parse_amount('1,010.'), 1010, '1,010.');
52
  assert.equal(kivi.parse_amount('9,080,070,060,050,040,030,020,010.00'), 9080070060050040030020010, '9,080,070,060,050,040,030,020,010.00');
53
  assert.equal(kivi.parse_amount('9,080,070,060,050,040,030,020,010.'), 9080070060050040030020010, '9,080,070,060,050,040,030,020,010.');
54

  
55
  assert.equal(kivi.parse_amount('10.98'), 10.98, '10.98');
56
  assert.equal(kivi.parse_amount('1010.98'), 1010.98, '1010.98');
57
  assert.equal(kivi.parse_amount('1,010.98'), 1010.98, '1,010.98');
58

  
59
  assert.equal(kivi.parse_amount('10.987654321'), 10.987654321, '10.987654321');
60
  assert.equal(kivi.parse_amount('1010.987654321'), 1010.987654321, '1010.987654321');
61
  assert.equal(kivi.parse_amount('1,010.987654321'), 1010.987654321, '1,010.987654321');
62
});
63

  
64
QUnit.test("kivi.parse_amount function English number style without thousand separator", function( assert ) {
65
  kivi.setup_formats({ numbers: '1000.00' });
66

  
67
  assert.equal(kivi.parse_amount('10.00'), 10, '10.00');
68
  assert.equal(kivi.parse_amount('10.'), 10, '10.');
69
  assert.equal(kivi.parse_amount('1010.00'), 1010, '1010.00');
70
  assert.equal(kivi.parse_amount('1010.'), 1010, '1010.');
71
  assert.equal(kivi.parse_amount('1,010.00'), 1010, '1,010.00');
72
  assert.equal(kivi.parse_amount('1,010.'), 1010, '1,010.');
73
  assert.equal(kivi.parse_amount('9,080,070,060,050,040,030,020,010.00'), 9080070060050040030020010, '9,080,070,060,050,040,030,020,010.00');
74
  assert.equal(kivi.parse_amount('9,080,070,060,050,040,030,020,010.'), 9080070060050040030020010, '9,080,070,060,050,040,030,020,010.');
75

  
76
  assert.equal(kivi.parse_amount('10.98'), 10.98, '10.98');
77
  assert.equal(kivi.parse_amount('1010.98'), 1010.98, '1010.98');
78
  assert.equal(kivi.parse_amount('1,010.98'), 1010.98, '1,010.98');
79

  
80
  assert.equal(kivi.parse_amount('10.987654321'), 10.987654321, '10.987654321');
81
  assert.equal(kivi.parse_amount('1010.987654321'), 1010.987654321, '1010.987654321');
82
  assert.equal(kivi.parse_amount('1,010.987654321'), 1010.987654321, '1,010.987654321');
83
});
js/t/kivi/parse_format_date.js
1
QUnit.test("kivi.parse_date function for German date style with dots", function( assert ) {
2
  kivi.setup_formats({ dates: "dd.mm.yy" });
3

  
4
  assert.deepEqual(kivi.parse_date("01.01.2007"), new Date(2007, 0, 1));
5
  assert.deepEqual(kivi.parse_date("1.1.2007"), new Date(2007, 0, 1));
6
  assert.deepEqual(kivi.parse_date("      01 .  1. 2007   "), new Date(2007, 0, 1));
7

  
8
  assert.deepEqual(kivi.parse_date("29.02.2008"), new Date(2008, 1, 29));
9

  
10
  assert.deepEqual(kivi.parse_date("11.12.2014"), new Date(2014, 11, 11));
11

  
12
  assert.deepEqual(kivi.parse_date("25.12."), new Date((new Date).getFullYear(), 11, 25));
13
  assert.deepEqual(kivi.parse_date("25.12"), new Date((new Date).getFullYear(), 11, 25));
14

  
15
  assert.deepEqual(kivi.parse_date("Totally Invalid!"), undefined);
16
});
17

  
18
QUnit.test("kivi.parse_date function for German date style with slashes", function( assert ) {
19
  kivi.setup_formats({ dates: "dd/mm/yy" });
20

  
21
  assert.deepEqual(kivi.parse_date("01/01/2007"), new Date(2007, 0, 1));
22
  assert.deepEqual(kivi.parse_date("1/1/2007"), new Date(2007, 0, 1));
23
  assert.deepEqual(kivi.parse_date("      01 /  1/ 2007   "), new Date(2007, 0, 1));
24

  
25
  assert.deepEqual(kivi.parse_date("29/02/2008"), new Date(2008, 1, 29));
26

  
27
  assert.deepEqual(kivi.parse_date("11/12/2014"), new Date(2014, 11, 11));
28

  
29
  assert.deepEqual(kivi.parse_date("25/12/"), new Date((new Date).getFullYear(), 11, 25));
30
  assert.deepEqual(kivi.parse_date("25/12"), new Date((new Date).getFullYear(), 11, 25));
31

  
32
  assert.deepEqual(kivi.parse_date("Totally Invalid!"), undefined);
33
});
34

  
35
QUnit.test("kivi.parse_date function for American date style", function( assert ) {
36
  kivi.setup_formats({ dates: "mm/dd/yy" });
37

  
38
  assert.deepEqual(kivi.parse_date("01/01/2007"), new Date(2007, 0, 1));
39
  assert.deepEqual(kivi.parse_date("1/1/2007"), new Date(2007, 0, 1));
40
  assert.deepEqual(kivi.parse_date("      01 /  1/ 2007   "), new Date(2007, 0, 1));
41

  
42
  assert.deepEqual(kivi.parse_date("02/29/2008"), new Date(2008, 1, 29));
43

  
44
  assert.deepEqual(kivi.parse_date("12/11/2014"), new Date(2014, 11, 11));
45

  
46
  assert.deepEqual(kivi.parse_date("12/25/"), new Date((new Date).getFullYear(), 11, 25));
47
  assert.deepEqual(kivi.parse_date("12/25"), new Date((new Date).getFullYear(), 11, 25));
48

  
49
  assert.deepEqual(kivi.parse_date("Totally Invalid!"), undefined);
50
});
51

  
52
QUnit.test("kivi.parse_date function for ISO date style", function( assert ) {
53
  kivi.setup_formats({ dates: "yyyy-mm-dd" });
54

  
55
  assert.deepEqual(kivi.parse_date("2007-01-01"), new Date(2007, 0, 1));
56
  assert.deepEqual(kivi.parse_date("2007-1-1"), new Date(2007, 0, 1));
57
  assert.deepEqual(kivi.parse_date("      2007 - 1     -01   "), new Date(2007, 0, 1));
58

  
59
  assert.deepEqual(kivi.parse_date("2008-02-29"), new Date(2008, 1, 29));
60

  
61
  assert.deepEqual(kivi.parse_date("2014-12-11"), new Date(2014, 11, 11));
62

  
63
  assert.deepEqual(kivi.parse_date("-12-25"), new Date((new Date).getFullYear(), 11, 25));
64
});
65

  
66
QUnit.test("kivi.format_date function for German date style with dots", function( assert ) {
67
  kivi.setup_formats({ dates: "dd.mm.yy" });
68

  
69
  assert.deepEqual(kivi.format_date(new Date(2007, 0, 1)), "01.01.2007");
70
  assert.deepEqual(kivi.format_date(new Date(2008, 1, 29)), "29.02.2008");
71
  assert.deepEqual(kivi.format_date(new Date(2014, 11, 11)), "11.12.2014");
72

  
73
  assert.deepEqual(kivi.format_date(new Date(undefined, undefined, undefined)), undefined);
74
});
75

  
76
QUnit.test("kivi.format_date function for German date style with slashes", function( assert ) {
77
  kivi.setup_formats({ dates: "dd/mm/yy" });
78

  
79
  assert.deepEqual(kivi.format_date(new Date(2007, 0, 1)), "01/01/2007");
80
  assert.deepEqual(kivi.format_date(new Date(2008, 1, 29)), "29/02/2008");
81
  assert.deepEqual(kivi.format_date(new Date(2014, 11, 11)), "11/12/2014");
82

  
83
  assert.deepEqual(kivi.format_date(new Date(undefined, undefined, undefined)), undefined);
84
});
85

  
86
QUnit.test("kivi.format_date function for American date style", function( assert ) {
87
  kivi.setup_formats({ dates: "mm/dd/yy" });
88

  
89
  assert.deepEqual(kivi.format_date(new Date(2007, 0, 1)), "01/01/2007");
90
  assert.deepEqual(kivi.format_date(new Date(2008, 1, 29)), "02/29/2008");
91
  assert.deepEqual(kivi.format_date(new Date(2014, 11, 11)), "12/11/2014");
92

  
93
  assert.deepEqual(kivi.format_date(new Date(undefined, undefined, undefined)), undefined);
94
});
95

  
96
QUnit.test("kivi.format_date function for ISO date style", function( assert ) {
97
  kivi.setup_formats({ dates: "yyyy-mm-dd" });
98

  
99
  assert.deepEqual(kivi.format_date(new Date(2007, 0, 1)), "2007-01-01");
100
  assert.deepEqual(kivi.format_date(new Date(2008, 1, 29)), "2008-02-29");
101
  assert.deepEqual(kivi.format_date(new Date(2014, 11, 11)), "2014-12-11");
102

  
103
  assert.deepEqual(kivi.format_date(new Date(undefined, undefined, undefined)), undefined);
104
});
js/t/kivi/round_amount.js
1
QUnit.test("kivi.round_amount function, 0..2 places", function( assert ) {
2
  assert.equal(kivi.round_amount(1.05, 2), '1.05', '1.05 @ 2');
3
  assert.equal(kivi.round_amount(1.05, 1), '1.1',  '1.05 @ 1');
4
  assert.equal(kivi.round_amount(1.05, 0), '1',    '1.05 @ 0');
5

  
6
  assert.equal(kivi.round_amount(1.045, 2), '1.05', '1.045 @ 2');
7
  assert.equal(kivi.round_amount(1.045, 1), '1',    '1.045 @ 1');
8
  assert.equal(kivi.round_amount(1.045, 0), '1',    '1.045 @ 0');
9

  
10
  assert.equal(kivi.round_amount(33.675, 2), '33.68', '33.675 @ 2');
11
  assert.equal(kivi.round_amount(33.675, 1), '33.7',  '33.675 @ 1');
12
  assert.equal(kivi.round_amount(33.675, 0), '34',    '33.675 @ 0');
13

  
14
  assert.equal(kivi.round_amount(64.475, 2), '64.48', '64.475 @ 2');
15
  assert.equal(kivi.round_amount(64.475, 1), '64.5',  '64.475 @ 1');
16
  assert.equal(kivi.round_amount(64.475, 0), '64',    '64.475 @ 0');
17

  
18
  assert.equal(kivi.round_amount(44.9 * 0.75, 2), '33.68', '44.9 * 0.75 @ 2');
19
  assert.equal(kivi.round_amount(44.9 * 0.75, 1), '33.7',  '44.9 * 0.75 @ 1');
20
  assert.equal(kivi.round_amount(44.9 * 0.75, 0), '34',    '44.9 * 0.75 @ 0');
21

  
22
  assert.equal(kivi.round_amount(143.20, 2), '143.2', '143.20 @ 2');
23
  assert.equal(kivi.round_amount(143.20, 1), '143.2', '143.20 @ 1');
24
  assert.equal(kivi.round_amount(143.20, 0), '143',   '143.20 @ 0');
25

  
26
  assert.equal(kivi.round_amount(149.175, 2), '149.18', '149.175 @ 2');
27
  assert.equal(kivi.round_amount(149.175, 1), '149.2',  '149.175 @ 1');
28
  assert.equal(kivi.round_amount(149.175, 0), '149',    '149.175 @ 0');
29

  
30
  assert.equal(kivi.round_amount(198.90 * 0.75, 2), '149.18', '198.90 * 0.75 @ 2');
31
  assert.equal(kivi.round_amount(198.90 * 0.75, 1), '149.2',  '198.90 * 0.75 @ 1');
32
  assert.equal(kivi.round_amount(198.90 * 0.75, 0), '149',    '198.90 * 0.75 @ 0');
33
});
34

  
35
QUnit.test("kivi.round_amount function, 0..5 places", function( assert ) {
36
  assert.equal(kivi.round_amount(64.475499, 5), '64.4755', '64.475499 @ 5');
37
  assert.equal(kivi.round_amount(64.475499, 4), '64.4755', '64.475499 @ 4');
38
  assert.equal(kivi.round_amount(64.475499, 3), '64.475',  '64.475499 @ 3');
39
  assert.equal(kivi.round_amount(64.475499, 2), '64.48',   '64.475499 @ 2');
40
  assert.equal(kivi.round_amount(64.475499, 1), '64.5',    '64.475499 @ 1');
41
  assert.equal(kivi.round_amount(64.475499, 0), '64',      '64.475499 @ 0');
42

  
43
  assert.equal(kivi.round_amount(64.475999, 5), '64.476', '64.475999 @ 5');
44
  assert.equal(kivi.round_amount(64.475999, 4), '64.476', '64.475999 @ 4');
45
  assert.equal(kivi.round_amount(64.475999, 3), '64.476', '64.475999 @ 3');
46
  assert.equal(kivi.round_amount(64.475999, 2), '64.48',  '64.475999 @ 2');
47
  assert.equal(kivi.round_amount(64.475999, 1), '64.5',   '64.475999 @ 1');
48
  assert.equal(kivi.round_amount(64.475999, 0), '64',     '64.475999 @ 0');
49
});
50

  
51
QUnit.test("kivi.round_amount function, negative values", function( assert ) {
52
  // Negative values
53
  assert.equal(kivi.round_amount(-1.05, 2), '-1.05', '-1.05 @ 2');
54
  assert.equal(kivi.round_amount(-1.05, 1), '-1.1',  '-1.05 @ 1');
55
  assert.equal(kivi.round_amount(-1.05, 0), '-1',    '-1.05 @ 0');
56

  
57
  assert.equal(kivi.round_amount(-1.045, 2), '-1.05', '-1.045 @ 2');
58
  assert.equal(kivi.round_amount(-1.045, 1), '-1',    '-1.045 @ 1');
59
  assert.equal(kivi.round_amount(-1.045, 0), '-1',    '-1.045 @ 0');
60

  
61
  assert.equal(kivi.round_amount(-33.675, 2), '-33.68', '33.675 @ 2');
62
  assert.equal(kivi.round_amount(-33.675, 1), '-33.7',  '33.675 @ 1');
63
  assert.equal(kivi.round_amount(-33.675, 0), '-34',    '33.675 @ 0');
64

  
65
  assert.equal(kivi.round_amount(-44.9 * 0.75, 2), '-33.68', '-44.9 * 0.75 @ 2');
66
  assert.equal(kivi.round_amount(-44.9 * 0.75, 1), '-33.7',  '-44.9 * 0.75 @ 1');
67
  assert.equal(kivi.round_amount(-44.9 * 0.75, 0), '-34',    '-44.9 * 0.75 @ 0');
68

  
69
  assert.equal(kivi.round_amount(-149.175, 2), '-149.18', '-149.175 @ 2');
70
  assert.equal(kivi.round_amount(-149.175, 1), '-149.2',  '-149.175 @ 1');
71
  assert.equal(kivi.round_amount(-149.175, 0), '-149',    '-149.175 @ 0');
72

  
73
  assert.equal(kivi.round_amount(-198.90 * 0.75, 2), '-149.18', '-198.90 * 0.75 @ 2');
74
  assert.equal(kivi.round_amount(-198.90 * 0.75, 1), '-149.2',  '-198.90 * 0.75 @ 1');
75
  assert.equal(kivi.round_amount(-198.90 * 0.75, 0), '-149',    '-198.90 * 0.75 @ 0');
76
});
77

  
78
QUnit.test("kivi.round_amount function, programmatic tests of all 0..1000", function( assert ) {
79
  $([ -1, 1 ]).each(function(dummy, sign) {
80
    for (var idx = 0; idx < 1000; idx++) {
81
      var num = (9900000 * sign) + idx;
82
      var str = "" + num;
83
      str     = str.substr(0, str.length - 2) + "." + str.substr(str.length - 2, 2);
84
      str     = str.replace(/0+$/, '').replace(/\.$/, '');
85

  
86
      num /= 100;
87
      num /= 5;
88
      num /= 3;
89
      num *= 5;
90
      num *= 3;
91

  
92
      assert.equal("" + kivi.round_amount(num, 2), str);
93
    }
94
  });
95
});
96

  
97
QUnit.test("kivi.round_amount function, up to 10 digits of Pi", function( assert ) {
98
  // round to any digit we like
99
  assert.equal(kivi.round_amount(Math.PI, 0),  '3',             "0 digits of π");
100
  assert.equal(kivi.round_amount(Math.PI, 1),  '3.1',           "1 digit of π");
101
  assert.equal(kivi.round_amount(Math.PI, 2),  '3.14',          "2 digits of π");
102
  assert.equal(kivi.round_amount(Math.PI, 3),  '3.142',         "3 digits of π");
103
  assert.equal(kivi.round_amount(Math.PI, 4),  '3.1416',        "4 digits of π");
104
  assert.equal(kivi.round_amount(Math.PI, 5),  '3.14159',       "5 digits of π");
105
  assert.equal(kivi.round_amount(Math.PI, 6),  '3.141593',      "6 digits of π");
106
  assert.equal(kivi.round_amount(Math.PI, 7),  '3.1415927',     "7 digits of π");
107
  assert.equal(kivi.round_amount(Math.PI, 8),  '3.14159265',    "8 digits of π");
108
  assert.equal(kivi.round_amount(Math.PI, 9),  '3.141592654',   "9 digits of π");
109
  assert.equal(kivi.round_amount(Math.PI, 10), '3.1415926536', "10 digits of π");
110
});
111

  
112
QUnit.test("kivi.round_amount function, other weird cases", function( assert ) {
113
  // A LOT of places:
114
  assert.equal(kivi.round_amount(1.2, 200), '1.2', '1.2 @ 200');
115
});
js/t/kivi/setup_formats.js
1
QUnit.test("kivi.setup_formats date format initialization", function( assert ) {
2
  kivi.setup_formats({ dates: "dd.mm.yy" });
3
  assert.deepEqual(kivi._date_format, { sep: '.', d: 0, m: 1, y: 2 } , "German date style with dots");
4

  
5
  kivi.setup_formats({ dates: "dd/mm/yy" });
6
  assert.deepEqual(kivi._date_format, { sep: '/', d: 0, m: 1, y: 2 } , "German date style with slashes");
7

  
8
  kivi.setup_formats({ dates: "mm/dd/yy" });
9
  assert.deepEqual(kivi._date_format, { sep: '/', d: 1, m: 0, y: 2 } , "American date style");
10

  
11
  kivi.setup_formats({ dates: "yyyy-mm-dd" });
12
  assert.deepEqual(kivi._date_format, { sep: '-', d: 2, m: 1, y: 0 } , "ISO date style");
13

  
14
  kivi.setup_formats({ dates: "dd.mm.yy" });
15
  kivi.setup_formats({ dates: "Totally invalid!" });
16
  assert.deepEqual(kivi._date_format, { sep: '.', d: 0, m: 1, y: 2 } , "Invalid date style should not change previously-set style");
17
});
18

  
19
QUnit.test("kivi.setup_formats number format initialization", function( assert ) {
20
  kivi.setup_formats({ numbers: "1.000,00" });
21
  assert.deepEqual(kivi._number_format, { decimalSep: ',', thousandSep: '.' } , "German number style with thousands separator");
22

  
23
  kivi.setup_formats({ numbers: "1000,00" });
24
  assert.deepEqual(kivi._number_format, { decimalSep: ',', thousandSep: '' } , "German number style without thousands separator");
25

  
26
  kivi.setup_formats({ numbers: "1,000.00" });
27
  assert.deepEqual(kivi._number_format, { decimalSep: '.', thousandSep: ',' } , "English number style with thousands separator");
28

  
29
  kivi.setup_formats({ numbers: "1000.00" });
30
  assert.deepEqual(kivi._number_format, { decimalSep: '.', thousandSep: '' } , "English number style without thousands separator");
31

  
32
  kivi.setup_formats({ numbers: "1.000,00" });
33
  kivi.setup_formats({ numbers: "Totally invalid!" });
34
  assert.deepEqual(kivi._number_format, { decimalSep: ',', thousandSep: '.' } , "Invalid number style should not change previously-set style");
35
});
templates/webpages/layout/javascript_setup.js
16 16
      buttonImageOnly: true
17 17
  }));
18 18

  
19
  kivi.setup_formats({
20
    numbers: '[% JavaScript.escape(MYCONFIG.numberformat) %]',
21
    dates:   '[% JavaScript.escape(MYCONFIG.dateformat) %]'
22
  });
23

  
19 24
  kivi.reinit_widgets();
20 25
[% END %]
21 26

  

Auch abrufbar als: Unified diff