UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

148 lines 5.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.batchAction = exports.batchQuery = exports.queryNodeByPath = exports.ActionType = void 0; var ActionType; (function (ActionType) { ActionType["create"] = "create"; ActionType["delete"] = "delete"; ActionType["update"] = "update"; })(ActionType || (exports.ActionType = ActionType = {})); function excludeProperties(node, excludes) { const newNode = { ...node?.toJSON?.() ?? node }; if (Array.isArray(excludes) && excludes.length) { excludes.forEach((exclude) => { delete newNode[exclude]; }); } return newNode; } // "app.configuration.groups[name=system].properties[name=system.custom.log.level]" function splitPath(path) { path = path || ''; const res = []; let prevDot = -1; let countLeftBracket = 0; for (let i = 0; i < path.length; i += 1) { if (path[i] === '[') countLeftBracket += 1; else if (path[i] === ']') countLeftBracket -= 1; if (path[i] === '.' && countLeftBracket === 0) { res.push(path.slice(prevDot + 1, i)); prevDot = i; } } res.push(path.slice(prevDot + 1)); return res; } /** * 通过路径查找节点 * @param json 根节点 * @param path * @returns */ function queryNodeByPath(json, path) { const pathArr = splitPath(path); let pathItem = pathArr.shift(); let node = json; while (pathItem) { const matchArr = pathItem.match(/\[(.+?)\]/); const arrayPropertyKey = pathItem.split('[')[0]; const arrOption = matchArr && matchArr[1] && matchArr[1].split('='); if (arrayPropertyKey && arrayPropertyKey !== 'app') { node = node?.[arrayPropertyKey]; if (Array.isArray(arrOption) && arrOption.length && Array.isArray(node)) { if (arrOption.length === 2) { node = node.filter((nodeItem) => nodeItem[arrOption[0]] === arrOption[1])[0]; } else { const index = +arrOption[0]; node = node[index]; } } } pathItem = pathArr.shift(); } return node; } exports.queryNodeByPath = queryNodeByPath; /** * 在 JSON 中批量查询 * @param json * @param queryList 查询列表 */ async function batchQuery(json, queryList) { const queryResultList = queryList.map(({ path, excludes }) => { const node = queryNodeByPath(json, path); if (node) { if (Array.isArray(node)) { return node.map((nodeItem) => excludeProperties(nodeItem, excludes)); } return excludeProperties(node, excludes); } return null; }); return queryResultList; } exports.batchQuery = batchQuery; /** * 在 JSON 中批量操作 * @param json * @param actionList 操作列表 */ async function batchAction(json, actionList) { actionList.forEach((query) => { const { action, path, object } = query; const node = queryNodeByPath(json, path); const pathArr = path?.split('.') || []; const pathItem = pathArr.pop(); const arrayPropertyKey = pathItem.split('[')[0]; let propertyIndex = -1; if (pathItem !== arrayPropertyKey) { pathArr.push(arrayPropertyKey); const matchArr = pathItem.match(/\[(.+?)\]/); const arrOption = matchArr && matchArr[1] && matchArr[1].split('='); if (Array.isArray(arrOption) && arrOption.length) { if (arrOption.length === 1) { const index = +arrOption[0]; propertyIndex = index; } } } const parentPath = pathArr.join('.'); let parentNode = queryNodeByPath(json, parentPath); if (!parentNode) { pathArr.pop(); const parentPath = pathArr.join('.'); parentNode = queryNodeByPath(json, parentPath); if (pathItem !== arrayPropertyKey) { parentNode[arrayPropertyKey] = []; } else { parentNode[arrayPropertyKey] = {}; } parentNode = parentNode[arrayPropertyKey]; } switch (action) { case ActionType.create: parentNode.splice(propertyIndex, 0, object); break; case ActionType.delete: { const index = parentNode.indexOf(node); if (index !== -1) { parentNode.splice(index, 1); } break; } case ActionType.update: for (const key in object) { if (Object.prototype.hasOwnProperty.call(object, key)) { node[key] = object[key]; } } break; } }); } exports.batchAction = batchAction; //# sourceMappingURL=jsoner.js.map