@lou.codes/iterables
Version:
🔁 Iterable and AsyncIterable utils
24 lines (23 loc) • 829 B
JavaScript
import { ownKeys } from "@lou.codes/constants/Reflect.js";
import { createIterableIterator } from "./createIterableIterator.js";
/**
* Yields all entries of an object (including symbols).
*
* @category Asynchronous Generators
* @example
* ```typescript
* const entries = objectEntries({ a: 1, b: 2 });
* entries.next(); // { value: ["a", 1], done: false }
* entries.next(); // { value: ["b", 2], done: false }
* entries.next(); // { value: undefined, done: true }
* ```
* @param input Object to get entries from.
* @returns Iterable with entries of the given object (including symbols).
*/
export const objectToEntries = input =>
createIterableIterator(async function* () {
// eslint-disable-next-line functional/no-loop-statements
for await (const key of ownKeys(input)) {
yield [key, input[key]];
}
});