LP#1402797 browser client interval parser
authorBill Erickson <berickxx@gmail.com>
Wed, 31 Dec 2014 20:28:48 +0000 (15:28 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 12 Feb 2015 17:03:08 +0000 (12:03 -0500)
Adds a new service on the core module for adding date handling
utilities.  Included in this commit is a new function:

egDate.intervalToSeconds(interval);

Includes Gruntfile additions and unit tests.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
Open-ILS/src/templates/staff/base_js.tt2
Open-ILS/web/js/ui/default/staff/Gruntfile.js
Open-ILS/web/js/ui/default/staff/services/coresvc.js
Open-ILS/web/js/ui/default/staff/services/date.js [new file with mode: 0644]
Open-ILS/web/js/ui/default/staff/test/karma.conf.js
Open-ILS/web/js/ui/default/staff/test/unit/egDate.js [new file with mode: 0644]

index 76bc5a3..9cf9d69 100644 (file)
@@ -30,6 +30,7 @@
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/navbar.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/statusbar.js"></script>
 <script src="[% ctx.media_prefix %]/js/ui/default/staff/services/ui.js"></script>
+<script src="[% ctx.media_prefix %]/js/ui/default/staff/services/date.js"></script>
 
 [% ELSE %]
 
index eb004dc..a3ba032 100644 (file)
@@ -102,6 +102,7 @@ module.exports = function(grunt) {
             'services/navbar.js',
             'services/statusbar.js',
             'services/ui.js',
+            'services/date.js',
         ],
         dest: 'build/js/<%= pkg.name %>.<%= pkg.version %>.min.js'
       }
index 6909978..de502cd 100644 (file)
@@ -9,9 +9,9 @@ angular.module('egCoreMod')
 
 .factory('egCore', 
        ['egIDL','egNet','egEnv','egOrg','egPCRUD','egEvent','egAuth',
-        'egPerm','egHatch','egPrint','egStartup','egStrings',
+        'egPerm','egHatch','egPrint','egStartup','egStrings','egDate',
 function(egIDL , egNet , egEnv , egOrg , egPCRUD , egEvent , egAuth , 
-         egPerm , egHatch , egPrint , egStartup , egStrings) {
+         egPerm , egHatch , egPrint , egStartup , egStrings , egDate) {
 
     return {
         idl     : egIDL,
@@ -25,7 +25,8 @@ function(egIDL , egNet , egEnv , egOrg , egPCRUD , egEvent , egAuth ,
         hatch   : egHatch,
         print   : egPrint,
         startup : egStartup,
-        strings : egStrings
+        strings : egStrings,
+        date    : egDate
     };
 
 }]);
diff --git a/Open-ILS/web/js/ui/default/staff/services/date.js b/Open-ILS/web/js/ui/default/staff/services/date.js
new file mode 100644 (file)
index 0000000..629b799
--- /dev/null
@@ -0,0 +1,61 @@
+/**
+ * Core Service - egDate
+ *
+ * Date utility functions.
+ *
+ */
+angular.module('egCoreMod')
+
+.factory('egDate', function() {
+
+    var service = {};
+
+    /**
+     * Converts an interval string to seconds.
+     *
+     * egDate.intervalToSeconds('1 min 2 seconds')) => 62
+     * egDate.intervalToSeconds('2 days')) => 172800
+     * egDate.intervalToSeconds('02:00:23')) => 7223
+     */
+    service.intervalToSeconds = function(interval) {
+        var d = new Date();
+        var start = d.getTime();
+        var parts = interval.split(' ');
+
+        for(var i = 0; i < parts.length; i += 2)  {
+
+            if (!parts[i+1]) {
+                // interval is a bare hour:min:sec string
+                var times = parts[i].split(':');
+                d.setHours(d.getHours() + Number(times[0]));
+                d.setMinutes(d.getMinutes() + Number(times[1]));
+                d.setSeconds(d.getSeconds() + Number(times[2]));
+                continue;
+            }
+
+            var count = Number(parts[i]);
+            var type = parts[i+1].replace(/s?,?$/,'');
+
+            if (type.match(/^s/)) {
+                d.setSeconds(d.getSeconds() + count);
+            } else if (type.match(/^min/)) {
+                d.setMinutes(d.getMinutes() + count);
+            } else if (type.match(/^h/)) {
+                d.setHours(d.getHours() + count);
+            } else if (type.match(/^d/)) {
+                d.setDate(d.getDate() + count);
+            } else if (type.match(/^mon/)) {
+                d.setMonth(d.getMonth() + count);
+            } else if (type.match(/^y/)) {
+                d.setFullYear(d.getFullYear() + count);
+            }
+        }
+
+        return Number((d.getTime() - start) / 1000);
+    }
+
+    return service;
+})
+
+
+
index a27ca66..5b9a272 100644 (file)
@@ -40,6 +40,7 @@ module.exports = function(config){
       'services/statusbar.js',
       'services/grid.js',
       'services/navbar.js',
+      'services/date.js',
       // load app scripts
       'app.js',
       'circ/**/*.js',
diff --git a/Open-ILS/web/js/ui/default/staff/test/unit/egDate.js b/Open-ILS/web/js/ui/default/staff/test/unit/egDate.js
new file mode 100644 (file)
index 0000000..f55fe9f
--- /dev/null
@@ -0,0 +1,18 @@
+'use strict';
+
+describe('egDate', function(){
+    beforeEach(module('egCoreMod'));
+
+    it('should parse a simple interval', inject(function(egDate) {
+        expect(egDate.intervalToSeconds('2 days')).toBe(172800);
+    }));
+
+    it('should parse a combined interval', inject(function(egDate) {
+        expect(egDate.intervalToSeconds('1 min 2 seconds')).toBe(62);
+    }));
+
+    it('should parse a time interval', inject(function(egDate) {
+        expect(egDate.intervalToSeconds('02:00:23')).toBe(7223);
+    }));
+
+});