|
26 | 26 | import isEmpty from 'lodash/isEmpty'; |
27 | 27 | import range from 'lodash/range'; |
28 | 28 |
|
29 | | -export const compose = (path1: string, path2: string) => { |
30 | | - let p1 = path1; |
31 | | - if (!isEmpty(path1) && !isEmpty(path2) && !path2.startsWith('[')) { |
32 | | - p1 = path1 + '.'; |
| 29 | +/** |
| 30 | + * Composes two JSON pointer. Pointer1 is appended to pointer2. |
| 31 | + * JSON pointer is seperated and start with '/' e.g: /foo/0 |
| 32 | + * |
| 33 | + * @param {string} pointer1 JSON pointer |
| 34 | + * @param {string} pointer2 JSON pointer |
| 35 | + * @returns {string} resulting JSON pointer |
| 36 | + */ |
| 37 | +export const compose = (pointer1: string, pointer2: string) => { |
| 38 | + let p2 = pointer2; |
| 39 | + if (!isEmpty(pointer2) && !pointer2.startsWith('/')) { |
| 40 | + p2 = '/' + pointer2; |
33 | 41 | } |
34 | 42 |
|
35 | | - if (isEmpty(p1)) { |
36 | | - return path2; |
37 | | - } else if (isEmpty(path2)) { |
38 | | - return p1; |
| 43 | + if (isEmpty(pointer1)) { |
| 44 | + return p2; |
| 45 | + } else if (isEmpty(pointer2)) { |
| 46 | + return pointer1; |
39 | 47 | } else { |
40 | | - return `${p1}${path2}`; |
| 48 | + return `${pointer1}${p2}`; |
41 | 49 | } |
42 | 50 | }; |
43 | 51 |
|
@@ -75,13 +83,25 @@ export const toDataPathSegments = (schemaPath: string): string[] => { |
75 | 83 | * Data paths can be used in field change event handlers like handleChange. |
76 | 84 | * |
77 | 85 | * @example |
78 | | - * toDataPath('#/properties/foo/properties/bar') === 'foo.bar') |
| 86 | + * toDataPath('#/properties/foo/properties/bar') === '/foo/bar') |
79 | 87 | * |
80 | 88 | * @param {string} schemaPath the schema path to be converted |
81 | 89 | * @returns {string} the data path |
82 | 90 | */ |
83 | 91 | export const toDataPath = (schemaPath: string): string => { |
84 | | - return toDataPathSegments(schemaPath).join('.'); |
| 92 | + return '/' + toDataPathSegments(schemaPath).join('/'); |
| 93 | +}; |
| 94 | + |
| 95 | +export const toLodashSegments = (jsonPointer: string): string[] => { |
| 96 | + let path = jsonPointer; |
| 97 | + if (jsonPointer && jsonPointer.startsWith('/')) { |
| 98 | + path = jsonPointer.substring(1); |
| 99 | + } |
| 100 | + return path ? path.split('/').map(decode) : []; |
| 101 | +}; |
| 102 | + |
| 103 | +export const toLodashPath = (jsonPointer: string) => { |
| 104 | + return toLodashSegments(jsonPointer).join('.'); |
85 | 105 | }; |
86 | 106 |
|
87 | 107 | /** |
|
0 commit comments