Skip to content

Commit

Permalink
Merge pull request #3 from jonschlinkert/append
Browse files Browse the repository at this point in the history
add append, appendString, and appendBuffer methods (closes #1)
  • Loading branch information
doowb authored Mar 29, 2017
2 parents 2ace0e8 + 172345e commit eee53fa
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 5 deletions.
105 changes: 105 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"test": "mocha"
},
"dependencies": {
"buffer-equal": "^1.0.0",
"normalize-path": "^2.0.1",
"strip-bom-string": "^0.1.2"
},
Expand Down Expand Up @@ -61,4 +62,4 @@
"reflinks": true
}
}
}
}
110 changes: 106 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,115 @@ 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() {
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');
});
});
});

0 comments on commit eee53fa

Please sign in to comment.