/** * null => '' * @param {*} data 要处理的数据 */ let null2str = function null2str(data) { for (let x in data) { if (data[x] === null) { // 如果是null 把直接内容转为 '' data[x] = ''; } else { if (Array.isArray(data[x])) { // 是数组遍历数组 递归继续处理 data[x] = data[x].map(z => { return null2str(z); }); } if (typeof (data[x]) === 'object') { // 是json 递归继续处理 data[x] = null2str(data[x]) } } } return data; } /** * 生成sql * @param [] strs 要处理的数据 sql_count、sql * @param {JSON} reqParams 请求参数 */ let replaceParams = function replaceParams(str, reqParas) { let pattern = /([#|$](\s{0,}){(\s{0,})(.*?)(\s{0,})})/g; let match = str[1].match(pattern); let status = 1; let msg = ''; if (!match) { } else { for (let index = 0; index < match.length; index++) { let paramName = getBraceStr(match[index]).replace(/\s*/g, "") if (!(paramName in reqParas)) { status = 0; msg = 'parameter ' + paramName + ' is not provided'; } str[0] = str[0].replace(match[index], reqParas[paramName]); str[1] = str[1].replace(match[index], reqParas[paramName]); } } return { "status": status, 'sql_count': str[0], 'sql': str[1], 'msg': msg }; } /** * 生成sql * @param [] strs 要处理的数据 sql_count、sql * @param {JSON} reqParams 请求参数 */ let replaceSyncParams = function replaceParams(str, reqParas) { let pattern = /([#|$](\s{0,}){(\s{0,})(.*?)(\s{0,})})/g; let match = str.match(pattern); let status = 1; let msg = ''; if (!match) { } else { for (let index = 0; index < match.length; index++) { let paramName = getBraceStr(match[index]).replace(/\s*/g, "") if (!(paramName in reqParas)) { status = 0; msg = 'parameter ' + paramName + ' is not provided'; } str = str.replace(match[index], reqParas[paramName]); } } return { "status": status, 'sql': str, 'msg': msg }; } /** * 取出大括号内的内容 * @param text * @returns {string} */ let getBraceStr = function getParenthesesStr(text) { let result = '' if (isObjEmpty(text)) return result let regex = /\{(.+?)\}/g; let options = text.match(regex) if (!isObjEmpty(options)) { let option = options[0] if (!isObjEmpty(option)) { result = option.substring(1, option.length - 1) } } return result } /** * 取出中括号内的内容 * @param text * @returns {string} */ let getBracketStr = function getBracketStr(text) { let result = '' if (isObjEmpty(text)) return result let regex = /\[(.+?)\]/g; let options = text.match(regex) if (!isObjEmpty(options)) { let option = options[0] if (!isObjEmpty(option)) { result = option.substring(1, option.length - 1) } } return result } /** * 取出小括号内的内容 * @param text * @returns {string} */ let getParenthesesStr = function getParenthesesStr(text) { let result = '' if (isObjEmpty(text)) return result let regex = /\((.+?)\)/g; let options = text.match(regex) if (!isObjEmpty(options)) { let option = options[0] if (!isObjEmpty(option)) { result = option.substring(1, option.length - 1) } } return result } function isObjEmpty(obj) { var name; for (name in obj) { return false; } return true; } function isStrEmpty(obj) { if(obj === null) return true; if(obj == '') return true; return false; } module.exports = { null2str, replaceParams, replaceSyncParams, getBraceStr, getBracketStr, getParenthesesStr, isObjEmpty, isStrEmpty }