UNPKG

postgrejs

Version:

Professional PostgreSQL client NodeJS

38 lines (37 loc) 1.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringifyArrayLiteral = stringifyArrayLiteral; const array_calculatedim_js_1 = require("./array-calculatedim.js"); function stringifyArrayLiteral(value, options, encode) { const dim = (0, array_calculatedim_js_1.arrayCalculateDim)(value); const writeDim = (arr, level) => { const elemCount = dim[level]; const out = []; for (let i = 0; i < elemCount; i++) { let x = arr && arr[i]; if (level < dim.length - 1) { if (x != null && !Array.isArray(x)) x = [x]; out.push(writeDim(x, level + 1)); continue; } // if value is null if (x == null) { out.push('NULL'); continue; } if (Array.isArray(x)) { out.push(stringifyArrayLiteral(x, options, encode)); continue; } if (encode) x = encode(x, options || {}); out.push(escapeArrayItem('' + x)); } return '{' + out.join(',') + '}'; }; return writeDim(value, 0); } function escapeArrayItem(str) { return '"' + str.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'; }