Skip to content

zipDataResult ​

Category
Tags
Export size
min 6.66 kB · gzip 2.74 kB

Pairs each item in context.data with its corresponding item in context.result by index. Handles both single-item and array data, normalizing them into an array of { data, result } pairs. Only works in after/around hooks for create, update, and patch methods.

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

Example ​

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

const pairs = zipDataResult(context)
pairs.forEach(({ data, result }) => { /* process each pair *\/ })

Type declaration ​

Show Type Declarations
ts
export type ZipDataResultOptions = {
  /**
   * Called if `context.data` and `context.result` are arrays of different
   * lengths.
   */
  onMismatch?: (context: HookContext) => void
  /**
   * How the items of `context.data` and `context.result` are paired: by their
   * position, or by the id of the data item - a data item without an id by
   * its position, if no other data item took that result by its id. Ids are
   * compared as strings, so bson `ObjectId`s match as well. The pairs are in
   * the order of `context.data`, followed by the results no data item got.
   *
   * @default 'index'
   *
   * @example
   * ```ts
   * // on a multi create with ids in the data, whatever order the result has
   * const pairs = zipDataResult(context, { by: 'id' })
   * ```
   */
  by?: "index" | "id"
}
export type ZipDataResultItem<D, R> = {
  data: D | undefined
  result: R | undefined
}
/**
 * Pairs each item in `context.data` with its corresponding item in `context.result` by index.
 * Handles both single-item and array data, normalizing them into an array of `{ data, result }` pairs.
 * Only works in `after`/`around` hooks for `create`, `update`, and `patch` methods.
 *
 * @example
 * ```ts
 *
 *
 * const pairs = zipDataResult(context)
 * pairs.forEach(({ data, result }) => { /* process each pair *\/ })
 * ```
 *
 * @see https://utils.feathersjs.com/utils/zip-data-result.html
 */
export declare function zipDataResult<
  H extends HookContext,
  D extends DataSingleHookContext<H> = DataSingleHookContext<H>,
  R extends ResultSingleHookContext<H> = ResultSingleHookContext<H>,
>(context: H, options?: ZipDataResultOptions): ZipDataResultItem<D, R>[]
ArgumentTypeDescription
contextH
optionsZipDataResultOptions

Options ​

onMismatch(context: HookContext) => void

Called if context.data and context.result are arrays of different lengths.

by'index' | 'id'default'index'

How the items of context.data and context.result are paired: by their position, or by the id of the data item - a data item without an id by its position, if no other data item took that result by its id. Ids are compared as strings, so bson ObjectIds match as well. The pairs are in the order of context.data, followed by the results no data item got.

Example
ts
// on a multi create with ids in the data, whatever order the result has
const pairs = zipDataResult(context, { by: 'id' })

Pairing by Id ​

By default, the items are paired by their position. On a multi create with ids in the data, by: 'id' pairs them by their id instead, whatever order the result has:

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

const pairs = zipDataResult(context, { by: 'id' })
  • A data item with an id gets the result with the same id, compared as strings, so bson ObjectIds match as well. Without such a result, result is undefined.
  • A data item without an id gets the result at its position, unless another data item took that result by its id.
  • The pairs are in the order of context.data, followed by the results no data item got, with data: undefined.

Released under the MIT License.