added utility function. given a list, creates a map of map[key] = obj, based on...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 5 Jan 2009 19:25:29 +0000 (19:25 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Mon, 5 Jan 2009 19:25:29 +0000 (19:25 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@11742 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/web/js/dojo/openils/Util.js

index 5f440fd..fbf22ef 100644 (file)
@@ -184,4 +184,24 @@ if(!dojo._hasResource["openils.Util"]) {
     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;
+    };
 }