Skip to content

Instantly share code, notes, and snippets.

@Farow
Forked from lavoiesl/extract-func-comment-string.js
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Farow/fd30b910556162259832 to your computer and use it in GitHub Desktop.
Save Farow/fd30b910556162259832 to your computer and use it in GitHub Desktop.
/*!
* Extract a function's comment, useful to have multiline string
* @link https://gist.github.com/lavoiesl/5880516
*
* Don't forget to use /*! to avoid the comment being removed in minification
*/
// Example:
// var myString = function_to_string(function(){/*!
// <p>
// foo bar
// </p>
// */});
function function_to_string(func, strip_leading_whitespace) {
if (strip_leading_whitespace === undefined) {
strip_leading_whitespace = 1;
}
let matches = func.toString().match(/function[\s\w]*?\(\)\s*?\{\s*?\/\*\!?\s*?\n([\s\S]+?)\s*?\*\/\s*\}/);
if (!matches) {
return false;
}
if (strip_leading_whitespace) {
matches[1] = matches[1].replace(/^(\t| {4})/gm, '');
}
return matches[1];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment