nav search
Data Center Software Security Transformation DevOps Business Personal Tech Science Emergent Tech Bootnotes BOFH

JavaScript spec gets strung out on padding

ECMAScript 2017 addresses left-pad gate, alongside various improvements

By Thomas Claburn, 12 Jul 2017

ECMAScript 2017, the latest edition of the specification upon which JavaScript is based, plugs a gap left by awkward extinction of some Node.js code last year.

In March, 2016, as a result of a naming conflict with instant messaging app Kik, developer Azer Koçulu withdrew more than 250 of his modules from NPM, the service Node.js developers use to install dependencies.

One of the NPM modules that disappeared was left-pad, code module for padding out the lefthand side of strings with zeroes or spaces. And because left-pad was used in other NPM packages, like JSCS, its removal presented problems for thousands of applications. The incident has become a cautionary tale among those writing JavaScript code.

Web developers have managed to move on, but now ECMAScript 2017, aka ES8, provides an official mechanism for string padding. The specification, initially proposed in 2015 before the left-pad incident, defines two new String methods: padStart and padEnd.

The proposal points out that string padding functions are widely used by a majority of websites and frameworks and notes that the absence of a native method to pad strings makes JavaScript needlessly painful.

"It is highly probable that the majority of current string padding implementations are inefficient," the proposal says. "Bringing this into the platform will improve performance of the web, and developer productivity as they no longer have to implement these common functions."

Thus JavaScript developers can now look forward to employing bits of code like this:

let t = ['Alice', 'Bob', 'Carol'];
t.forEach(p => console.log(p.padStart(8, '-')));

The resulting output pads the list of names out to eight character spaces each:


There are other more interesting additions to ECMAScript, however.

"I'm most excited about Async functions, Trailing commas, and Object.entries," tweeted Kent C Dodds, a PayPal JavaScript engineer active in the JavaScript community.

Async functions, already implemented in some browsers, provide a solution to "callback hell," the JavaScript programming pattern known for creating inverted pyramids of difficult-to-read code.

ES2015's Promises helped somewhat. But ES2017's Async/Await implementation means complex server queries can be built in a far more readable and maintainable way.

The addition of Object.entries and Object.values should simplify iterating over JavaScript objects to obtain object keys and their associated values. The former returns an array of key-value pairs in an object; the later returns an array just composed of values, to complement the already existing Object.keys.

Trailing commas, meanwhile, allow lists of parameters in a function to conclude with a trailing comma after the last element. This lets developers put additional parameters on a new line, without adding a comma on the previous line to extend the series. Though seemingly trivial, reducing the number of lines where editing might occur is likely to reduce errors.

Looking further out to ECMAScript 2018, the ninth edition of the specification, we may see Async interation, Regexp Lookbehind Assertions, and Private fields, among other novelties. ®

The Register - Independent news and views for the tech sector. Part of Situation Publishing