Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract getNewLineChar functionality. #4

Merged
merged 1 commit into from
Mar 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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