Replace trimLeft/trimRight polyfills with helper methods.

This commit is contained in:
David Anson 2017-12-13 21:41:28 -08:00
parent 39d39db961
commit 1184281c87
5 changed files with 21 additions and 36 deletions

View file

@ -1,26 +0,0 @@
"use strict";
// Polyfills for browsers that do not support String.trimLeft/Right
function trimLeftPolyfill() {
return this.replace(/^\s*/, "");
}
/* istanbul ignore if */
if (!String.prototype.trimLeft) {
String.prototype.trimLeft = trimLeftPolyfill;
}
function trimRightPolyfill() {
return this.replace(/\s*$/, "");
}
/* istanbul ignore if */
if (!String.prototype.trimRight) {
String.prototype.trimRight = trimRightPolyfill;
}
// Export for testing
/* istanbul ignore else */
if ((typeof module !== "undefined") && module.exports) {
module.exports = {
"trimLeftPolyfill": trimLeftPolyfill,
"trimRightPolyfill": trimRightPolyfill
};
}

View file

@ -33,7 +33,7 @@ function escapeForRegExp(str) {
// Returns the indent for a token
function indentFor(token) {
var line = token.line.replace(/^[\s>]*(> |>)/, "");
return line.length - line.trimLeft().length;
return line.length - shared.trimLeft(line).length;
}
// Returns the heading style for a heading token
@ -49,7 +49,7 @@ function headingStyleFor(token) {
// Returns the unordered list style for a list item token
function unorderedListStyleFor(token) {
switch (token.line.trimLeft().substr(0, 1)) {
switch (shared.trimLeft(token.line).substr(0, 1)) {
case "-":
return "dash";
case "+":
@ -368,7 +368,7 @@ module.exports = [
(listItemLineNumbers.indexOf(lineNumber) === -1)) {
var expected = (brSpaces < 2) ? 0 : brSpaces;
errors.addDetailIf(lineNumber,
expected, line.length - line.trimRight().length);
expected, line.length - shared.trimRight(line).length);
}
});
}
@ -1036,8 +1036,8 @@ module.exports = [
linkText = "";
} else if (child.type === "link_close") {
inLink = false;
var left = linkText.trimLeft().length !== linkText.length;
var right = linkText.trimRight().length !== linkText.length;
var left = shared.trimLeft(linkText).length !== linkText.length;
var right = shared.trimRight(linkText).length !== linkText.length;
if (left || right) {
errors.addContext(
token.lineNumber, "[" + linkText + "]", left, right);

View file

@ -14,6 +14,18 @@ module.exports.inlineCommentRe = inlineCommentRe;
// readFile options for reading with the UTF-8 encoding
module.exports.utf8Encoding = { "encoding": "utf8" };
// Trims whitespace from the left (start) of a string
function trimLeft(str) {
return str.replace(/^\s*/, "");
}
module.exports.trimLeft = trimLeft;
// Trims whitespace from the right (end) of a string
function trimRight(str) {
return str.replace(/\s*$/, "");
}
module.exports.trimRight = trimRight;
// Applies key/value pairs from src to dst, returning dst
function assign(dst, src) {
Object.keys(src).forEach(function forKey(key) {

View file

@ -18,7 +18,7 @@
"debug": "node debug node_modules/nodeunit/bin/nodeunit",
"lint": "eslint lib test schema && eslint --env browser --global markdownit --global markdownlint --rule \"no-unused-vars: 0, no-extend-native: 0, max-statements: 0, no-console: 0\" demo && eslint --rule \"no-console: 0, no-shadow: 0, object-property-newline: 0\" example",
"build-config-schema": "node schema/build-config-schema.js",
"build-demo": "cpy node_modules/markdown-it/dist/markdown-it.min.js demo && cd demo && rimraf markdownlint-browser.* && cpy file-header.js . --rename=markdownlint-browser.js && browserify browser-polyfills.js ../lib/markdownlint.js --standalone markdownlint >> markdownlint-browser.js && uglifyjs markdownlint-browser.js --compress --mangle --comments --output markdownlint-browser.min.js",
"build-demo": "cpy node_modules/markdown-it/dist/markdown-it.min.js demo && cd demo && rimraf markdownlint-browser.* && cpy file-header.js . --rename=markdownlint-browser.js && browserify ../lib/markdownlint.js --standalone markdownlint >> markdownlint-browser.js && uglifyjs markdownlint-browser.js --compress --mangle --comments --output markdownlint-browser.min.js",
"build-example": "npm install --no-save --ignore-scripts grunt grunt-cli gulp through2",
"example": "cd example && node standalone.js && grunt markdownlint --force && gulp markdownlint"
},

View file

@ -8,7 +8,6 @@ var tv4 = require("tv4");
var markdownlint = require("../lib/markdownlint");
var shared = require("../lib/shared");
var rules = require("../lib/rules");
var polyfills = require("../demo/browser-polyfills");
var defaultConfig = require("./markdownlint-test-default-config.json");
var configSchema = require("../schema/markdownlint-config-schema.json");
@ -1295,7 +1294,7 @@ function clearHtmlCommentTextEmbedded(test) {
test.done();
};
module.exports.trimPolyfills = function trimPolyfills(test) {
module.exports.trimLeftRight = function trimLeftRight(test) {
var inputs = [
"text text",
" text text ",
@ -1315,9 +1314,9 @@ module.exports.trimPolyfills = function trimPolyfills(test) {
];
test.expect(inputs.length * 2);
inputs.forEach(function forInput(input) {
test.equal(polyfills.trimLeftPolyfill.call(input), input.trimLeft(),
test.equal(shared.trimLeft(input), input.trimLeft(),
"trimLeft incorrect for '" + input + "'");
test.equal(polyfills.trimRightPolyfill.call(input), input.trimRight(),
test.equal(shared.trimRight(input), input.trimRight(),
"trimRight incorrect for '" + input + "'");
});
test.done();