Validator: time - striktere Prüfung und tests
authorSven Schöling <s.schoeling@linet-services.de>
Fri, 23 Feb 2018 14:32:36 +0000 (15:32 +0100)
committerSven Schöling <s.schoeling@linet-services.de>
Fri, 23 Feb 2018 14:34:02 +0000 (15:34 +0100)
js/kivi.js
js/t/kivi/parse_format_time.js

index 7c1f00a..4448921 100644 (file)
@@ -138,6 +138,10 @@ namespace("kivi", function(ns) {
 
     var parts = time.replace(/\s+/g, "").split(ns._time_format.sep);
     if (parts.length == 2) {
+      for (var idx in parts) {
+        if (Number.isNaN(Number.parseInt(parts[idx])))
+          return undefined;
+      }
       now.setHours(parts[ns._time_format.h], parts[ns._time_format.m]);
       return now;
     } else
index 901de64..393d790 100644 (file)
@@ -5,25 +5,43 @@ function custom_time(h,m) {
 }
 
 QUnit.test("kivi.parse_time function for German time style with colon", function( assert ) {
+  assert.equalTimes = function(actual, expected, message) {
+    console.log(this);
+    var result = (expected === undefined && actual === undefined)
+                || (expected === null      && actual === null)
+                || (expected instanceof Date && actual instanceof Date &&
+                    expected.getHours()   == actual.getHours() &&
+                    expected.getMinutes() == actual.getMinutes());
+
+    this.push( {
+        result: result,
+        actual: actual,
+        expected: expected,
+        message: message
+    } );
+  }
+
   kivi.setup_formats({ times: "hh:mm" });
 
-  assert.deepEqual(kivi.parse_time("12:34"), custom_time(12,34));
-  assert.deepEqual(kivi.parse_time("10:00"), custom_time(10,0));
-  assert.deepEqual(kivi.parse_time("      12 :  23  "), custom_time(12,23));
+  assert.equalTimes(kivi.parse_time("12:34"), custom_time(12,34));
+  assert.equalTimes(kivi.parse_time("10:00"), custom_time(10,0));
+  assert.equalTimes(kivi.parse_time("      12 :  23  ") - custom_time(12,23));
 
-  assert.deepEqual(kivi.parse_time("00:20"), custom_time(0,20));
+  assert.equalTimes(kivi.parse_time("00:20"), custom_time(0,20));
 
-  assert.deepEqual(kivi.parse_time("23:60"), custom_time(23,60));
+  assert.equalTimes(kivi.parse_time("23:60"), custom_time(23,60));
 
-  assert.deepEqual(kivi.parse_time("1142"), custom_time(11,42));
+  assert.equalTimes(kivi.parse_time("1142"), custom_time(11,42));
 
-  assert.deepEqual(kivi.parse_time("Totally Invalid!"), undefined);
-  assert.deepEqual(kivi.parse_time("."), undefined);
-  assert.deepEqual(kivi.parse_time(".."), undefined);
-  assert.deepEqual(kivi.parse_time(":"), custom_time(0,0));
-  assert.deepEqual(kivi.parse_time("::"), undefined);
-  assert.deepEqual(kivi.parse_time(""), null);
-  assert.deepEqual(kivi.parse_time("0"), new Date());
-  assert.deepEqual(kivi.parse_time("29:20008"), custom_time(29,20008));
+  assert.equalTimes(kivi.parse_time("Totally Invalid!"), undefined);
+  assert.equalTimes(kivi.parse_time("."), undefined);
+  assert.equalTimes(kivi.parse_time(".."), undefined);
+  assert.equalTimes(kivi.parse_time(":"), undefined);
+  assert.equalTimes(kivi.parse_time("::"), undefined);
+  assert.equalTimes(kivi.parse_time("aa:bb"), undefined);
+  assert.equalTimes(kivi.parse_time("aasd:bbaf"), undefined);
+  assert.equalTimes(kivi.parse_time(""), null);
+  assert.equalTimes(kivi.parse_time("0"), new Date());
+  assert.equalTimes(kivi.parse_time("29:20008"), custom_time(29,20008));
 });