list.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * $Id: TinyMCE_DOMUtils.class.js 91 2006-10-02 14:53:22Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
  6. */
  7. new function() {
  8. function isset(o) {
  9. return typeof(o) != 'undefined';
  10. };
  11. /**#@+
  12. * @class This class contains common array and list management methods.
  13. * @member mox.List
  14. * @static
  15. */
  16. mox.create('static mox.List', {
  17. /**#@+
  18. * @method
  19. */
  20. /**
  21. * Filters the specified array by the specified filter function.
  22. *
  23. * @param {Array} a Array to filter out elements from.
  24. * @param {function} f Function to call for each array item. If this function returns true the item will be placed in output.
  25. * @param {Number} si Optional start index.
  26. * @param {Number} ei Optional end index.
  27. * @return {Array} Filtered array.
  28. */
  29. filter : function(a, f, si, ei) {
  30. var i, o = [];
  31. si = isset(si) ? si : 0;
  32. ei = isset(ei) ? ei : a.length;
  33. for (i=si; i<ei; i++) {
  34. if (f(i, a[i]))
  35. o.push(a[i]);
  36. }
  37. return o;
  38. },
  39. map : function(a, f, si, ei) {
  40. var i;
  41. si = isset(si) ? si : 0;
  42. ei = isset(ei) ? ei : a.length;
  43. for (i=si; i<ei; i++)
  44. f(i, a[i]);
  45. },
  46. indexOf : function(a, v) {
  47. var i;
  48. for (i=0; i<a.length; i++) {
  49. if (a[i] == v)
  50. return i;
  51. }
  52. return -1;
  53. },
  54. each : function(a, f) {
  55. var n, v;
  56. for (n in a) {
  57. if ((v = f(n, a[n])))
  58. return v;
  59. }
  60. return false;
  61. },
  62. getKeys : function(a) {
  63. var o = [];
  64. this.each(a, function(n, v) {
  65. o.push(n);
  66. });
  67. return o;
  68. },
  69. hasValue : function(a, rv) {
  70. var s = 0;
  71. this.each(a, function(n, v) {
  72. return s = (v == rv);
  73. });
  74. return s;
  75. },
  76. remove : function(a, rv) {
  77. return this.filter(a, function(i, v) {
  78. return v != rv;
  79. });
  80. }
  81. /**#@-*/
  82. });
  83. };