Skip to content

Commit

Permalink
Add new utils with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed Mar 6, 2020
1 parent 9c8e7f3 commit 52e7ea6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/textics.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -45,22 +40,30 @@ 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.
*
* @param {string} str
* @returns
*/
function initCounting(str) {
const pre = excludeDoubleChar(str);
const pre = unifyNewLineChar(str);

const init = excludeNewline(pre);

const linesArr = splitToArray(init);

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,
Expand Down
41 changes: 41 additions & 0 deletions src/utils.js
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
};
38 changes: 38 additions & 0 deletions test/utils.test.js
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);
});
});
});

0 comments on commit 52e7ea6

Please sign in to comment.