diff --git a/index.d.ts b/index.d.ts index 150e727..76a858b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -2,7 +2,7 @@ declare const detectNewline: { /** Detect the dominant newline character of a string. - @returns Detected newline or `null` when no newline character is found. + @returns The detected newline or `undefined` when no newline character is found. @example ``` @@ -12,7 +12,7 @@ declare const detectNewline: { //=> '\n' ``` */ - (string: string): '\r\n' | '\n' | null; + (string: string): '\r\n' | '\n' | undefined; /** Detect the dominant newline character of a string. diff --git a/index.js b/index.js index e24045a..919598d 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ 'use strict'; + const detectNewline = string => { if (typeof string !== 'string') { throw new TypeError('Expected a string'); @@ -7,10 +8,10 @@ const detectNewline = string => { const newlines = string.match(/(?:\r?\n)/g) || []; if (newlines.length === 0) { - return null; + return; } - const crlf = newlines.filter(el => el === '\r\n').length; + const crlf = newlines.filter(newline => newline === '\r\n').length; const lf = newlines.length - crlf; return crlf > lf ? '\r\n' : '\n'; diff --git a/index.test-d.ts b/index.test-d.ts index 2cc3210..9593f0f 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; import detectNewline = require('.'); -expectType<'\r\n' | '\n' | null>(detectNewline('foo\nbar\nbaz\r\n')); +expectType<'\r\n' | '\n' | undefined>(detectNewline('foo\nbar\nbaz\r\n')); expectType<'\r\n' | '\n'>(detectNewline.graceful('foo\nbar\nbaz\r\n')); diff --git a/readme.md b/readme.md index c0d2274..f1a1b9a 100644 --- a/readme.md +++ b/readme.md @@ -24,11 +24,11 @@ detectNewline('foo\nbar\nbaz\r\n'); ### 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(string) -Returns detected newline or `\n` when no newline character is found. +Returns the detected newline or `\n` when no newline character is found. ## Related @@ -39,4 +39,4 @@ Returns detected newline or `\n` when no newline character is found. ## License -MIT © [Sindre Sorhus](http://sindresorhus.com) +MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js index a09e013..8d0b183 100644 --- a/test.js +++ b/test.js @@ -6,6 +6,6 @@ test('main', t => { t.is(detectNewline('foo\r\nbar\r\nbaz\n'), '\r\n'); t.is(detectNewline('foo\nbar\nbaz\r\n'), '\n'); t.is(detectNewline('foo\nbar\r\n'), '\n'); - t.is(detectNewline('foo'), null); + t.is(detectNewline('foo'), undefined); t.is(detectNewline.graceful('foo'), '\n'); });