json-file-merger
Version:
A Node.js package for merging large JSON files using streams
72 lines • 2.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFileSize = getFileSize;
exports.formatBytes = formatBytes;
exports.calculateOptimalChunkSize = calculateOptimalChunkSize;
exports.getJsonFilesFromDirectory = getJsonFilesFromDirectory;
exports.expandGlobPatterns = expandGlobPatterns;
const fs_1 = require("fs");
const glob_1 = require("glob");
const path_1 = __importDefault(require("path"));
/**
* Gets the size of a file in bytes
* @param filePath Path to the file
* @returns Promise that resolves to the file size in bytes
*/
async function getFileSize(filePath) {
const stats = await fs_1.promises.stat(filePath);
return stats.size;
}
/**
* Formats bytes into a human-readable string
* @param bytes Number of bytes
* @returns Formatted string with appropriate unit (B, KB, MB, GB, TB)
*/
function formatBytes(bytes) {
if (bytes === 0)
return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
}
/**
* Calculates an optimal chunk size for file processing
* @param totalBytes Total size of files to process
* @returns Optimal chunk size in bytes
*/
function calculateOptimalChunkSize(totalBytes) {
// Base chunk size of 64KB
const baseChunkSize = 64 * 1024;
// For files larger than 1GB, use larger chunks
if (totalBytes > 1024 * 1024 * 1024) {
return Math.min(baseChunkSize * 16, 1024 * 1024); // Max 1MB
}
return baseChunkSize;
}
/**
* Gets all JSON files from a directory
* @param directory Path to the directory
* @returns Array of JSON file paths
*/
async function getJsonFilesFromDirectory(directory) {
const pattern = path_1.default.join(directory, '*.json');
return await (0, glob_1.glob)(pattern);
}
/**
* Expands glob patterns to get matching files
* @param patterns Array of glob patterns
* @returns Array of matching file paths
*/
async function expandGlobPatterns(patterns) {
const files = [];
for (const pattern of patterns) {
const matches = await (0, glob_1.glob)(pattern);
files.push(...matches);
}
return files;
}
//# sourceMappingURL=file-utils.js.map