Skip to content

Commit 5716908

Browse files
authored
Inclusion of xtoggle
A function that removes a value from and array, and then retruns the original value to its index, when the function is called again with the same value
1 parent 3d97657 commit 5716908

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Arrays.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ Array.prototype.insert = function (item, index) {
327327
ret.add(arr[i]);
328328
}
329329
arr.clear();
330-
arr = ret;
330+
arr.addRange(ret);
331+
arr.prev = [];
331332
return ret;
332333
}
333334

@@ -688,3 +689,24 @@ Array.prototype.trim = function () {
688689
});
689690
return ret;
690691
}
692+
693+
/* An enhanced version of the togGle() function.
694+
Toggling the arraY toggle(value) On and Off Like a switch*/
695+
Array.prototype.xtoggle = function (item) {
696+
var arr = this;
697+
if (arr.indexOf(item) >= 0) {
698+
var index = arr.indexOf(item);
699+
arr.prev.push({index: index, value: item});
700+
arr.remove(item);
701+
}
702+
else {
703+
var prevValue = arr.prev.where('value', item).first();
704+
if (typeof prevValue.index != 'undefined') {
705+
arr.insert(prevValue.value, prevValue.index);
706+
arr.prev.remove(prevValue.index, 'index');
707+
}
708+
else arr.push(item);
709+
}
710+
return arr;
711+
};
712+

0 commit comments

Comments
 (0)