import { IStream } from "./Interfaces"; import { fromObject, fromObjectKeys, fromObjectValues } from "./Methods"; import { InplaceStreamFactory, TypesafeStreamFactory } from "./StreamFactory"; /** @internal */ function patch(force: boolean, type: {prototype: P}, getStream: (object: T) => Iterable, wrapStream: (iterable: Iterable) => IStream, name: string = "stream") { if (Object.hasOwnProperty.call(type.prototype, name)) { if (!force) { return; } delete (type.prototype as any)[name]; } Object.defineProperty(type.prototype, name, { configurable: true, enumerable: false, value(this: T): IStream { return wrapStream(getStream(this)); }, writable: false, }); } /** * Patches a few stream convenience methods to the prototype of objects: * * String.stream() `Stream` Creates a stream over all characters. * * Array.stream() `Stream` Creates a stream over all items. * * Set.stream() `Stream` Creates a stream over all items. * * Map.stream() `Stream` Creates a stream over all key-value pairs. * * Object.stream() `Stream<{key: string, value: T}>` Creates a stream over all key-value pairs. * * Object.keys() `Stream` Creates a stream over all keys. * * Object.values() `Stream` Creates a stream over all values. * * ```javascript * require("elbe").monkeyPatch(); * * "foo".stream() // => Stream["f", "o", "o"] * * [1,2,3].stream() // => Stream[1, 2, 3] * * {foo: 42, bar: 99}.keys() // => Stream["foo", "bar"] * ``` * * @param inplace Iff `true`, uses {@link InplaceStreamFactory} or {@link TypesafeStreamFactory} otherwise. * @param force Iff `true`, always add the methods to the prototype. Otherwise, does nothing if the methods exist already. */ export function monkeyPatch(inplace: boolean = false, force: boolean = false): void { const stream = inplace ? InplaceStreamFactory.stream : TypesafeStreamFactory.stream; patch(force, Array, array => array, stream); patch, Set, Set>(force, Set, set => set.values(), stream); patch, Map, [any, any]>(force, Map, map => map.entries(), stream); // tslint:disable-next-line:ban-types patch(force, Object, object => fromObject(object), stream); // tslint:disable-next-line:ban-types patch(force, Object, object => fromObjectKeys(object), stream, "keys"); // tslint:disable-next-line:ban-types patch(force, Object, object => fromObjectValues(object), stream, "values"); // tslint:disable-next-line:ban-types patch(force, String, s => s, stream); }