p5.js-web-editor/server/utils/generateFileSystemSafeName.js

17 lines
532 B
JavaScript
Raw Normal View History

2018-12-15 07:05:52 +00:00
/**
* generate file system safe string for a given string
2018-12-15 07:05:52 +00:00
* that can be used as a valid file name
* in all operating systems
* @param {String} string
* @param {String} replacer (optional) character to replace invalid characters
*/
function generateFileSystemSafeName(string, replacer) {
2018-12-15 07:05:52 +00:00
// from here https://serverfault.com/a/242134
const INVALID_CHARS_REGEX = /[*/?:\\<>|"\u0000-\u001F]/g;
const slug = string.replace(INVALID_CHARS_REGEX, replacer || '');
return slug;
}
export default generateFileSystemSafeName;