JavaScript: remove leading and trailing spaces

Ramble Preamble

Hot on the heels of finishing off one of my non-JavaScript mini projects – the reason why there’s a lull of late on this blog – I’d come to the weary conclusion that somethings you just need JavaScript for. The project in question is a web port of a simple csv convertor desktop app built with python and tkinter.

The web version was done using flask – a python based micro web framework. Most of the logic required was done with the desktop version, which was just simply transferred to the server side code and worked without a hitch. The snag was that the user input had to be sent via a HTTP POST message that would be picked up by the flask routing system, processed and then sent back, not ideal in of itself but stuck with it as I couldn’t find any alternative. The problem was that the post message had a size limit and thereby dropped anything over 1567 rows…

…what can be done in the browser should be done in the browser.

Enter the JavaScript

Realisation slowly dawned that JavaScript is needed, as what can be done in the browser should be done in the browser. Everything was fine until I found out that JavaScript was limited in terms of being able to trim leading or trailing spaces, or so I thought.

A quick search of something similar to Python’s lstrip or rstrip brings up several articles which talk about either using the trim() function introduced with ES5 (ECMAScript 5), extending it using the prototype chain or, prior to that, just crafting your own function using regex expressions.

It just so happens that there are native functions built in that accomplish what I need –

String.trimStart() – Remove trailing spaces from the start

String.trimEnd() – Remove trailing spaces from the end

It’s great for removing any and all trailing white space, including new lines which was causingĀ  trouble with my output. However, unlike their counterparts in Python, these functions do not allow passing any character inputs as filters. For e.g. in python, specific characters such as trailing commas or formatting tags like carriage returns or new lines can be passed for removal

Convert to CSV

In my case my code would append a space followed by a comma ” ,” to the end of each row input. This presents a trailing comma which the Python rstrip would take care of in one swoop, of the fell kind. However, thanks to finding the trimEnd() method, the solution that presents itself is very simple if not crude. Come of think of it, I could have easily solved the problem of trailing space with just the trim() function. Anyway here’s the sols:

After having to troubleshoot why my slice wasn’t working as it should – there was still trailing white space – I came to the sobering relisation that the split function does in fact split everything into a new comma separated list (doh). Anyway the extra formatting on line 10, as it was renamed to later on, does exactly that now, but wasn’t needed. Ah well, hindsight is 1 they say…which is what I calculated 20/20 to be – true.

So here’s to polishing off this project with just the bare minimum functionality for the moment, which is convert to CSV.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.