Node.js examples for String:Padding
Pad a string to a certain length with another string; similar to PHP's str_pad() function.
/* Pad a string to a certain length with another string; similar to PHP's str_pad() function.// w w w .j a va 2 s . co m * Derived from code by Carlos Reche <carlosreche at yahoo.com>. * len - Pad the string up to this length * (pad) - String used for padding (default: a single space) * (type) - Type can be String.PAD_LEFT, String.PAD_RIGHT (default) or String.PAD_BOTH */ String.PAD_LEFT = 0; String.PAD_RIGHT = 1; String.PAD_BOTH = 2; String.prototype.pad = function (len, pad, type) { var string = this; var append = new String (); len = isNaN (len) ? 0 : len - string.length; pad = typeof (pad) == 'string' ? pad : ' '; if (type == String.PAD_BOTH) { string = string.pad (Math.floor (len / 2) + string.length, pad, String.PAD_LEFT); return (string.pad (Math.ceil (len / 2) + string.length, pad, String.PAD_RIGHT)); } while ((len -= pad.length) > 0) append += pad; append += pad.substr (0, len + pad.length); return (type == String.PAD_LEFT ? append.concat (string) : string.concat (append)); } /* Generate a uniformly distributed random integer within the range <min> .. <max>. * (min) - Lower limit: random >= min (default: 0) * (max) - Upper limit: random <= max (default: 1) */ Math.randomInt = function (min, max) { if (! isFinite (min)) min = 0; if (! isFinite (max)) max = 1; return (Math.floor ((Math.random () % 1) * (max - min + 1) + min)); }