UNPKG

@jsenv/core

Version:

Tool to develop, test and build js projects

27 lines (25 loc) 856 B
export const ensureUnixLineBreaks = (stringOrBuffer) => { if (typeof stringOrBuffer === "string") { const stringWithLinuxBreaks = stringOrBuffer.replace(/\r\n/g, "\n"); return stringWithLinuxBreaks; } return ensureUnixLineBreaksOnBuffer(stringOrBuffer); }; // https://github.com/nodejs/help/issues/1738#issuecomment-458460503 const ensureUnixLineBreaksOnBuffer = (buffer) => { const int32Array = new Int32Array(buffer, 0, buffer.length); const int32ArrayWithLineBreaksNormalized = int32Array.filter( (element, index, typedArray) => { if (element === 0x0d) { if (typedArray[index + 1] === 0x0a) { // Windows -> Unix return false; } // Mac OS -> Unix typedArray[index] = 0x0a; } return true; }, ); return Buffer.from(int32ArrayWithLineBreaksNormalized); };