import unzip from "unzip-stream"; /** * Convert a snake-cased string to a camel-cased one. * @param {string} str Original string. * @returns {string} Transformed string. */ export const snakeToCamelCase = str => str.replace(/([-_][a-z])/gu, group => group.toUpperCase().replace("-", "").replace("_", "")); /** * Check if a value is a JS object. * @param {*} value Value to check. * @returns {boolean} Whether `value` is a JS object. */ export const isObject = value => value !== null && typeof value === "object"; /** * Check if two arrays are equal in a shallow manner. * @param {Array} array1 First array. * @param {Array} array2 Second array. * @returns {boolean} Whether the two arrays are equal. */ export const arraysEqual = (array1, array2) => ( array1.length === array2.length && array1.every((elt1, index) => elt1 === array2[index]) ); /** * Find a file in a zipped stream and unzip it. * @param {stream.Readable} data Input zipped stream. * @param {string} fileName Name of the file to find. * @returns {Promise.} Stream of the unzipped file. */ export const unzipFile = (data, fileName) => new Promise((res, rej) => { // eslint-disable-next-line new-cap const stream = data.pipe(unzip.Parse()); let found = false; stream.on("entry", entry => { if (entry.type !== "File" || entry.path !== fileName) { entry.autodrain(); return; } found = true; res(entry); }); stream.on("end", () => { if (!found) { rej(new Error(`File ${fileName} not found in archive`)); } }); stream.on("error", err => rej(err)); }); /** * Display the time component of a date. * @param {Date} date Date to display. * @returns {string} Formatted date as 'HH:MM:SS'. */ export const displayTime = date => [ date.getHours(), date.getMinutes(), date.getSeconds() ].map(number => number.toString().padStart(2, "0")).join(":");