Query and Mutation Matching
matchQuery
Determines if a query matches the given filters.Parameters
The filter criteria to match against.
Filter queries by their active state.
If
true, the query key must match exactly. If false, partial matching is used.The query key or key prefix to match.
Filter queries by their stale state.
Filter queries by their fetch status.
Custom predicate function for advanced filtering.
The query instance to test against the filters.
Returns
boolean - Returns true if the query matches all specified filters, false otherwise.
matchMutation
Determines if a mutation matches the given filters.Parameters
The filter criteria to match against.
If
true, the mutation key must match exactly. If false, partial matching is used.The mutation key or key prefix to match.
Filter mutations by their status.
Custom predicate function for advanced filtering.
The mutation instance to test against the filters.
Returns
boolean - Returns true if the mutation matches all specified filters, false otherwise.
Key Hashing and Matching
hashKey
Generates a stable hash from a query or mutation key.Parameters
The key to hash. Can be any serializable array.
Returns
string - A stable JSON string representation of the key with object keys sorted.
Example
Object properties in the key are sorted alphabetically to ensure consistent hashing regardless of property order.
hashQueryKeyByOptions
Generates a query key hash using options-specific hash function if provided.Parameters
The query key to hash.
Optional query options containing a custom hash function.
Returns
string - The hashed query key using the custom hash function if provided, or the default hashKey function.
partialMatchKey
Checks if keyb partially matches with key a.
Parameters
The full key to match against.
The partial key or key prefix to check.
Returns
boolean - Returns true if b is a partial match of a.
Example
Data Transformation
replaceEqualDeep
Performs deep equality checking and structural sharing between two values.Parameters
The previous value to compare against.
The new value to compare and potentially share structure with.
The current recursion depth. Stops recursion at depth 500 to prevent stack overflow.
Returns
T - Returns a if b is deeply equal, otherwise returns a new object/array with shared references for equal children.
Example
This function is used internally by TanStack Query for structural sharing, which helps prevent unnecessary re-renders by maintaining referential equality for unchanged data.
replaceData
Replaces query data with structural sharing based on options.Parameters
The previous data value.
The new data value.
Query options containing
structuralSharing configuration.Returns
TData - The new data, potentially with structural sharing applied.
keepPreviousData
Utility function that returns the previous data unchanged.Parameters
The previous data value.
Returns
T | undefined - The same previous data value passed in.
Example
Object Comparison
shallowEqualObjects
Performs shallow equality comparison between two objects.Parameters
The first object to compare.
The second object to compare.
Returns
boolean - Returns true if objects have the same keys and values (using === comparison), false otherwise.
Type Guards
isPlainArray
Checks if a value is a plain array.Returns
boolean - Returns true if the value is an array with only numeric indices.
isPlainObject
Checks if a value is a plain JavaScript object.Returns
boolean - Returns true if the value is a plain object created by {} or new Object(), false for class instances, arrays, null, etc.
isValidTimeout
Checks if a value is a valid timeout duration.Returns
boolean - Returns true if the value is a number >= 0 and not Infinity.
Time Utilities
timeUntilStale
Calculates the time in milliseconds until data becomes stale.Parameters
The timestamp when the data was last updated (in milliseconds).
The stale time duration in milliseconds.
Returns
number - The number of milliseconds until the data becomes stale, or 0 if already stale.
resolveStaleTime
Resolves a stale time value, handling both static values and functions.Returns
number | 'static' | undefined - The resolved stale time value.
resolveEnabled
Resolves an enabled value, handling both static values and functions.Returns
boolean | undefined - The resolved enabled value.
sleep
Creates a promise that resolves after a specified timeout.Parameters
The timeout duration in milliseconds.
Returns
Promise<void> - A promise that resolves after the specified timeout.
Array Utilities
addToEnd
Adds an item to the end of an array with optional maximum length.Parameters
The source array.
The item to add.
Maximum array length. If exceeded, items are removed from the start. Use
0 for no limit.Returns
Array<T> - A new array with the item added to the end.
addToStart
Adds an item to the start of an array with optional maximum length.Parameters
The source array.
The item to add.
Maximum array length. If exceeded, items are removed from the end. Use
0 for no limit.Returns
Array<T> - A new array with the item added to the start.
Functional Utilities
functionalUpdate
Applies an updater that can be either a value or a function.Parameters
Either a direct value or a function that takes the input and returns the output.
The input value to pass to the updater function.
Returns
TOutput - If updater is a function, returns the result of calling it with input. Otherwise, returns updater directly.
noop
A no-operation function that does nothing.Example
ensureQueryFn
Ensures a valid query function exists or returns an appropriate fallback.Returns
QueryFunction - A valid query function, or a function that returns a rejected promise if no valid function is available.
shouldThrowError
Determines if an error should be thrown based on configuration.Parameters
Can be a boolean, a function, or undefined.
Parameters to pass to the function if
throwOnError is a function.Returns
boolean - Returns true if errors should be thrown.
Constants
skipToken
A special symbol used to skip query execution.Example
isServer
Boolean indicating if the code is running in a server environment.true if window is undefined or Deno is detected in global scope.