All files Sequence.ts

83.33% Statements 30/36
66.67% Branches 4/6
88.89% Functions 8/9
86.67% Lines 26/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 545x   5x 5x 5x   5x       54x 39x 15x 6x   9x 2x 4x           5x 16x 59x 59x   16x     5x               5x 1x     5x 2x     5x 1x   5x   5x  
import { Collection, isIterable } from './utils'
import collect from './collect'
import zip from './zip'
import repeat from './repeat'
import map from './map'
 
class Sequence<T> implements Iterable<T> {
  [Symbol.iterator]: () => Iterator<T>
 
  constructor(collection: Collection<T>) {
    if(typeof collection === 'function') {
      this[Symbol.iterator] = collection
    } else if(isIterable(collection)) {
      this[Symbol.iterator] = () => collection[Symbol.iterator]()
    } else {
      this[Symbol.iterator] = function* () {
        for(let i = 0; i < collection.length; i++) {
          yield collection[i]
        }
      }
    }
  }
 
  toArray() {
    const result = []
    for(const value of this) {
      result.push(value)
    }
    return result
  }
 
  join(separator = '') {
    let result = ''
    for(const value of this) {
      result += separator + value
    }
    return result
  }
 
  zip<U>(collection: Collection<U>) {
    return zip(this, collection)
  }
 
  repeat(times?: number) {
    return repeat(this, times)
  }
 
  map<U>(fn: (value: T, index: number) => U) {
    return map(this, fn)
  }
}
 
export default Sequence