Created
June 12, 2017 19:08
-
-
Save mattpodwysocki/1e4ad1a7d4e688dbd3f20f0141384e38 to your computer and use it in GitHub Desktop.
Adds ofEntries, ofKeys and ofValues to Iterable/AsyncIterable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import { IterableX } from '../iterable'; | |
import { map } from './map'; | |
function makeTuple<TFirst, TSecond>(x: TFirst, y: TSecond): [TFirst, TSecond] { | |
return [x, y]; | |
} | |
class OfEntriesIterable<TSource> extends IterableX<[string, TSource]> { | |
private _source: { [key: string]: TSource }; | |
constructor(source: { [key: string]: TSource }) { | |
super(); | |
this._source = source; | |
} | |
[Symbol.iterator]() { | |
return map(Object.keys(this._source), key => makeTuple(key, this._source[key]))[Symbol.iterator](); | |
} | |
} | |
export function ofEntries<TSource>(source: { [key: string]: TSource }): IterableX<[string, TSource]> { | |
return new OfEntriesIterable<TSource>(source); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import { IterableX } from '../iterable'; | |
class OfKeysIterable<TSource> extends IterableX<string> { | |
private _source: { [key: string]: TSource }; | |
constructor(source: { [key: string]: TSource }) { | |
super(); | |
this._source = source; | |
} | |
[Symbol.iterator]() { | |
return Object.keys(this._source)[Symbol.iterator](); | |
} | |
} | |
export function ofKeys<TSource>(source: { [key: string]: TSource }): IterableX<string> { | |
return new OfKeysIterable<TSource>(source); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import { IterableX } from '../iterable'; | |
import { map } from './map'; | |
class OfValuesIterable<TSource> extends IterableX<TSource> { | |
private _source: { [key: string]: TSource }; | |
constructor(source: { [key: string]: TSource }) { | |
super(); | |
this._source = source; | |
} | |
[Symbol.iterator]() { | |
return map(Object.keys(this._source), key => this._source[key])[Symbol.iterator](); | |
} | |
} | |
export function ofValues<TSource>(source: { [key: string]: TSource }): IterableX<TSource> { | |
return new OfValuesIterable<TSource>(source); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment