| OLD | NEW |
| (Empty) | |
| 1 /** |
| 2 * @license |
| 3 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
| 4 * |
| 5 * This code may only be used under the BSD style license found at polymer.githu
b.io/LICENSE.txt |
| 6 * The complete set of authors may be found at polymer.github.io/AUTHORS.txt |
| 7 * The complete set of contributors may be found at polymer.github.io/CONTRIBUTO
RS.txt |
| 8 * Code distributed by Google as part of the polymer project is also subject to |
| 9 * an additional IP rights grant found at polymer.github.io/PATENTS.txt |
| 10 */ |
| 11 (function(scope) { |
| 12 'use strict'; |
| 13 |
| 14 var parse = scope.parse || require('./parsing').parse; |
| 15 |
| 16 scope.defaults = { |
| 17 // Methods are aligned up to this much padding. |
| 18 maxMethodPadding: 40, |
| 19 // A string to prefix each line with. |
| 20 indent: '', |
| 21 // A string to show for stack lines that are missing a method. |
| 22 methodPlaceholder: '<unknown>', |
| 23 // A list of Strings/RegExps that will be stripped from `location` values on |
| 24 // each line (via `String#replace`). |
| 25 locationStrip: [], |
| 26 // A list of Strings/RegExps that indicate that a line is *not* important, and |
| 27 // should be styled as such. |
| 28 unimportantLocation: [], |
| 29 // A filter function to completely remove lines |
| 30 filter: function() { return false; }, |
| 31 // styles are functions that take a string and return that string when styled. |
| 32 styles: { |
| 33 method: passthrough, |
| 34 location: passthrough, |
| 35 line: passthrough, |
| 36 column: passthrough, |
| 37 unimportant: passthrough, |
| 38 }, |
| 39 }; |
| 40 |
| 41 // See Tero Tolonen's answer at |
| 42 // http://stackoverflow.com/questions/17575790/environment-detection-node-js-or-
browser |
| 43 /*jshint -W054 */ |
| 44 var isNode = new Function('try {return this===global;}catch(e){return false;}'); |
| 45 |
| 46 // For Stacky-in-Node, we default to colored stacks. |
| 47 if (isNode()) { |
| 48 var chalk = require('chalk'); |
| 49 |
| 50 scope.defaults.styles = { |
| 51 method: chalk.magenta, |
| 52 location: chalk.blue, |
| 53 line: chalk.cyan, |
| 54 column: chalk.cyan, |
| 55 unimportant: chalk.dim |
| 56 }; |
| 57 } |
| 58 |
| 59 |
| 60 function pretty(stackOrParsed, options) { |
| 61 options = mergeDefaults(options || {}, scope.defaults); |
| 62 var lines = Array.isArray(stackOrParsed) ? stackOrParsed : parse(stackOrParsed
); |
| 63 lines = clean(lines, options); |
| 64 |
| 65 var padSize = methodPadding(lines, options); |
| 66 var parts = lines.map(function(line) { |
| 67 var method = line.method || options.methodPlaceholder; |
| 68 var pad = options.indent + padding(padSize - method.length); |
| 69 |
| 70 var locationBits = [ |
| 71 options.styles.location(line.location), |
| 72 options.styles.line(line.line), |
| 73 ]; |
| 74 if ('column' in line) { |
| 75 locationBits.push(options.styles.column(line.column)); |
| 76 } |
| 77 var location = locationBits.join(':'); |
| 78 |
| 79 var text = pad + options.styles.method(method) + ' at ' + location; |
| 80 if (!line.important) { |
| 81 text = options.styles.unimportant(text); |
| 82 } |
| 83 return text; |
| 84 }); |
| 85 |
| 86 return parts.join('\n'); |
| 87 } |
| 88 |
| 89 function clean(lines, options) { |
| 90 var result = []; |
| 91 for (var i = 0, line; line = lines[i]; i++) { |
| 92 if (options.filter(line)) continue; |
| 93 line.location = cleanLocation(line.location, options); |
| 94 line.important = isImportant(line, options); |
| 95 result.push(line); |
| 96 } |
| 97 |
| 98 return result; |
| 99 } |
| 100 |
| 101 // Utility |
| 102 |
| 103 function passthrough(string) { |
| 104 return string; |
| 105 } |
| 106 |
| 107 function mergeDefaults(options, defaults) { |
| 108 var result = Object.create(defaults); |
| 109 Object.keys(options).forEach(function(key) { |
| 110 var value = options[key]; |
| 111 if (typeof value === 'object' && !Array.isArray(value)) { |
| 112 value = mergeDefaults(value, defaults[key]); |
| 113 } |
| 114 result[key] = value; |
| 115 }); |
| 116 return result; |
| 117 } |
| 118 |
| 119 function methodPadding(lines, options) { |
| 120 var size = options.methodPlaceholder.length; |
| 121 for (var i = 0, line; line = lines[i]; i++) { |
| 122 size = Math.min(options.maxMethodPadding, Math.max(size, line.method.length)
); |
| 123 } |
| 124 return size; |
| 125 } |
| 126 |
| 127 function padding(length) { |
| 128 var result = ''; |
| 129 for (var i = 0; i < length; i++) { |
| 130 result = result + ' '; |
| 131 } |
| 132 return result; |
| 133 } |
| 134 |
| 135 function cleanLocation(location, options) { |
| 136 if (options.locationStrip) { |
| 137 for (var i = 0, matcher; matcher = options.locationStrip[i]; i++) { |
| 138 location = location.replace(matcher, ''); |
| 139 } |
| 140 } |
| 141 |
| 142 return location; |
| 143 } |
| 144 |
| 145 function isImportant(line, options) { |
| 146 if (options.unimportantLocation) { |
| 147 for (var i = 0, matcher; matcher = options.unimportantLocation[i]; i++) { |
| 148 if (line.location.match(matcher)) return false; |
| 149 } |
| 150 } |
| 151 |
| 152 return true; |
| 153 } |
| 154 |
| 155 scope.clean = clean; |
| 156 scope.pretty = pretty; |
| 157 })(typeof module !== 'undefined' ? module.exports : (this.Stacky = this.Stacky |
| {})); |
| 158 |
| OLD | NEW |