UNPKG

pgsql-deparser

Version:
98 lines (97 loc) 3.18 kB
"use strict"; /* eslint-disable no-restricted-syntax */ Object.defineProperty(exports, "__esModule", { value: true }); exports.cleanTreeWithStmt = exports.cleanTree = exports.transform = exports.cleanLines = void 0; const cleanLines = (sql) => { return sql .split('\n') .map((l) => l.trim()) .filter((a) => a) .join('\n'); }; exports.cleanLines = cleanLines; const transform = (obj, props) => { let copy = null; // Handle the 3 simple types, and null or undefined if (obj == null || typeof obj !== 'object') { return obj; } // Handle Date if (obj instanceof Date) { copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { copy = []; for (let i = 0, len = obj.length; i < len; i++) { copy[i] = (0, exports.transform)(obj[i], props); } return copy; } // Handle Object if (obj instanceof Object || typeof obj === 'object') { copy = {}; for (const attr in obj) { if (obj.hasOwnProperty(attr)) { if (props.hasOwnProperty(attr)) { if (typeof props[attr] === 'function') { copy[attr] = props[attr](obj[attr]); } else if (props[attr].hasOwnProperty(obj[attr])) { copy[attr] = props[attr][obj[attr]]; } else { copy[attr] = (0, exports.transform)(obj[attr], props); } } else { copy[attr] = (0, exports.transform)(obj[attr], props); } } else { copy[attr] = (0, exports.transform)(obj[attr], props); } } return copy; } throw new Error("Unable to copy obj! Its type isn't supported."); }; exports.transform = transform; const noop = () => undefined; const cleanTree = (tree) => { return (0, exports.transform)(tree, { stmt_len: noop, stmt_location: noop, location: noop, DefElem: (obj) => { if (obj.defname === 'as') { if (Array.isArray(obj.arg) && obj.arg.length) { // function obj.arg[0].String.sval = obj.arg[0].String.sval.trim(); } else if (obj.arg.List && obj.arg.List.items) { // function obj.arg.List.items[0].String.sval = obj.arg.List.items[0].String.sval.trim(); } else { // do stmt obj.arg.String.sval = obj.arg.String.sval.trim(); } return (0, exports.cleanTree)(obj); } else { return (0, exports.cleanTree)(obj); } } }); }; exports.cleanTree = cleanTree; const cleanTreeWithStmt = (tree) => { return (0, exports.transform)(tree, { stmt_location: noop, location: noop }); }; exports.cleanTreeWithStmt = cleanTreeWithStmt;