Removal.js 763 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * Remove from the beginning of a collection until it is empty.
  3. *
  4. * This is a null-safe operation that ensures elements
  5. * are being removed from the given collection until the
  6. * collection is empty.
  7. *
  8. * The implementation deals with the fact that a remove operation
  9. * may touch, i.e. remove multiple elements in the collection
  10. * at a time.
  11. *
  12. * @param {Array<Object>} [collection]
  13. * @param {Function} removeFn
  14. *
  15. * @return {Array<Object>} the cleared collection
  16. */
  17. export function saveClear(collection, removeFn) {
  18. if (typeof removeFn !== 'function') {
  19. throw new Error('removeFn iterator must be a function');
  20. }
  21. if (!collection) {
  22. return;
  23. }
  24. var e;
  25. while ((e = collection[0])) {
  26. removeFn(e);
  27. }
  28. return collection;
  29. }