1 /** 2 * Returns all of the property names (keys) of the specified object (a map). The 3 * order of the returned array is not defined. 4 * 5 * @param map an object. 6 * @returns {string[]} an array of strings corresponding to the keys. 7 * @see #entries 8 */ 9 pv.keys = function(map) { 10 var array = []; 11 for (var key in map) { 12 array.push(key); 13 } 14 return array; 15 }; 16 17 /** 18 * Returns all of the entries (key-value pairs) of the specified object (a 19 * map). The order of the returned array is not defined. Each key-value pair is 20 * represented as an object with <tt>key</tt> and <tt>value</tt> attributes, 21 * e.g., <tt>{key: "foo", value: 42}</tt>. 22 * 23 * @param map an object. 24 * @returns {array} an array of key-value pairs corresponding to the keys. 25 */ 26 pv.entries = function(map) { 27 var array = []; 28 for (var key in map) { 29 array.push({ key: key, value: map[key] }); 30 } 31 return array; 32 }; 33 34 /** 35 * Returns all of the values (attribute values) of the specified object (a 36 * map). The order of the returned array is not defined. 37 * 38 * @param map an object. 39 * @returns {array} an array of objects corresponding to the values. 40 * @see #entries 41 */ 42 pv.values = function(map) { 43 var array = []; 44 for (var key in map) { 45 array.push(map[key]); 46 } 47 return array; 48 }; 49 50 /** 51 * Returns a map constructed from the specified <tt>keys</tt>, using the 52 * function <tt>f</tt> to compute the value for each key. The single argument to 53 * the value function is the key. The callback is invoked only for indexes of 54 * the array which have assigned values; it is not invoked for indexes which 55 * have been deleted or which have never been assigned values. 56 * 57 * <p>For example, this expression creates a map from strings to string length: 58 * 59 * <pre>pv.dict(["one", "three", "seventeen"], function(s) s.length)</pre> 60 * 61 * The returned value is <tt>{one: 3, three: 5, seventeen: 9}</tt>. Accessor 62 * functions can refer to <tt>this.index</tt>. 63 * 64 * @param {array} keys an array. 65 * @param {function} f a value function. 66 * @returns a map from keys to values. 67 */ 68 pv.dict = function(keys, f) { 69 var m = {}, o = {}; 70 for (var i = 0; i < keys.length; i++) { 71 if (i in keys) { 72 var k = keys[i]; 73 o.index = i; 74 m[k] = f.call(o, k); 75 } 76 } 77 return m; 78 }; 79