37491

Yeoman prompt: how generate a valid filename from a string?

Question:

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:

this._.dasherize(str)

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 slugify function:

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 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"

Recommend

  • Trying to find and install libstdc++.so.5 to run C++ with mex for MATLAB
  • PHP replace a random word of a string
  • Tables - Worksheet_Change Fires Multiple Times
  • Using parses Facebook login api, results in current user null
  • Parsing an NSXMLElement in Cocoa
  • JQuery replacing special characters
  • JMock- java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch()
  • Middleman: referencing URL stored in a data file from markdown
  • How to wait for page to finish loading all content before running script or how to best detect major
  • MemoryCache.Set return removed cache item
  • How to append a div via PHP and AJAX Post
  • Using sed to extract string values
  • looking for cleaner code when embedding razor calls in text
  • Problem glTexGen in Open GL ES 2.0
  • Add a div to replace Video after Video Plays Through
  • Use of qualified name in function parameter
  • PHP: Very simple Encode/Decode string
  • Calling C function from lua
  • custom string delimiters stringtemplate-4
  • Programatically open file in visual studio
  • Creating a C++ function that calls other Lua function
  • Why isn't my “Fizz Buzz” test in R working?
  • Validate jQuery plugin, field not required
  • What does “t” refer to in this SQL?
  • CERN ROOT exporting data to plain text
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Django simple Captcha “No module named fields” error
  • How do I access an unhandled exception in an MVC Error view?
  • Why does access(2) check for real and not effective UID?
  • Google Custom Search with transparent background
  • Projection media query: browser support and workarounds?
  • vba code to select only visible cells in specific column except heading
  • When should I choose bucket sort over other sorting algorithms?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • A cron job substitute?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?
  • To Get the radio button value in ruby on rails