|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
505 bytes
|
| Line | |
|---|
| 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';
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.