Skip to main content

Posts

Showing posts from January, 2020

Convert a `String` to Windows/Linux/Mac filename safe `String`

String.prototype.fToFilenameSafeStringI preserve a consistent naming convention throughout all the different tech layer making up a solution. For example backend DB object name will have same name as JavaScript object and will have the same CSS wrapping class name.This doesn't always work as certain characters are not allowed in some contexts, one being filenames character restrictions. A quick solution is to pipe the String through encodeURIComponent(...) but that is somewhat aggressive and results in filenames that are not human friendly.The below function encodes only the illegal characters which results in more readable filenames. String.prototype.fToFilenameSafeString = function() { /* unsafe characters list (https://stackoverflow.com/a/31976060/913223) : < (less than) > (greater than) : (colon - sometimes works, but is actually NTFS Alternate Data Streams) " (double quote) / (forward slash) \ (backslash) | (vertical bar or pipe) ? (question mark) …