Comparing version
const widthsVerdana11 = require('./widths-verdana-11.json') | ||
const astralRegex = require('unicode-astral-regex') | ||
function calcWidth (charWidthTable) { | ||
const SCALE = 10 | ||
const SCALE = 10 // Prevent results like 60.599999999999994 | ||
const widthTable = charWidthTable.map(w => Math.round(w * SCALE)) | ||
return function (text) { | ||
if (typeof text !== 'string') { | ||
return 0 | ||
} else { | ||
let total = 0 | ||
let i = text.length | ||
let charCode = 0 | ||
while (i--) { | ||
charCode = text[i].charCodeAt() | ||
total += widthTable[charCode < 127 ? charCode : 64] // "@" for overflows | ||
} | ||
return total / SCALE | ||
widthTable[64] = widthTable[64] + 2 // Slightly increase width of "@" by 0.2px | ||
return function (text, astral) { | ||
typeAssert(typeof text === 'string', 'Input must be string') | ||
if (astral) text = text.match(astralRegex) | ||
let total = 0 | ||
let code = 0 | ||
let i = text.length | ||
while (i--) { | ||
code = text[i].charCodeAt() | ||
total += widthTable[code < 127 ? code : 64] // Width as "@" for overflows | ||
} | ||
return total / SCALE | ||
} | ||
} | ||
const typeAssert = (assertion, message) => { | ||
if (!assertion) throw new TypeError(message) | ||
} | ||
module.exports = { | ||
Verdana11: calcWidth(widthsVerdana11) | ||
} |
@@ -10,3 +10,4 @@ module.exports = { | ||
grey: '999', | ||
gray: '999', | ||
cyan: '4BA' | ||
} |
const calcWidth = require('./calc-text-width.js').Verdana11 | ||
const colorPresets = require('./color-presets.js') | ||
module.exports = function ({subject, status, color, style}) { | ||
module.exports = function ({subject, status, color, style, emoji, icon}) { | ||
color = colorPresets[color] || color || colorPresets['blue'] | ||
const sbRectWidth = calcWidth(subject) + 11 | ||
const stRectWidth = calcWidth(status) + 11 | ||
const stTextWidth = calcWidth(status, emoji) | ||
const sbRectWidth = calcWidth(subject, emoji) + 10 + (icon ? 15 : 0) | ||
const stRectWidth = stTextWidth + 10 | ||
const width = sbRectWidth + stRectWidth | ||
@@ -13,3 +14,3 @@ | ||
return ` | ||
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="20"> | ||
<svg width="${width}" height="20" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
<g> | ||
@@ -20,7 +21,7 @@ <rect width="${sbRectWidth}" height="20" fill="#555"/> | ||
<g fill="#fff" text-anchor="start" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11"> | ||
<text x="7" y="14.8" fill="#000" opacity="0.1">${subject}</text> | ||
<text x="6" y="13.8">${subject}</text> | ||
<text x="${sbRectWidth + 5.5}" y="14.8" fill="#000" opacity="0.1">${status}</text> | ||
<text x="${sbRectWidth + 4.5}" y="13.8">${status}</text> | ||
</g> | ||
<text x="${icon ? '22' : '6'}" y="14.8" fill="#000" opacity="0.1">${subject}</text> | ||
<text x="${icon ? '21' : '5'}" y="13.8">${subject}</text> | ||
<text x="${sbRectWidth + 5.5}" y="14.8" fill="#000" opacity="0.1" textLength="${stTextWidth}">${status}</text> | ||
<text x="${sbRectWidth + 4.5}" y="13.8" textLength="${stTextWidth}">${status}</text> | ||
</g>${icon ? genIconMarkup(icon) : ''} | ||
</svg> | ||
@@ -31,3 +32,3 @@ ` | ||
return ` | ||
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="20"> | ||
<svg width="${width}" height="20" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
<linearGradient id="a" x2="0" y2="100%"> | ||
@@ -44,9 +45,13 @@ <stop offset="0" stop-color="#EEE" stop-opacity=".1"/> | ||
<g fill="#fff" text-anchor="start" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11"> | ||
<text x="7" y="14.8" fill="#000" opacity="0.25">${subject}</text> | ||
<text x="6" y="13.8">${subject}</text> | ||
<text x="${sbRectWidth + 5.5}" y="14.8" fill="#000" opacity="0.25">${status}</text> | ||
<text x="${sbRectWidth + 4.5}" y="13.8">${status}</text> | ||
</g> | ||
<text x="${icon ? '22' : '6.2'}" y="14.8" fill="#000" opacity="0.25">${subject}</text> | ||
<text x="${icon ? '21' : '5.2'}" y="13.8">${subject}</text> | ||
<text x="${sbRectWidth + 5.5}" y="14.8" fill="#000" opacity="0.25" textLength="${stTextWidth}">${status}</text> | ||
<text x="${sbRectWidth + 4.5}" y="13.8" textLength="${stTextWidth}">${status}</text> | ||
</g>${icon ? genIconMarkup(icon) : ''} | ||
</svg> | ||
` | ||
} | ||
function genIconMarkup (iconB64) { | ||
return `<image x="4.5" y="3" width="14" height="14" xlink:href="${iconB64}"/>` | ||
} |
{ | ||
"name": "badgen", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Fast svg badge generator.", | ||
"repository": "amio/badgen", | ||
"author": "Amio <amio.cn@gmail.com>", | ||
@@ -12,11 +13,15 @@ "license": "ISC", | ||
"preview": "node preview/serve.js", | ||
"snaptests": "TAP_SNAPSHOT=1 npm test", | ||
"pretest": "npm run lint", | ||
"test": "tap test/*.spec.js --reporter spec" | ||
"test": "tap test/*.spec.js --reporter spec --coverage" | ||
}, | ||
"devDependencies": { | ||
"benchmark": "^2.1.4", | ||
"serve-marked": "0.0.1", | ||
"serve-marked": "0.2.0", | ||
"standard": "^11.0.1", | ||
"tap": "^12.0.1" | ||
}, | ||
"dependencies": { | ||
"unicode-astral-regex": "^1.0.1" | ||
} | ||
} |
@@ -1,8 +0,13 @@ | ||
# badgen [![npm-version][npm-badge]][npm-link] [![install size][pp-badge]][pp-link] | ||
# badgen | ||
Fast, handcraft, pure JavaScript badge generator. | ||
[![npm version][npm-badge]][npm-link] | ||
[![install size][pp-badge]][pp-link] | ||
[![Coverage Status][cr-badge]][cr-link] | ||
- ⚡️ Fast (see [benchmarks](#benchmarks)) | ||
- 🌀 Zero dependency (compare with 11 deps for [gh-badges][gh-badges-link] which being used on [shields.io][shields-io]) | ||
Fast handcraft svg badge generator. | ||
- 🌀 1 dependency ([unicode-astral-regex][uar-link]) | ||
- ⚡️ Fast by design (see [benchmarks](#benchmarks)) | ||
- 👯 Pure JavaScript, running in node & browser | ||
## Usage | ||
@@ -60,3 +65,4 @@ | ||
[pp-link]: https://packagephobia.now.sh/result?p=badgen | ||
[shields-io]: https://shields.io | ||
[gh-badges-link]: https://www.npmjs.com/package/gh-badges | ||
[cr-badge]: https://coveralls.io/repos/github/amio/badgen/badge.svg?branch=master | ||
[cr-link]: https://coveralls.io/github/amio/badgen?branch=master | ||
[uar-link]: https://www.npmjs.com/package/unicode-astral-regex |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
6756
11.76%87
7.41%68
9.68%1
Infinity%6
-14.29%+ Added
+ Added