Yeoman Prompt: How Generate A Valid Filename From A String?
Solution 1:
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:
this._.dasherize(str)
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:
From the documentation:
Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash.
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.
Solution 2:
For Yeoman generators, the common way to handle this is to use this._.dasherize(str)
in the JavaScript generator code or <%= _.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"
Post a Comment for "Yeoman Prompt: How Generate A Valid Filename From A String?"