<A>(isLeaf: (value: unknown) => value is A): (
input: object
) => Array<[bracketPath: string, value: A]>Flattens a nested object into bracket-path entries, filtering leaf values by a type guard.
When to use
Use when you need a schema getter to serialize structured objects to flat key-value entries.
- Building custom
FormDataorURLSearchParamsencoders.
Details
- This is the inverse of makeTreeRecord.
- Takes a nested object and produces flat
[bracketPath, value]pairs suitable forFormDataorURLSearchParams. - Returns a curried function: first call provides the leaf type guard, second call provides the object.
- Recursively traverses objects and arrays.
- If all elements of an array are leaves, encodes them as multiple entries with the same key
(e.g.
tags=a&tags=b). Otherwise uses indexed bracket paths (e.g.items[0],items[1]). - Non-leaf values that aren't objects or arrays are silently skipped.
Example (Flattening an object to bracket paths)
import { Predicate, SchemaGetter } from "effect"
const collectStrings = SchemaGetter.collectBracketPathEntries(Predicate.isString)
const entries = collectStrings({ user: { name: "Alice", tags: ["admin", "editor"] } })
// [["user[name]", "Alice"], ["user[tags]", "admin"], ["user[tags]", "editor"]]export function function collectBracketPathEntries<A>(
isLeaf: (value: unknown) => value is A
): (
input: object
) => Array<[bracketPath: string, value: A]>
Flattens a nested object into bracket-path entries, filtering leaf values by a type guard.
When to use
Use when you need a schema getter to serialize structured objects to flat
key-value entries.
- Building custom
FormData or URLSearchParams encoders.
Details
- This is the inverse of
makeTreeRecord
.
- Takes a nested object and produces flat
[bracketPath, value] pairs suitable for
FormData or URLSearchParams.
- Returns a curried function: first call provides the leaf type guard, second call provides the object.
- Recursively traverses objects and arrays.
- If all elements of an array are leaves, encodes them as multiple entries with the same key
(e.g.
tags=a&tags=b). Otherwise uses indexed bracket paths (e.g. items[0], items[1]).
- Non-leaf values that aren't objects or arrays are silently skipped.
Example (Flattening an object to bracket paths)
import { Predicate, SchemaGetter } from "effect"
const collectStrings = SchemaGetter.collectBracketPathEntries(Predicate.isString)
const entries = collectStrings({ user: { name: "Alice", tags: ["admin", "editor"] } })
// [["user[name]", "Alice"], ["user[tags]", "admin"], ["user[tags]", "editor"]]
collectBracketPathEntries<function (type parameter) A in collectBracketPathEntries<A>(isLeaf: (value: unknown) => value is A): (input: object) => Array<[bracketPath: string, value: A]>A>(isLeaf: (value: unknown) => value is AisLeaf: (value: unknownvalue: unknown) => value: unknownvalue is function (type parameter) A in collectBracketPathEntries<A>(isLeaf: (value: unknown) => value is A): (input: object) => Array<[bracketPath: string, value: A]>A) {
return (input: objectinput: object): interface Array<T>Array<[stringbracketPath: string, Avalue: function (type parameter) A in collectBracketPathEntries<A>(isLeaf: (value: unknown) => value is A): (input: object) => Array<[bracketPath: string, value: A]>A]> => {
const const bracketPathEntries: [string, A][]bracketPathEntries: interface Array<T>Array<[string, function (type parameter) A in collectBracketPathEntries<A>(isLeaf: (value: unknown) => value is A): (input: object) => Array<[bracketPath: string, value: A]>A]> = []
function function (local function) append(key: string, value: unknown): voidappend(key: stringkey: string, value: unknownvalue: unknown): void {
if (isLeaf: (value: unknown) => value is AisLeaf(value: unknownvalue)) {
const bracketPathEntries: [string, A][]bracketPathEntries.Array<[string, A]>.push(...items: [string, A][]): numberAppends new elements to the end of an array, and returns the new length of the array.
push([key: stringkey, value: Avalue])
} else if (var Array: ArrayConstructorArray.ArrayConstructor.isArray(arg: any): arg is any[]isArray(value: unknownvalue)) {
// If all values are leaves, encode as multiple entries with the same key
const const allLeaves: booleanallLeaves = value: any[]value.Array<any>.every<A>(predicate: (value: any, index: number, array: any[]) => value is A, thisArg?: any): this is S[] (+1 overload)Determines whether all the members of an array satisfy the specified test.
every(isLeaf: (value: unknown) => value is AisLeaf)
if (const allLeaves: booleanallLeaves) {
value: A[]value.Array<A>.forEach(callbackfn: (value: A, index: number, array: A[]) => void, thisArg?: any): voidPerforms the specified action for each element in an array.
forEach((v: Av) => {
const bracketPathEntries: [string, A][]bracketPathEntries.Array<[string, A]>.push(...items: [string, A][]): numberAppends new elements to the end of an array, and returns the new length of the array.
push([key: stringkey, v: Av])
})
} else {
value: any[]value.Array<any>.forEach(callbackfn: (value: any, index: number, array: any[]) => void, thisArg?: any): voidPerforms the specified action for each element in an array.
forEach((v: anyv, i: numberi) => {
function (local function) append(key: string, value: unknown): voidappend(`${key: stringkey}[${i: numberi}]`, v: anyv)
})
}
} else if (typeof value: unknownvalue === "object" && value: object | nullvalue !== null) {
for (const [const k: stringk, const v: anyv] of var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.entries(o: {}): [string, any][] (+1 overload)Returns an array of key/values of the enumerable own properties of an object
entries(value: objectvalue)) {
function (local function) append(key: string, value: unknown): voidappend(`${key: stringkey}[${const k: stringk}]`, const v: anyv)
}
}
}
for (const [const key: stringkey, const value: anyvalue] of var Object: ObjectConstructorProvides functionality common to all JavaScript objects.
Object.ObjectConstructor.entries(o: {}): [string, any][] (+1 overload)Returns an array of key/values of the enumerable own properties of an object
entries(input: objectinput)) {
function (local function) append(key: string, value: unknown): voidappend(const key: stringkey, const value: anyvalue)
}
return const bracketPathEntries: [string, A][]bracketPathEntries
}
}