(array, item, callback, context)
| 24 | * @return {*|Array.<*>} The item, or array of items, that were successfully removed from the array. |
| 25 | */ |
| 26 | var Remove = function (array, item, callback, context) |
| 27 | { |
| 28 | if (context === undefined) { context = array; } |
| 29 | |
| 30 | var index; |
| 31 | |
| 32 | // Fast path to avoid array mutation and iteration |
| 33 | if (!Array.isArray(item)) |
| 34 | { |
| 35 | index = array.indexOf(item); |
| 36 | |
| 37 | if (index !== -1) |
| 38 | { |
| 39 | SpliceOne(array, index); |
| 40 | |
| 41 | if (callback) |
| 42 | { |
| 43 | callback.call(context, item); |
| 44 | } |
| 45 | |
| 46 | return item; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | return null; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // If we got this far, we have an array of items to remove |
| 55 | |
| 56 | var itemLength = item.length - 1; |
| 57 | var removed = []; |
| 58 | |
| 59 | while (itemLength >= 0) |
| 60 | { |
| 61 | var entry = item[itemLength]; |
| 62 | |
| 63 | index = array.indexOf(entry); |
| 64 | |
| 65 | if (index !== -1) |
| 66 | { |
| 67 | SpliceOne(array, index); |
| 68 | |
| 69 | removed.push(entry); |
| 70 | |
| 71 | if (callback) |
| 72 | { |
| 73 | callback.call(context, entry); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | itemLength--; |
| 78 | } |
| 79 | |
| 80 | return removed; |
| 81 | }; |
| 82 | |
| 83 | module.exports = Remove; |
no test coverage detected
searching dependent graphs…