浏览代码

fix: Replace custom `repeatString` function with `repeat()` (#2820)

chore: Replace custom repeatString functions with ECMAScript String.repeat()
Florian Dreier 2 年之前
父节点
当前提交
72ee2d620a
共有 8 个文件被更改,包括 10 次插入107 次删除
  1. 1 9
      docs/demo/demo.js
  2. 1 9
      docs/demo/worker.js
  3. 2 22
      lib/marked.cjs
  4. 2 22
      lib/marked.esm.js
  5. 2 22
      lib/marked.umd.js
  6. 0 0
      marked.min.js
  7. 2 3
      src/Lexer.js
  8. 0 20
      src/helpers.js

+ 1 - 9
docs/demo/demo.js

@@ -326,14 +326,6 @@ function isArray(arr) {
   return Object.prototype.toString.call(arr) === '[object Array]';
 }
 
-function stringRepeat(char, times) {
-  var s = '';
-  for (var i = 0; i < times; i++) {
-    s += char;
-  }
-  return s;
-}
-
 function jsonString(input, level) {
   level = level || 0;
   if (isArray(input)) {
@@ -344,7 +336,7 @@ function jsonString(input, level) {
         i;
     if (!isArray(input[0]) && typeof input[0] === 'object' && input[0] !== null) {
       for (i = 0; i < input.length; i++) {
-        items.push(stringRepeat(' ', 2 * level) + jsonString(input[i], level + 1));
+        items.push(' '.repeat(2 * level) + jsonString(input[i], level + 1));
       }
       return '[\n' + items.join('\n') + '\n]';
     }

+ 1 - 9
docs/demo/worker.js

@@ -87,14 +87,6 @@ function parse(e) {
   }
 }
 
-function stringRepeat(char, times) {
-  var s = '';
-  for (var i = 0; i < times; i++) {
-    s += char;
-  }
-  return s;
-}
-
 function jsonString(input, level) {
   level = level || 0;
   if (Array.isArray(input)) {
@@ -105,7 +97,7 @@ function jsonString(input, level) {
         i;
     if (!Array.isArray(input[0]) && typeof input[0] === 'object' && input[0] !== null) {
       for (i = 0; i < input.length; i++) {
-        items.push(stringRepeat(' ', 2 * level) + jsonString(input[i], level + 1));
+        items.push(' '.repeat(2 * level) + jsonString(input[i], level + 1));
       }
       return '[\n' + items.join('\n') + '\n]';
     }

+ 2 - 22
lib/marked.cjs

@@ -372,26 +372,6 @@ function checkDeprecations(opt, callback) {
   }
 }
 
-// copied from https://stackoverflow.com/a/5450113/806777
-/**
- * @param {string} pattern
- * @param {number} count
- */
-function repeatString(pattern, count) {
-  if (count < 1) {
-    return '';
-  }
-  var result = '';
-  while (count > 1) {
-    if (count & 1) {
-      result += pattern;
-    }
-    count >>= 1;
-    pattern += pattern;
-  }
-  return result + pattern;
-}
-
 function outputLink(cap, link, raw, lexer) {
   var href = link.href;
   var title = link.title ? escape(link.title) : null;
@@ -1649,14 +1629,14 @@ var Lexer = /*#__PURE__*/function () {
       if (links.length > 0) {
         while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
           if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
-            maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
+            maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
           }
         }
       }
     }
     // Mask out other blocks
     while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
-      maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
+      maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
     }
 
     // Mask out escaped em & strong delimiters

+ 2 - 22
lib/marked.esm.js

@@ -312,26 +312,6 @@ function checkDeprecations(opt, callback) {
   }
 }
 
-// copied from https://stackoverflow.com/a/5450113/806777
-/**
- * @param {string} pattern
- * @param {number} count
- */
-function repeatString(pattern, count) {
-  if (count < 1) {
-    return '';
-  }
-  let result = '';
-  while (count > 1) {
-    if (count & 1) {
-      result += pattern;
-    }
-    count >>= 1;
-    pattern += pattern;
-  }
-  return result + pattern;
-}
-
 function outputLink(cap, link, raw, lexer) {
   const href = link.href;
   const title = link.title ? escape(link.title) : null;
@@ -1761,14 +1741,14 @@ class Lexer {
       if (links.length > 0) {
         while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
           if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
-            maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
+            maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
           }
         }
       }
     }
     // Mask out other blocks
     while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
-      maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
+      maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
     }
 
     // Mask out escaped em & strong delimiters

+ 2 - 22
lib/marked.umd.js

@@ -376,26 +376,6 @@
     }
   }
 
-  // copied from https://stackoverflow.com/a/5450113/806777
-  /**
-   * @param {string} pattern
-   * @param {number} count
-   */
-  function repeatString(pattern, count) {
-    if (count < 1) {
-      return '';
-    }
-    var result = '';
-    while (count > 1) {
-      if (count & 1) {
-        result += pattern;
-      }
-      count >>= 1;
-      pattern += pattern;
-    }
-    return result + pattern;
-  }
-
   function outputLink(cap, link, raw, lexer) {
     var href = link.href;
     var title = link.title ? escape(link.title) : null;
@@ -1653,14 +1633,14 @@
         if (links.length > 0) {
           while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
             if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
-              maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
+              maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
             }
           }
         }
       }
       // Mask out other blocks
       while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
-        maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
+        maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
       }
 
       // Mask out escaped em & strong delimiters

文件差异内容过多而无法显示
+ 0 - 0
marked.min.js


+ 2 - 3
src/Lexer.js

@@ -1,7 +1,6 @@
 import { Tokenizer } from './Tokenizer.js';
 import { defaults } from './defaults.js';
 import { block, inline } from './rules.js';
-import { repeatString } from './helpers.js';
 
 /**
  * smartypants text replacement
@@ -338,14 +337,14 @@ export class Lexer {
       if (links.length > 0) {
         while ((match = this.tokenizer.rules.inline.reflinkSearch.exec(maskedSrc)) != null) {
           if (links.includes(match[0].slice(match[0].lastIndexOf('[') + 1, -1))) {
-            maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
+            maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.reflinkSearch.lastIndex);
           }
         }
       }
     }
     // Mask out other blocks
     while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
-      maskedSrc = maskedSrc.slice(0, match.index) + '[' + repeatString('a', match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
+      maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
     }
 
     // Mask out escaped em & strong delimiters

+ 0 - 20
src/helpers.js

@@ -269,23 +269,3 @@ export function checkDeprecations(opt, callback) {
     console.warn('marked(): headerIds and headerPrefix parameters enabled by default, but are deprecated since version 5.0.0, and will be removed in the future. To clear this warning, install  https://www.npmjs.com/package/marked-gfm-heading-id, or disable by setting `{headerIds: false}`.');
   }
 }
-
-// copied from https://stackoverflow.com/a/5450113/806777
-/**
- * @param {string} pattern
- * @param {number} count
- */
-export function repeatString(pattern, count) {
-  if (count < 1) {
-    return '';
-  }
-  let result = '';
-  while (count > 1) {
-    if (count & 1) {
-      result += pattern;
-    }
-    count >>= 1;
-    pattern += pattern;
-  }
-  return result + pattern;
-}

部分文件因为文件数量过多而无法显示