Remove trimLeft/trimRight helpers; use native implementations.

This commit is contained in:
David Anson 2019-04-17 14:54:27 -07:00
parent 22c36c388f
commit 73511ff677
4 changed files with 7 additions and 46 deletions

View file

@ -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;

View file

@ -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 ") +

View file

@ -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,

View file

@ -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 =