Yeoman提示:如何从字符串生成有效的文件名? [英] Yeoman prompt: how generate a valid filename from a string?
问题描述
是否在Yeoman或Node中实现了一种从字符串生成有效文件名的方法?
Is there a method implemented in Yeoman or in Node to generate a valid filename from a string?
我的目的是将带重音的字母替换为普通字母,将空格替换为破折号等.
My aim is to replace accented letters by normal letters, spaces by dashes, etc.
推荐答案
基本上,您所需要的只是一个删除特殊字符并可能使用任意系统替换它们的函数.
Basically, all you need is a function that removes special characters and perhaps replaces them using an arbitrary system.
passy已经命名了一个选项,该选项可以利用:
One option was already named by passy, which is to make use of:
this._.dasherize(str)
尽管如此,您可能还会使用其他一些选项.例如,您可以查看 underscore.string模块,该模块为此提供了一些功能.从这些方面,我强烈建议您使用slugify
函数:
Nevertheless, there are some additional options you might use. E.g., you might check out the underscore.string module, which provides some functions for this. From these, I'd highly recommend the slugify
function:
从文档:
将文本转换为URL段.用破折号替换空格,强调字符和特殊字符.
Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash.
举个例子:
_.slugify("Un éléphant à l'orée du bois")
=> 'un-elephant-a-loree-du-bois';
这应该正是您所需要的,并且仍然保持良好的可读性.
This should be exactly what you need, and still keeps a good readability.
希望这会有所帮助.
这篇关于Yeoman提示:如何从字符串生成有效的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!