nth.js 473 B

1234567891011121314151617181920212223242526
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module utils/nth
  7. */
  8. /**
  9. * Returns `nth` (starts from `0` of course) item of an `iterable`.
  10. *
  11. * @param {Number} index
  12. * @param {Iterable.<*>} iterable
  13. * @returns {*}
  14. */
  15. export default function nth( index, iterable ) {
  16. for ( let item of iterable ) {
  17. if ( index === 0 ) {
  18. return item;
  19. }
  20. index -= 1;
  21. }
  22. return null;
  23. }