ArrayUtils.ts 831 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. function remove<T>(array: T[], item: T) {
  2. let index = array.indexOf(item);
  3. if (index >= 0) {
  4. array.splice(index, 1);
  5. return true;
  6. }
  7. return false;
  8. }
  9. function removeAt<T>(array: T[], index: number) {
  10. if (index >= 0) {
  11. array.splice(index, 1);
  12. return true;
  13. }
  14. return false;
  15. }
  16. function insert<T>(array: T[], i: number, item: T) {
  17. if (i > array.length) {
  18. array.push(item);
  19. return array;
  20. }
  21. return array.splice(i, 0, item);
  22. }
  23. function contains<T>(array: T[], item: T) {
  24. return array.indexOf(item) >= 0;
  25. }
  26. function clear<T>(array: T[]) {
  27. return array.splice(0, array.length);
  28. }
  29. function addOnce<T>(array: T[], item: T) {
  30. if (array.indexOf(item) >= 0) return array.length;
  31. else return array.push(item);
  32. }
  33. export default {
  34. addOnce,
  35. clear,
  36. contains,
  37. insert,
  38. removeAt,
  39. remove,
  40. };