Skip to content

Commit

Permalink
Add documentation for createIterator utilities. Use inferface for Nor…
Browse files Browse the repository at this point in the history
…malizeValue, because type alias does not seem to able to pick up documentation for parameters
  • Loading branch information
octet-stream committed Feb 18, 2024
1 parent 8817b09 commit d784a2b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/objectToFormData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface TestParams {
const withSetializerTest = test.macro((t, params: TestParams) => {
const {input, expected, strict, serializer = objectToFormData} = params

const actual = Array.from(serializer(input, strict))
const actual = Array.from(serializer(input, {strict}))

t.deepEqual(actual, expected)
})
Expand Down
25 changes: 13 additions & 12 deletions src/objectToFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ import type {Input} from "./Input.js"
type Methods = "set" | "append"

/**
* Value normalizer.
*
* This function will be called on each *scalar* value, before it's added to FormData instanceю
*
* @param value - Current entry value
* @param name - The name of the entry
* @param path - Entry's path within original object
* Value normalizer callback
*/
export type NormalizeValue = (
value: unknown,
name: string,
path: Path
) => string | Blob
export interface NormalizeValue {
/**
* Value normalizer callback.
*
* This function will be called on each *scalar* value, before it's added to FormData instanceю
*
* @param value - Current entry value
* @param name - The name of the entry
* @param path - Entry's path within original object
*/
(value: unknown, name: string, path: Path): string | Blob
}

const noopNormalizeValue: NormalizeValue = value => value as string | Blob // Cast value type because FormData will normalize it anyway

Expand Down
30 changes: 27 additions & 3 deletions src/utils/createIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import type {Input} from "../Input.js"

type Entry = [PathEntry, unknown]

/**
* Creates iterator allowing to iterate over the `input` entries.
*
* @param input - Iterable object, either POJO or Array
*
* @internal
*/
function* createEntriesIterator(
input: Input
): Generator<Entry, void, undefined> {
Expand All @@ -18,14 +25,23 @@ function* createEntriesIterator(
yield* entries
}

/**
* Creates recursive iterator allowing to walk through the object's nested fields.
*
* The recursion termitates once any unsupported object, or scalar type is reached.
*
* @param input - Iterable object, either POJO or Array
* @param path - Path to current level field from the top of the `input`
* @param strict - Indicates whether or not to omit every `false` values. Applied enabled. Does not affect boolean array values
*
* @internal
*/
function* createRecursiveIterator(
input: Input,
path: Path,
strict: boolean
): Generator<[Path, unknown], void, undefined> {
const entries = createEntriesIterator(input)

for (const [key, value] of entries) {
for (const [key, value] of createEntriesIterator(input)) {
const nextPath = [...path, key]
if (Array.isArray(value) || isPlainObject(value)) {
yield* createRecursiveIterator(value, nextPath, strict)
Expand All @@ -38,6 +54,14 @@ function* createRecursiveIterator(
}
}

/**
* Creates iterator allowing to recursively iterate over given `input` iverable object. The object must be either POJO or Array.
*
* @param input
* @param strict
*
* @internal
*/
export const createIterator = (
input: Input,
strict = false
Expand Down

0 comments on commit d784a2b

Please sign in to comment.