{"version":3,"file":"transformQuery.mjs","names":[],"sources":["../src/utils/transformQuery.ts"],"sourcesContent":["/**\n * Recursively steps through a query object ({@link index!RuleGroupType RuleGroupType} or {@link index!RuleGroupTypeIC RuleGroupTypeIC}),\n * passing each {@link index!RuleType RuleType} object to a provided `ruleProcessor` function and returning a\n * new query object if there were any referential changes.\n *\n * @module transformQuery\n */\n\nimport type { RuleGroupType, RuleGroupTypeAny, RuleGroupTypeIC, RuleType } from '../types';\nimport { isRuleGroup, isRuleGroupType } from './isRuleGroup';\nimport { isUnsafeKey } from './objectUtils';\n\nconst remapProperties = (\n  obj: Record<string, unknown>,\n  propertyMap: Record<string, string | false>,\n  deleteRemappedProperties: boolean\n): Record<string, unknown> => {\n  const result: Record<string, unknown> = {};\n\n  for (const key in obj) {\n    if (isUnsafeKey(key)) continue;\n    const mappedKey = propertyMap[key];\n    if (mappedKey === false) {\n      continue;\n    }\n    if (mappedKey && key !== mappedKey) {\n      if (!isUnsafeKey(mappedKey)) {\n        result[mappedKey] = obj[key];\n      }\n      if (!deleteRemappedProperties) {\n        result[key] = obj[key];\n      }\n    } else {\n      result[key] = obj[key];\n    }\n  }\n\n  return result;\n};\n\n/**\n * Options object for {@link index!transformQuery transformQuery}.\n */\nexport interface TransformQueryOptions<RG extends RuleGroupTypeAny = RuleGroupType> {\n  /**\n   * When a rule is encountered in the hierarchy, it will be replaced\n   * with the result of this function.\n   *\n   * @defaultValue `r => r`\n   */\n  // oxlint-disable-next-line typescript/no-explicit-any\n  ruleProcessor?: (rule: RuleType) => any;\n  /**\n   * When a group is encountered in the hierarchy (including the root group, the\n   * query itself), it will be replaced with the result of this function.\n   *\n   * @defaultValue `rg => rg`\n   */\n  // oxlint-disable-next-line typescript/no-explicit-any\n  ruleGroupProcessor?: (ruleGroup: RG) => Record<string, any>;\n  /**\n   * For each rule and group in the query, any properties matching a key\n   * in this object will be renamed to the corresponding value. To retain both\n   * the new _and_ the original properties, set `deleteRemappedProperties`\n   * to `false`.\n   *\n   * If a key has a value of `false`, the corresponding property will be removed\n   * without being copied to a new property name. (Warning: `{ rules: false }`\n   * will prevent recursion and only return the processed root group.)\n   *\n   * @defaultValue `{}`\n   *\n   * @example\n   * ```\n   *   transformQuery(\n   *     { combinator: 'and', not: true, rules: [] },\n   *     { propertyMap: { combinator: 'AndOr', not: false } }\n   *   )\n   *   // Returns: { AndOr: 'and', rules: [] }\n   * ```\n   */\n  propertyMap?: Record<string, string | false>;\n  /**\n   * Any combinator values (including independent combinators) will be translated\n   * from the key in this object to the value.\n   *\n   * @defaultValue `{}`\n   *\n   * @example\n   * ```\n   *   transformQuery(\n   *     { combinator: 'and', rules: [] },\n   *     { combinatorMap: { and: '&&', or: '||' } }\n   *   )\n   *   // Returns: { combinator: '&&', rules: [] }\n   * ```\n   */\n  combinatorMap?: Record<string, string>;\n  /**\n   * Any operator values will be translated from the key in this object to the value.\n   *\n   * @defaultValue `{}`\n   *\n   * @example\n   * ```\n   *   transformQuery(\n   *     { combinator: 'and', rules: [{ field: 'name', operator: '=', value: 'Steve Vai' }] },\n   *     { operatorMap: { '=': 'is' } }\n   *   )\n   *   // Returns:\n   *   // {\n   *   //   combinator: 'and',\n   *   //   rules: [{ field: 'name', operator: 'is', value: 'Steve Vai' }]\n   *   // }\n   * ```\n   */\n  operatorMap?: Record<string, string>;\n  /**\n   * Prevents the `path` property (see {@link index!Path Path}) from being added to each\n   * rule and group in the hierarchy.\n   *\n   * @defaultValue `false`\n   */\n  omitPath?: boolean;\n  /**\n   * Original properties remapped according to the `propertyMap` option will be removed.\n   *\n   * @defaultValue `true`\n   *\n   * @example\n   * ```\n   *   transformQuery(\n   *     { combinator: 'and', rules: [] },\n   *     { propertyMap: { combinator: 'AndOr' }, deleteRemappedProperties: false }\n   *   )\n   *   // Returns: { combinator: 'and', AndOr: 'and', rules: [] }\n   * ```\n   */\n  deleteRemappedProperties?: boolean;\n}\n\n/**\n * Recursively process a query heirarchy using this versatile utility function.\n *\n * [Documentation](https://react-querybuilder.js.org/docs/utils/misc#transformquery)\n */\nexport function transformQuery(\n  query: RuleGroupType,\n  options?: TransformQueryOptions\n  // oxlint-disable-next-line typescript/no-explicit-any\n): any;\n/**\n * Recursively process a query heirarchy with independent combinators using this\n * versatile utility function.\n *\n * [Documentation](https://react-querybuilder.js.org/docs/utils/misc#transformquery)\n */\nexport function transformQuery(\n  query: RuleGroupTypeIC,\n  options?: TransformQueryOptions<RuleGroupTypeIC>\n  // oxlint-disable-next-line typescript/no-explicit-any\n): any;\nexport function transformQuery<RG extends RuleGroupTypeAny>(\n  query: RG,\n  options: TransformQueryOptions<RG> = {}\n) {\n  const {\n    ruleProcessor = r => r,\n    ruleGroupProcessor = rg => rg,\n    propertyMap = {},\n    combinatorMap = {},\n    operatorMap = {},\n    omitPath = false,\n    deleteRemappedProperties = true,\n  } = options;\n\n  // oxlint-disable-next-line typescript/no-explicit-any\n  const processGroup = (rg: RuleGroupTypeAny): any => ({\n    ...ruleGroupProcessor(\n      remapProperties(\n        {\n          ...rg,\n          ...(isRuleGroupType(rg)\n            ? { combinator: combinatorMap[rg.combinator] ?? rg.combinator }\n            : {}),\n        },\n        propertyMap,\n        deleteRemappedProperties\n      ) as RG\n    ),\n    ...(propertyMap['rules'] === false\n      ? null\n      : {\n          // oxlint-disable-next-line typescript/no-explicit-any\n          [propertyMap['rules'] ?? 'rules']: rg.rules.map((r: any, idx) => {\n            const pathObject = omitPath ? null : { path: [...rg.path!, idx] };\n            if (typeof r === 'string') {\n              // independent combinators\n              return combinatorMap[r] ?? r;\n            } else if (isRuleGroup(r)) {\n              // sub-groups\n              return processGroup({ ...r, ...pathObject });\n            }\n            // rules\n            return ruleProcessor(\n              remapProperties(\n                {\n                  ...r,\n                  ...pathObject,\n                  ...('operator' in r ? { operator: operatorMap[r.operator] ?? r.operator } : {}),\n                },\n                propertyMap,\n                deleteRemappedProperties\n              ) as unknown as RuleType\n            );\n          }),\n        }),\n  });\n\n  return processGroup({ ...query, ...(omitPath ? null : { path: [] }) });\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