Files
devbox/js/cat-text.js
T
gitea f454ea8da7 首页加分类小按钮 + 参照 deertools 扩到 201 个工具
- app.js: 首页粘性搜索条 + 分类 chip(带数量),实时过滤,不再翻半天
- 新增 lib-simple.js(textTool / convTool / genTool 工厂)
- 新增 9 个 catalog:cat-text / cat-encode / cat-convert / cat-random / cat-dev / cat-css / cat-device / cat-office / cat-life
- 新增分组:随机 / 检测 / 办公 / 生活
- 工具数 44 → 201;全部 mount 通过(jsdom 201/201)
2026-11-20 14:50:14 +08:00

160 lines
18 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
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.
/* DevBox · 文本处理全家桶 */
(function () {
'use strict'
const T = DB.textTool
const lines = (s) => s.split(/\r?\n/)
const words = (s) => (s.match(/[\u4e00-\u9fa5]|[A-Za-z0-9_'’-]+/g) || [])
T({ id: 'case-convert', name: '大小写与命名转换', group: 'text', icon: 'text', desc: '全大写、小写、首字母大写、驼峰、蛇形、短横线、常量名一次互转', keywords: ['case', 'camel', 'snake', 'kebab', '大小写', '驼峰', '命名'],
ph: 'my variable name', fn: (s) => {
const ws = s.trim().split(/[\s_\-]+|(?=[A-Z])/).filter(Boolean)
const up = s.toUpperCase(), low = s.toLowerCase()
return [[up, '全大写'], [low, '全小写'], [s.replace(/\b\w/g, (c) => c.toUpperCase()), '每词首字母大写'],
[ws.map((w, i) => i ? w[0].toUpperCase() + w.slice(1).toLowerCase() : w.toLowerCase()).join(''), '小驼峰'],
[ws.map((w) => w[0].toUpperCase() + w.slice(1).toLowerCase()).join(''), '大驼峰'],
[ws.map((w) => w.toLowerCase()).join('_'), '蛇形 snake_case'],
[ws.map((w) => w.toLowerCase()).join('-'), '短横线 kebab-case'],
[ws.map((w) => w.toUpperCase()).join('_'), '常量 CONSTANT_CASE'],
].map(([v, n]) => `${n.padEnd(14, ' ')} ${v}`).join('\n')
} })
T({ id: 'text-reverse', name: '文本倒序', group: 'text', icon: 'text', desc: '整段倒着排,也能按行倒序、按词倒序', keywords: ['reverse', '倒序', '反转'], ph: 'abc 123 你好',
fn: (s) => `整体倒序:${Array.from(s).reverse().join('')}\n按行倒序:${lines(s).reverse().join('\n')}\n按词倒序:${words(s).reverse().join(' ')}` })
T({ id: 'text-sort', name: '文本排序', group: 'text', icon: 'text', desc: '按行排序:字典序、长度、数字大小、去重', keywords: ['sort', '排序', '字典序', '按长度'], ph: 'banana\napple\n3\n12\n1',
fn: (s) => { const l = lines(s).filter((x) => x.trim() !== ''); return [
['字典序', [...l].sort((a, b) => a.localeCompare(b, 'zh'))],
['倒序', [...l].sort((a, b) => b.localeCompare(a, 'zh'))],
['按长度', [...l].sort((a, b) => a.length - b.length)],
['按数字', [...l].sort((a, b) => (parseFloat(a) || 0) - (parseFloat(b) || 0))],
].map(([n, arr]) => `# ${n}\n${arr.join('\n')}`).join('\n\n') } })
T({ id: 'text-dedupe', name: '去重', group: 'text', icon: 'text', desc: '按行去重,可选保留首次出现顺序或统计重复次数', keywords: ['dedupe', 'unique', '去重', '重复'], ph: 'a\nb\na\nc\nb',
fn: (s) => { const l = lines(s); const seen = new Map(); l.forEach((x) => seen.set(x, (seen.get(x) || 0) + 1))
return `去重后(${seen.size} 行):\n${[...seen.keys()].join('\n')}\n\n重复统计:\n${[...seen].filter(([, n]) => n > 1).map(([x, n]) => `${n}× ${x}`).join('\n') || '无重复行'}` } })
T({ id: 'text-shuffle', name: '打乱顺序', group: 'text', icon: 'text', desc: '按行打乱、按字符打乱,随手就能抽顺序', keywords: ['shuffle', '打乱', '随机排列', '洗牌'], ph: '第一行\n第二行\n第三行',
fn: (s) => { const sh = (a) => { const b = [...a]; for (let i = b.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1));[b[i], b[j]] = [b[j], b[i]] } return b }
return `按行打乱:\n${sh(lines(s)).join('\n')}\n\n按字符打乱:\n${sh(Array.from(s.replace(/\n/g, ''))).join('')}` } })
T({ id: 'text-repeater', name: '重复文本', group: 'text', icon: 'text', desc: '一行重复 N 次,可加序号,做占位或测试数据方便', keywords: ['repeat', '重复', '批量'], ph: '第 {n} 行',
fn: (s) => Array.from({ length: 5 }, (_, i) => s.replace(/\{n\}/g, i + 1)).join('\n') + '\n(这里示例 5 次,{n} 会替换成序号)' })
T({ id: 'text-merge', name: '文本合并', group: 'text', icon: 'text', desc: '按行合并成一行:空格、逗号、加引号、加括号', keywords: ['merge', 'join', '合并', '拼接'], ph: 'a\nb\nc',
fn: (s) => { const l = lines(s).filter((x) => x.trim() !== ''); return [
['空格', l.join(' ')], ['英文逗号', l.join(',')], ['逗号 + 空格', l.join(', ')],
['换行转逗号(英文)', l.map((x) => `'${x}'`).join(', ')], ['SQL IN', `(${l.map((x) => (isNaN(x) ? `'${x}'` : x)).join(', ')})`],
['JSON 数组', JSON.stringify(l, null, 2)], ['竖线', l.join(' | ')],
].map(([n, v]) => `# ${n}\n${v}`).join('\n\n') } })
T({ id: 'text-split', name: '文本拆分', group: 'text', icon: 'text', desc: '按分隔符切成多行,或按 N 个字符一段切', keywords: ['split', '拆分', '分割'], ph: 'a,b,c,d',
fn: (s) => `按逗号:\n${s.split(/[,,]/).map((x) => x.trim()).filter(Boolean).join('\n')}\n\n按空格:\n${s.split(/\s+/).filter(Boolean).join('\n')}\n\n每 10 字一段:\n${(s.match(/[\s\S]{1,10}/g) || []).join('\n')}` })
T({ id: 'text-columns', name: '文本转列', group: 'text', icon: 'text', desc: '把一列文本变多列,适合做对齐的清单', keywords: ['columns', '列', '排版'], ph: '1\n2\n3\n4\n5\n6',
fn: (s) => { const l = lines(s).filter((x) => x.trim() !== ''); const w = Math.ceil(l.length / 3); const cols = [l.slice(0, w), l.slice(w, 2 * w), l.slice(2 * w)]
return cols[0].map((_, i) => cols.map((c) => (c[i] || '').padEnd(14, ' ')).join('')).join('\n') } })
T({ id: 'text-cleaner', name: '文本清洗', group: 'text', icon: 'text', desc: '去多余空格、全角转半角、去不可见字符、统一换行', keywords: ['clean', '清洗', '空格', '不可见'], ph: '带 全角空格 和 多余空格 的文本',
fn: (s) => s.replace(/\r\n?/g, '\n').replace(/[\u200b-\u200f\ufeff]/g, '').replace(/[\u00a0\u3000]/g, ' ').replace(/[ \t]+/g, ' ').replace(/ *\n */g, '\n').replace(/\n{3,}/g, '\n\n').trim() })
T({ id: 'remove-blank', name: '删除空行', group: 'text', icon: 'text', desc: '去掉空行与只有空格的行', keywords: ['blank', '空行'], ph: 'a\n\n \nb', fn: (s) => lines(s).filter((l) => l.trim() !== '').join('\n') })
T({ id: 'remove-punctuation', name: '去标点', group: 'text', icon: 'text', desc: '去掉中英文标点,只留文字与数字', keywords: ['punctuation', '标点'], ph: '你好,世界!Hello, world.',
fn: (s) => s.replace(/[!-/:-@[-`{-~]/g, '').replace(/[,。!?;:、“”‘’()《》【】…—~·]/g, '') })
T({ id: 'line-number', name: '加行号', group: 'text', icon: 'text', desc: '给每行加序号,多种格式', keywords: ['line number', '行号'], ph: '第一行\n第二行',
fn: (s) => lines(s).map((l, i) => `${String(i + 1).padStart(3, ' ')}. ${l}`).join('\n') })
T({ id: 'prefix-suffix', name: '批量加前后缀', group: 'text', icon: 'text', desc: '每行加前缀 / 后缀,写 SQL、代码数组很方便', keywords: ['prefix', 'suffix', '前后缀', '批量'], ph: 'a\nb\nc',
fn: (s) => lines(s).map((l) => `"${l}",`).join('\n') + '\n\n--- 前缀 # ---\n' + lines(s).map((l) => `# ${l}`).join('\n') })
T({ id: 'find-replace', name: '批量查找替换', group: 'text', icon: 'text', desc: '普通替换 + 正则替换 + 去标签,一并给你', keywords: ['find', 'replace', '替换', '正则替换'], ph: 'Hello World Hello',
fn: (s) => `普通(Hello→你好):\n${s.split('Hello').join('你好')}\n\n正则(数字→#):\n${s.replace(/\d+/g, '#')}\n\n去 HTML 标签:\n${s.replace(/<[^>]+>/g, '')}` })
T({ id: 'word-count', name: '字数统计', group: 'text', icon: 'text', desc: '字符、汉字、单词、行数、段落、阅读时长全给你', keywords: ['count', '字数', '统计'], ph: '把一段文字粘进来…',
fn: (s) => { const zh = (s.match(/[\u4e00-\u9fa5]/g) || []).length; const en = (s.match(/[A-Za-z]+/g) || []).length
return [`总字符:${s.length}`, `不含空格:${s.replace(/\s/g, '').length}`, `汉字:${zh}`, `英文单词:${en}`, `行数:${lines(s).length}`,
`段落:${s.split(/\n\s*\n/).filter((x) => x.trim()).length}`, `阅读时长:约 ${Math.max(1, Math.round((zh + en * 1.5) / 400))} 分钟`].join('\n') } })
T({ id: 'char-limit-check', name: '字数限制检查', group: 'text', icon: 'text', desc: '按常见平台限制告诉你超没超(微博 140、小红书 1000…)', keywords: ['limit', '限制', '字数', '微博'], ph: '要检查的文案…',
fn: (s) => `当前 ${s.length} 字(不含空格 ${s.replace(/\s/g, '').length})\n` + [['微博', 140], ['小红书正文', 1000], ['小红书标题', 20], ['抖音标题', 55], ['公众号摘要', 120], ['SEO 标题', 60], ['SEO 描述', 160]]
.map(([n, m]) => `${s.replace(/\s/g, '').length <= m ? '✓' : '✗'} ${n}:${m} 字,${s.replace(/\s/g, '').length > m ? `超 ${s.replace(/\s/g, '').length - m}` : `剩 ${m - s.replace(/\s/g, '').length}`}`).join('\n') })
T({ id: 'reading-time', name: '阅读时长', group: 'text', icon: 'text', desc: '按中文 400 字/分、英文 200 词/分估算朗读与阅读时间', keywords: ['reading', '时长', '朗读'], ph: '文章内容…',
fn: (s) => { const zh = (s.match(/[\u4e00-\u9fa5]/g) || []).length; const en = (s.match(/[A-Za-z]+/g) || []).length
return `阅读:约 ${Math.max(1, Math.round(zh / 400 + en / 200))} 分钟\n朗读:约 ${Math.max(1, Math.round(zh / 240 + en / 140))} 分钟\n字数:${zh + en}` } })
T({ id: 'keyword-density', name: '关键词密度', group: 'text', icon: 'text', desc: '统计高频词与密度,写标题、做 SEO 用', keywords: ['keyword', 'density', '密度', 'seo'], ph: '把文章粘进来…',
fn: (s) => { const cnt = new Map()
for (const w of words(s)) { const k = w.toLowerCase(); if (k.length < 2) continue; cnt.set(k, (cnt.get(k) || 0) + 1) }
const total = [...cnt.values()].reduce((a, b) => a + b, 0) || 1
return [...cnt].sort((a, b) => b[1] - a[1]).slice(0, 25).map(([w, n]) => `${String(n).padStart(3, ' ')} ${(n / total * 100).toFixed(2)}% ${w}`).join('\n') } })
T({ id: 'text-similarity', name: '文本相似度', group: 'text', icon: 'text', desc: '按字符二元组算相似度,两段文字差多少一目了然', keywords: ['similarity', '相似度', '查重'], ph: '第一段\n---\n第二段(用单行 --- 分隔)',
fn: (s) => { const [a, b] = s.split(/^---$/m); if (a === undefined || b === undefined) throw new Error('请用单独一行 --- 分隔两段文本')
const bi = (t) => { const m = new Map(); const x = t.replace(/\s/g, ''); for (let i = 0; i < x.length - 1; i++) { const k = x.slice(i, i + 2); m.set(k, (m.get(k) || 0) + 1) } return m }
const A = bi(a), B = bi(b); let inter = 0, ua = 0, ub = 0
for (const [k, v] of A) { ua += v; inter += Math.min(v, B.get(k) || 0) } for (const v of B.values()) ub += v
const dice = (2 * inter) / (ua + ub || 1)
return `相似度:${(dice * 100).toFixed(1)}%\nA 长度 ${a.replace(/\s/g, '').length},B 长度 ${b.replace(/\s/g, '').length}` } })
T({ id: 'sentence-split', name: '分句', group: 'text', icon: 'text', desc: '按中英文句号问号叹号把长文切成一句一行', keywords: ['sentence', '分句'], ph: '第一句。第二句!第三句?',
fn: (s) => s.split(/(?<=[。!?!?;;\n])/).map((x) => x.trim()).filter(Boolean).map((x, i) => `${i + 1}. ${x}`).join('\n') })
T({ id: 'acronym-extract', name: '提取缩写', group: 'text', icon: 'text', desc: '把一句话做成首字母缩写', keywords: ['acronym', '缩写'], ph: 'Application Programming Interface',
fn: (s) => words(s).map((w) => w[0].toUpperCase()).join('') })
T({ id: 'word-frequency', name: '词频表', group: 'text', icon: 'text', desc: '逐词计数并排序,导出 CSV 也行', keywords: ['frequency', '词频'], ph: '把文本粘进来…',
fn: (s) => { const m = new Map(); for (const w of words(s)) { const k = w.toLowerCase(); if (k.length < 2) continue; m.set(k, (m.get(k) || 0) + 1) }
return [...m].sort((a, b) => b[1] - a[1]).map(([w, n], i) => `${i + 1},${w},${n}`).join('\n') } })
T({ id: 'text-redact', name: '敏感信息打码', group: 'text', icon: 'shield', desc: '一键遮住手机号、邮箱、身份证、银行卡,发截图前用', keywords: ['redact', '打码', '脱敏', '隐私'], ph: '张三 13812345678 a@b.com 6222021234567890',
fn: (s) => s.replace(/(?<!\d)(1[3-9]\d)\d{4}(\d{4})(?!\d)/g, '$1****$2')
.replace(/([A-Za-z0-9._%+-]{2})[A-Za-z0-9._%+-]*@([A-Za-z0-9.-]+\.[A-Za-z]{2,})/g, '$1***@$2')
.replace(/(?<!\d)(\d{6})\d{8}(\d{3}[\dXx])(?!\d)/g, '$1********$2')
.replace(/(?<!\d)(\d{4})\d{8,11}(\d{4})(?!\d)/g, '$1********$2') })
T({ id: 'mirror-text', name: '镜像文字', group: 'text', icon: 'text', desc: '左右翻转文字(Unicode 镜像字符),恶搞用', keywords: ['mirror', '镜像', '翻转'], ph: 'hello 你好',
fn: (s) => Array.from(s).reverse().map((c) => ({ a: 'a', b: 'b', c: 'c', d: 'd', e: 'e', p: 'q' }[c] || c)).join('') })
T({ id: 'upside-down-text', name: '倒立文字', group: 'text', icon: 'text', desc: '把文字翻转倒过来发出去', keywords: ['upside down', '倒立', '翻转'], ph: 'hello world',
fn: (s) => { const M = { a: 'ɐ', b: 'q', c: 'ɔ', d: 'p', e: 'ǝ', f: 'ɟ', g: 'ƃ', h: 'ɥ', i: 'ᴉ', j: 'ɾ', k: 'ʞ', l: 'l', m: 'ɯ', n: 'u', o: 'o', p: 'd', q: 'b', r: 'ɹ', s: 's', t: 'ʇ', u: 'n', v: 'ʌ', w: 'ʍ', x: 'x', y: 'ʎ', z: 'z', '?': '¿', '!': '¡', '.': '˙' }
return Array.from(s.toLowerCase()).reverse().map((c) => M[c] || c).join('') } })
T({ id: 'vertical-text', name: '竖排文字', group: 'text', icon: 'text', desc: '把每行文字竖向排成一列,做古风排版', keywords: ['vertical', '竖排'], ph: '床前明月光',
fn: (s) => Array.from(s.replace(/\s/g, '')).join('\n') })
T({ id: 'small-text', name: '上下标文字', group: 'text', icon: 'text', desc: '把数字和字母转成上下标,写化学式、脚注方便', keywords: ['superscript', 'subscript', '上标', '下标'], ph: 'H2O x2',
fn: (s) => { const SUP = { 0: '⁰', 1: '¹', 2: '²', 3: '³', 4: '⁴', 5: '⁵', 6: '⁶', 7: '⁷', 8: '⁸', 9: '⁹', '+': '⁺', '-': '⁻', '=': '⁼', '(': '⁽', ')': '⁾', a: 'ᵃ', b: 'ᵇ', c: 'ᶜ', d: 'ᵈ', e: 'ᵉ', n: 'ⁿ', x: 'ˣ', y: 'ʸ' }
const SUB = { 0: '₀', 1: '₁', 2: '₂', 3: '₃', 4: '₄', 5: '₅', 6: '₆', 7: '₇', 8: '₈', 9: '₉', '+': '₊', '-': '₋', '=': '₌', '(': '₍', ')': '₎', a: 'ₐ', e: 'ₑ', o: 'ₒ', x: 'ₓ' }
return `上标:${Array.from(s).map((c) => SUP[c] || c).join('')}\n下标:${Array.from(s).map((c) => SUB[c] || c).join('')}` } })
T({ id: 'strikethrough-text', name: '删除线文字', group: 'text', icon: 'text', desc: '给每个字加删除线,价格划线那种', keywords: ['strikethrough', '删除线'], ph: '¥199',
fn: (s) => Array.from(s).map((c) => (c === ' ' ? ' ' : c + '\u0336')).join('') })
T({ id: 'letter-spacing-text', name: '字间距加宽', group: 'text', icon: 'text', desc: '在字之间插空格,做印章、标题风', keywords: ['letter spacing', '字间距', '间隔'], ph: '哈基米',
fn: (s) => Array.from(s).join(' ') })
T({ id: 'emoji-remover', name: '去 emoji', group: 'text', icon: 'text', desc: '清掉表情符号,只留文字(存数据库前常需要)', keywords: ['emoji', '表情'], ph: '你好😀世界🎉',
fn: (s) => s.replace(/[\u{1F300}-\u{1FAFF}\u{2600}-\u{27BF}\u{FE0F}\u{1F1E6}-\u{1F1FF}]/gu, '') })
T({ id: 'special-symbols', name: '特殊符号库', group: 'text', icon: 'text', desc: '一大票符号点一下就复制:编号、箭头、星星、打勾', keywords: ['symbols', '符号', '箭头', '打勾'], ph: '(点下面按钮直接复制)',
fn: () => ['① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩', '❶ ❷ ❸ ❹ ❺', 'Ⅰ Ⅱ Ⅲ Ⅳ Ⅴ', '→ ← ↑ ↓ ↔ ⇒ ⇐ ⇔ ➜ ➤', '★ ☆ ✦ ✧ ✨ ❤ ♥ ♡ ✓ ✔ ✗ ✘', '◆ ◇ ● ○ ■ □ ▲ △ ▼ ▽', '§ ¶ © ® ™ ° ℃ ℉ ‰ ¥ € £ $', '☀ ☁ ☂ ❄ ⚡ ☎ ✉ ✂ ✎ ✏', '♪ ♫ ♬ ♭ ♮ ♯', '☑ ☐ ☒ ⚑ ⚐ ⚠']
.join('\n\n') })
T({ id: 'kaomoji', name: '颜文字', group: 'text', icon: 'text', desc: '一大票颜文字,点一下复制走', keywords: ['kaomoji', '颜文字', '表情'], ph: '(结果在下)',
fn: () => ['(๑•̀ㅂ•́)و✧', '( ͡° ͜ʖ ͡°)', '(╯°□°)╯︵ ┻━┻', '┬─┬ ノ( ゜-゜ノ)', '(っ˘ω˘ς )', '( •̀ ω •́ )✧', '٩(◕‿◕)۶', '(^▽^)', 'ಠ_ಠ', '(♥ω♥*)', '(¬‿¬)', 'ᕕ( ᐛ )ᕗ', '(ง •̀_•́)ง', 'ʕ•ᴥ•ʔ', '(づ。◕‿‿◕。)づ'].join(' ') })
T({ id: 'lorem-ipsum', name: '假文填充(英文)', group: 'text', icon: 'text', desc: 'Lorem ipsum 占位文字,做设计稿排版', keywords: ['lorem', '假文', '占位'], ph: '(结果在下)',
fn: () => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.' })
T({ id: 'lorem-chinese', name: '假文填充(中文)', group: 'text', icon: 'text', desc: '中文占位段落,字数够用、读起来不别扭', keywords: ['中文假文', '占位'], ph: '(结果在下)',
fn: () => '春天的风把柳条吹得很软,街上的人走得也慢。有人拎着刚买的菜,有人在路口等一盏灯变绿。日子就在这些小事情里,一点点向前。' })
T({ id: 'mock-data', name: '假数据生成', group: 'text', icon: 'bolt', desc: '批量生成姓名、手机号、邮箱、地址的 JSON,联调用', keywords: ['mock', '假数据', '测试数据', 'faker'], ph: '(点生成)',
fn: () => { const xing = '赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨', ming = '伟芳娜秀英敏静丽强磊洋艳勇军杰涛明超'
const arr = Array.from({ length: 10 }, () => { const n = xing[Math.floor(Math.random() * xing.length)] + ming[Math.floor(Math.random() * ming.length)] + (Math.random() < .5 ? ming[Math.floor(Math.random() * ming.length)] : '')
return { name: n, phone: '1' + [3, 5, 7, 8, 9][Math.floor(Math.random() * 5)] + String(Math.floor(Math.random() * 1e9)).padStart(9, '0'), email: `user${Math.floor(Math.random() * 9000 + 1000)}@example.com`, city: ['北京', '上海', '广州', '深圳', '杭州', '成都'][Math.floor(Math.random() * 6)] } })
return JSON.stringify(arr, null, 2) } })
})()