HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

String interpolation library

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
interpolationstringlibrary

Problem

I was bored over the last couple of days and wrote up a string interpolation library for JavaScript.

I'm very pleased with its functionality, it passes it 79 tests cross browser and the comments and README seem fine too.

My main concern here are the regular expressions. I'm not a real pro in that area so I suspect there could be some enhancement to them.

Another thing is readability of the code as well as how good the comments are. I'd like to have "unclear" sections of the code pointed out so I can improve the naming / comments.

The library brings with it the Formatter factory and the FormatError.

Basic usage

> format = Formatter()
> format('Good {} Sir {}.', 'morning', 'Lancelot')
'Good morning Sir Lancelot.'

> format('Good {time} Sir {name}.', 'morning', 'Lancelot')
'Good morning Sir Lancelot.'

> format('Good {0} Sir {1}.', ['morning', 'Lancelot'])
'Good morning Sir Lancelot.'


Source

```
(function(undefined) {
'use strict';

// Formatter factory
function Formatter(formats) {
function f() {
return format(f.formats, arguments);
}
f.formats = formats || {};
return f;
}

// Default formatters
Formatter.formats = {
repeat: repeat,

join: function(value, str) {
return value.join(str || ', ');
},

upper: function(value) {
return value.toUpperCase();
},

lower: function(value) {
return value.toLowerCase();
},

lpad: function(value, length, str) {
return pad(value, length, str, 'l');
},

rpad: function(value, length, str) {
return pad(value, length, str, 'r');
},

pad: function(value, length, str) {
return pad(value, length, str);
},

surround: function(value, left, right) {
return left + value + (right || left);
},

hex: function(value, lead) {
return (lead ?

Solution

I have not looked at your regexes in detail. Could you please explain them? The rest of the code could use better documentation as well.

I did find two serious code correctness issues:

-
Your :join() formatter will not work with the empty string as the delimiter.

return value.join(str || ', ');    // Boolean('') === false


-
Your :pad() formatter, when used for center padding, will not correctly add an odd number of padding characters, as the padding on either side is rounded down.

} else {
    return repeat(str, len - ~~(len / 2))
           + value
           + repeat(str, ~~(len / 2));
}


In general, using tricks such as ~~ tends to reduce the code's clarity, and this is an excellent example of that. You should use the Math.floor() and Math.ceil() functions instead if that is what you intend.

Fix these issues, and of course add corresponding tests. Also document the fact that :pad() and the other padding functions are only intended to work with a single padding character.

Code Snippets

return value.join(str || ', ');    // Boolean('') === false
} else {
    return repeat(str, len - ~~(len / 2))
           + value
           + repeat(str, ~~(len / 2));
}

Context

StackExchange Code Review Q#860, answer score: 2

Revisions (0)

No revisions yet.