UNPKG

@lark-project/cli

Version:

飞书项目插件开发工具

43 lines (42 loc) 1.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyMutation = exports.parsePath = void 0; function parsePath(path) { const segs = []; for (const part of path.split('.')) { const m = part.match(/^([^[\]]+)((?:\[\d+\])*)$/); if (!m) throw new Error(`Invalid path segment: ${part}`); segs.push(m[1]); for (const im of m[2].match(/\[\d+\]/g) || []) { segs.push(Number(im.slice(1, -1))); } } return segs; } exports.parsePath = parsePath; function applyMutation(config, mutation) { const clone = structuredClone(config); const segs = parsePath(mutation.path); let parent = clone; for (let i = 0; i < segs.length - 1; i++) { parent = parent[segs[i]]; // 中间段缺失或为基本类型时必须抛错:否则后面 `parent[last] = value` 会在 // 字符串/数字上静默 no-op,探针作者只会看到一条"stored 无 diff"的假通过。 if (parent == null || typeof parent !== 'object') { throw new Error(`Path not navigable at segment "${segs[i]}" (not an object/array): ${mutation.path}`); } } const last = segs[segs.length - 1]; if (mutation.op === 'delete') { if (Array.isArray(parent) && typeof last === 'number') parent.splice(last, 1); else delete parent[last]; } else { parent[last] = mutation.value; } return clone; } exports.applyMutation = applyMutation;