Skip to content

Commit

Permalink
Merge pull request #4 from jalal246/dev
Browse files Browse the repository at this point in the history
Extract getNewLineChar functionality.
  • Loading branch information
jalal246 committed Mar 11, 2020
2 parents f6f5bb4 + 979e8f9 commit 6007a1d
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/textics.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
const NL_R = /\r/g;
const NL_RN = /\r\n/g;
const NL_N = /\n/g;

const regSpace = /\s/g;

/**
* validates string
*
* @param {string} str
* @returns {boolean}
*/
function isValid(str) {
return str && typeof str === "string" && str.length > 0;
}

/**
* Extracts new line used char in a given string.
*
* @param {string} str
* @returns {string}
*/
function getNewLineChar(str) {
const NL_R = /\r/g;
const NL_RN = /\r\n/g;
const NL_N = /\n/g;

let lineChar = NL_N;

if (NL_R.test(str)) {
lineChar = NL_R;
} else if (NL_RN.test(str)) {
lineChar = NL_RN;
}

return lineChar;
}

/**
* Counts lines, words, chars and spaces for a given string.
*
Expand All @@ -29,13 +51,9 @@ function textics(str) {
};
}

let regNewLine = NL_N;
const regNewLine = getNewLineChar(str);

if (NL_R.test(str)) {
regNewLine = NL_R;
} else if (NL_RN.test(str)) {
regNewLine = NL_RN;
}
const regSpace = /\s/g;

/**
* Getting total string length.
Expand Down

0 comments on commit 6007a1d

Please sign in to comment.