array.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Generated by CoffeeScript 1.6.3
  2. var array;
  3. module.exports = array = {
  4. /*
  5. Tries to turn anything into an array.
  6. */
  7. from: function(r) {
  8. return Array.prototype.slice.call(r);
  9. },
  10. /*
  11. Clone of an array. Properties will be shallow copies.
  12. */
  13. simpleClone: function(a) {
  14. return a.slice(0);
  15. },
  16. shallowEqual: function(a1, a2) {
  17. var i, val, _i, _len;
  18. if (!(Array.isArray(a1) && Array.isArray(a2) && a1.length === a2.length)) {
  19. return false;
  20. }
  21. for (i = _i = 0, _len = a1.length; _i < _len; i = ++_i) {
  22. val = a1[i];
  23. if (a2[i] !== val) {
  24. return false;
  25. }
  26. }
  27. return true;
  28. },
  29. pluck: function(a, i) {
  30. var index, value, _i, _len;
  31. if (a.length < 1) {
  32. return a;
  33. }
  34. for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
  35. value = a[index];
  36. if (index > i) {
  37. a[index - 1] = a[index];
  38. }
  39. }
  40. a.length = a.length - 1;
  41. return a;
  42. },
  43. pluckItem: function(a, item) {
  44. var index, removed, value, _i, _len;
  45. if (a.length < 1) {
  46. return a;
  47. }
  48. removed = 0;
  49. for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
  50. value = a[index];
  51. if (value === item) {
  52. removed++;
  53. continue;
  54. }
  55. if (removed !== 0) {
  56. a[index - removed] = a[index];
  57. }
  58. }
  59. if (removed > 0) {
  60. a.length = a.length - removed;
  61. }
  62. return a;
  63. },
  64. pluckOneItem: function(a, item) {
  65. var index, reached, value, _i, _len;
  66. if (a.length < 1) {
  67. return a;
  68. }
  69. reached = false;
  70. for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
  71. value = a[index];
  72. if (!reached) {
  73. if (value === item) {
  74. reached = true;
  75. continue;
  76. }
  77. } else {
  78. a[index - 1] = a[index];
  79. }
  80. }
  81. if (reached) {
  82. a.length = a.length - 1;
  83. }
  84. return a;
  85. },
  86. pluckByCallback: function(a, cb) {
  87. var index, removed, value, _i, _len;
  88. if (a.length < 1) {
  89. return a;
  90. }
  91. removed = 0;
  92. for (index = _i = 0, _len = a.length; _i < _len; index = ++_i) {
  93. value = a[index];
  94. if (cb(value, index)) {
  95. removed++;
  96. continue;
  97. }
  98. if (removed !== 0) {
  99. a[index - removed] = a[index];
  100. }
  101. }
  102. if (removed > 0) {
  103. a.length = a.length - removed;
  104. }
  105. return a;
  106. },
  107. pluckMultiple: function(array, indexesToRemove) {
  108. var i, removedSoFar, _i, _len;
  109. if (array.length < 1) {
  110. return array;
  111. }
  112. removedSoFar = 0;
  113. indexesToRemove.sort();
  114. for (_i = 0, _len = indexesToRemove.length; _i < _len; _i++) {
  115. i = indexesToRemove[_i];
  116. this.pluck(array, i - removedSoFar);
  117. removedSoFar++;
  118. }
  119. return array;
  120. },
  121. injectByCallback: function(a, toInject, shouldInject) {
  122. var i, len, val, valA, valB, _i, _len;
  123. valA = null;
  124. valB = null;
  125. len = a.length;
  126. if (len < 1) {
  127. a.push(toInject);
  128. return a;
  129. }
  130. for (i = _i = 0, _len = a.length; _i < _len; i = ++_i) {
  131. val = a[i];
  132. valA = valB;
  133. valB = val;
  134. if (shouldInject(valA, valB, toInject)) {
  135. return a.splice(i, 0, toInject);
  136. }
  137. }
  138. a.push(toInject);
  139. return a;
  140. },
  141. injectInIndex: function(a, index, toInject) {
  142. var i, len, toPut, toPutNext;
  143. len = a.length;
  144. i = index;
  145. if (len < 1) {
  146. a.push(toInject);
  147. return a;
  148. }
  149. toPut = toInject;
  150. toPutNext = null;
  151. for(; i <= len; i++){
  152. toPutNext = a[i];
  153. a[i] = toPut;
  154. toPut = toPutNext;
  155. };
  156. return null;
  157. }
  158. };