utils.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { find, mergeWith } from 'lodash';
  2. import { FormFeedback, FormPathService } from '@flowgram.ai/form-core';
  3. import { FormValidateReturn } from '@flowgram.ai/form/src/types';
  4. import { type FieldModel, FieldName } from '@flowgram.ai/form';
  5. import { EffectOptions } from './types';
  6. export function findMatchedInMap<T = any>(
  7. field: FieldModel<any>,
  8. validateMap: Record<FieldName, T> | undefined
  9. ): T | undefined {
  10. if (!validateMap) {
  11. return;
  12. }
  13. if (validateMap[field.name]) {
  14. return validateMap[field.name];
  15. }
  16. const found = find(Object.keys(validateMap), (key) => {
  17. if (key.startsWith('regex:')) {
  18. const regex = RegExp(key.split(':')[1]);
  19. return regex.test(field.name);
  20. }
  21. return false;
  22. });
  23. if (found) {
  24. return validateMap[found];
  25. }
  26. }
  27. export function formFeedbacksToNodeCoreFormFeedbacks(
  28. formFeedbacks: FormValidateReturn
  29. ): FormFeedback[] {
  30. return formFeedbacks.map(
  31. (f) =>
  32. ({
  33. feedbackStatus: f.level,
  34. feedbackText: f.message,
  35. path: f.name,
  36. } as FormFeedback)
  37. );
  38. }
  39. export function convertGlobPath(path: string) {
  40. if (path.startsWith('/')) {
  41. const parts = FormPathService.normalize(path).slice(1).split('/');
  42. return parts.join('.');
  43. }
  44. return path;
  45. }
  46. export function mergeEffectMap(
  47. origin: Record<string, EffectOptions[]>,
  48. source: Record<string, EffectOptions[]>
  49. ) {
  50. return mergeWith(origin, source, function (objValue: EffectOptions[], srcValue: EffectOptions[]) {
  51. return (objValue || []).concat(srcValue);
  52. });
  53. }