Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mixing of numbers and Chinese and English #239

Open
szx741 opened this issue Aug 5, 2022 · 3 comments
Open

Mixing of numbers and Chinese and English #239

szx741 opened this issue Aug 5, 2022 · 3 comments

Comments

@szx741
Copy link

szx741 commented Aug 5, 2022

想要数字前后也要有分隔符
slugify('2022年中文01英文nihao02')
现在的slugify是这样子的
2022nian-zhong-wen-01ying-wen-nihao02
但我想要
2022-nian-zhong-wen-01-ying-wen-nihao-02
不知道有没有这样子的功能

@dzcpy
Copy link
Owner

dzcpy commented Aug 6, 2022

Well, it supposed to work that way, I'll do some investigation. Meanwhile you can implement it with a workaround like:

slugify('2022年中文01英文nihao02'.replace(/(\d+)/g, ' $1 ')).replace(/^-|-$/g, '')

@szx741
Copy link
Author

szx741 commented Sep 3, 2022

你好,我又发现有一点问题,中文在前,英文在后,中间会有横杠,如果中文在后,英文在前,就没有横杠连接了。
比如
slugify('你好world') //ni-hao-world
slugify('hello世界') //helloshi-jie

@lionel-rowe
Copy link

You can get better Chinese results by disabling the fixChineseSpacing option and instead use the web-standard Intl.Segmenter API:

import { slugify } from 'transliteration'

function slugifyWords(str) {
    // you can use 'zh-CN' or another locale instead of 'en-US', but 'en-US' still works fine, even for Chinese text
    return [...new Intl.Segmenter('en-US', { granularity: 'word' }).segment(str)]
        .filter((x) => x.isWordLike)
        .map((x) => x.segment)
        .map((x) => slugify(x, { fixChineseSpacing: false }))
        .join('-')
}

console.log(slugifyWords('2022年中文01英文nihao02'))
// 2022-nian-zhongwen-01-yingwen-nihao02
console.log(slugifyWords('你好world'))
// nihao-world
console.log(slugifyWords('hello世界'))
// hello-shijie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants