Skip to content

walkQuery

Category
Export size
min 1.20 kB · gzip 0.59 kB

Walks every property of a Feathers query (including nested $and/$or/$nor arrays) and calls the walker function for each one. The walker receives the property name, operator, value, path, and a stop function, and can return a replacement value. Calling stop() halts the traversal early. Returns a new query only if changes were made.

ts
  import {  } from 'feathers-utils/utils';

Examples

ts
import { walkQuery } from 'feathers-utils/utils'

const query = walkQuery({ age: { $gt: '18' } }, ({ value, operator }) => {
  if (operator === '$gt') return Number(value)
})
// => { age: { $gt: 18 } }
ts
// stop early once a property is found
let found = false
walkQuery(query, ({ property, stop }) => {
  if (property === 'isTemplate') {
    found = true
    stop()
  }
})

Type declaration

Show Type Declarations
ts
export type WalkQueryOptions = {
  property: string
  operator: string | undefined
  value: any
  path: (string | number)[]
  /**
   * Stops the traversal. Any replacement value returned from the current walker
   * call is still applied, but no further properties are visited.
   */
  stop: () => void
}
export type WalkQueryCallback = (options: WalkQueryOptions) => any
/**
 * Walks every property of a Feathers query (including nested `$and`/`$or`/`$nor` arrays)
 * and calls the `walker` function for each one. The walker receives the property name, operator,
 * value, path, and a `stop` function, and can return a replacement value. Calling `stop()` halts
 * the traversal early. Returns a new query only if changes were made.
 *
 * @example
 * ```ts
 *
 *
 * const query = walkQuery({ age: { $gt: '18' } }, ({ value, operator }) => {
 *   if (operator === '$gt') return Number(value)
 * })
 * // => { age: { $gt: 18 } }
 * ```
 *
 * @example
 * ```ts
 * // stop early once a property is found
 * let found = false
 * walkQuery(query, ({ property, stop }) => {
 *   if (property === 'isTemplate') {
 *     found = true
 *     stop()
 *   }
 * })
 * ```
 *
 * @see https://utils.feathersjs.com/utils/walk-query.html
 */
export declare const walkQuery: <Q extends Query>(
  query: Q,
  walker: WalkQueryCallback,
) => Q
ArgumentTypeDescription
queryQ
walkerWalkQueryCallback

Released under the MIT License.