-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (35 loc) · 992 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
var os = require('os');
/**
* 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
*/
module.exports = function appendString(str, suffix) {
if (!suffix || !suffix.length) {
return str;
}
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('');
};