From 73511ff677064924d08ef8fbabed331d1d2dc667 Mon Sep 17 00:00:00 2001 From: David Anson Date: Wed, 17 Apr 2019 14:54:27 -0700 Subject: [PATCH] Remove trimLeft/trimRight helpers; use native implementations. --- helpers/helpers.js | 13 +------------ lib/md009.js | 6 +++--- lib/md039.js | 6 +++--- test/markdownlint-test.js | 28 ---------------------------- 4 files changed, 7 insertions(+), 46 deletions(-) diff --git a/helpers/helpers.js b/helpers/helpers.js index 463c6f18..2f2c1f0a 100644 --- a/helpers/helpers.js +++ b/helpers/helpers.js @@ -24,17 +24,6 @@ module.exports.orderedListItemMarkerRe = /^[\s>]*0*(\d+)[.)]/; // 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 -module.exports.trimRight = function trimRight(str) { - return str.replace(/\s*$/, ""); -}; - // Applies key/value pairs from src to dst, returning dst function assign(dst, src) { Object.keys(src).forEach(function forKey(key) { @@ -129,7 +118,7 @@ module.exports.escapeForRegExp = function escapeForRegExp(str) { // Returns the indent for a token function indentFor(token) { const line = token.line.replace(/^[\s>]*(> |>)/, ""); - return line.length - trimLeft(line).length; + return line.length - line.trimLeft().length; } module.exports.indentFor = indentFor; diff --git a/lib/md009.js b/lib/md009.js index 55025b19..18add32e 100644 --- a/lib/md009.js +++ b/lib/md009.js @@ -2,8 +2,8 @@ "use strict"; -const { addError, filterTokens, forEachLine, includesSorted, rangeFromRegExp, - trimRight } = require("../helpers"); +const { addError, filterTokens, forEachLine, includesSorted, rangeFromRegExp } = + require("../helpers"); const { lineMetadata } = require("./cache"); const trailingSpaceRe = /\s+$/; @@ -34,7 +34,7 @@ module.exports = { const lineNumber = lineIndex + 1; if (trailingSpaceRe.test(line) && !includesSorted(listItemLineNumbers, lineNumber)) { - const actual = line.length - trimRight(line).length; + const actual = line.length - line.trimRight().length; if (expected !== actual) { addError(onError, lineNumber, "Expected: " + (expected === 0 ? "" : "0 or ") + diff --git a/lib/md039.js b/lib/md039.js index 5f708241..fb59d67f 100644 --- a/lib/md039.js +++ b/lib/md039.js @@ -2,7 +2,7 @@ "use strict"; -const { addErrorContext, filterTokens, rangeFromRegExp, trimLeft, trimRight } = +const { addErrorContext, filterTokens, rangeFromRegExp } = require("../helpers"); const spaceInLinkRe = /\[(?:\s+(?:[^\]]*?)\s*|(?:[^\]]*?)\s+)](?=\(\S*\))/; @@ -21,8 +21,8 @@ module.exports = { linkText = ""; } else if (child.type === "link_close") { inLink = false; - const left = trimLeft(linkText).length !== linkText.length; - const right = trimRight(linkText).length !== linkText.length; + const left = linkText.trimLeft().length !== linkText.length; + const right = linkText.trimRight().length !== linkText.length; if (left || right) { addErrorContext(onError, token.lineNumber, "[" + linkText + "]", left, right, diff --git a/test/markdownlint-test.js b/test/markdownlint-test.js index 9ee5c1fd..a96153b2 100644 --- a/test/markdownlint-test.js +++ b/test/markdownlint-test.js @@ -1459,34 +1459,6 @@ module.exports.includesSorted = function includesSorted(test) { test.done(); }; -module.exports.trimLeftRight = function trimLeftRight(test) { - const inputs = [ - "text text", - " text text ", - " text text ", - // ECMAScript Whitespace - "\u0009 text text \u0009", - "\u000b text text \u000b", - "\u000c text text \u000c", - "\u0020 text text \u0020", - "\u00a0 text text \u00a0", - "\ufeff text text \ufeff", - // ECMAScript LineTerminator - "\u000a text text \u000a", - "\u000d text text \u000d", - "\u2028 text text \u2028", - "\u2029 text text \u2029" - ]; - test.expect(inputs.length * 2); - inputs.forEach(function forInput(input) { - test.equal(helpers.trimLeft(input), input.trimLeft(), - "trimLeft incorrect for '" + input + "'"); - test.equal(helpers.trimRight(input), input.trimRight(), - "trimRight incorrect for '" + input + "'"); - }); - test.done(); -}; - module.exports.forEachInlineCodeSpan = function forEachInlineCodeSpan(test) { test.expect(94); const testCases =