Reusable Accessible Mapping Platform

API Docs for: 3.0.0
Show:

File: src\js\RAMP\Utils\dictionary.js

  1. /* global define */
  2.  
  3. /**
  4. * Utility module containint useful static classes.
  5. *
  6. * @module Utils
  7. */
  8.  
  9. /**
  10. * A set of useful functions for manipulating dictionaries.
  11. *
  12. *
  13. * @class Dictionary
  14. * @static
  15. * @uses dojo/_base/array
  16. * @uses dojo/_base/lang
  17. * @uses Util
  18. */
  19. define([
  20. "dojo/_base/array", "dojo/_base/lang",
  21. "utils/util"
  22. ],
  23.  
  24. function (
  25. dojoArray, dojoLang,
  26. UtilMisc) {
  27. "use strict";
  28.  
  29. return {
  30. /**
  31. * Iterates over the key-value pair in the given dictionary (in arbitrary order) and
  32. * calls the given function on each key-value pair.
  33. *
  34. * @method forEachEntry
  35. * @static
  36. * @param {Object} dict a dictionary object
  37. * @param {Function} fcn a function that takes the following parameters:
  38. * - the `key`
  39. * - the `value`
  40. * - `obj` (an optional parameter) that contains two fields, exit and returnVal. If exit is set to true,
  41. * forEachEntry terminates right away (without iterating over the rest of the dictionary). The
  42. * return value of forEackKeyValuePair is returnVal (which by default is undefined).</param>
  43. * @param {Function} [sortFcn] true if the dictionary keys are to be sorted before iterating
  44. * @param {Object} [scope] The scope to be hitched to the given fcn
  45. */
  46. forEachEntry: function (dict, fcn, sortFcn, scope) {
  47. if (!UtilMisc.isUndefined(scope)) {
  48. fcn = dojoLang.hitch(scope, fcn);
  49. }
  50.  
  51. var keys = [];
  52.  
  53. for (var key in dict) {
  54. if (dict.hasOwnProperty(key)) {
  55. keys.push(key);
  56. }
  57. }
  58.  
  59. if (sortFcn) {
  60. keys.sort(sortFcn);
  61. }
  62.  
  63. dojoArray.forEach(keys, function (key) {
  64. fcn(key, dict[key]);
  65. });
  66. },
  67.  
  68. /**
  69. * Iterates over the key-value pair in the given dictionary and
  70. * returns the index of the first element to cause the given fcn to return
  71. * true.
  72. *
  73. * @method find
  74. * @static
  75. * @param {Object} dict a dictionary object
  76. * @param {Function} fcn a function that takes the following parameters:
  77. * - the `key`
  78. * - the `value`
  79. * and returns true if the given key or value satisfy some condition
  80. * @param {Function} [compareFcn] a comparator function (takes two arguments and returns an integer) used to sort the keys
  81. */
  82. find: function (dict, fcn, compareFcn) {
  83. var keys = [];
  84.  
  85. for (var key in dict) {
  86. if (dict.hasOwnProperty(key)) {
  87. keys.push(key);
  88. }
  89. }
  90.  
  91. if (compareFcn) {
  92. keys.sort(compareFcn);
  93. }
  94.  
  95. var index = -1;
  96. dojoArray.some(keys, function (key, i) {
  97. if (fcn(key, dict[key])) {
  98. index = i;
  99. return true;
  100. }
  101. });
  102.  
  103. return index;
  104. },
  105.  
  106. filter: function (dict, predicate) {
  107. var filteredDict = {};
  108. this.forEachEntry(dict, function (key, value) {
  109. if (predicate(key, value)) {
  110. filteredDict[key] = value;
  111. }
  112. });
  113. return filteredDict;
  114. },
  115.  
  116. /**
  117. * Returns the number of keys in the given dictionary.
  118. *
  119. * @method length
  120. * @param {Object} dict a dictionary
  121. * @return {Integer} the number of keys in the dictionary
  122. * @static
  123. */
  124. length: function (dict) {
  125. return Object.keys(dict).length;
  126. },
  127.  
  128. /**
  129. * Returns true if the given dictionary is empty (i.e. has no keys)
  130. *
  131. * @method isEmpty
  132. * @static
  133. * @param {Object} dict a dictionary
  134. * @return {Boolean} true if the dictionary is empty, false otherwise
  135. */
  136. isEmpty: function (dict) {
  137. return this.length(dict) === 0;
  138. },
  139.  
  140. /**
  141. * Returns a shallow copy of the given dictionary.
  142. *
  143. * @static
  144. * @method clone
  145. * @param {Object} dict a dictionary
  146. * @return {Object} a shallow copy of the given dictionary
  147. */
  148. clone: function (dict) {
  149. var copy = {};
  150. this.forEachEntry(dict, function (key, value) {
  151. copy[key] = value;
  152. });
  153. return copy;
  154. },
  155.  
  156. /**
  157. * Converts the given array into a dictionary, where the values of the dictionary are values in
  158. * the array, and the keys are the return value of the given function.
  159. *
  160. * @static
  161. * @method arrayToMap
  162. * @param {Object} arr an array
  163. * @param {Function} fcn a function that takes one argument and returns a key for the given argument
  164. * @return {Object} a dictionary
  165. */
  166. arrayToMap: function (arr, fcn) {
  167. var dict = {};
  168. dojoArray.forEach(arr, function (element) {
  169. dict[fcn(arr)] = element;
  170. });
  171. return dict;
  172. },
  173.  
  174. /**
  175. * Convert the given array into a dictionary, where the keys of the dictionary are the elements in the
  176. * first array and the values of the dictionary are elements in the second array. The size of the two arrays
  177. * must match.
  178. *
  179. * @static
  180. * @method zip
  181. * @param {Object} arr1 the key array
  182. * @param {Object} arr2 the value array
  183. * @return {Object} a dictionary
  184. */
  185. zip: function (arr1, arr2) {
  186. if (arr1.length !== arr2.length) {
  187. throw "Array lengths differ";
  188. }
  189.  
  190. var dict = {};
  191. dojoArray.forEach(arr1, function (element, i) {
  192. dict[element] = arr2[i];
  193. });
  194. return dict;
  195. }
  196. };
  197. });