detect-newline
Advanced tools
+25
| declare const detectNewline: { | ||
| /** | ||
| Detect the dominant newline character of a string. | ||
| @returns The detected newline or `undefined` when no newline character is found. | ||
| @example | ||
| ``` | ||
| import detectNewline = require('detect-newline'); | ||
| detectNewline('foo\nbar\nbaz\r\n'); | ||
| //=> '\n' | ||
| ``` | ||
| */ | ||
| (string: string): '\r\n' | '\n' | undefined; | ||
| /** | ||
| Detect the dominant newline character of a string. | ||
| @returns Returns detected newline or `\n` when no newline character is found. | ||
| */ | ||
| graceful(string: string): '\r\n' | '\n'; | ||
| }; | ||
| export = detectNewline; |
+9
-12
| 'use strict'; | ||
| module.exports = function (str) { | ||
| if (typeof str !== 'string') { | ||
| const detectNewline = string => { | ||
| if (typeof string !== 'string') { | ||
| throw new TypeError('Expected a string'); | ||
| } | ||
| var newlines = (str.match(/(?:\r?\n)/g) || []); | ||
| const newlines = string.match(/(?:\r?\n)/g) || []; | ||
| if (newlines.length === 0) { | ||
| return null; | ||
| return; | ||
| } | ||
| var crlf = newlines.filter(function (el) { | ||
| return el === '\r\n'; | ||
| }).length; | ||
| const crlf = newlines.filter(newline => newline === '\r\n').length; | ||
| const lf = newlines.length - crlf; | ||
| var lf = newlines.length - crlf; | ||
| return crlf > lf ? '\r\n' : '\n'; | ||
| }; | ||
| module.exports.graceful = function (str) { | ||
| return module.exports(str) || '\n'; | ||
| }; | ||
| module.exports = detectNewline; | ||
| module.exports.graceful = string => detectNewline(string) || '\n'; |
+37
-35
| { | ||
| "name": "detect-newline", | ||
| "version": "2.1.0", | ||
| "description": "Detect the dominant newline character of a string", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/detect-newline", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=0.10.0" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && ava" | ||
| }, | ||
| "files": [ | ||
| "index.js" | ||
| ], | ||
| "keywords": [ | ||
| "newline", | ||
| "linebreak", | ||
| "line-break", | ||
| "line", | ||
| "lf", | ||
| "crlf", | ||
| "eol", | ||
| "linefeed", | ||
| "character", | ||
| "char" | ||
| ], | ||
| "devDependencies": { | ||
| "ava": "*", | ||
| "xo": "*" | ||
| } | ||
| "name": "detect-newline", | ||
| "version": "3.0.0", | ||
| "description": "Detect the dominant newline character of a string", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/detect-newline", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=8" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && ava && tsd" | ||
| }, | ||
| "files": [ | ||
| "index.js", | ||
| "index.d.ts" | ||
| ], | ||
| "keywords": [ | ||
| "newline", | ||
| "linebreak", | ||
| "line-break", | ||
| "line", | ||
| "lf", | ||
| "crlf", | ||
| "eol", | ||
| "linefeed", | ||
| "character", | ||
| "char" | ||
| ], | ||
| "devDependencies": { | ||
| "ava": "^1.4.1", | ||
| "tsd": "^0.7.2", | ||
| "xo": "^0.24.0" | ||
| } | ||
| } |
+6
-6
@@ -9,3 +9,3 @@ # detect-newline [](https://travis-ci.org/sindresorhus/detect-newline) | ||
| ``` | ||
| $ npm install --save detect-newline | ||
| $ npm install detect-newline | ||
| ``` | ||
@@ -26,9 +26,9 @@ | ||
| ### detectNewline(input) | ||
| ### detectNewline(string) | ||
| Returns detected newline or `null` when no newline character is found. | ||
| Returns the detected newline or `undefined` when no newline character is found. | ||
| ### detectNewline.graceful(input) | ||
| ### detectNewline.graceful(string) | ||
| Returns detected newline or `\n` when no newline character is found. | ||
| Returns the detected newline or `\n` when no newline character is found. | ||
@@ -44,2 +44,2 @@ | ||
| MIT © [Sindre Sorhus](http://sindresorhus.com) | ||
| MIT © [Sindre Sorhus](https://sindresorhus.com) |
Sorry, the diff of this file is not supported yet
3652
17.96%5
25%34
88.89%3
50%