-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* \r = CR (Carriage Return) → Used as a new line character in Mac OS before X | ||
* \n = LF (Line Feed) → Used as a new line character in Unix/Mac OS X | ||
* \r\n = CR + LF → Used as a new line character in Windows | ||
* | ||
* It replaces \r or \r\n with \n. Which enable us to deal with unique new line | ||
* character. | ||
* | ||
* @see {https://stackoverflow.com/a/15433225/6348157} | ||
* @param {string} str | ||
* @returns {string} modified with str with \n as new line | ||
*/ | ||
function unifyNewLineChar(str) { | ||
return /\r|\r\n/.test(str) ? str.replace(/\r|\r\n/g, "\n") : str; | ||
} | ||
|
||
/** | ||
* Checks string, if there is spaces in new lines, it removes it. | ||
* | ||
* @param {string} str | ||
* @returns {string} -without spaces in new lines | ||
*/ | ||
function excludeNewline(str) { | ||
return /\n /.test(str) ? str.replace(/\n /g, "\n") : str; | ||
} | ||
|
||
/** | ||
* Splits each line of given string to array element. | ||
* | ||
* @param {string} str | ||
* @returns {Array} | ||
*/ | ||
function str2Array(str) { | ||
return str.trim().split(/\r|\n/); | ||
} | ||
|
||
module.exports = { | ||
unifyNewLineChar, | ||
excludeNewline, | ||
str2Array | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-env mocha */ | ||
|
||
const { expect } = require("chai"); | ||
const { | ||
unifyNewLineChar, | ||
excludeNewline, | ||
str2Array | ||
} = require("../src/utils.js"); | ||
|
||
describe.only("utils", () => { | ||
describe("unifyNewLineChar", () => { | ||
it("replaces R", () => { | ||
const TXT_SAMPLE_R = "Hello\rThere\r!"; | ||
|
||
const result = unifyNewLineChar(TXT_SAMPLE_R); | ||
|
||
expect(/\r/.test(result)).to.be.equal(false); | ||
expect(/\n/.test(result)).to.be.equal(true); | ||
}); | ||
|
||
it("replaces RN", () => { | ||
const TXT_SAMPLE_R = "Hello\r\nThere\r\n!"; | ||
|
||
const result = unifyNewLineChar(TXT_SAMPLE_R); | ||
|
||
expect(/\r\n/.test(result)).to.be.equal(false); | ||
expect(/\n/.test(result)).to.be.equal(true); | ||
}); | ||
|
||
it("returns the same input", () => { | ||
const TXT_SAMPLE_R = "Hello\nThere\n!"; | ||
|
||
const result = unifyNewLineChar(TXT_SAMPLE_R); | ||
|
||
expect(/\n/.test(result)).to.be.equal(true); | ||
}); | ||
}); | ||
}); |