/** * Returns all of the property names (keys) of the specified object (a map). The * order of the returned array is not defined. * * @param map an object. * @returns {string[]} an array of strings corresponding to the keys. * @see #entries */ pv.keys = function(map) { var array = []; for (var key in map) { array.push(key); } return array; }; /** * Returns all of the entries (key-value pairs) of the specified object (a * map). The order of the returned array is not defined. Each key-value pair is * represented as an object with key and value attributes, * e.g., {key: "foo", value: 42}. * * @param map an object. * @returns {array} an array of key-value pairs corresponding to the keys. */ pv.entries = function(map) { var array = []; for (var key in map) { array.push({ key: key, value: map[key] }); } return array; }; /** * Returns all of the values (attribute values) of the specified object (a * map). The order of the returned array is not defined. * * @param map an object. * @returns {array} an array of objects corresponding to the values. * @see #entries */ pv.values = function(map) { var array = []; for (var key in map) { array.push(map[key]); } return array; }; /** * Returns a map constructed from the specified keys, using the * function f to compute the value for each key. The single argument to * the value function is the key. The callback is invoked only for indexes of * the array which have assigned values; it is not invoked for indexes which * have been deleted or which have never been assigned values. * *

For example, this expression creates a map from strings to string length: * *

pv.dict(["one", "three", "seventeen"], function(s) s.length)
* * The returned value is {one: 3, three: 5, seventeen: 9}. Accessor * functions can refer to this.index. * * @param {array} keys an array. * @param {function} f a value function. * @returns a map from keys to values. */ pv.dict = function(keys, f) { var m = {}, o = {}; for (var i = 0; i < keys.length; i++) { if (i in keys) { var k = keys[i]; o.index = i; m[k] = f.call(o, k); } } return m; };