MCPcopy Create free account
hub / github.com/astercloud/aster / addData

Function addData

ui/src/protocol/path-resolver.ts:449–493  ·  view source on GitHub ↗
(data: DataMap, path: string, value: DataValue)

Source from the content-addressed store, hash-verified

447 * @returns 是否添加成功
448 */
449export function addData(data: DataMap, path: string, value: DataValue): boolean {
450 if (!isValidJsonPointer(path)) {
451 return false;
452 }
453
454 // 空路径:合并到根对象
455 if (path === '') {
456 if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
457 Object.assign(data, value);
458 return true;
459 }
460 return false;
461 }
462
463 // 获取当前值
464 const current = getData(data, path);
465
466 // 路径不存在:创建并设置
467 if (current === null) {
468 return setData(data, path, value);
469 }
470
471 // 目标是数组:追加元素
472 if (Array.isArray(current)) {
473 if (Array.isArray(value)) {
474 current.push(...value);
475 }
476 else {
477 current.push(value);
478 }
479 return true;
480 }
481
482 // 目标是对象:合并属性
483 if (typeof current === 'object' && current !== null) {
484 if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
485 Object.assign(current, value);
486 return true;
487 }
488 return false;
489 }
490
491 // 基本类型:无法添加,改为替换
492 return setData(data, path, value);
493}
494
495/**
496 * 移除数据模型中指定路径的数据(remove 操作)

Calls 3

isValidJsonPointerFunction · 0.85
getDataFunction · 0.85
setDataFunction · 0.85

Tested by

no test coverage detected