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