Comparing version 0.0.5 to 0.0.6
158
index.js
@@ -1,5 +0,7 @@ | ||
module.exports = ansiHTML; | ||
'use strict' | ||
module.exports = ansiHTML | ||
// Reference to https://github.com/sindresorhus/ansi-regex | ||
var re_ansi = /(?:(?:\u001b\[)|\u009b)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\u001b[A-M]/; | ||
var _regANSI = /(?:(?:\u001b\[)|\u009b)(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\u001b[A-M]/ | ||
@@ -17,3 +19,3 @@ var _defColors = { | ||
darkgrey: '888' | ||
}; | ||
} | ||
var _styles = { | ||
@@ -28,3 +30,3 @@ 30: 'black', | ||
37: 'lightgrey' | ||
}; | ||
} | ||
var _openTags = { | ||
@@ -36,4 +38,4 @@ '1': 'font-weight:bold', // bold | ||
'8': 'display:none', // hidden | ||
'9': '<del>', // delete | ||
}; | ||
'9': '<del>' // delete | ||
} | ||
var _closeTags = { | ||
@@ -43,7 +45,8 @@ '23': '</i>', // reset italic | ||
'29': '</del>' // reset delete | ||
}; | ||
[0, 21, 22, 27, 28, 39, 49].forEach(function (n) { | ||
_closeTags[n] = '</span>'; | ||
}); | ||
} | ||
;[0, 21, 22, 27, 28, 39, 49].forEach(function (n) { | ||
_closeTags[n] = '</span>' | ||
}) | ||
/** | ||
@@ -54,38 +57,38 @@ * Converts text with ANSI color codes to HTML markup. | ||
*/ | ||
function ansiHTML(text) { | ||
function ansiHTML (text) { | ||
// Returns the text if the string has no ANSI escape code. | ||
if (!re_ansi.test(text)) { | ||
return text; | ||
if (!_regANSI.test(text)) { | ||
return text | ||
} | ||
// Cache opened sequence. | ||
var ansiCodes = []; | ||
var ansiCodes = [] | ||
// Replace with markup. | ||
var ret = text.replace(/\033\[(\d+)*m/g, function (match, seq) { | ||
var ot = _openTags[seq]; | ||
var ot = _openTags[seq] | ||
if (ot) { | ||
// If current sequence has been opened, close it. | ||
if (!!~ansiCodes.indexOf(seq)) { | ||
ansiCodes.pop(); | ||
return '</span>'; | ||
if (!!~ansiCodes.indexOf(seq)) { // eslint-disable-line no-extra-boolean-cast | ||
ansiCodes.pop() | ||
return '</span>' | ||
} | ||
// Open tag. | ||
ansiCodes.push(seq); | ||
return ot[0] == '<' ? ot : '<span style="' + ot + ';">'; | ||
ansiCodes.push(seq) | ||
return ot[0] === '<' ? ot : '<span style="' + ot + ';">' | ||
} | ||
var ct = _closeTags[seq]; | ||
var ct = _closeTags[seq] | ||
if (ct) { | ||
// Pop sequence | ||
ansiCodes.pop(); | ||
return ct; | ||
ansiCodes.pop() | ||
return ct | ||
} | ||
return ''; | ||
}); | ||
return '' | ||
}) | ||
// Make sure tags are closed. | ||
var l = ansiCodes.length; | ||
(l > 0) && (ret += Array(l + 1).join('</span>')); | ||
var l = ansiCodes.length | ||
;(l > 0) && (ret += Array(l + 1).join('</span>')) | ||
return ret; | ||
return ret | ||
} | ||
@@ -98,39 +101,39 @@ | ||
ansiHTML.setColors = function (colors) { | ||
if (typeof colors != 'object') { | ||
throw new Error('`colors` parameter must be an Object.'); | ||
if (typeof colors !== 'object') { | ||
throw new Error('`colors` parameter must be an Object.') | ||
} | ||
var _finalColors = {}; | ||
var _finalColors = {} | ||
for (var key in _defColors) { | ||
var hex = colors.hasOwnProperty(key) ? colors[key] : null; | ||
var hex = colors.hasOwnProperty(key) ? colors[key] : null | ||
if (!hex) { | ||
_finalColors[key] = _defColors[key]; | ||
continue; | ||
_finalColors[key] = _defColors[key] | ||
continue | ||
} | ||
if ('reset' == key) { | ||
if(typeof hex == 'string'){ | ||
hex = [hex]; | ||
} | ||
if (!Array.isArray(hex) || hex.length == 0 || hex.some(function (h) { | ||
return typeof h != 'string'; | ||
})) { | ||
throw new Error('The value of `' + key + '` property must be an Array and each item could only be a hex string, e.g.: FF0000'); | ||
if ('reset' === key) { | ||
if (typeof hex === 'string') { | ||
hex = [hex] | ||
} | ||
var defHexColor = _defColors[key]; | ||
if(!hex[0]){ | ||
hex[0] = defHexColor[0]; | ||
if (!Array.isArray(hex) || hex.length === 0 || hex.some(function (h) { | ||
return typeof h !== 'string' | ||
})) { | ||
throw new Error('The value of `' + key + '` property must be an Array and each item could only be a hex string, e.g.: FF0000') | ||
} | ||
if (hex.length == 1 || !hex[1]) { | ||
hex = [hex[0]]; | ||
hex.push(defHexColor[1]); | ||
var defHexColor = _defColors[key] | ||
if (!hex[0]) { | ||
hex[0] = defHexColor[0] | ||
} | ||
if (hex.length === 1 || !hex[1]) { | ||
hex = [hex[0]] | ||
hex.push(defHexColor[1]) | ||
} | ||
hex = hex.slice(0, 2); | ||
} else if (typeof hex != 'string') { | ||
throw new Error('The value of `' + key + '` property must be a hex string, e.g.: FF0000'); | ||
hex = hex.slice(0, 2) | ||
} else if (typeof hex !== 'string') { | ||
throw new Error('The value of `' + key + '` property must be a hex string, e.g.: FF0000') | ||
} | ||
_finalColors[key] = hex; | ||
_finalColors[key] = hex | ||
} | ||
_setTags(_finalColors); | ||
}; | ||
_setTags(_finalColors) | ||
} | ||
@@ -140,5 +143,5 @@ /** | ||
*/ | ||
ansiHTML.reset = function(){ | ||
_setTags(_defColors); | ||
}; | ||
ansiHTML.reset = function () { | ||
_setTags(_defColors) | ||
} | ||
@@ -149,28 +152,33 @@ /** | ||
*/ | ||
ansiHTML.tags = { | ||
get open() { | ||
return _openTags; | ||
}, | ||
get close() { | ||
return _closeTags; | ||
} | ||
}; | ||
ansiHTML.tags = {} | ||
function _setTags(colors) { | ||
if (Object.defineProperty) { | ||
Object.defineProperty(ansiHTML.tags, 'open', { | ||
get: function () { return _openTags } | ||
}) | ||
Object.defineProperty(ansiHTML.tags, 'close', { | ||
get: function () { return _closeTags } | ||
}) | ||
} else { | ||
ansiHTML.tags.open = _openTags | ||
ansiHTML.tags.close = _closeTags | ||
} | ||
function _setTags (colors) { | ||
// reset all | ||
_openTags['0'] = 'font-weight:normal;opacity:1;color:#' + colors.reset[0] + ';background:#' + colors.reset[1]; | ||
_openTags['0'] = 'font-weight:normal;opacity:1;color:#' + colors.reset[0] + ';background:#' + colors.reset[1] | ||
// inverse | ||
_openTags['7'] = 'color:#' + colors.reset[1] + ';background:#' + colors.reset[0]; | ||
_openTags['7'] = 'color:#' + colors.reset[1] + ';background:#' + colors.reset[0] | ||
// dark grey | ||
_openTags['90'] = 'color:#' + colors.darkgrey; | ||
_openTags['90'] = 'color:#' + colors.darkgrey | ||
for (var code in _styles) { | ||
var color = _styles[code]; | ||
var oriColor = colors[color] || '000'; | ||
_openTags[code] = 'color:#' + oriColor; | ||
code = parseInt(code); | ||
_openTags[(code + 10).toString()] = 'background:#' + oriColor; | ||
var color = _styles[code] | ||
var oriColor = colors[color] || '000' | ||
_openTags[code] = 'color:#' + oriColor | ||
code = parseInt(code) | ||
_openTags[(code + 10).toString()] = 'background:#' + oriColor | ||
} | ||
} | ||
ansiHTML.reset(); | ||
ansiHTML.reset() |
{ | ||
"name": "ansi-html", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "An elegant lib that converts the chalked (ANSI) text to HTML.", | ||
@@ -9,2 +9,5 @@ "main": "index.js", | ||
}, | ||
"bin": { | ||
"ansi-html": "./bin/ansi-html" | ||
}, | ||
"repository": { | ||
@@ -22,3 +25,3 @@ "type": "git", | ||
}, | ||
"license": "Apache, Version 2.0", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
@@ -33,9 +36,18 @@ "url": "https://github.com/Tjatse/ansi-html/issues" | ||
"devDependencies": { | ||
"chalk": "~1.1.1", | ||
"mocha": "^1.21.4", | ||
"chai": "^1.9.1", | ||
"lodash": "~2.4.1" | ||
"chalk": "^1.1.3", | ||
"lodash": "^2.4.2" | ||
}, | ||
"readmeFilename": "README.md", | ||
"homepage": "https://github.com/Tjatse/ansi-html" | ||
"homepage": "https://github.com/Tjatse/ansi-html", | ||
"standard": { | ||
"ignore": [], | ||
"globals": [ | ||
"describe", | ||
"it", | ||
"before", | ||
"after" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
0
20049
6
158