(path: string, contextPath?: string)
| 137 | * @returns 解析后的绝对路径 |
| 138 | */ |
| 139 | export function resolvePath(path: string, contextPath?: string): string { |
| 140 | if (typeof path !== 'string') { |
| 141 | throw new PathError( |
| 142 | 'Path must be a string', |
| 143 | PathErrorCodes.INVALID_PATH, |
| 144 | { path }, |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | // 空路径表示根 |
| 149 | if (path === '') { |
| 150 | return ''; |
| 151 | } |
| 152 | |
| 153 | // 绝对路径直接返回 |
| 154 | if (path.startsWith('/')) { |
| 155 | return path; |
| 156 | } |
| 157 | |
| 158 | // 相对路径需要上下文 |
| 159 | if (!contextPath) { |
| 160 | // 没有上下文时,将相对路径转换为绝对路径 |
| 161 | return '/' + path; |
| 162 | } |
| 163 | |
| 164 | // 解析上下文路径 |
| 165 | const contextTokens = parseJsonPointer(contextPath); |
| 166 | |
| 167 | // 解析相对路径段 |
| 168 | const relativeTokens = path.split('/'); |
| 169 | |
| 170 | // 合并路径 |
| 171 | const resultTokens = [...contextTokens, ...relativeTokens]; |
| 172 | |
| 173 | return toJsonPointer(resultTokens); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * 从数据模型中获取指定路径的数据 |
no test coverage detected