Skip to content

Commit

Permalink
Seperate functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jalal246 committed Mar 6, 2020
1 parent 20ef2cb commit 9c8e7f3
Showing 1 changed file with 83 additions and 23 deletions.
106 changes: 83 additions & 23 deletions src/textics.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,94 @@
const textics = (str) => {
/**
* exclude double char from being count
*/
function excludeDoubleChar(str) {
return /\r\n/.test(str) ? str.replace(/\r\n/g, "\n") : str;
}

/**
* exclude newline with a start spacing
*/
function excludeNewline(str) {
return /\n /.test(str) ? str.replace(/\n /g, "\n") : str;
}

/**
*
* split each line to array
* @param {*} str
* @returns
*/
function splitToArray(str) {
return str.trim().split(/\r|\n/);
}

function count(linesArr) {
let words = 0;
let chars = 0;

const lines = linesArr.length;

let char;
for (let i = 0; i < lines; i += 1) {
const wordsArr = linesArr[i].trim().split(/\s+/);
for (let k = 0; k < wordsArr.length; k += 1) {
char = wordsArr[k].length;
chars += char;
if (char > 1) words += 1;
}
}

return {
lines,
words,
chars
};
}

/**
* Counts lines, words, chars and spaces for a given string.
*
* @param {string} str
* @returns
*/
function initCounting(str) {
const pre = excludeDoubleChar(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;

return {
lines,
words,
chars,
spaces
};
}

/**
* Validates then calls function count.
*/
function textics(str) {
let lines = 0;
let words = 0;
let chars = 0;
let spaces = 0;
if (str.length > 0) {
let pre = str;
if (/\r\n/.test(str)) pre = str.replace(/\r\n/g, '\n'); // exclude double char from bieng count
let init = pre;
if (/\n /.test(str)) init = pre.replace(/\n /g, '\n'); // exclude newline with a start spacing
const linesArr = init.trim().split(/\r|\n/);
lines = linesArr.length;
let char;
for (let i = 0; i < lines; i += 1) {
const wordsArr = linesArr[i].trim().split(/\s+/);
for (let k = 0; k < wordsArr.length; k += 1) {
char = wordsArr[k].length;
chars += char;
if (char > 1) words += 1;
}
}
spaces = lines === 1
? pre.length - chars
: pre.length - chars - lines;

if (str && typeof str === "string" && str.length > 0) {
({ lines, words, chars, spaces } = initCounting(str));
}

return {
lines,
words,
chars,
spaces,
spaces
};
};
}

export default textics;
module.exports = textics;

0 comments on commit 9c8e7f3

Please sign in to comment.