Connecting Tech Pros Worldwide Forums | Help | Site Map

Javascript formatter?

John Dalberg
Guest
 
Posts: n/a
#1: Jul 23 '05


I am using a utility that creates Javascript. However the javascript is one
hugely long stream of characters. Is there a utility that can format the
Javascipt so that it put each statement in a seperate line and indents
properly?


--
John Dalberg

Ivo
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Javascript formatter?


"John Dalberg" <john_dd@hotmail.com> wrote[color=blue]
> a hugely long stream of characters. Is there a utility that can format
> the Javascipt so that it put each statement in a seperate line and
> indents properly?[/color]

Different ideas what is proper indentation exist. I use this:
< http://4umi.com/web/bookmarklet/edit.htm >
It will also tell you if it finds an error.
hth
--
Ivo




Ira Baxter
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Javascript formatter?



"John Dalberg" <john_dd@hotmail.com> wrote in message
news:wr7tuu7ieuac.1vyt3n2hortqx.dlg@40tude.net...[color=blue]
>
>
> I am using a utility that creates Javascript. However the javascript is[/color]
one[color=blue]
> hugely long stream of characters. Is there a utility that can format the
> Javascipt so that it put each statement in a seperate line and indents
> properly?[/color]

See http://www.semdesigns.com/Products/F...Formatter.html

--
Ira D. Baxter, Ph.D., CTO 512-250-1018
Semantic Designs, Inc. www.semdesigns.com


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Javascript formatter?


"Ira Baxter" <idbaxter@semdesigns.com> writes:
[color=blue]
> "John Dalberg" <john_dd@hotmail.com> wrote in message
> news:wr7tuu7ieuac.1vyt3n2hortqx.dlg@40tude.net...[/color]
[color=blue][color=green]
>> Is there a utility that can format the Javascipt so that it put
>> each statement in a seperate line and indents properly?[/color]
>
> See http://www.semdesigns.com/Products/F...Formatter.html[/color]

Or let the browser do it:
---
<script type="text/javascript">
function reformat(code) {
try {
var func = Function("",code);
var text = func.toString();
var match = text.match(/function\s*\w*\(\)\s*\{([\s\S]*)}/);
if (match) {
return match[1];
} else {
return "ERROR\n" + code; //something wrong.
}
} catch (e) {
return "ERROR: " + e + "\n" + code;
}
}
</script>
<form action=""
onsubmit="this.elements['input'].value =
reformat(this.elements['input'].value);
return false;">
<textarea name="input" rows="10" cols="72">code here</textarea><br><input type="submit" value="Reformat">
</form>
---
Personally, I prefer the way Mozilla formats its Javascript :)
IE doesn't do any formatting at all, so don't use that.

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dr John Stockton
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Javascript formatter?


JRS: In article <br8toyhb.fsf@hotpop.com>, dated Tue, 5 Apr 2005
20:44:00, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lrn@hotpop.com> posted :
[color=blue]
> var text = func.toString();[/color]
[color=blue]
>Personally, I prefer the way Mozilla formats its Javascript :)
>IE doesn't do any formatting at all, so don't use that.[/color]

Code display within my javascript pages is largely based on
function.toString(); I only see the results in MSIE.

I write my code (E&OE) to fit within 69-character-wide boxes (as seen in
IE 4), and I choose the height of each box to suit the code.

Are the results generally acceptable in other browsers, and could
anything be done to make the average better (given the wide use of IE)?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#6: Jul 23 '05

re: Javascript formatter?


Dr John Stockton <spam@merlyn.demon.co.uk> writes:
[color=blue][color=green]
>> var text = func.toString();[/color][/color]
[color=blue]
> Code display within my javascript pages is largely based on
> function.toString(); I only see the results in MSIE.
>
> I write my code (E&OE) to fit within 69-character-wide boxes (as seen in
> IE 4), and I choose the height of each box to suit the code.[/color]

Yes, checking in IE, I can see that the code doesn't have to overflow the
box :)
[color=blue]
> Are the results generally acceptable in other browsers, and could
> anything be done to make the average better (given the wide use of IE)?[/color]

The boxes overflow in both Mozilla and Opera.

One of the problems is the number of empty lines between functions in
boxes with more than one function. In IE, there is one line, in both
Mozilla and Opera, there are three (an extra line before and after
each function). It shouldn't hurt to remove these empty lines, if they
exist.

Another difference is that IE doesn't reformat the body of the
function at all, whereas the other browsers do. In particular, they
move "}"'s to a line of their own. Opera also moves "{"'s to their
own line (the thing that makes me prefer Mozilla's format).
I don't see a simple solution to this.

The non-IE browsers also doesn't include comments in the output. All
in all, lines are probably shorter than in IE.

A non-simple solution would be to make the ShowFF function calculate
the necessary number of lines for the textarea, instead of providing
it as an argument. E.g.:

---
function trim(str) {
var match = /\S([\s\S]*\S)*/.exec(str);
return match ? match[0] : "";
}

function countLines(str, lineLength) {//counts lines when wrapped at lineLength
var lines = str.split(/[\n\r]/g);
var lineCount = lines.length;
for (var i = 0; i < lines.length; i++) {
lineCount += Math.floor(lines[i].length / lineLength);
}
return lineCount;
}

function ShowFF() { // Args are functions, last Arg is unused
var Len = arguments.length-1, S = ""
for (var j=0 ; j<Len ; j++) {
if (j>0) { S += "\n\n"; }
S += trim(arguments[j].toString());
}
var numLines = countLines(S, BoxX);
Depict(BoxX, numLines, S, "lightgreen") ; return "" }

function ShowDo(Fn, Ht) { // N.B. this calls Fn()
var string = trim(Fn.toString());
Depict(BoxX, countLines(string, BoxX), string, "red" ) ; Fn() ; return "" }

---

Good luck
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Dr John Stockton
Guest
 
Posts: n/a
#7: Jul 23 '05

re: Javascript formatter?


JRS: In article <is2yuz0b.fsf@hotpop.com>, dated Thu, 7 Apr 2005
22:12:52, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lrn@hotpop.com> posted :[color=blue]
>Dr John Stockton <spam@merlyn.demon.co.uk> writes:
>[color=green][color=darkred]
>>> var text = func.toString();[/color][/color]
>[color=green]
>> Code display within my javascript pages is largely based on
>> function.toString(); I only see the results in MSIE.
>>
>> I write my code (E&OE) to fit within 69-character-wide boxes (as seen in
>> IE 4), and I choose the height of each box to suit the code.[/color]
>
>Yes, checking in IE, I can see that the code doesn't have to overflow the
>box :)
>[color=green]
>> Are the results generally acceptable in other browsers, and could
>> anything be done to make the average better (given the wide use of IE)?[/color]
>
>The boxes overflow in both Mozilla and Opera.
> ...
>A non-simple solution would be to make the ShowFF function calculate
>the necessary number of lines for the textarea, instead of providing
>it as an argument. E.g.:
> ...[/color]

It's not quite that easy, since when I have

ShowFF(UsefulFunction(s), Demo(s)OfThatFunction, Length)

I often choose to make the box show only UsefulFunction(s) , with
Demo(s)OfThatFunction being visible to those who have noticed the thumb
on the vertical scroll-bar. You may not have seen the absence of the
thumb in cases without demo functions :-( .

Perhaps I'll make an empty parameter terminate size-counting.

Since I have 311 ShowFF and 122 ShowDo in 35 files, I think I'll use new
names and make the change slowly ...

[color=blue]
>Good luck[/color]

With code written by you, surely that is not needed?

Thanks.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dr John Stockton
Guest
 
Posts: n/a
#8: Jul 23 '05

re: Javascript formatter?


JRS: In article <is2yuz0b.fsf@hotpop.com>, dated Thu, 7 Apr 2005
22:12:52, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lrn@hotpop.com> posted :
[color=blue]
> var lines = str.split(/[\n\r]/g);[/color]

In IE4, it appears that blank lines do not generate a corresponding
element in the array lines .

ISTR reading about varying behaviours of split().

Test page, showing old & new code in operation, is
<URL:http://www.merlyn.demon.co.uk/js-boxes.htm> .


Code should be self-documenting; but how to do a self-documenting blank
line ???

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dr John Stockton
Guest
 
Posts: n/a
#9: Jul 23 '05

re: Javascript formatter?


JRS: In article <Hn3NlrFvtvWCFwRO@merlyn.demon.co.uk>, dated Mon, 11 Apr 2005 23:34:23, seen in news:comp.lang.javascript, Dr John Stockton <spam@merlyn.demon.co.uk> posted :[color=blue]
>JRS: In article <is2yuz0b.fsf@hotpop.com>, dated Thu, 7 Apr 2005
>22:12:52, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
><lrn@hotpop.com> posted :
>[color=green]
>> var lines = str.split(/[\n\r]/g);[/color][/color]
[color=blue]
> <URL:http://www.merlyn.demon.co.uk/js-boxes.htm> .[/color]


This, I think, does what countLines should have done :-


function LineCount(S, M) { // counts lines when wrapped at lineLength - blank lines ? A test blank line follows :-

var j = xj = R = N = 0, L = S.length, X = 1, C
while (j<L) { C = S.charCodeAt(j++)
if (C!=10 && C!=13) continue
if (C==10) N++
if (C==13) R++
X += Math.max(Math.floor((j-xj-2)/M), 0) ; xj = j }
// Not good enough, as MSIE splits at whitespace
return X + Math.max(N, R) }

// countLines = LineCount


Note that from "function" to ":-" is coded, posted, and transmitted
as a single line; how you see it depends on your choice of service,
software, and settings.

If the code is written so that coded line-length fits in box-size,
as I "always" do, then there is no need on my system to allow for
line-wrap. Is there then an actual need to consider line-wrap in
any other system? If there is, do other browsers wrap at exactly N,
rather than at the previous whitespace?

H'mmm - IE4 has a bug; although whitespace is reduced to newline
at such a wrap, the choice of wrap point is affected by the size
of the whitespace. However, that should only matter here if a
line is wrapped more than once.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Dr John Stockton
Guest
 
Posts: n/a
#10: Aug 23 '05

re: Javascript formatter?


JRS: In article <is2yuz0b.fsf@hotpop.com>, dated Thu, 7 Apr 2005
22:12:52, seen in news:comp.lang.javascript, Lasse Reichstein Nielsen
<lrn@hotpop.com> posted :[color=blue]
>Dr John Stockton <spam@merlyn.demon.co.uk> writes:
>[color=green][color=darkred]
>>> var text = func.toString();[/color][/color]
>[color=green]
>> Code display within my javascript pages is largely based on
>> function.toString(); I only see the results in MSIE.[/color][/color]
[color=blue]
>Good luck[/color]

That worked.

Now I'm trying to improve the Pop-Up code display. It now puts a <pre>
rather than a <textarea> in the new window, and allows the window to
scroll. I also propose to incorporate auto-calculation of the "rows"
parameter, as in the Code Display section a little higher up the page.

It can be seen in the section Btn & PopCode which is just above
<URL:http://www.merlyn.demon.co.uk/js-nclds.htm#Inc3>.


Questions :

(A) In PopCode, the window height and width are somewhat pragmatical :-
var Wndw = window.open("", "X"+new Date().getTime(), // /scr-bar?
"height=" + (17*btn.btnargs[1]+28) + ",width=" + (8*BoxX+20) +
",resizable,scrollbars")
where btnargs[1] is the number of lines I want to show, BoxX the number
of characters per line I want to show, 20 and 28 give the right and
bottom margins matching the top and left ones, all empirical for my
present browser setup.

Without undue effort, can the initial height and width be set in due
proportion to the actual font size?


(B) In Btn, should document.close() be there (Btn is for execution
in-line during page display)? It's there as a result of possibly-
misinterpreted advice.


(C) Can SafeHTML be coded so that it is shown properly both in the page
proper and in the pop-up, and if so how? Its real code is
function SafeHTML(S) { // may be displayed incorrectly
return S.replace(/&/g, "&amp;").
replace(/</g, "&lt;").replace(/>/g, "&gt;") }



(D) Anything else?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Closed Thread


Similar JavaScript / Ajax / DHTML bytes