首页轮播,裁剪标题,按钮间距
Test Runner / hello (push) Successful in 3s

This commit is contained in:
2026-08-25 12:04:57 +08:00
parent bd6f7a378e
commit 65356953ed
7 changed files with 148 additions and 17 deletions
+41
View File
@@ -0,0 +1,41 @@
/**
* 商品标题处理工具
*
* 背景:后端返回的 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
}