import { ArgumentError } from './commons/ErrorModels' ; import { isIterator } from './commons/utility' ; import { IterableProtocol } from './commons/types' ; function * take ( predicate , iter ) { if ( validateTakeInputTypes ( predicate , iter ) ) { throw new ArgumentError ( 'Please check arguments type.' ) ; } let index = 0 ; while ( index ++ < predicate ) { yield iter . next ( ) . value ; } } function * takeWhile ( predicate , iter ) { if ( validateTakeWhileInputTypes ( predicate , iter ) ) { throw new ArgumentError ( 'Please check arguments type.' ) ; } while ( true ) { const result = iter . next ( ) ; if ( result . done ) { break ; } if ( predicate ( result . value ) ) { yield result . value ; } } } function validateTakeInputTypes ( predicate , iter ) { return typeof predicate !== 'number' || ! isIterator ( iter ) ; } function validateTakeWhileInputTypes ( predicate , iter ) { return typeof predicate !== 'function' || ! isIterator ( iter ) ; } export { take , takeWhile } ;