react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / cli-table / lib / cli-table / utils.js
80687 views1/**2* Repeats a string.3*4* @param {String} char(s)5* @param {Number} number of times6* @return {String} repeated string7*/89exports.repeat = function (str, times){10return Array(times + 1).join(str);11};1213/**14* Pads a string15*16* @api public17*/1819exports.pad = function (str, len, pad, dir) {20if (len + 1 >= str.length)21switch (dir){22case 'left':23str = Array(len + 1 - str.length).join(pad) + str;24break;2526case 'both':27var right = Math.ceil((padlen = len - str.length) / 2);28var left = padlen - right;29str = Array(left + 1).join(pad) + str + Array(right + 1).join(pad);30break;3132default:33str = str + Array(len + 1 - str.length).join(pad);34};3536return str;37};3839/**40* Truncates a string41*42* @api public43*/4445exports.truncate = function (str, length, chr){46chr = chr || '…';47return str.length >= length ? str.substr(0, length - chr.length) + chr : str;48};4950/**51* Copies and merges options with defaults.52*53* @param {Object} defaults54* @param {Object} supplied options55* @return {Object} new (merged) object56*/5758function clone(a){59var b;60if (Array.isArray(a)){61b = [];62for (var i = 0, l = a.length; i < l; i++)63b.push(typeof a[i] == 'object' ? clone(a[i]) : a[i]);64return b;65} else if (typeof a == 'object'){66b = {};67for (var i in a)68b[i] = typeof a[i] == 'object' ? clone(a[i]) : a[i];69return b;70}71return a;72};7374exports.options = function (defaults, opts){75var c = clone(opts);76for (var i in defaults)77if (!(i in opts))78c[i] = defaults[i];79return c;80};818283//84// For consideration of terminal "color" programs like colors.js,85// which can add ANSI escape color codes to strings,86// we destyle the ANSI color escape codes for padding calculations.87//88// see: http://en.wikipedia.org/wiki/ANSI_escape_code89//90exports.strlen = function(str){91var code = /\u001b\[\d+m/g;92var stripped = ("" + str).replace(code,'');93return stripped.length;94}959697