(data: DataValue, path: string)
| 323 | * @returns 路径是否存在 |
| 324 | */ |
| 325 | export function hasPath(data: DataValue, path: string): boolean { |
| 326 | if (!isValidJsonPointer(path)) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | if (path === '') { |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | const tokens = parseJsonPointer(path); |
| 335 | let current: DataValue | undefined = data; |
| 336 | |
| 337 | for (const token of tokens) { |
| 338 | if (current === null || current === undefined) { |
| 339 | return false; |
| 340 | } |
| 341 | |
| 342 | if (Array.isArray(current)) { |
| 343 | const index = parseInt(token, 10); |
| 344 | if (isNaN(index) || index < 0 || index >= current.length) { |
| 345 | return false; |
| 346 | } |
| 347 | current = current[index] as DataValue | undefined; |
| 348 | } |
| 349 | else if (typeof current === 'object') { |
| 350 | const obj = current as DataMap; |
| 351 | if (!(token in obj)) { |
| 352 | return false; |
| 353 | } |
| 354 | current = obj[token]; |
| 355 | } |
| 356 | else { |
| 357 | return false; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | return true; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * 删除数据模型中指定路径的数据 |
no test coverage detected