-
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
1 changed file
with
83 additions
and
23 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
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; |