Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

textlint-rule-en-capitalization

Package Overview
Dependencies
Maintainers
3
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

textlint-rule-en-capitalization - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

38

lib/captalize.js

@@ -5,4 +5,5 @@ // MIT © 2017 azu

Object.defineProperty(exports, "__esModule", {
value: true
value: true
});
exports.lowerFirstCharacter = exports.upperFirstCharacter = exports.isCapitalized = exports.getPosFromSingleWord = void 0;

@@ -12,10 +13,13 @@ var _require = require("en-pos"),

var getPosFromSingleWord = exports.getPosFromSingleWord = function getPosFromSingleWord(word) {
var tags = new Tag([word]).initial() // initial dictionary and pattern based tagging
.smooth().tags; // further context based smoothing
return tags[0];
var getPosFromSingleWord = function getPosFromSingleWord(word) {
var tags = new Tag([word]).initial() // initial dictionary and pattern based tagging
.smooth().tags; // further context based smoothing
return tags[0];
};
var isCapitalized = exports.isCapitalized = function isCapitalized(string) {
return (/^[A-Z]/.test(string)
);
exports.getPosFromSingleWord = getPosFromSingleWord;
var isCapitalized = function isCapitalized(string) {
return /^[A-Z]/.test(string);
};

@@ -27,4 +31,8 @@ /**

*/
var upperFirstCharacter = exports.upperFirstCharacter = function upperFirstCharacter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
exports.isCapitalized = isCapitalized;
var upperFirstCharacter = function upperFirstCharacter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};

@@ -35,5 +43,11 @@ /**

*/
var lowerFirstCharacter = exports.lowerFirstCharacter = function lowerFirstCharacter(string) {
return string.charAt(0).toLocaleLowerCase() + string.slice(1);
exports.upperFirstCharacter = upperFirstCharacter;
var lowerFirstCharacter = function lowerFirstCharacter(string) {
return string.charAt(0).toLocaleLowerCase() + string.slice(1);
};
exports.lowerFirstCharacter = lowerFirstCharacter;
//# sourceMappingURL=captalize.js.map

@@ -16,26 +16,31 @@ // MIT © 2017 azu

var REPORT_TYPE = {
Heading: "Heading",
Paragraph: "Paragraph",
List: "List"
Heading: "Heading",
Paragraph: "Paragraph",
List: "List"
};
var shouldNotCapitalized = function shouldNotCapitalized(string, allowWords) {
// allow words
var shouldAllowed = allowWords.some(function (allowWord) {
return allowWord === string;
});
if (shouldAllowed) {
return true;
}
// A quotation
if (!/^\w/.test(string)) {
return true;
}
// proper word
var pos = (0, _captalize.getPosFromSingleWord)(string);
if (/^NNP/.test(pos)) {
return true;
}
return false;
// allow words
var shouldAllowed = allowWords.some(function (allowWord) {
return allowWord === string;
});
if (shouldAllowed) {
return true;
} // A quotation
if (!/^\w/.test(string)) {
return true;
} // proper word
var pos = (0, _captalize.getPosFromSingleWord)(string);
if (/^NNP/.test(pos)) {
return true;
}
return false;
};
/**

@@ -52,126 +57,145 @@ * @param node

*/
var checkNode = function checkNode(_ref) {
var node = _ref.node,
Syntax = _ref.Syntax,
getSource = _ref.getSource,
report = _ref.report,
RuleError = _ref.RuleError,
fixer = _ref.fixer,
allowFigures = _ref.allowFigures,
allowWords = _ref.allowWords,
reportType = _ref.reportType;
var node = _ref.node,
Syntax = _ref.Syntax,
getSource = _ref.getSource,
report = _ref.report,
RuleError = _ref.RuleError,
fixer = _ref.fixer,
allowFigures = _ref.allowFigures,
allowWords = _ref.allowWords,
reportType = _ref.reportType;
var DocumentURL = "https://owl.english.purdue.edu/owl/resource/592/01/";
var paragraphNode = splitAST(node);
paragraphNode.children.filter(function (sentence) {
return sentence.type === SentenceSyntax.Sentence;
}).forEach(function (sentence) {
var sentenceFirstNode = sentence.children[0];
var DocumentURL = "https://owl.english.purdue.edu/owl/resource/592/01/";
var paragraphNode = splitAST(node);
paragraphNode.children.filter(function (sentence) {
return sentence.type === SentenceSyntax.Sentence;
}).forEach(function (sentence) {
var sentenceFirstNode = sentence.children[0];
if (!sentenceFirstNode) {
return;
}
// check first word is String
if (sentenceFirstNode.type === Syntax.Str) {
var text = sentenceFirstNode.value;
var firstWord = text.split(/\s/)[0];
if ((0, _captalize.isCapitalized)(firstWord) || shouldNotCapitalized(firstWord, allowWords)) {
return;
}
var index = 0;
return report(sentenceFirstNode, new RuleError(reportType + ": Follow the standard capitalization rules for American English.\nSee " + DocumentURL, {
index: index,
fix: fixer.replaceTextRange([index, index + firstWord.length], (0, _captalize.upperFirstCharacter)(firstWord))
}));
} else if (allowFigures && sentenceFirstNode.type === Syntax.Image && typeof sentenceFirstNode.alt === "string") {
var _text = sentenceFirstNode.alt;
if ((0, _captalize.isCapitalized)(_text) || shouldNotCapitalized(_text, allowWords)) {
return;
}
return report(sentenceFirstNode, new RuleError("Image alt: Follow the standard capitalization rules for American English\nSee " + DocumentURL));
}
});
if (!sentenceFirstNode) {
return;
} // check first word is String
if (sentenceFirstNode.type === Syntax.Str) {
var text = sentenceFirstNode.value;
var firstWord = text.split(/\s/)[0];
if ((0, _captalize.isCapitalized)(firstWord) || shouldNotCapitalized(firstWord, allowWords)) {
return;
}
var index = 0;
return report(sentenceFirstNode, new RuleError("".concat(reportType, ": Follow the standard capitalization rules for American English.\nSee ").concat(DocumentURL), {
index: index,
fix: fixer.replaceTextRange([index, index + firstWord.length], (0, _captalize.upperFirstCharacter)(firstWord))
}));
} else if (allowFigures && sentenceFirstNode.type === Syntax.Image && typeof sentenceFirstNode.alt === "string") {
var _text = sentenceFirstNode.alt;
if ((0, _captalize.isCapitalized)(_text) || shouldNotCapitalized(_text, allowWords)) {
return;
}
return report(sentenceFirstNode, new RuleError("Image alt: Follow the standard capitalization rules for American English\nSee ".concat(DocumentURL)));
}
});
};
var DefaultOptions = {
// allow lower-case words in Header
allowHeading: true,
// allow lower-case words in Image alt
allowFigures: true,
// allow lower-case words in ListItem
allowLists: true,
// allow lower-case words in anywhere
allowWords: []
// allow lower-case words in Header
allowHeading: true,
// allow lower-case words in Image alt
allowFigures: true,
// allow lower-case words in ListItem
allowLists: true,
// allow lower-case words in anywhere
allowWords: []
};
var report = function report(context) {
var _ref2;
var _ref2;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var Syntax = context.Syntax,
RuleError = context.RuleError,
getSource = context.getSource,
fixer = context.fixer,
report = context.report;
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var Syntax = context.Syntax,
RuleError = context.RuleError,
getSource = context.getSource,
fixer = context.fixer,
report = context.report;
var allowHeading = options.allowHeading !== undefined ? options.allowHeading : DefaultOptions.allowHeading;
var allowLists = options.allowLists !== undefined ? options.allowLists : DefaultOptions.allowLists;
var allowFigures = options.allowFigures !== undefined ? options.allowFigures : DefaultOptions.allowFigures;
var allowWords = Array.isArray(options.allowWords) ? options.allowWords : DefaultOptions.allowWords;
var helper = new RuleHelper(context);
return _ref2 = {}, _defineProperty(_ref2, Syntax.Header, function (node) {
// options
if (!allowHeading) {
return;
}
var allowHeading = options.allowHeading !== undefined ? options.allowHeading : DefaultOptions.allowHeading;
var allowLists = options.allowLists !== undefined ? options.allowLists : DefaultOptions.allowLists;
var allowFigures = options.allowFigures !== undefined ? options.allowFigures : DefaultOptions.allowFigures;
var allowWords = Array.isArray(options.allowWords) ? options.allowWords : DefaultOptions.allowWords;
var helper = new RuleHelper(context);
return _ref2 = {}, _defineProperty(_ref2, Syntax.Header, function (node) {
// options
if (!allowHeading) {
return;
}
checkNode({
node: node,
Syntax: Syntax,
getSource: getSource,
report: report,
RuleError: RuleError,
fixer: fixer,
allowFigures: allowFigures,
allowWords: allowWords,
reportType: REPORT_TYPE.Heading
});
}), _defineProperty(_ref2, Syntax.Paragraph, function (node) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
if (helper.isChildNode(node, [Syntax.ListItem])) {
return;
}
checkNode({
node: node,
Syntax: Syntax,
getSource: getSource,
report: report,
RuleError: RuleError,
fixer: fixer,
allowFigures: allowFigures,
allowWords: allowWords,
reportType: REPORT_TYPE.Paragraph
});
}), _defineProperty(_ref2, Syntax.ListItem, function (node) {
if (!allowLists) {
return;
}
node.children.forEach(function (paragraph) {
checkNode({
node: paragraph,
Syntax: Syntax,
getSource: getSource,
report: report,
RuleError: RuleError,
fixer: fixer,
allowFigures: allowFigures,
allowWords: allowWords,
reportType: REPORT_TYPE.List
});
});
}), _ref2;
checkNode({
node: node,
Syntax: Syntax,
getSource: getSource,
report: report,
RuleError: RuleError,
fixer: fixer,
allowFigures: allowFigures,
allowWords: allowWords,
reportType: REPORT_TYPE.Heading
});
}), _defineProperty(_ref2, Syntax.Paragraph, function (node) {
if (helper.isChildNode(node, [Syntax.Link, Syntax.Image, Syntax.BlockQuote, Syntax.Emphasis])) {
return;
}
if (helper.isChildNode(node, [Syntax.ListItem])) {
return;
}
checkNode({
node: node,
Syntax: Syntax,
getSource: getSource,
report: report,
RuleError: RuleError,
fixer: fixer,
allowFigures: allowFigures,
allowWords: allowWords,
reportType: REPORT_TYPE.Paragraph
});
}), _defineProperty(_ref2, Syntax.ListItem, function (node) {
if (!allowLists) {
return;
}
node.children.forEach(function (blockNode) {
// Ignore other block node
// CodeBlock is a part of block node
// https://github.com/textlint-rule/textlint-rule-en-capitalization/issues/4
if (blockNode.type !== Syntax.Paragraph) {
return;
}
checkNode({
node: blockNode,
Syntax: Syntax,
getSource: getSource,
report: report,
RuleError: RuleError,
fixer: fixer,
allowFigures: allowFigures,
allowWords: allowWords,
reportType: REPORT_TYPE.List
});
});
}), _ref2;
};
module.exports = {
linter: report,
fixer: report
linter: report,
fixer: report
};
//# sourceMappingURL=textlint-rule-en-capitalization.js.map

@@ -14,3 +14,3 @@ {

"name": "textlint-rule-en-capitalization",
"version": "2.0.1",
"version": "2.0.2",
"description": "textlint rule that check capitalization in english text.",

@@ -21,4 +21,2 @@ "main": "lib/textlint-rule-en-capitalization.js",

"prettier": "prettier --write '**/*.{js,jsx,ts,tsx,css}'",
"precommit": "lint-staged",
"postcommit": "git reset",
"prepublish": "npm run --if-present build",

@@ -41,6 +39,6 @@ "build": "textlint-scripts build",

"babel-register": "^6.26.0",
"husky": "^0.14.3",
"lint-staged": "^6.0.0",
"prettier": "^1.9.2",
"textlint-scripts": "^1.4.0"
"husky": "^2.1.0",
"lint-staged": "^8.1.5",
"prettier": "^1.17.0",
"textlint-scripts": "^2.1.0"
},

@@ -59,5 +57,11 @@ "prettier": {

"en-pos": "^1.0.16",
"sentence-splitter": "^3.0.1",
"textlint-rule-helper": "^2.0.0"
"sentence-splitter": "^3.0.11",
"textlint-rule-helper": "^2.1.1"
},
"husky": {
"hooks": {
"post-commit": "git reset",
"pre-commit": "lint-staged"
}
}
}

@@ -46,44 +46,49 @@ // MIT © 2017 azu

const paragraphNode = splitAST(node);
paragraphNode.children.filter(sentence => sentence.type === SentenceSyntax.Sentence).forEach(sentence => {
const sentenceFirstNode = sentence.children[0];
if (!sentenceFirstNode) {
return;
}
// check first word is String
if (sentenceFirstNode.type === Syntax.Str) {
const text = sentenceFirstNode.value;
const firstWord = text.split(/\s/)[0];
if (isCapitalized(firstWord) || shouldNotCapitalized(firstWord, allowWords)) {
paragraphNode.children
.filter(sentence => sentence.type === SentenceSyntax.Sentence)
.forEach(sentence => {
const sentenceFirstNode = sentence.children[0];
if (!sentenceFirstNode) {
return;
}
const index = 0;
return report(
sentenceFirstNode,
new RuleError(
`${reportType}: Follow the standard capitalization rules for American English.
// check first word is String
if (sentenceFirstNode.type === Syntax.Str) {
const text = sentenceFirstNode.value;
const firstWord = text.split(/\s/)[0];
if (isCapitalized(firstWord) || shouldNotCapitalized(firstWord, allowWords)) {
return;
}
const index = 0;
return report(
sentenceFirstNode,
new RuleError(
`${reportType}: Follow the standard capitalization rules for American English.
See ${DocumentURL}`,
{
index: index,
fix: fixer.replaceTextRange([index, index + firstWord.length], upperFirstCharacter(firstWord))
}
)
);
} else if (
allowFigures &&
sentenceFirstNode.type === Syntax.Image &&
typeof sentenceFirstNode.alt === "string"
) {
const text = sentenceFirstNode.alt;
if (isCapitalized(text) || shouldNotCapitalized(text, allowWords)) {
return;
{
index: index,
fix: fixer.replaceTextRange(
[index, index + firstWord.length],
upperFirstCharacter(firstWord)
)
}
)
);
} else if (
allowFigures &&
sentenceFirstNode.type === Syntax.Image &&
typeof sentenceFirstNode.alt === "string"
) {
const text = sentenceFirstNode.alt;
if (isCapitalized(text) || shouldNotCapitalized(text, allowWords)) {
return;
}
return report(
sentenceFirstNode,
new RuleError(
`Image alt: Follow the standard capitalization rules for American English
See ${DocumentURL}`
)
);
}
return report(
sentenceFirstNode,
new RuleError(
`Image alt: Follow the standard capitalization rules for American English
See ${DocumentURL}`
)
);
}
});
});
};

@@ -149,5 +154,11 @@

}
node.children.forEach(paragraph => {
node.children.forEach(blockNode => {
// Ignore other block node
// CodeBlock is a part of block node
// https://github.com/textlint-rule/textlint-rule-en-capitalization/issues/4
if (blockNode.type !== Syntax.Paragraph) {
return;
}
checkNode({
node: paragraph,
node: blockNode,
Syntax,

@@ -154,0 +165,0 @@ getSource,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc