addToArray.js 725 B

12345678910111213141516171819202122232425
  1. import defaultValue from "../../Core/defaultValue.js";
  2. /**
  3. * Adds an element to an array and returns the element's index.
  4. *
  5. * @param {Array} array The array to add to.
  6. * @param {object} element The element to add.
  7. * @param {boolean} [checkDuplicates=false] When <code>true</code>, if a duplicate element is found its index is returned and <code>element</code> is not added to the array.
  8. *
  9. * @private
  10. */
  11. function addToArray(array, element, checkDuplicates) {
  12. checkDuplicates = defaultValue(checkDuplicates, false);
  13. if (checkDuplicates) {
  14. const index = array.indexOf(element);
  15. if (index > -1) {
  16. return index;
  17. }
  18. }
  19. array.push(element);
  20. return array.length - 1;
  21. }
  22. export default addToArray;