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/README.md CHANGED
@@ -194,4 +194,36 @@ $bus.off('test', listener)
194
194
  // 移除所有监听器
195
195
  $bus.removeAllListeners()
196
196
 
197
- ```
197
+ ```
198
+
199
+ ### 文本关键词高亮(highlightKeyword)
200
+ 高亮文本中匹配的关键词,支持不区分大小写匹配,并允许自定义标签与样式。
201
+
202
+ ```javascript
203
+ import { highlightKeyword } from 'jsly'
204
+
205
+ const text = '泡泡音乐是一款非常好用的音乐播放器'
206
+ const result = highlightKeyword(text, '音乐')
207
+ console.log(result)
208
+ // => '泡泡<span style="background-color: #ff0;color: #001;">音乐</span>是一款非常好用的<span style="background-color: #ff0;color: #001;">音乐</span>播放器'
209
+
210
+ ```
211
+
212
+ 自定义标签与样式
213
+ ```javascript
214
+ highlightKeyword('Hello World', 'world', {
215
+ tag: 'mark',
216
+ bgColor: '#000',
217
+ color: '#0f0'
218
+ })
219
+
220
+ ```
221
+
222
+ 配置说明
223
+ | 参数 | 类型 | 默认值 | 说明 |
224
+ |------|------|---------|------|
225
+ | `text` | `string` | 必填 | 原始文本 |
226
+ | `keyword` | `string` | 必填 | 要高亮的关键词(大小写不敏感) |
227
+ | `config.tag` | `string` | `'span'` | 包裹关键词的标签名 |
228
+ | `config.bgColor` | `string` | `'#ff0'` | 背景颜色 |
229
+ | `config.color` | `string` | `'#001'` | 文本颜色 |
package/dist/index.cjs.js CHANGED
@@ -7628,6 +7628,67 @@ class EventBus {
7628
7628
  // 导出单例
7629
7629
  const $bus = new EventBus();
7630
7630
 
7631
+ /**
7632
+ * 转义正则表达式中的特殊字符。
7633
+ *
7634
+ * @private
7635
+ * @function escapeRegExp
7636
+ * @description
7637
+ * 将字符串中的正则表达式特殊字符(如 `. * + ? ^ $ { } ( ) | [ ] \\`)全部转义,
7638
+ * 以便安全地用于构建正则表达式。通常用于用户输入的关键字过滤,避免因
7639
+ * 正则表达式语法导致的匹配异常。
7640
+ *
7641
+ * @param {string} str - 需要进行转义的原始字符串。
7642
+ * @returns {string} 已转义、可安全用于 RegExp 的字符串。
7643
+ *
7644
+ * @example
7645
+ * escapeRegExp("a+b*c") // => "a\\+b\\*c"
7646
+ */
7647
+ function escapeRegExp(str) {
7648
+ return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
7649
+ }
7650
+
7651
+ /**
7652
+ * 高亮文本中的关键词。
7653
+ *
7654
+ * @function highlightKeyword
7655
+ * @description
7656
+ * 在给定的文本中查找所有匹配的关键词(不区分大小写),并使用带样式的
7657
+ * HTML 标签(默认 `<span>`)包裹,使其以高亮方式显示。该方法适用于搜索
7658
+ * 结果展示、文本匹配提示等场景。
7659
+ *
7660
+ * @param {string} text - 原始完整文本。
7661
+ * @param {string} keyword - 需要高亮显示的关键词。如果为空,则直接返回原文本。
7662
+ * @param {Object} [config] - 配置对象,用于自定义高亮标签和样式。
7663
+ * @param {string} [config.tag="span"] - 包裹关键词的 HTML 标签名。
7664
+ * @param {string} [config.bgColor="#ff0"] - 高亮背景颜色。
7665
+ * @param {string} [config.color="#001"] - 关键词文字颜色。
7666
+ *
7667
+ * @returns {string} 返回插入带样式标签后的 HTML 字符串。
7668
+ *
7669
+ * @example
7670
+ * highlightKeyword(
7671
+ * "泡泡音乐是一款好用的播放器",
7672
+ * "音乐"
7673
+ * )
7674
+ * // => '泡泡<span style="background-color: #ff0;color: #001;">音乐</span>是一款好用的播放器'
7675
+ */
7676
+ function highlightKeyword(text, keyword, config = {
7677
+ tag: "span",
7678
+ bgColor: "#ff0",
7679
+ color: "#001"
7680
+ }) {
7681
+ if (!keyword) return text;
7682
+ const {
7683
+ tag,
7684
+ bgColor,
7685
+ color
7686
+ } = config;
7687
+ const pattern = new RegExp(escapeRegExp(keyword), "gi");
7688
+ const style = `background-color: ${bgColor};color: ${color};`;
7689
+ return text.replace(pattern, match => `<${tag} style="${style}">${match}</${tag}>`);
7690
+ }
7691
+
7631
7692
  exports.$bus = $bus;
7632
7693
  exports.buildObjFormData = buildObjFormData;
7633
7694
  exports.camelToSnake = camelToSnake;
@@ -7638,6 +7699,7 @@ exports.copyToClipboard = copyToClipboard;
7638
7699
  exports.debounce = debounce;
7639
7700
  exports.deepDiff = deepDiff;
7640
7701
  exports.generateBrowserId = generateBrowserId;
7702
+ exports.highlightKeyword = highlightKeyword;
7641
7703
  exports.randomHash = randomHash;
7642
7704
  exports.sha256 = sha256;
7643
7705
  exports.shallowDiff = shallowDiff;