From 3b9e0d6b95050bca4d8fe6ce355b6bc3d3e94aaa Mon Sep 17 00:00:00 2001 From: Arvind S Date: Sat, 3 Oct 2020 16:56:13 +0530 Subject: [PATCH 1/4] moved getValueOrEmptyString to stringUtils --- src/apexmodels/apexModel.js | 23 +++++++++-------------- src/utils/stringUtils.js | 13 +++++++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 src/utils/stringUtils.js diff --git a/src/apexmodels/apexModel.js b/src/apexmodels/apexModel.js index 69c95b0..a41244f 100644 --- a/src/apexmodels/apexModel.js +++ b/src/apexmodels/apexModel.js @@ -1,6 +1,9 @@ +const StringUtils = require('../utils/stringUtils'); + class ApexModel { constructor(accessModifiers) { this.accessModifiers = accessModifiers; + this.stringUtils = new StringUtils(); } getNameLine() { @@ -18,7 +21,7 @@ class ApexModel { } getDescription() { - return this.getValueOrEmptyString(this.description); + return this.stringUtils.getValueOrEmptyString(this.description); } setDescription(description) { @@ -26,7 +29,7 @@ class ApexModel { } getAuthor() { - return this.getValueOrEmptyString(this.author); + return this.stringUtils.getValueOrEmptyString(this.author); } setAuthor(author) { @@ -34,7 +37,7 @@ class ApexModel { } getDate() { - return this.getValueOrEmptyString(this.date); + return this.stringUtils.getValueOrEmptyString(this.date); } setDate(date) { @@ -42,7 +45,7 @@ class ApexModel { } getReturns() { - return this.getValueOrEmptyString(this.returns); + return this.stringUtils.getValueOrEmptyString(this.returns); } setReturns(returns) { @@ -50,7 +53,7 @@ class ApexModel { } getExample() { - return this.getValueOrEmptyString(this.example); + return this.stringUtils.getValueOrEmptyString(this.example); } setExample(example) { @@ -58,21 +61,13 @@ class ApexModel { } getScope() { - return this.getValueOrEmptyString(this.scope); + return this.stringUtils.getValueOrEmptyString(this.scope); } setScope(scope) { this.scope = scope; } - getValueOrEmptyString(value) { - if(!value) { - return ''; - } - - return value; - } - parseScope() { this.scope = null; diff --git a/src/utils/stringUtils.js b/src/utils/stringUtils.js new file mode 100644 index 0000000..cfebf46 --- /dev/null +++ b/src/utils/stringUtils.js @@ -0,0 +1,13 @@ +class StringUtils { + + /** + * @summary + * Returns an empty string if its a null-ish value, otherwise returns + * the original string. + * + * @param {string} str + */ + getValueOrEmptyString(str) { + return str ? str : ''; + } +} \ No newline at end of file From c42d64817d5a31c178bed10f03868c2286c6a492 Mon Sep 17 00:00:00 2001 From: Arvind S Date: Sat, 3 Oct 2020 17:02:51 +0530 Subject: [PATCH 2/4] moved setPrevWord to stringUtils --- src/apexParsing/apexParser.js | 26 -------------------------- src/apexmodels/methodModel.js | 28 +--------------------------- src/utils/stringUtils.js | 26 +++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 54 deletions(-) diff --git a/src/apexParsing/apexParser.js b/src/apexParsing/apexParser.js index 3327cf7..2dbcb67 100644 --- a/src/apexParsing/apexParser.js +++ b/src/apexParsing/apexParser.js @@ -417,32 +417,6 @@ class ApexParser { }); } - strPrevWord(str, iSearch) { - if (!str || iSearch >= str.length) { - return null; - } - - let iStart; - let iEnd = 0; - for (iStart = iSearch - 1; iStart >= 0; iStart--) { - if (iEnd === 0) { - if (str.charAt(iStart) === ' ') { - continue; - } - iEnd = iStart + 1; - } else if (str.charAt(iStart) === ' ') { - iStart++; - break; - } - } - - if (iStart === -1) { - return null; - } else { - return str.substring(iStart, iEnd); - } - } - countChars(str, ch) { let count = 0; for (let i = 0; i < str.length; ++i) { diff --git a/src/apexmodels/methodModel.js b/src/apexmodels/methodModel.js index a96594c..1ec66e9 100644 --- a/src/apexmodels/methodModel.js +++ b/src/apexmodels/methodModel.js @@ -39,38 +39,12 @@ class MethodModel extends ApexModel { if (nameLine && nameLine.length > 0) { const lastindex = nameLine.indexOf("("); if (lastindex >= 0) { - const methodName = this.strPrevWord(nameLine, lastindex); + const methodName = this.stringUtils.getPrevWord(nameLine, lastindex); return methodName; } } return ""; } - - strPrevWord(str, iSearch) { - if (!str || iSearch >= str.length) { - return null; - } - - let iStart; - let iEnd = 0; - for (iStart = iSearch - 1; iStart >= 0; iStart--) { - if (iEnd === 0) { - if (str.charAt(iStart) === ' ') { - continue; - } - iEnd = iStart + 1; - } else if (str.charAt(iStart) === ' ') { - iStart++; - break; - } - } - - if (iStart === -1) { - return null; - } else { - return str.substring(iStart, iEnd); - } - } } module.exports = MethodModel; \ No newline at end of file diff --git a/src/utils/stringUtils.js b/src/utils/stringUtils.js index cfebf46..a0a857a 100644 --- a/src/utils/stringUtils.js +++ b/src/utils/stringUtils.js @@ -10,4 +10,28 @@ class StringUtils { getValueOrEmptyString(str) { return str ? str : ''; } -} \ No newline at end of file + + /** + * + * @param {string} str + * @param {number} iSearch + */ + getPrevWord(str, iSearch) { + if (!str || iSearch >= str.length) return null; + let iStart = 0, iEnd = 0; + for (iStart = iSearch - 1; iStart >= 0; iStart--) { + if (iEnd === 0) { + if (str.charAt(iStart) === ' ') continue; + iEnd = iStart + 1; + } + else if (str.charAt(iStart) === ' ') { + iStart++; + break; + } + } + + return iStart < 0 ? null : str.substring(iStart, iEnd); + } +} + +module.exports = StringUtils; \ No newline at end of file From 9e9850557c2e67fefcda36eb4aa4548637122273 Mon Sep 17 00:00:00 2001 From: Arvind S Date: Sat, 3 Oct 2020 17:44:56 +0530 Subject: [PATCH 3/4] moved countChars and strContainingScope to stringUtils --- src/apexParsing/apexParser.js | 28 +++++----------------------- src/apexmodels/apexModel.js | 12 +----------- src/utils/stringUtils.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 34 deletions(-) diff --git a/src/apexParsing/apexParser.js b/src/apexParsing/apexParser.js index 2dbcb67..63e33fc 100644 --- a/src/apexParsing/apexParser.js +++ b/src/apexParsing/apexParser.js @@ -3,11 +3,13 @@ const fs = require('fs'); const ClassModel = require('../apexmodels/classModel.js'); const MethodModel = require('../apexmodels/methodModel.js'); const PropertyModel = require('../apexmodels/propertyModel.js'); +const StringUtils = require('../utils/stringUtils'); class ApexParser { constructor(accessModifiers,sourceDirectory) { this.accessModifiers = accessModifiers; this.sourceDirectory = sourceDirectory; + this.stringUtils = new StringUtils(); } parseFileContents(filePath) { @@ -93,8 +95,8 @@ class ApexParser { } // keep track of our nesting so we know which class we are in - let openCurlies = this.countChars(strLine, '{'); - let closeCurlies = this.countChars(strLine, '}'); + let openCurlies = this.stringUtils.countChars(strLine, '{'); + let closeCurlies = this.stringUtils.countChars(strLine, '}'); nestedCurlyBraceDepth += openCurlies; nestedCurlyBraceDepth -= closeCurlies; @@ -123,7 +125,7 @@ class ApexParser { } // ignore lines not dealing with scope - if (!this.strContainsScope(strLine) && + if (!this.stringUtils.getMatchingSubstring(strLine, this.accessModifiers) && // interface methods don't have scope !(cModel && cModel.getIsInterface() @@ -416,26 +418,6 @@ class ApexParser { } }); } - - countChars(str, ch) { - let count = 0; - for (let i = 0; i < str.length; ++i) { - if (str.charAt(i) === ch) { - ++count; - } - } - return count; - } - - strContainsScope(str) { - str = str.toLowerCase(); - for (let i = 0; i < this.accessModifiers.length; i++) { - if (str.toLowerCase().includes(this.accessModifiers[i].toLowerCase() + " ")) { - return this.accessModifiers[i]; - } - } - return null; - } } module.exports = ApexParser; \ No newline at end of file diff --git a/src/apexmodels/apexModel.js b/src/apexmodels/apexModel.js index a41244f..4519a30 100644 --- a/src/apexmodels/apexModel.js +++ b/src/apexmodels/apexModel.js @@ -72,23 +72,13 @@ class ApexModel { this.scope = null; if(this.nameLine) { - let str = this.strContainsScope(this.nameLine); + let str = this.stringUtils.getMatchingSubstring(this.nameLine, this.accessModifiers); if (str) { this.scope = str; } this.scope = this.nameLine; } } - - strContainsScope(str) { - str = str.toLowerCase(); - for (let i = 0; i < this.accessModifiers.length; i++) { - if (str.toLowerCase().includes(this.accessModifiers[i].toLowerCase() + " ")) { - return this.accessModifiers[i]; - } - } - return null; - } } module.exports = ApexModel; \ No newline at end of file diff --git a/src/utils/stringUtils.js b/src/utils/stringUtils.js index a0a857a..42552e9 100644 --- a/src/utils/stringUtils.js +++ b/src/utils/stringUtils.js @@ -32,6 +32,40 @@ class StringUtils { return iStart < 0 ? null : str.substring(iStart, iEnd); } + + /** + * + * @param {string} str + * @param {string} ch + */ + countChars(str, ch) { + let count = 0; + for (let i = 0; i < str.length; ++i) { + if (str.charAt(i) === ch) { + ++count; + } + } + return count; + } + + /** + * @summary + * Does a case-insensitive match to check if any of the options exist + * in the original string. It returns the string that exists, if any. + * Otherwise it returns null. + * + * @param {string} str + * @param {string[]} options + */ + getMatchingSubstring(str, options) { + const s = str.toLowerCase(); + for (let i = 0; i < options.length; i++) { + if (s.includes(options[i].toLowerCase() + " ")) { + return options[i]; + } + } + return null; + } } module.exports = StringUtils; \ No newline at end of file From 59124fcdb8f584ed095d4c34995c63684fc1067d Mon Sep 17 00:00:00 2001 From: Arvind S Date: Sat, 3 Oct 2020 18:48:38 +0530 Subject: [PATCH 4/4] added more documentation for string utils --- src/utils/stringUtils.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/stringUtils.js b/src/utils/stringUtils.js index 42552e9..e45293b 100644 --- a/src/utils/stringUtils.js +++ b/src/utils/stringUtils.js @@ -12,6 +12,8 @@ class StringUtils { } /** + * @summary + * Returns the previous word just before a given index in a string. * * @param {string} str * @param {number} iSearch @@ -34,6 +36,9 @@ class StringUtils { } /** + * @summary + * Fairly obvious, counts the number of times a character occurs + * in a string. * * @param {string} str * @param {string} ch