Files
RyRh 65356953ed
Test Runner / hello (push) Successful in 3s
首页轮播,裁剪标题,按钮间距
2026-08-25 12:04:57 +08:00

42 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 商品标题处理工具
*
* 背景:后端返回的 goodName 常包含「前半段品牌/系列 + 空格 + 后半段 SKU/描述」,
* 首页/列表页卡片空间有限,只显示空格前的前半部分。
*/
/**
* 截断商品标题:返回第一个空格(或全角空格)之前的内容。
* - 防错:null/undefined/非字符串/空串 → 返回 ''
* - 没有空格 → 原样返回(去掉首尾空白后)
* - 多个连续空格 → 只看第一个
* - 全角空格 " " 也视为空格
*
* @param {string | any} title 原始商品标题(goodName
* @returns {string} 截断后的标题
*/
function truncateProductTitle(title) {
if (title == null) return ''
if (typeof title !== 'string') {
try {
title = String(title)
} catch (e) {
return ''
}
}
// 去掉首尾空白
const s = title.trim()
if (!s) return ''
// 找第一个空格(半角 / 全角 / Tab / NBSP 都算)
const match = s.match(/[\s\u3000\u00a0]/)
if (!match) return s
const idx = match.index
if (idx <= 0) return s
return s.slice(0, idx)
}
module.exports = {
truncateProductTitle
}