Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created June 12, 2017 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/1e4ad1a7d4e688dbd3f20f0141384e38 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/1e4ad1a7d4e688dbd3f20f0141384e38 to your computer and use it in GitHub Desktop.
Adds ofEntries, ofKeys and ofValues to Iterable/AsyncIterable
'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);
}
'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);
}
'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