From 52e7ea61d20f56c0a0a60b196d157425d1984caa Mon Sep 17 00:00:00 2001 From: jalal246 Date: Fri, 6 Mar 2020 17:58:55 +0200 Subject: [PATCH] Add new utils with tests --- src/textics.js | 19 +++++++++++-------- src/utils.js | 41 +++++++++++++++++++++++++++++++++++++++++ test/utils.test.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/utils.js create mode 100644 test/utils.test.js diff --git a/src/textics.js b/src/textics.js index 88e1591..7ece217 100644 --- a/src/textics.js +++ b/src/textics.js @@ -1,9 +1,4 @@ -/** - * exclude double char from being count - */ -function excludeDoubleChar(str) { - return /\r\n/.test(str) ? str.replace(/\r\n/g, "\n") : str; -} +const { unifyNewLineChar } = require("./utils"); /** * exclude newline with a start spacing @@ -45,6 +40,14 @@ function count(linesArr) { }; } +function calculateSpaces(lines, str, chars) { + const { length } = str; + + const spaces = lines === 1 ? length - chars : length - chars - lines; + + return spaces; +} + /** * Counts lines, words, chars and spaces for a given string. * @@ -52,7 +55,7 @@ function count(linesArr) { * @returns */ function initCounting(str) { - const pre = excludeDoubleChar(str); + const pre = unifyNewLineChar(str); const init = excludeNewline(pre); @@ -60,7 +63,7 @@ function initCounting(str) { const { lines, words, chars } = count(linesArr); - const spaces = lines === 1 ? pre.length - chars : pre.length - chars - lines; + const spaces = calculateSpaces(lines, pre, chars); return { lines, diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..516fbc9 --- /dev/null +++ b/src/utils.js @@ -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 +}; diff --git a/test/utils.test.js b/test/utils.test.js new file mode 100644 index 0000000..731c625 --- /dev/null +++ b/test/utils.test.js @@ -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); + }); + }); +});