[open-ils-commits] r11742 - trunk/Open-ILS/web/js/dojo/openils

svn at svn.open-ils.org svn at svn.open-ils.org
Mon Jan 5 14:25:33 EST 2009


Author: erickson
Date: 2009-01-05 14:25:29 -0500 (Mon, 05 Jan 2009)
New Revision: 11742

Modified:
   trunk/Open-ILS/web/js/dojo/openils/Util.js
Log:
added utility function.  given a list, creates a map of map[key] = obj, based on the selected key.  key can be a function or scalar "string" attribute

Modified: trunk/Open-ILS/web/js/dojo/openils/Util.js
===================================================================
--- trunk/Open-ILS/web/js/dojo/openils/Util.js	2009-01-05 16:49:04 UTC (rev 11741)
+++ trunk/Open-ILS/web/js/dojo/openils/Util.js	2009-01-05 19:25:29 UTC (rev 11742)
@@ -184,4 +184,24 @@
     openils.Util.isTrue = function(val) {
         return (val && val != '0' && !(val+'').match(/^f$/i));
     };
+
+    /**
+     * Turns a list into a mapped object.
+     * @param list The list
+     * @param pkey The field to use as the map key 
+     * @param isFunc If true, the map key field is an accessor function 
+     * that will return the value of the map key
+     */
+    openils.Util.mapList = function(list, pkey, isFunc) {
+        if(!(list && pkey)) 
+            return null;
+        var map = {};
+        for(var i in list) {
+            if(isFunc)
+                map[list[i][pkey]()] = list[i];
+            else
+                map[list[i][pkey]] = list[i];
+        }
+        return map;
+    };
 }



More information about the open-ils-commits mailing list