Skip to content

Commit

Permalink
docs(core): add JSDoc for getId
Browse files Browse the repository at this point in the history
  • Loading branch information
ricokahler committed Sep 16, 2024
1 parent d94115a commit 1b696ca
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/sanity/src/core/form/store/utils/getId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ const idCache = new WeakMap<object, string>()
const undefinedKey = Symbol('GetIdUndefined')
const nullKey = Symbol('GetIdNull')

/**
* Generates a stable ID for various types of values, including `undefined`, `null`, objects, functions, and symbols.
*
* - **Primitives (string, number, boolean):** The value itself is used as the ID.
* - **Undefined and null:** Special symbols (`undefinedKey` and `nullKey`) are used to generate unique IDs.
* - **Objects and functions:** An ID is generated using the `nanoid` library and cached in a `WeakMap` for stable future retrieval.
*
* This function is used to reconcile inputs in `prepareFormState` immutably, allowing IDs to be generated and cached based
* on the reference of the object. This ensures that memoization functions can use these IDs for consistent hashing without
* recalculating on each call, as the inputs themselves are immutably edited.
*
* @internal
*/
export function getId(value: unknown): string {
switch (typeof value) {
case 'undefined': {
Expand Down

0 comments on commit 1b696ca

Please sign in to comment.