glob.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. // test src/glob.ts
  6. import { describe, expect, it } from 'vitest';
  7. import { Glob } from '@flowgram.ai/form';
  8. describe('glob', () => {
  9. it('return original path array if no *', () => {
  10. const obj = { a: { b: { c: 1 } } };
  11. expect(Glob.findMatchPaths(obj, 'a.b.c')).toEqual(['a.b.c']);
  12. });
  13. it('object: when * is in middle of the path', () => {
  14. const obj = {
  15. a: { b: { c: 1 } },
  16. x: { y: { z: 2 } },
  17. };
  18. expect(Glob.findMatchPaths(obj, 'a.*.c')).toEqual(['a.b.c']);
  19. });
  20. it('object:when * is at the end of the path', () => {
  21. const obj = {
  22. a: { b: { c: 1 } },
  23. x: { y: { z: 2 } },
  24. };
  25. expect(Glob.findMatchPaths(obj, 'a.*')).toEqual(['a.b']);
  26. });
  27. it('object:when * is at the start of the path', () => {
  28. const obj = {
  29. a: { b: { c: 1 } },
  30. x: { y: { z: 2 } },
  31. };
  32. expect(Glob.findMatchPaths(obj, '*.y')).toEqual(['x.y']);
  33. });
  34. it('array: when * is at the end of the path', () => {
  35. const obj = {
  36. other: 100,
  37. arr: [
  38. {
  39. x: 1,
  40. y: { a: 1, b: 2 },
  41. },
  42. {
  43. x: 10,
  44. y: {
  45. a: 10,
  46. b: 20,
  47. },
  48. },
  49. ],
  50. };
  51. expect(Glob.findMatchPaths(obj, 'arr.*')).toEqual(['arr.0', 'arr.1']);
  52. });
  53. it('array: when * is at the start of the path', () => {
  54. const arr = [
  55. {
  56. x: 1,
  57. y: { a: 1, b: 2 },
  58. },
  59. {
  60. x: 10,
  61. y: {
  62. a: 10,
  63. b: 20,
  64. },
  65. },
  66. ];
  67. expect(Glob.findMatchPaths(arr, '*')).toEqual(['0', '1']);
  68. });
  69. it('array: when * is in the middle of the path', () => {
  70. const obj = {
  71. other: 100,
  72. arr: [
  73. {
  74. x: 1,
  75. y: { a: 1, b: 2 },
  76. },
  77. {
  78. x: 10,
  79. y: {
  80. a: 10,
  81. b: 20,
  82. },
  83. },
  84. ],
  85. };
  86. expect(Glob.findMatchPaths(obj, 'arr.*.y')).toEqual(['arr.0.y', 'arr.1.y']);
  87. });
  88. });