import type { Unfoldable } from "@principia/prelude"; import type * as HKT from "@principia/prelude/HKT"; import * as O from "../Option"; import { collect_ } from "./combinators"; import type { ReadonlyRecord } from "./model"; /** * @category Destructors * @since 1.0.0 */ export const toArray = (r: ReadonlyRecord): ReadonlyArray => collect_(r, (k, a) => [k, a]); /** * Unfolds a record into a list of key/value pairs * * @category Destructors * @since 1.0.0 */ export const toUnfoldable = (U: Unfoldable) => ( r: ReadonlyRecord ): HKT.Kind< F, C, HKT.Initial, HKT.Initial, HKT.Initial, HKT.Initial, HKT.Initial, HKT.Initial, HKT.Initial, HKT.Initial, HKT.Initial, readonly [N, A] > => { const arr = toArray(r); const len = arr.length; return U.unfold(0, (b) => (b < len ? O.some([arr[b], b + 1]) : O.none())); };