📜 ⬆️ ⬇️

Where are you? Heredoc from javascript

console.log(hered0c);


- !
.
,
!
... © HEREDOC

I will not torment in, javascript really no heredoc

But damn it! This did not prevent “HEREDOC” from writing with multi-text text that it does not exist.

This example was tested by me on Midori and Chrome. Somehow in Opere and Firefox I have no doubt, but on IE you always have to hope)). So there is reason for optimism!
')
This method has the only limitation: it’s impossible to use these 2 symbols * / in such a sequence — in principle, all scripted heredocs have such limitations. Just usually they allow us to choose those characters that will not be used inside heredoc.

Let's start:
 !function (root) { function heredoc(fn) { return fn.toString().split('\n').slice(1,-1).join('\n') + '\n' } var stripPattern = /^\s*(?=[^\s]+)/mg heredoc.strip = function(fn) { var text = heredoc(fn) var indentLen = text.match(stripPattern) .reduce(function (min, line) { return Math.min(min, line.length) }, Infinity) var indent = new RegExp('^\\s{' + indentLen + '}', 'mg') return indentLen > 0 ? text.replace(indent, '') : text } if (typeof exports === 'object') { module.exports = heredoc } else if (typeof define === 'function' && define.amd) { define(function() { return heredoc }) } else { root.heredoc = heredoc } }(this) 


And now we are testing:

 var hered0c = heredoc(function () {/*      -  !       .       ,       !         ... © HEREDOC */}); console.log(hered0c); 


Github
Jsfiddle

Test for suitability for use in different browsers right here !
If it's not difficult for anyone, please write down where “heredoc” does not work.

Source: https://habr.com/ru/post/230887/


All Articles