UNPKG

@ocap/util

Version:

utils shared across multiple forge js libs, works in both node.js and browser

26 lines (25 loc) 989 B
//#region src/get-list-field.ts const getByPath = (obj, path) => path.split(".").reduce((o, k) => o?.[k], obj); const getListField = (obj, key) => getByPath(obj, `${key}List`) || getByPath(obj, key) || []; /** * Write a repeated field to its canonical (no-suffix) form and remove the * jspb `xxxList` twin. Keeps pipe code insulated from protobuf vs CBOR * provenance while ensuring only one source of truth remains on `obj`. * * ⚠️ Must use `delete` (not `= undefined`) — see CLAUDE.md Critical Lesson #1. */ const setListField = (obj, key, value) => { delete obj[`${key}List`]; obj[key] = value; }; /** * Remove both `xxx` and `xxxList` twins so the field is absent from any * subsequent spread / pick / serialization. Mirrors `setListField` for * the "clear" case. Uses `delete` for the same reason as `setListField`. */ const deleteListField = (obj, key) => { delete obj[`${key}List`]; delete obj[key]; }; //#endregion export { deleteListField, getListField, setListField };