Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require Node.js 8, add TypeScript definition (#7)
- Loading branch information
1 parent
6d96229
commit 841de59
Showing
12 changed files
with
95 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
* text=auto | ||
* text=auto eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
sudo: false | ||
language: node_js | ||
node_js: | ||
- '5' | ||
- '4' | ||
- '0.12' | ||
- '0.10' | ||
- '10' | ||
- '8' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
declare const detectNewline: { | ||
/** | ||
Detect the dominant newline character of a string. | ||
@returns Detected newline or `null` when no newline character is found. | ||
@example | ||
``` | ||
import detectNewline = require('detect-newline'); | ||
detectNewline('foo\nbar\nbaz\r\n'); | ||
//=> '\n' | ||
``` | ||
*/ | ||
(string: string): '\r\n' | '\n' | null; | ||
|
||
/** | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
'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; | ||
} | ||
|
||
var crlf = newlines.filter(function (el) { | ||
return el === '\r\n'; | ||
}).length; | ||
|
||
var lf = newlines.length - crlf; | ||
const crlf = newlines.filter(el => el === '\r\n').length; | ||
const 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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import {expectType} from 'tsd'; | ||
import detectNewline = require('.'); | ||
|
||
expectType<'\r\n' | '\n' | null>(detectNewline('foo\nbar\nbaz\r\n')); | ||
expectType<'\r\n' | '\n'>(detectNewline.graceful('foo\nbar\nbaz\r\n')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,9 @@ | ||
The MIT License (MIT) | ||
MIT License | ||
|
||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
{ | ||
"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": "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": ">=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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import test from 'ava'; | ||
import m from './'; | ||
import detectNewline from '.'; | ||
|
||
test(t => { | ||
t.is(m('foo\r\nbar\r\nbaz\n\n\n'), '\n'); | ||
t.is(m('foo\r\nbar\r\nbaz\n'), '\r\n'); | ||
t.is(m('foo\nbar\nbaz\r\n'), '\n'); | ||
t.is(m('foo\nbar\r\n'), '\n'); | ||
t.is(m('foo'), null); | ||
t.is(m.graceful('foo'), '\n'); | ||
test('main', t => { | ||
t.is(detectNewline('foo\r\nbar\r\nbaz\n\n\n'), '\n'); | ||
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.graceful('foo'), '\n'); | ||
}); |