From 09c418fbcf55a67f47b4c0f2523fefcad2c76642 Mon Sep 17 00:00:00 2001 From: doowb Date: Wed, 29 Mar 2017 14:14:01 -0400 Subject: [PATCH 1/2] add append, appendString, and appendBuffer methods per #1 --- index.js | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- test.js | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index cf33a47..1f79f85 100644 --- a/index.js +++ b/index.js @@ -7,10 +7,14 @@ 'use strict'; var os = require('os'); +var equals = require('buffer-equal'); var normalize = require('normalize-path'); var stripBOM = require('strip-bom-string'); var file = module.exports; +file.cr = new Buffer('\r\n'); +file.nl = new Buffer('\n'); + /** * Normalize slashes in the given filepath to forward slashes. * Note that this is a simple replacement of `\\` with `/`, and @@ -96,3 +100,104 @@ file.stripBOM = function(str) { } return stripBOM(str); }; + +/** + * Append a string or buffer to another string or buffer ensuring to preserve line ending characters. + * + * ```js + * console.log([append(new Buffer('abc\r\n'), new Buffer('def')).toString()]); + * //=> [ 'abc\r\ndef\r\n' ] + * + * console.log([append(new Buffer('abc\n'), new Buffer('def')).toString()]); + * //=> [ 'abc\ndef\n' ] + * + * // uses os.EOL when a line ending is not found + * console.log([append(new Buffer('abc'), new Buffer('def')).toString()]); + * //=> [ 'abc\ndef' ] + * + * console.log([append('abc\r\n', 'def')]); + * //=> [ 'abc\r\ndef\r\n' ] + * + * console.log([append('abc\n', 'def')]); + * //=> [ 'abc\ndef\n' ] + * + * // uses os.EOL when a line ending is not found + * console.log([append('abc', 'def')]); + * //=> [ 'abc\ndef' ] + * ``` + * @param {String|Buffer} `prefix` String or Buffer that will be used to check for an existing line ending. The suffix is appended to this. + * @param {String|Buffer} `suffix` String or Buffer that will be appended to the prefix. + * @return {String|Buffer} Final String or Buffer + * @api public + */ + +file.append = function(prefix, suffix) { + if (Buffer.isBuffer(prefix)) { + return file.appendBuffer(prefix, suffix); + } + return file.appendString(prefix, suffix); +}; + +/** + * Append a string to another string ensuring to preserve line ending characters. + * + * ```js + * console.log([appendString('abc\r\n', 'def')]); + * //=> [ 'abc\r\ndef\r\n' ] + * + * console.log([appendString('abc\n', 'def')]); + * //=> [ 'abc\ndef\n' ] + * + * // uses os.EOL when a line ending is not found + * console.log([appendString('abc', 'def')]); + * //=> [ 'abc\ndef' ] + * ``` + * @param {String} `str` String that will be used to check for an existing line ending. The suffix is appended to this. + * @param {String} `suffix` String that will be appended to the str. + * @return {String} Final String + * @api public + */ + +file.appendString = function(str, suffix) { + var eol; + if (str.slice(-2) === '\r\n') { + eol = '\r\n'; + } else if (str.slice(-1) === '\n') { + eol = '\n'; + } else { + return [str, os.EOL, suffix].join(''); + } + return [str, suffix, eol].join(''); +}; + +/** + * Append a buffer to another buffer ensuring to preserve line ending characters. + * + * ```js + * console.log([appendBuffer(new Buffer('abc\r\n'), new Buffer('def')).toString()]); + * //=> [ 'abc\r\ndef\r\n' ] + * + * console.log([appendBuffer(new Buffer('abc\n'), new Buffer('def')).toString()]); + * //=> [ 'abc\ndef\n' ] + * + * // uses os.EOL when a line ending is not found + * console.log([appendBuffer(new Buffer('abc'), new Buffer('def')).toString()]); + * //=> [ 'abc\ndef' ] + * * ``` + * @param {Buffer} `buf` Buffer that will be used to check for an existing line ending. The suffix is appended to this. + * @param {Buffer} `suffix` Buffer that will be appended to the buf. + * @return {Buffer} Final Buffer + * @api public + */ + +file.appendBuffer = function(buf, suffix) { + var eol; + if (equals(buf.slice(-2), file.cr)) { + eol = file.cr; + } else if (equals(buf.slice(-1), file.nl)) { + eol = file.nl; + } else { + return Buffer.concat([buf, new Buffer(os.EOL), new Buffer(suffix)]); + } + return Buffer.concat([buf, new Buffer(suffix), eol]); +}; diff --git a/package.json b/package.json index d1bcfc9..2217407 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "test": "mocha" }, "dependencies": { + "buffer-equal": "^1.0.0", "normalize-path": "^2.0.1", "strip-bom-string": "^0.1.2" }, @@ -61,4 +62,4 @@ "reflinks": true } } -} \ No newline at end of file +} diff --git a/test.js b/test.js index 022e104..53ecf2f 100644 --- a/test.js +++ b/test.js @@ -33,4 +33,106 @@ describe('file-normalize', function() { it('should strip byte-order marks', function() { assert.equal(file.stripBOM('\ufefffoo\nbar'), 'foo\nbar'); }); + + describe('appendBuffer', function() { + it('should append the buffer suffix to the buffer prefix with \\r\\n', function() { + assert.deepEqual(file.appendBuffer(new Buffer('abc\r\n'), new Buffer('def')), new Buffer('abc\r\ndef\r\n')); + }); + + it('should append the buffer suffix to the buffer prefix with \\n:', function() { + assert.deepEqual(file.appendBuffer(new Buffer('abc\n'), new Buffer('def')), new Buffer('abc\ndef\n')); + }); + + it('should append the buffer suffix to the buffer prefix without EOL', function() { + assert.deepEqual(file.appendBuffer(new Buffer('abc'), new Buffer('def')), new Buffer('abc' + os.EOL + 'def')); + }); + + it('should append the string suffix to the buffer prefix with \\r\\n', function() { + assert.deepEqual(file.appendBuffer(new Buffer('abc\r\n'), 'def'), new Buffer('abc\r\ndef\r\n')); + }); + + it('should append the string suffix to the buffer prefix with \\n:', function() { + assert.deepEqual(file.appendBuffer(new Buffer('abc\n'), 'def'), new Buffer('abc\ndef\n')); + }); + + it('should append the string suffix to the buffer prefix without EOL', function() { + assert.deepEqual(file.appendBuffer(new Buffer('abc'), 'def'), new Buffer('abc' + os.EOL + 'def')); + }); + }); + + describe('appendString', function() { + it('should append the string suffix to the string prefix with \\r\\n', function() { + assert.equal(file.appendString('abc\r\n', 'def'), 'abc\r\ndef\r\n'); + }); + + it('should append the string suffix to the string prefix with \\n:', function() { + assert.equal(file.appendString('abc\n', 'def'), 'abc\ndef\n'); + }); + + it('should append the string suffix to the string prefix without EOL', function() { + assert.equal(file.appendString('abc', 'def'), 'abc' + os.EOL + 'def'); + }); + + it('should append the buffer suffix to the string prefix with \\r\\n', function() { + assert.equal(file.appendString('abc\r\n', new Buffer('def')), 'abc\r\ndef\r\n'); + }); + + it('should append the buffer suffix to the string prefix with \\n:', function() { + assert.equal(file.appendString('abc\n', new Buffer('def')), 'abc\ndef\n'); + }); + + it('should append the buffer suffix to the string prefix without EOL', function() { + assert.equal(file.appendString('abc', new Buffer('def')), 'abc' + os.EOL + 'def'); + }); + }); + + describe('append', function() { + it('should append the buffer suffix to the buffer prefix with \\r\\n', function() { + assert.deepEqual(file.append(new Buffer('abc\r\n'), new Buffer('def')), new Buffer('abc\r\ndef\r\n')); + }); + + it('should append the buffer suffix to the buffer prefix with \\n:', function() { + assert.deepEqual(file.append(new Buffer('abc\n'), new Buffer('def')), new Buffer('abc\ndef\n')); + }); + + it('should append the buffer suffix to the buffer prefix without EOL', function() { + assert.deepEqual(file.append(new Buffer('abc'), new Buffer('def')), new Buffer('abc' + os.EOL + 'def')); + }); + + it('should append the string suffix to the string prefix with \\r\\n', function() { + assert.equal(file.append('abc\r\n', 'def'), 'abc\r\ndef\r\n'); + }); + + it('should append the string suffix to the string prefix with \\n:', function() { + assert.equal(file.append('abc\n', 'def'), 'abc\ndef\n'); + }); + + it('should append the string suffix to the string prefix without EOL', function() { + assert.equal(file.append('abc', 'def'), 'abc' + os.EOL + 'def'); + }); + + it('should append the string suffix to the buffer prefix with \\r\\n', function() { + assert.deepEqual(file.append(new Buffer('abc\r\n'), 'def'), new Buffer('abc\r\ndef\r\n')); + }); + + it('should append the string suffix to the buffer prefix with \\n:', function() { + assert.deepEqual(file.append(new Buffer('abc\n'), 'def'), new Buffer('abc\ndef\n')); + }); + + it('should append the string suffix to the buffer prefix without EOL', function() { + assert.deepEqual(file.append(new Buffer('abc'), 'def'), new Buffer('abc' + os.EOL + 'def')); + }); + + it('should append the buffer suffix to the string prefix with \\r\\n', function() { + assert.equal(file.append('abc\r\n', new Buffer('def')), 'abc\r\ndef\r\n'); + }); + + it('should append the buffer suffix to the string prefix with \\n:', function() { + assert.equal(file.append('abc\n', new Buffer('def')), 'abc\ndef\n'); + }); + + it('should append the buffer suffix to the string prefix without EOL', function() { + assert.equal(file.append('abc', new Buffer('def')), 'abc' + os.EOL + 'def'); + }); + }); }); From 172345e11298b09ddbb9f9f51c969377b1ab3a66 Mon Sep 17 00:00:00 2001 From: doowb Date: Wed, 29 Mar 2017 15:54:13 -0400 Subject: [PATCH 2/2] add eol override to normalizeEOL tests --- test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test.js b/test.js index 53ecf2f..0d6e611 100644 --- a/test.js +++ b/test.js @@ -24,10 +24,10 @@ describe('file-normalize', function() { }); it('should normalize EOL to unix newlines', function() { - assert.equal(file.normalizeEOL('foo\r\nbar'), 'foo\nbar'); - assert.equal(file.normalizeEOL('foo\r\n\r\nbar'), 'foo\n\nbar'); - assert.equal(file.normalizeEOL('foo\rbar'), 'foo\nbar'); - assert.equal(file.normalizeEOL('foo\nbar'), 'foo\nbar'); + assert.equal(file.normalizeEOL('foo\r\nbar', '\n'), 'foo\nbar'); + assert.equal(file.normalizeEOL('foo\r\n\r\nbar', '\n'), 'foo\n\nbar'); + assert.equal(file.normalizeEOL('foo\rbar', '\n'), 'foo\nbar'); + assert.equal(file.normalizeEOL('foo\nbar', '\n'), 'foo\nbar'); }); it('should strip byte-order marks', function() {