Forked from lavoiesl/extract-func-comment-string.js
Last active
August 29, 2015 14:19
-
-
Save Farow/fd30b910556162259832 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* 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