diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8f9d77e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +indent_style = tab +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[{package.json,*.yml}] +indent_style = space +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..2a2f1c6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,5 @@ +language: node_js +node_js: + - 'stable' + - '0.12' + - '0.10' diff --git a/index.js b/index.js new file mode 100644 index 0000000..da31efd --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +'use strict'; + +var fn = module.exports = function (x) { + return fn.end(fn.start(x)); +}; + +fn.start = function (x) { + return x.replace(/^[\r\n]+/, ''); +}; + +fn.end = function (x) { + return x.replace(/[\r\n]+$/, ''); +}; diff --git a/license b/license new file mode 100644 index 0000000..654d0bf --- /dev/null +++ b/license @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Sindre Sorhus (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: + +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. diff --git a/package.json b/package.json new file mode 100644 index 0000000..b3ac117 --- /dev/null +++ b/package.json @@ -0,0 +1,42 @@ +{ + "name": "trim-newlines", + "version": "0.0.0", + "description": "Trim newlines from the start and/or end of a string", + "license": "MIT", + "repository": "sindresorhus/trim-newlines", + "author": { + "name": "Sindre Sorhus", + "email": "sindresorhus@gmail.com", + "url": "sindresorhus.com" + }, + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "xo && ava" + }, + "files": [ + "index.js" + ], + "keywords": [ + "trim", + "newline", + "newlines", + "linebreak", + "lf", + "crlf", + "left", + "right", + "start", + "end", + "string", + "str", + "remove", + "delete", + "strip" + ], + "devDependencies": { + "ava": "*", + "xo": "*" + } +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..fedb3ca --- /dev/null +++ b/readme.md @@ -0,0 +1,46 @@ +# trim-newlines [![Build Status](https://travis-ci.org/sindresorhus/trim-newlines.svg?branch=master)](https://travis-ci.org/sindresorhus/trim-newlines) + +> Trim [newlines](https://en.wikipedia.org/wiki/Newline) from the start and/or end of a string + + +## Install + +``` +$ npm install --save trim-newlines +``` + + +## Usage + +```js +var trimNewlines = require('trim-newlines'); + +trimNewlines('\nunicorn\r\n'); +//=> 'unicorn' +``` + + +## API + +### trimNewlines(input) + +Trim from the start and end of a string. + +### trimNewlines.start(input) + +Trim from the start of a string. + +### trimNewlines.end(input) + +Trim from the end of a string. + + +## Related + +- [trim-left](https://github.com/sindresorhus/trim-left) - Similar to `String#trim()` but removes only whitespace on the left +- [trim-right](https://github.com/sindresorhus/trim-right) - Similar to `String#trim()` but removes only whitespace on the right. + + +## License + +MIT © [Sindre Sorhus](http://sindresorhus.com) diff --git a/test.js b/test.js new file mode 100644 index 0000000..ec9529d --- /dev/null +++ b/test.js @@ -0,0 +1,28 @@ +import test from 'ava'; +import fn from './'; + +test('main', t => { + t.is(fn('\nx\n'), 'x'); + t.is(fn('\n\n\nx\n\n\n'), 'x'); + t.is(fn('\r\nx\r\n'), 'x'); + t.is(fn('\n\r\n\nx\n\r\n\n'), 'x'); + t.end(); +}); + +test('start', t => { + t.is(fn.start('\nx'), 'x'); + t.is(fn.start('\r\nx'), 'x'); + t.is(fn.start('\n\n\n\nx'), 'x'); + t.is(fn.start('\n\n\r\n\nx'), 'x'); + t.is(fn.start('x\n\n\r\n\n'), 'x\n\n\r\n\n'); + t.end(); +}); + +test('end', t => { + t.is(fn.end('x\n'), 'x'); + t.is(fn.end('x\r\n'), 'x'); + t.is(fn.end('x\n\n\n\n'), 'x'); + t.is(fn.end('x\n\n\r\n\n'), 'x'); + t.is(fn.end('\n\n\r\n\nx'), '\n\n\r\n\nx'); + t.end(); +});