Skip to content

Instantly share code, notes, and snippets.

@deleted
Last active December 14, 2015 22:28
Show Gist options
  • Save deleted/5158472 to your computer and use it in GitHub Desktop.
Save deleted/5158472 to your computer and use it in GitHub Desktop.
I'm starting to think that maybe coffescript is actually okay... I would not have been able to wrap my primitive brain around this regular expression without it's block regex feature.
# An implementation of http://docs.python.org/2/library/string.html#format-specification-mini-language
# WARNING: Not complete.
applyFormat = (value, formatSpec) ->
pattern = ///
([^{}](?=[<>=^]))?([<>]^)? # fill & align
([\+\-\x20])? # sign
(\#)? # integer base specifier
(0)? # zero-padding
(\d+)? # width
(,)? # use a comma thousands-seperator
(?:\.(\d+))? # precision
([bcdeEfFgGnosxX%])? # type
///
[fill, align, sign, hash, zeropad, width, comma, precision, type] = formatSpec.match(pattern)[1..]
if zeropad
fill = "0"
align = "="
if ! align
align = '>'
switch type
when 'b', 'c', 'd', 'o', 'x', 'X', 'n' # integer
isNumeric = true
value = '' + parseInt(value)
when 'e','E','f','F','g','G','n','%' # float
isNumeric = true
value = parseFloat(value)
if precision
value = value.toFixed(parseInt(precision))
else
value = ''+value
when 's' #string
isNumeric = false
value = '' + value
if isNumeric && sign
if sign in ["+"," "]
if value[0] != '-'
value = sign + value
if fill
while value.length < parseInt(width)
switch align
when '='
if value[0] in "+- "
value = value[0] + fill + value[1..]
else
value = fill + value
when '<'
value = value + fill
when '>'
value = fill + value
when '^'
throw new Error("Not implemented")
return value
applyFormat = function(value, formatSpec) {
var align, comma, fill, hash, isNumeric, pattern, precision, sign, type, width, zeropad, _ref, _ref1;
pattern = /([^{}](?=[<>=^]))?([<>]^)?([\+\-\x20])?(\#)?(0)?(\d+)?(,)?(?:\.(\d+))?([bcdeEfFgGnosxX%])?/;
_ref = formatSpec.match(pattern).slice(1), fill = _ref[0], align = _ref[1], sign = _ref[2], hash = _ref[3], zeropad = _ref[4], width = _ref[5], comma = _ref[6], precision = _ref[7], type = _ref[8];
if (zeropad) {
fill = "0";
align = "=";
}
if (!align) {
align = '>';
}
switch (type) {
case 'b':
case 'c':
case 'd':
case 'o':
case 'x':
case 'X':
case 'n':
isNumeric = true;
value = '' + parseInt(value);
break;
case 'e':
case 'E':
case 'f':
case 'F':
case 'g':
case 'G':
case 'n':
case '%':
isNumeric = true;
value = parseFloat(value);
if (precision) {
value = value.toFixed(parseInt(precision));
} else {
value = '' + value;
}
break;
case 's':
isNumeric = false;
value = '' + value;
}
if (isNumeric && sign) {
if (sign === "+" || sign === " ") {
if (value[0] !== '-') {
value = sign + value;
}
}
}
if (fill) {
while (value.length < parseInt(width)) {
switch (align) {
case '=':
if (_ref1 = value[0], __indexOf.call("+- ", _ref1) >= 0) {
value = value[0] + fill + value.slice(1);
} else {
value = fill + value;
}
break;
case '<':
value = value + fill;
break;
case '>':
value = fill + value;
break;
case '^':
throw new Error("Not implemented");
}
}
}
return value;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment