array.ts 788 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. export function removeMatching(array, testFunc) {
  2. let removeCnt = 0
  3. let i = 0
  4. while (i < array.length) {
  5. if (testFunc(array[i])) { // truthy value means *remove*
  6. array.splice(i, 1)
  7. removeCnt++
  8. } else {
  9. i++
  10. }
  11. }
  12. return removeCnt
  13. }
  14. export function removeExact(array, exactVal) {
  15. let removeCnt = 0
  16. let i = 0
  17. while (i < array.length) {
  18. if (array[i] === exactVal) {
  19. array.splice(i, 1)
  20. removeCnt++
  21. } else {
  22. i++
  23. }
  24. }
  25. return removeCnt
  26. }
  27. export function isArraysEqual(a0, a1) {
  28. let len = a0.length
  29. let i
  30. if (len == null || len !== a1.length) { // not array? or not same length?
  31. return false
  32. }
  33. for (i = 0; i < len; i++) {
  34. if (a0[i] !== a1[i]) {
  35. return false
  36. }
  37. }
  38. return true
  39. }