Skip to content

Commit

Permalink
fix: Fix bugs to ignore Capital letter at the beginning of the Senten…
Browse files Browse the repository at this point in the history
…ce (#6)

* fix bugs

* add comment and get rid of some redundant code

* fix change required by author and passed all the testcase
  • Loading branch information
donganzh authored Apr 29, 2020
1 parent 1775e0f commit 7d23982
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"textlint-scripts": "^2.1.0"
},
"dependencies": {
"array-includes": "^3.0.1",
"array-includes": "^3.0.3",
"is-capitalized": "^1.0.0"
}
}
22 changes: 13 additions & 9 deletions src/textlint-rule-unexpanded-acronym.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Step
3. expandedAcronymList.includes(acronymList)
- Not found Acronym and throw error
*/
export default function (context, options = {}) {
module.exports = function (context, options = {}) {
const minAcronymLength = options.min_acronym_len || defaultOptions.min_acronym_len;
const maxAcronymLength = options.max_acronym_len || defaultOptions.max_acronym_len;
const ignoreAcronymList = options.ignore_acronyms || defaultOptions.ignore_acronyms;
Expand Down Expand Up @@ -67,18 +67,22 @@ export default function (context, options = {}) {
} else if (!includes(acronymJoiningWords, word) // ignore of and...
&& acronymCreator.canExtractAcronym()) {
// Create Acronym
var acronym = acronymCreator.extractAcronym();
if (isWordSatisfy(acronym)) {
expandedAcronymList.push(acronym);
}
var acronyms = acronymCreator.extractAcronym();
acronyms.forEach(acronym => {
if (isWordSatisfy(acronym)) {
expandedAcronymList.push(acronym);
}
});
}
});
if (acronymCreator.canExtractAcronym()) {
// Create Acronym
var acronym = acronymCreator.extractAcronym();
if (isWordSatisfy(acronym)) {
expandedAcronymList.push(acronym);
}
var acronyms = acronymCreator.extractAcronym();
acronyms.forEach(acronym => {
if (isWordSatisfy(acronym)) {
expandedAcronymList.push(acronym);
}
});
}
},
[Syntax.Document + ":exit"](node){
Expand Down
31 changes: 21 additions & 10 deletions src/word-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,29 @@ export function expandOneWordToAcronym(CapitalWord) {
}
/*
* create Acronym from words.
* @param words
* @returns {string}
* @example XMLHttpRequest -> XHR
* @example World Health Organization -> WHO
* @param {string[]} words
* @returns (1)string if only one word (2) array if multiple words
*/
export function expandWordsToAcronym(words) {
//XMLHttpRequest -> XHR
if (words.length === 1) {
return expandOneWordToAcronym(words[0]);
return [expandOneWordToAcronym(words[0])];
}
else{
const result = [];
//In American Broadcast Company -> ["C", "BC", "ABC", "IABC"]
words.reverse().reduce((acronym, word, i) => {
acronym.unshift(word.charAt(0))
result.push(acronym.join(""))
return acronym;
}, []);

//In American Broadcast Company -> ["I", "IA", "IAB", "IABC"]
words.reverse().reduce((acronym, word, i) => {
acronym.push(word.charAt(0));
result.push(acronym.join(""))
return acronym;
}, []);
return result;
}
// World Health Organization -> WHO
return words.reduce((acronym, word) => {
acronym += word.charAt(0);
return acronym;
}, "");
}
4 changes: 3 additions & 1 deletion test/textlint-rule-unexpanded-acronym-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ always remember (or at least consider).this foodstuff's effect on one's ever-exp
Now we know what SOC stands for`,
// capitalized word
"ABC can stand form the Australian Broadcasting Commission",
"XHR is XMLHttpRequest",
"XHR is known as XMLHttpRequest.",
{
text: `
VHSIC stands for "Very High Speed Integrated Circuit".
Expand All @@ -23,6 +23,8 @@ SQL: ([siːkwəl] or ess-cue-el) Structured Query Language.
IEEE: (I triple E) Institute of Electrical and Electronics Engineers
NAACP: (N double A C P) National Association for the Advancement of Colored People
NCAA: (N C double A or N C two A or N C A A) National Collegiate Athletic Association
In the Amazon Web Service, it can be short as AWS.
Enter the Amazon Resource Names(ARN) for the Lambda you created in the setting
`
},
// options
Expand Down

0 comments on commit 7d23982

Please sign in to comment.