Home > Web Front-end > JS Tutorial > body text

arrayToDict function in tRPC source code

Barbara Streisand
Release: 2024-11-04 05:00:01
Original
601 people have browsed it

In this article, we analyze arrayToDict function found in tRPC source code.

// https://github.com/trpc/trpc/pull/669
function arrayToDict(array: unknown[]) {
 const dict: Record<number, unknown> = {};
 for (let index = 0; index < array.length; index++) {
 const element = array[index];
 dict[index] = element;
 }
 return dict;
}
Copy after login

This function is straight forward. dict is an object initialized above the for loop. In this for loop, array[index] is assigned to element and dict is an object that array indices as keys and values being array items based on index.

Although this function is simple, a lot was discussed in PR: https://github.com/trpc/trpc/pull/669 before this solution was presented to KATT (Creator of tRPC).

This pull request was about — fix: batching with zod .optional() input.

PR’s description:

- JSON.stringify([undefined]) === [null]

- this causes issues with .optional() zod schemas as it expects an object or undefined, not null

- fixed by ugly hack that runs validator twice when it fails if the raw input was === null.

- fixed by @simonedelmann’s idea

I would recommend going through this pull request conversation to give you an insight on how decisions are made. This conversation shows how ideas are bounced around and also talk about what could break based on an idea, if it’s just an edge-case and they finally choose to go with arrayToDict idea.

In this pull request, you also find All Contributors Github app that gives

attribution to contributor based on an idea. This is what All Contributors is about — Recognize all contributors, not just the ones who push code. With All Contributes, even if you present an idea without writing any code, you could make them a contributor with a comment:



@all-contributors add @username for reviews

and @all-contributors adds tagged user as a contributor.

About us:

At Thinkthroo, we study large open source projects and provide architectural guides. We have developed resubale Components, built with tailwind, that you can use in your project. We offer Next.js, React and Node development services.

Book a meeting with us to discuss your project.

arrayToDict function in tRPC source code

arrayToDict function in tRPC source code

References:

  1. https://github.com/trpc/trpc/blob/next/packages/client/src/links/internals/httpUtils.ts#L57

  2. https://github.com/trpc/trpc/pull/669

The above is the detailed content of arrayToDict function in tRPC source code. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template