jsly 3.0.6 → 3.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -7626,5 +7626,66 @@ class EventBus {
7626
7626
  // 导出单例
7627
7627
  const $bus = new EventBus();
7628
7628
 
7629
- export { $bus, buildObjFormData, camelToSnake, convertKeys, copyByClipboardAPI, copyByExecCommand, copyToClipboard, debounce, deepDiff, generateBrowserId, randomHash, sha256, shallowDiff, snakeToCamel, throttle, toggleConvertCase };
7629
+ /**
7630
+ * 转义正则表达式中的特殊字符。
7631
+ *
7632
+ * @private
7633
+ * @function escapeRegExp
7634
+ * @description
7635
+ * 将字符串中的正则表达式特殊字符(如 `. * + ? ^ $ { } ( ) | [ ] \\`)全部转义,
7636
+ * 以便安全地用于构建正则表达式。通常用于用户输入的关键字过滤,避免因
7637
+ * 正则表达式语法导致的匹配异常。
7638
+ *
7639
+ * @param {string} str - 需要进行转义的原始字符串。
7640
+ * @returns {string} 已转义、可安全用于 RegExp 的字符串。
7641
+ *
7642
+ * @example
7643
+ * escapeRegExp("a+b*c") // => "a\\+b\\*c"
7644
+ */
7645
+ function escapeRegExp(str) {
7646
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
7647
+ }
7648
+
7649
+ /**
7650
+ * 高亮文本中的关键词。
7651
+ *
7652
+ * @function highlightKeyword
7653
+ * @description
7654
+ * 在给定的文本中查找所有匹配的关键词(不区分大小写),并使用带样式的
7655
+ * HTML 标签(默认 `<span>`)包裹,使其以高亮方式显示。该方法适用于搜索
7656
+ * 结果展示、文本匹配提示等场景。
7657
+ *
7658
+ * @param {string} text - 原始完整文本。
7659
+ * @param {string} keyword - 需要高亮显示的关键词。如果为空,则直接返回原文本。
7660
+ * @param {Object} [config] - 配置对象,用于自定义高亮标签和样式。
7661
+ * @param {string} [config.tag="span"] - 包裹关键词的 HTML 标签名。
7662
+ * @param {string} [config.bgColor="#ff0"] - 高亮背景颜色。
7663
+ * @param {string} [config.color="#001"] - 关键词文字颜色。
7664
+ *
7665
+ * @returns {string} 返回插入带样式标签后的 HTML 字符串。
7666
+ *
7667
+ * @example
7668
+ * highlightKeyword(
7669
+ * "泡泡音乐是一款好用的播放器",
7670
+ * "音乐"
7671
+ * )
7672
+ * // => '泡泡<span style="background-color: #ff0;color: #001;">音乐</span>是一款好用的播放器'
7673
+ */
7674
+ function highlightKeyword(text, keyword, config = {
7675
+ tag: "span",
7676
+ bgColor: "#ff0",
7677
+ color: "#001"
7678
+ }) {
7679
+ if (!keyword) return text;
7680
+ const {
7681
+ tag,
7682
+ bgColor,
7683
+ color
7684
+ } = config;
7685
+ const pattern = new RegExp(escapeRegExp(keyword), "gi");
7686
+ const style = `background-color: ${bgColor};color: ${color};`;
7687
+ return text.replace(pattern, match => `<${tag} style="${style}">${match}</${tag}>`);
7688
+ }
7689
+
7690
+ export { $bus, buildObjFormData, camelToSnake, convertKeys, copyByClipboardAPI, copyByExecCommand, copyToClipboard, debounce, deepDiff, generateBrowserId, highlightKeyword, randomHash, sha256, shallowDiff, snakeToCamel, throttle, toggleConvertCase };
7630
7691
  //# sourceMappingURL=index.esm.js.map