structs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Generics method for collection-like classes and objects.
  16. *
  17. * @author arv@google.com (Erik Arvidsson)
  18. *
  19. * This file contains functions to work with collections. It supports using
  20. * Map, Set, Array and Object and other classes that implement collection-like
  21. * methods.
  22. */
  23. goog.provide('goog.structs');
  24. goog.require('goog.array');
  25. goog.require('goog.object');
  26. // We treat an object as a dictionary if it has getKeys or it is an object that
  27. // isn't arrayLike.
  28. /**
  29. * Returns the number of values in the collection-like object.
  30. * @param {Object} col The collection-like object.
  31. * @return {number} The number of values in the collection-like object.
  32. */
  33. goog.structs.getCount = function(col) {
  34. if (col.getCount && typeof col.getCount == 'function') {
  35. return col.getCount();
  36. }
  37. if (goog.isArrayLike(col) || goog.isString(col)) {
  38. return col.length;
  39. }
  40. return goog.object.getCount(col);
  41. };
  42. /**
  43. * Returns the values of the collection-like object.
  44. * @param {Object} col The collection-like object.
  45. * @return {!Array<?>} The values in the collection-like object.
  46. */
  47. goog.structs.getValues = function(col) {
  48. if (col.getValues && typeof col.getValues == 'function') {
  49. return col.getValues();
  50. }
  51. if (goog.isString(col)) {
  52. return col.split('');
  53. }
  54. if (goog.isArrayLike(col)) {
  55. var rv = [];
  56. var l = col.length;
  57. for (var i = 0; i < l; i++) {
  58. rv.push(col[i]);
  59. }
  60. return rv;
  61. }
  62. return goog.object.getValues(col);
  63. };
  64. /**
  65. * Returns the keys of the collection. Some collections have no notion of
  66. * keys/indexes and this function will return undefined in those cases.
  67. * @param {Object} col The collection-like object.
  68. * @return {!Array|undefined} The keys in the collection.
  69. */
  70. goog.structs.getKeys = function(col) {
  71. if (col.getKeys && typeof col.getKeys == 'function') {
  72. return col.getKeys();
  73. }
  74. // if we have getValues but no getKeys we know this is a key-less collection
  75. if (col.getValues && typeof col.getValues == 'function') {
  76. return undefined;
  77. }
  78. if (goog.isArrayLike(col) || goog.isString(col)) {
  79. var rv = [];
  80. var l = col.length;
  81. for (var i = 0; i < l; i++) {
  82. rv.push(i);
  83. }
  84. return rv;
  85. }
  86. return goog.object.getKeys(col);
  87. };
  88. /**
  89. * Whether the collection contains the given value. This is O(n) and uses
  90. * equals (==) to test the existence.
  91. * @param {Object} col The collection-like object.
  92. * @param {*} val The value to check for.
  93. * @return {boolean} True if the map contains the value.
  94. */
  95. goog.structs.contains = function(col, val) {
  96. if (col.contains && typeof col.contains == 'function') {
  97. return col.contains(val);
  98. }
  99. if (col.containsValue && typeof col.containsValue == 'function') {
  100. return col.containsValue(val);
  101. }
  102. if (goog.isArrayLike(col) || goog.isString(col)) {
  103. return goog.array.contains(/** @type {!Array<?>} */ (col), val);
  104. }
  105. return goog.object.containsValue(col, val);
  106. };
  107. /**
  108. * Whether the collection is empty.
  109. * @param {Object} col The collection-like object.
  110. * @return {boolean} True if empty.
  111. */
  112. goog.structs.isEmpty = function(col) {
  113. if (col.isEmpty && typeof col.isEmpty == 'function') {
  114. return col.isEmpty();
  115. }
  116. // We do not use goog.string.isEmptyOrWhitespace because here we treat the
  117. // string as
  118. // collection and as such even whitespace matters
  119. if (goog.isArrayLike(col) || goog.isString(col)) {
  120. return goog.array.isEmpty(/** @type {!Array<?>} */ (col));
  121. }
  122. return goog.object.isEmpty(col);
  123. };
  124. /**
  125. * Removes all the elements from the collection.
  126. * @param {Object} col The collection-like object.
  127. */
  128. goog.structs.clear = function(col) {
  129. // NOTE(arv): This should not contain strings because strings are immutable
  130. if (col.clear && typeof col.clear == 'function') {
  131. col.clear();
  132. } else if (goog.isArrayLike(col)) {
  133. goog.array.clear(/** @type {IArrayLike<?>} */ (col));
  134. } else {
  135. goog.object.clear(col);
  136. }
  137. };
  138. /**
  139. * Calls a function for each value in a collection. The function takes
  140. * three arguments; the value, the key and the collection.
  141. *
  142. * @param {S} col The collection-like object.
  143. * @param {function(this:T,?,?,S):?} f The function to call for every value.
  144. * This function takes
  145. * 3 arguments (the value, the key or undefined if the collection has no
  146. * notion of keys, and the collection) and the return value is irrelevant.
  147. * @param {T=} opt_obj The object to be used as the value of 'this'
  148. * within {@code f}.
  149. * @template T,S
  150. * @deprecated Use a more specific method, e.g. goog.array.forEach,
  151. * goog.object.forEach, or for-of.
  152. */
  153. goog.structs.forEach = function(col, f, opt_obj) {
  154. if (col.forEach && typeof col.forEach == 'function') {
  155. col.forEach(f, opt_obj);
  156. } else if (goog.isArrayLike(col) || goog.isString(col)) {
  157. goog.array.forEach(/** @type {!Array<?>} */ (col), f, opt_obj);
  158. } else {
  159. var keys = goog.structs.getKeys(col);
  160. var values = goog.structs.getValues(col);
  161. var l = values.length;
  162. for (var i = 0; i < l; i++) {
  163. f.call(/** @type {?} */ (opt_obj), values[i], keys && keys[i], col);
  164. }
  165. }
  166. };
  167. /**
  168. * Calls a function for every value in the collection. When a call returns true,
  169. * adds the value to a new collection (Array is returned by default).
  170. *
  171. * @param {S} col The collection-like object.
  172. * @param {function(this:T,?,?,S):boolean} f The function to call for every
  173. * value. This function takes
  174. * 3 arguments (the value, the key or undefined if the collection has no
  175. * notion of keys, and the collection) and should return a Boolean. If the
  176. * return value is true the value is added to the result collection. If it
  177. * is false the value is not included.
  178. * @param {T=} opt_obj The object to be used as the value of 'this'
  179. * within {@code f}.
  180. * @return {!Object|!Array<?>} A new collection where the passed values are
  181. * present. If col is a key-less collection an array is returned. If col
  182. * has keys and values a plain old JS object is returned.
  183. * @template T,S
  184. */
  185. goog.structs.filter = function(col, f, opt_obj) {
  186. if (typeof col.filter == 'function') {
  187. return col.filter(f, opt_obj);
  188. }
  189. if (goog.isArrayLike(col) || goog.isString(col)) {
  190. return goog.array.filter(/** @type {!Array<?>} */ (col), f, opt_obj);
  191. }
  192. var rv;
  193. var keys = goog.structs.getKeys(col);
  194. var values = goog.structs.getValues(col);
  195. var l = values.length;
  196. if (keys) {
  197. rv = {};
  198. for (var i = 0; i < l; i++) {
  199. if (f.call(/** @type {?} */ (opt_obj), values[i], keys[i], col)) {
  200. rv[keys[i]] = values[i];
  201. }
  202. }
  203. } else {
  204. // We should not use goog.array.filter here since we want to make sure that
  205. // the index is undefined as well as make sure that col is passed to the
  206. // function.
  207. rv = [];
  208. for (var i = 0; i < l; i++) {
  209. if (f.call(opt_obj, values[i], undefined, col)) {
  210. rv.push(values[i]);
  211. }
  212. }
  213. }
  214. return rv;
  215. };
  216. /**
  217. * Calls a function for every value in the collection and adds the result into a
  218. * new collection (defaults to creating a new Array).
  219. *
  220. * @param {S} col The collection-like object.
  221. * @param {function(this:T,?,?,S):V} f The function to call for every value.
  222. * This function takes 3 arguments (the value, the key or undefined if the
  223. * collection has no notion of keys, and the collection) and should return
  224. * something. The result will be used as the value in the new collection.
  225. * @param {T=} opt_obj The object to be used as the value of 'this'
  226. * within {@code f}.
  227. * @return {!Object<V>|!Array<V>} A new collection with the new values. If
  228. * col is a key-less collection an array is returned. If col has keys and
  229. * values a plain old JS object is returned.
  230. * @template T,S,V
  231. */
  232. goog.structs.map = function(col, f, opt_obj) {
  233. if (typeof col.map == 'function') {
  234. return col.map(f, opt_obj);
  235. }
  236. if (goog.isArrayLike(col) || goog.isString(col)) {
  237. return goog.array.map(/** @type {!Array<?>} */ (col), f, opt_obj);
  238. }
  239. var rv;
  240. var keys = goog.structs.getKeys(col);
  241. var values = goog.structs.getValues(col);
  242. var l = values.length;
  243. if (keys) {
  244. rv = {};
  245. for (var i = 0; i < l; i++) {
  246. rv[keys[i]] = f.call(/** @type {?} */ (opt_obj), values[i], keys[i], col);
  247. }
  248. } else {
  249. // We should not use goog.array.map here since we want to make sure that
  250. // the index is undefined as well as make sure that col is passed to the
  251. // function.
  252. rv = [];
  253. for (var i = 0; i < l; i++) {
  254. rv[i] = f.call(/** @type {?} */ (opt_obj), values[i], undefined, col);
  255. }
  256. }
  257. return rv;
  258. };
  259. /**
  260. * Calls f for each value in a collection. If any call returns true this returns
  261. * true (without checking the rest). If all returns false this returns false.
  262. *
  263. * @param {S} col The collection-like object.
  264. * @param {function(this:T,?,?,S):boolean} f The function to call for every
  265. * value. This function takes 3 arguments (the value, the key or undefined
  266. * if the collection has no notion of keys, and the collection) and should
  267. * return a boolean.
  268. * @param {T=} opt_obj The object to be used as the value of 'this'
  269. * within {@code f}.
  270. * @return {boolean} True if any value passes the test.
  271. * @template T,S
  272. */
  273. goog.structs.some = function(col, f, opt_obj) {
  274. if (typeof col.some == 'function') {
  275. return col.some(f, opt_obj);
  276. }
  277. if (goog.isArrayLike(col) || goog.isString(col)) {
  278. return goog.array.some(/** @type {!Array<?>} */ (col), f, opt_obj);
  279. }
  280. var keys = goog.structs.getKeys(col);
  281. var values = goog.structs.getValues(col);
  282. var l = values.length;
  283. for (var i = 0; i < l; i++) {
  284. if (f.call(/** @type {?} */ (opt_obj), values[i], keys && keys[i], col)) {
  285. return true;
  286. }
  287. }
  288. return false;
  289. };
  290. /**
  291. * Calls f for each value in a collection. If all calls return true this return
  292. * true this returns true. If any returns false this returns false at this point
  293. * and does not continue to check the remaining values.
  294. *
  295. * @param {S} col The collection-like object.
  296. * @param {function(this:T,?,?,S):boolean} f The function to call for every
  297. * value. This function takes 3 arguments (the value, the key or
  298. * undefined if the collection has no notion of keys, and the collection)
  299. * and should return a boolean.
  300. * @param {T=} opt_obj The object to be used as the value of 'this'
  301. * within {@code f}.
  302. * @return {boolean} True if all key-value pairs pass the test.
  303. * @template T,S
  304. */
  305. goog.structs.every = function(col, f, opt_obj) {
  306. if (typeof col.every == 'function') {
  307. return col.every(f, opt_obj);
  308. }
  309. if (goog.isArrayLike(col) || goog.isString(col)) {
  310. return goog.array.every(/** @type {!Array<?>} */ (col), f, opt_obj);
  311. }
  312. var keys = goog.structs.getKeys(col);
  313. var values = goog.structs.getValues(col);
  314. var l = values.length;
  315. for (var i = 0; i < l; i++) {
  316. if (!f.call(/** @type {?} */ (opt_obj), values[i], keys && keys[i], col)) {
  317. return false;
  318. }
  319. }
  320. return true;
  321. };