diff --git a/README.md b/README.md index 200401a..58a7ede 100644 --- a/README.md +++ b/README.md @@ -27,5 +27,13 @@ var eol = require('eol') - Normalize line endings in text to CR (Mac OS) - @return string with line endings normalized to `\r` +### `eol.before(text)` +- Add linebreak before text +- @return string with linebreak added before text + +### `eol.after(text)` +- Add linebreak after text +- @return string with linebreak added after text + ## License MIT diff --git a/eol.js b/eol.js index 2113f95..958c45b 100644 --- a/eol.js +++ b/eol.js @@ -5,8 +5,17 @@ var api = {} var isWindows = typeof process != 'undefined' && 'win32' === process.platform + var linebreak = isWindows ? '\r\n' : '\n' var newline = /\r\n|\r|\n/g + function before(text) { + return linebreak + text + } + + function after(text) { + return text + linebreak + } + function converts(to) { return function(text) { return text.replace(newline, to) @@ -16,7 +25,8 @@ api['lf'] = converts('\n') api['cr'] = converts('\r') api['crlf'] = converts('\r\n') - api['auto'] = converts(isWindows ? '\r\n' : '\n') - + api['auto'] = converts(linebreak) + api['before'] = before + api['after'] = after return api }); diff --git a/package.json b/package.json index ced52b5..3b08d44 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "eol", "description": "Newline character converter", - "version": "0.3.0", + "version": "0.4.0", "homepage": "https://github.com/ryanve/eol", "license": "MIT", "author": "Ryan Van Etten", diff --git a/test.js b/test.js index 7f03a62..0a7e3a0 100644 --- a/test.js +++ b/test.js @@ -39,5 +39,10 @@ return eol.auto(sample) === eol[method](sample); }) === 2) + aok('before', eol.lf(eol.before('text')) === '\ntext') + aok('before2', eol.lf(eol.before('\ntext\n')) === '\n\ntext\n') + aok('after', eol.lf(eol.after('text')) === 'text\n') + aok('after2', eol.lf(eol.after('\ntext\n')) === '\ntext\n\n') + aok.log('All tests passed =)') }(this);