8
0

nth.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import nth from '../src/nth';
  6. describe( 'utils', () => {
  7. describe( 'nth', () => {
  8. it( 'should return 0th item', () => {
  9. expect( nth( 0, getGenerator() ) ).to.equal( 11 );
  10. } );
  11. it( 'should return the last item', () => {
  12. expect( nth( 2, getGenerator() ) ).to.equal( 33 );
  13. } );
  14. it( 'should return null if out of range (bottom)', () => {
  15. expect( nth( -1, getGenerator() ) ).to.be.null;
  16. } );
  17. it( 'should return null if out of range (top)', () => {
  18. expect( nth( 3, getGenerator() ) ).to.be.null;
  19. } );
  20. it( 'should return null if iterator is empty', () => {
  21. expect( nth( 0, [] ) ).to.be.null;
  22. } );
  23. it( 'should consume the given generator', () => {
  24. const generator = getGenerator();
  25. nth( 0, generator );
  26. expect( generator.next().done ).to.equal( true );
  27. } );
  28. it( 'should stop inside the given iterator', () => {
  29. const collection = [ 11, 22, 33 ];
  30. const iterator = collection[ Symbol.iterator ]();
  31. nth( 0, iterator );
  32. expect( iterator.next().value ).to.equal( 22 );
  33. } );
  34. function* getGenerator() {
  35. yield 11;
  36. yield 22;
  37. yield 33;
  38. }
  39. } );
  40. } );