Commits

Luciano Ramalho committed b6a1974

refactored isisdm_t2 library using the JavaScript module pattern

  • Participants
  • Parent commits bb1c12f

Comments (0)

Files changed (9)

File lilacs/_attachments/tests.html

 	<link rel="stylesheet" href="vendor/qunit/qunit.css" type="text/css" media="screen">
 	<script type="text/javascript" src="vendor/qunit/qunit.js"></script>
 	<!-- Your project file goes here -->
-	<script type="text/javascript" src="vendor/isisdm_t2/get.js"></script>
-	<script type="text/javascript" src="vendor/isisdm_t2/getall.js"></script>
-	<script type="text/javascript" src="vendor/isisdm_t2/getsub.js"></script>
-	<script type="text/javascript" src="vendor/isisdm_t2/getallsub.js"></script>
+	<script type="text/javascript" src="vendor/isisdm_t2/isisdm.js"></script>
 	<!-- Your tests file goes here -->
 	<script type="text/javascript" src="vendor/isisdm_t2/tests.js"></script>
 </head>

File lilacs/vendor/isisdm_t2/_attachments/get.js

-var get = function(record, tag, missing) {
-    // assuming an ISIS-JSON type 2 record, returns the first occurrence
-    // of a field identified by tag; otherwise returns value of missing
-    var first_occ, content, i;
-    if (record.hasOwnProperty(tag)) {
-        first_occ = record[tag][0];
-        content = (first_occ[0][0]==='_') ? [first_occ[0][1]] : [];
-        for (i=content.length; i<first_occ.length; i++) {
-            content.push('^'+first_occ[i][0]+first_occ[i][1]);
-        }
-        return content.join(" ");
-    }
-    else {
-        return missing;
-    }
-};

File lilacs/vendor/isisdm_t2/_attachments/getall.js

-var getall = function(record, tag) {
-    // assuming an ISIS-JSON type 2 record, returns the first occurrence
-    // of a field identified by tag; otherwise returns value of missing
-    var all, occur, content, i, j;
-    if (record.hasOwnProperty(tag)) {
-        all = [];
-        for (i=0; i<record[tag].length; i++) {
-            occur = record[tag][i];
-            content = (occur[0][0]==='_') ? [occur[0][1]] : [];
-            for (j=content.length; j<occur.length; j++) {
-                content.push('^'+occur[j][0]+occur[j][1]);
-            }
-            all.push(content.join(" "));
-        }
-        return all;
-    }
-    else {
-        return [];
-    }
-};

File lilacs/vendor/isisdm_t2/_attachments/getallsub.js

-var getallsub = function(record, tag, key, missing) {
-    var all, occur, subf, i, j;
-    if (record.hasOwnProperty(tag)) {
-        all = [];
-        for (i=0; i<record[tag].length; i++) {
-            occur = record[tag][i];
-            subf = missing;
-            for (j=0; j<occur.length; j++) {
-                if (occur[j][0] == key || "*" === key) {
-                    subf = occur[j][1];
-                    break;
-                }
-            }
-            all.push(subf);
-        }
-        return all;
-    }
-    else {
-        return [];
-    }
-};

File lilacs/vendor/isisdm_t2/_attachments/getsub.js

-var getsub = function(record, tag, key, missing) {
-    // assuming an ISIS-JSON type 2 record, returns the subfield
-    // identified by the key from the first occurrence of a field
-    // identified by the tag; otherwise returns value of missing;
-    // the special key "*" returns the first subfield, which may or may
-    // not be the main subfield "_"
-    var first_occ, i;
-    if (record.hasOwnProperty(tag)) {
-        first_occ = record[tag][0];
-        for (i=0; i<first_occ.length; i++) {
-            if (first_occ[i][0] == key || "*" === key) {
-                return first_occ[i][1];
-            }
-        }
-    }
-    return missing;
-};

File lilacs/vendor/isisdm_t2/_attachments/isisdm.js

+var ISIS = (function () { 
+    var my = {};
+
+    function getonesub(field, key, missing) {
+        var i;
+        for (i=0; i<field.length; i++) {
+            // the == below is intentional, so that a key 1 matches "1", as
+            // it would when matching a key in a JavaScript object
+            if ((key == field[i][0]) || (key === "*")) {
+                return field[i][1];
+            }
+        }
+        return missing;
+    };
+    
+    my.getallraw = function(record, tag, stop) {
+        if (! (tag in record)) return [];
+        if (typeof stop === "number") {
+            return record[tag].slice(0, stop);
+        } else {
+            return record[tag];
+        }
+    }
+
+    my.get = function(record, tag, missing) {
+        // assuming an ISIS-JSON type 2 record, returns the first occurrence
+        // of a field identified by tag; otherwise returns missing value;
+        var fields = my.getall(record, tag, 1);
+        return fields.length > 0 ? fields[0] : missing;
+    };
+    my.getall = function(record, tag, stop) {
+        // assuming an ISIS-JSON type 2 record, returns the every occurrence
+        // of a field identified by tag; otherwise returns value of missing;
+        // the optional stop argument limits the number of occurrences returned;
+        var fields, field, res, i, j;
+        fields = my.getallraw(record, tag, stop);
+        res = [];
+        for (i=0; i<fields.length; i++) {
+            field = fields[i];
+            content = (field[0][0]==='_') ? [field[0][1]] : [];
+            for (j=content.length; j<field.length; j++) {
+                content.push('^'+field[j][0]+field[j][1]);
+            }
+            res.push(content.join(" "));
+        }
+        return res;
+    };
+
+    my.getallsub = function(record, tag, key, missing) {
+        var fields, res, i;
+        fields = my.getallraw(record, tag);
+        res = [];
+        for (i=0; i<fields.length; i++) {
+            res.push(getonesub(fields[i], key, missing));
+        }
+        return res;
+    };
+
+    my.getsub = function(record, tag, key, missing) {
+        var fields = my.getallraw(record, tag, 1);
+        return fields.length > 0 ? getonesub(fields[0], key, missing) : missing;
+    }
+    
+    
+    return my;
+}());

File lilacs/vendor/isisdm_t2/_attachments/tests.js

           ]
 }
 test('get(record, tag, missing)', function() {
-    equals(get(rec, "2"),'538886', 'get(rec, "2")');
-    equals(get(rec, "10"),'Kanda, Paulo Afonso ^1USP ^2FMUSP ^3CRDC ^pBrasil ^cSão Paulo ^rorg', 'get(rec, "10")');
-    equals(get(rec, "900"),'^aAlpha ^bBeta', 'get(rec, "900")');
-    equals(get(rec, "999"),undefined, 'get(rec, "999")');
-    equals(get(rec, "999", '?'), '?', 'get(rec, "999")');
+    equals(ISIS.get(rec, "2"),'538886', 'get(rec, "2")');
+    equals(ISIS.get(rec, "10"),'Kanda, Paulo Afonso ^1USP ^2FMUSP ^3CRDC ^pBrasil ^cSão Paulo ^rorg', 'get(rec, "10")');
+    equals(ISIS.get(rec, "900"),'^aAlpha ^bBeta', 'get(rec, "900")');
+    equals(ISIS.get(rec, "999"),undefined, 'get(rec, "999")');
+    equals(ISIS.get(rec, "999", '?'), '?', 'get(rec, "999")');
 });
 test('getall(record, tag)', function() {
-    same(getall(rec, "2"),['538886'], 'getall(rec, "2")');
-    same(getall(rec, "4"),["LILACS", "LLXPEDT"], 'getall(rec, "4")');
-    same(getall(rec, "900"),['^aAlpha ^bBeta', '^aananas ^bbanana'], 'getall(rec, "900")');
-    same(getall(rec, "999"), [], 'getall(rec, "999")');
+    same(ISIS.getall(rec, "2"),['538886'], 'getall(rec, "2")');
+    same(ISIS.getall(rec, "4"),["LILACS", "LLXPEDT"], 'getall(rec, "4")');
+    same(ISIS.getall(rec, "4", 1),["LILACS"], 'getall(rec, "4", 1)');
+    same(ISIS.getall(rec, "4", 100),["LILACS", "LLXPEDT"], 'getall(rec, "4", 100)');
+    same(ISIS.getall(rec, "900"),['^aAlpha ^bBeta', '^aananas ^bbanana'], 'getall(rec, "900")');
+    same(ISIS.getall(rec, "999"), [], 'getall(rec, "999")');
 });
 test('getsub(record, tag, key, missing)', function() {
-    equals(getsub(rec, "10", "p"), 'Brasil', 'getsub(rec, "10", "p")');
-    equals(getsub(rec, 10, "_"), 'Kanda, Paulo Afonso', 'getsub(rec, 10, "_")');
-    equals(getsub(rec, 10, "*"), 'Kanda, Paulo Afonso', 'getsub(rec, 10, "*")');
-    equals(getsub(rec, 900, "*"), 'Alpha', 'getsub(rec, 900, "*")');
-    equals(getsub(rec, "999", "x"),undefined, 'getsub(rec, "999", "x")');
-    equals(getsub(rec, "999", "x", ""), "", 'getsub(rec, "999", "x", "")');
+    equals(ISIS.getsub(rec, "10", "p"), 'Brasil', 'getsub(rec, "10", "p")');
+    equals(ISIS.getsub(rec, 10, "_"), 'Kanda, Paulo Afonso', 'getsub(rec, 10, "_")');
+    equals(ISIS.getsub(rec, 10, "*"), 'Kanda, Paulo Afonso', 'getsub(rec, 10, "*")');
+    equals(ISIS.getsub(rec, 900, "*"), 'Alpha', 'getsub(rec, 900, "*")');
+    equals(ISIS.getsub(rec, "999", "x"),undefined, 'getsub(rec, "999", "x")');
+    equals(ISIS.getsub(rec, "999", "x", ""), "", 'getsub(rec, "999", "x", "")');
 });
 test('getallsub(record, tag, key, missing)', function() {
-    same(getallsub(rec, 2, "_"),['538886'], 'getallsub(rec, 2, "_")');
-    same(getallsub(rec, 900, "b"),['Beta', 'banana'], 'getallsub(rec, 900, "b")');
-    same(getallsub(rec, 910, "a"),['Alpha', undefined], 'getallsub(rec, 910, "a")');
+    same(ISIS.getallsub(rec, 2, "_"),['538886'], 'getallsub(rec, 2, "_")');
+    same(ISIS.getallsub(rec, 900, "b"),['Beta', 'banana'], 'getallsub(rec, 900, "b")');
+    same(ISIS.getallsub(rec, 910, "a"),['Alpha', undefined], 'getallsub(rec, 910, "a")');
 });
 
 

File lilacs/views/affiliation/map.js

 function(doc) {
-    // !code vendor/isisdm_t2/_attachments/getallsub.js
+    // !code vendor/isisdm_t2/_attachments/isisdm.js
 
-    var i, occurs = getallsub(doc, 10, 1, '??');
+    var i, occurs = ISIS.getallsub(doc, 10, "1", '??');
 
     for (i=0; i<occurs.length; i++) {
         emit(occurs[i].trim(), 1);

File lilacs/views/countries/map.js

 function(doc) {
-    // !code vendor/isisdm_t2/_attachments/get.js
+    // !code vendor/isisdm_t2/_attachments/isisdm.js
 
-    emit(get(doc, 67, '??'), 1);
+    emit(ISIS.get(doc, 67, '??'), 1);
 }