UNPKG

jsonweaver

Version:

A simple utility to transform JSON data into CSV, XML, YAML, JSONLines and Markdown table formats.

30 lines (29 loc) 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toJsonLines = toJsonLines; exports.toJsonLinesStream = toJsonLinesStream; const stream_1 = require("stream"); function toJsonLines(jsonArray, options) { if (!Array.isArray(jsonArray)) { throw new Error('Input must be an array of JSON objects.'); } if (!jsonArray.every(item => item && typeof item === 'object' && !Array.isArray(item))) { throw new Error('All elements in the array must be valid JSON objects.'); } const { indent = 0, excludeKeys = [] } = options || {}; return jsonArray .map(obj => { const filteredObj = Object.assign({}, obj); excludeKeys.forEach(key => delete filteredObj[key]); return JSON.stringify(filteredObj, null, indent); }) .join('\n'); } ; function toJsonLinesStream(jsonArray) { if (!Array.isArray(jsonArray)) { throw new Error('Input must be an array of JSON objects.'); } return stream_1.Readable.from(jsonArray.map(obj => JSON.stringify(obj) + '\n')); } ;