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.Answer1:
Basically, all you need is a function that removes special characters and perhaps replaces them using an arbitrary system.
One option was already named by passy, which is to make use of:
Nevertheless, there are some additional options you might use. E.g., you might check out the <a href="http://epeli.github.io/underscore.string/" rel="nofollow">underscore.string module</a>, which provides some functions for this. From these, I'd highly recommend the
From the <a href="https://github.com/epeli/underscore.string#readme" rel="nofollow">documentation</a>:<blockquote>
Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash.</blockquote>
To provide an example:
_.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.
Hope this helps.Answer2:
For Yeoman generators, the common way to handle this is to use
<%= _.dasherize(str) %> in templates. It doesn't take care of accented letters, but those should be valid filenames anyway.
console.log(this._.dasherize("some userProvided string")); // output: "some-user-provided-string"