Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add index param to callback of .map and .forEach #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/iterate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,44 @@ import * as assert from 'assert'
import { IteratorWithOperators } from './iterate'

describe('IteratorWithOperators', () => {
describe('map', () => {
it('should map all values to another value', () => {
const iterator = new IteratorWithOperators([1, 2, 'a', 'b'][Symbol.iterator]())
const mapped = iterator.map(value => value + '-mapped')

assert.equal(mapped.next().value, '1-mapped')
assert.equal(mapped.next().value, '2-mapped')
assert.equal(mapped.next().value, 'a-mapped')
assert.equal(mapped.next().value, 'b-mapped')
assert.equal(mapped.next().done, true)
})
it('should map have access to the index of the the element', () => {
const iterator = new IteratorWithOperators([1, 2, 'a', 'b'][Symbol.iterator]())
const mapped = iterator.map((value, index) => value + '-' + index)

assert.equal(mapped.next().value, '1-0')
assert.equal(mapped.next().value, '2-1')
assert.equal(mapped.next().value, 'a-2')
assert.equal(mapped.next().value, 'b-3')
assert.equal(mapped.next().done, true)
})
})
describe('forEach', () => {
it('should loop through each value', () => {
const values = [1, 2, 'a', 'b']
const valuesIterator = values[Symbol.iterator]()

const iterator = new IteratorWithOperators(values[Symbol.iterator]())

iterator.forEach((element, index) => {
const { value, done } = valuesIterator.next()
assert.equal(element, value)
assert.equal(done, false)
})

assert.equal(valuesIterator.next().done, true)
})
})
describe('join', () => {
it('should join all emitted values as strings', () => {
const iterator = new IteratorWithOperators([1, 2, 'a', 'b'][Symbol.iterator]())
Expand Down
7 changes: 4 additions & 3 deletions src/iterate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class IteratorWithOperators<T> implements IterableIterator<T> {
/**
* Returns a new Iterator by running each element thru iteratee
*/
map<R>(iteratee: (value: T) => R): IteratorWithOperators<R> {
map<R>(iteratee: (value: T, index: number) => R): IteratorWithOperators<R> {
return new IteratorWithOperators(new MapIterator(this.source, iteratee))
}

Expand Down Expand Up @@ -192,14 +192,15 @@ export class IteratorWithOperators<T> implements IterableIterator<T> {
/**
* Iterates and invokes `iteratee` for every element emitted by the Iterator
*/
forEach(iteratee: (value: T) => any): void {
forEach(iteratee: (value: T, index: number) => any): void {
let result: IteratorResult<T>
let index = 0;
while (true) {
result = this.source.next()
if (result.done) {
break
}
iteratee(result.value)
iteratee(result.value, index++)
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
* An iterator that emits results by running each element through a provided predicate
*/
export class MapIterator<T, R> implements Iterator<R> {
constructor(private source: Iterator<T>, private iteratee: (value: T) => R) {}
private index: number

constructor(private source: Iterator<T>, private iteratee: (value: T, index: number) => R) {
this.index = 0
}

next(): IteratorResult<R> {
const { value, done } = this.source.next()
return { value: !done && this.iteratee(value), done } as IteratorResult<R>
return { value: !done && this.iteratee(value, this.index++), done } as IteratorResult<R>
}
}