index.js
1 'use strict'; 2 3 const detectNewline = string => { 4 if (typeof string !== 'string') { 5 throw new TypeError('Expected a string'); 6 } 7 8 const newlines = string.match(/(?:\r?\n)/g) || []; 9 10 if (newlines.length === 0) { 11 return; 12 } 13 14 const crlf = newlines.filter(newline => newline === '\r\n').length; 15 const lf = newlines.length - crlf; 16 17 return crlf > lf ? '\r\n' : '\n'; 18 }; 19 20 module.exports = detectNewline; 21 module.exports.graceful = string => (typeof string === 'string' && detectNewline(string)) || '\n';