LP#1309717: Pad out short 00[678] user/miker/ff-pad-datafields
authorMike Rylander <mrylander@gmail.com>
Fri, 18 Apr 2014 19:37:54 +0000 (15:37 -0400)
committerMike Rylander <mrylander@gmail.com>
Fri, 18 Apr 2014 19:37:54 +0000 (15:37 -0400)
There are ways (many) where the 00X fields of a record may be saved
without trailing spaces to pad out the correct length. This causes
problems for the fixed field editor grid, which uses the substring()
method to read and stitch together the data in the fixed fields.
Specifically, if trailing space is truncated, both read and insertion
can fail in annoying ways.

To fix this, we should always pad out the 00X fields to their
appropriate lengths before reading or modifying them.

Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/web/js/dojo/MARC/FixedFields.js

index b4df704..ab23483 100644 (file)
@@ -34,7 +34,22 @@ if(!dojo._hasResource["MARC.FixedFields"]) {
         MFHD : { Type : /[uvxy]{1}/,  BLvl : /.{1}/ }
     };
 
-    MARC.Record._ff_pos = {
+    // See: http://jsperf.com/repeating-strings/11 -- stringFill4()
+    MARC.Record._padString = function (result, n, x) {
+        n = n - result.length;
+        if (!x) x = ' ';
+
+        for (var pad = '';;) {
+            if (n & 1) pad += x;
+            if (n >>= 1) x += x;
+            else break;
+        }
+
+        if (pad) result += pad;
+        return result;
+    }
+
+   MARC.Record._ff_pos = {
         AccM : {
             _8 : {
                 SCO : {start: 24, len : 6, def : ' ' },
@@ -1672,6 +1687,7 @@ if(!dojo._hasResource["MARC.FixedFields"]) {
     
     MARC.Record.prototype.videorecordingFormatName = function () {
         var _7 = this.field('007').data;
+        _7 = MARC.Record._padString(_7, 23);
     
         if (_7 && _7.match(/^v/)) {
             var _v_e = _7.substr(
@@ -1687,6 +1703,7 @@ if(!dojo._hasResource["MARC.FixedFields"]) {
     
     MARC.Record.prototype.videorecordingFormatCode = function () {
         var _7 = this.field('007').data;
+        _7 = MARC.Record._padString(_7, 23);
     
         if (_7 && _7.match(/^v/)) {
             return _7.substr(
@@ -1705,6 +1722,9 @@ if(!dojo._hasResource["MARC.FixedFields"]) {
         var _8 = this.field('008').data;
         var _6 = this.field('006').data;
     
+        _8 = MARC.Record._padString(_8, 40);
+        _6 = MARC.Record._padString(_6, 18);
+
         var rtype = this.recordType();
     
         var val;
@@ -1771,6 +1791,9 @@ if(!dojo._hasResource["MARC.FixedFields"]) {
         var _l = this.leader;
         var _8 = this.field('008').data;
         var _6 = this.field('006').data;
+
+        _8 = MARC.Record._padString(_8, 40);
+        _6 = MARC.Record._padString(_6, 18);
     
         var rtype = this.recordType();