Comparing version 0.1.0 to 0.2.0
@@ -8,8 +8,9 @@ // Generate on https://codesandbox.io/s/lr4ynm652m | ||
function calcWidth (charWidthTable) { | ||
const widthTable = charWidthTable.map(w => w + 0.4) // Add letter-spacing | ||
return function (text) { | ||
if (typeof text !== 'string') { | ||
throw new TypeError('<text> should be string.') | ||
return 0 | ||
} else { | ||
return Array.from(text).reduce((total, curr) => { | ||
return total + charWidthTable[curr.charCodeAt()] | ||
return total + widthTable[curr.charCodeAt()] || widthTable[64] // "@" | ||
}, 0) | ||
@@ -16,0 +17,0 @@ } |
const calcWidth = require('./calc-text-width.js').Arial12 | ||
const colorPresets = require('./color-presets.js') | ||
module.exports = function ({subject, status, color = '4C1'}) { | ||
// const charWidth = 7.3 | ||
const calcFactor = 0.4 | ||
module.exports = function ({subject, status, color}) { | ||
color = colorPresets[color] || color || colorPresets['green'] | ||
const sbTextWidth = calcWidth(subject) + subject.length * calcFactor | ||
// const sbTextWidth = subject.length * charWidth | ||
const sbTextWidth = calcWidth(subject) | ||
const sbRectWidth = sbTextWidth + 10 | ||
const sbTextCenter = sbRectWidth / 2 | ||
const stTextWidth = calcWidth(status) + status.length * calcFactor | ||
// const stTextWidth = status.length * charWidth | ||
const stTextWidth = calcWidth(status) | ||
const stRectWidth = stTextWidth + 12 | ||
@@ -31,5 +29,5 @@ const stTextCenter = sbRectWidth + stRectWidth / 2 - 1 | ||
<g fill="#fff" text-anchor="middle" font-family="Arial,sans-serif" font-size="12"> | ||
<text x="${sbTextCenter + 1}" y="15" textLength="${sbTextWidth}" fill="#0004">${subject}</text> | ||
<text x="${sbTextCenter + 1}" y="15" textLength="${sbTextWidth}" fill="#000" opacity="0.2">${subject}</text> | ||
<text x="${sbTextCenter}" y="14" textLength="${sbTextWidth}">${subject}</text> | ||
<text x="${stTextCenter + 1}" y="15" textLength="${stTextWidth}" fill="#0004">${status}</text> | ||
<text x="${stTextCenter + 1}" y="15" textLength="${stTextWidth}" fill="#000" opacity="0.2">${status}</text> | ||
<text x="${stTextCenter}" y="14" textLength="${stTextWidth}">${status}</text> | ||
@@ -36,0 +34,0 @@ </g> <script xmlns=""/> |
{ | ||
"name": "badgen", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Hand-crafted badge generator.", | ||
@@ -17,2 +17,3 @@ "author": "Amio <amio.cn@gmail.com>", | ||
"find-my-way": "^1.13.0", | ||
"lru-cache": "^4.1.3", | ||
"standard": "^11.0.1", | ||
@@ -19,0 +20,0 @@ "tap": "^12.0.1" |
@@ -1,2 +0,2 @@ | ||
# badgen | ||
# badgen [![npm-version][npm-badge]][npm-link] [![install size][pp-badge]][pp-link] | ||
@@ -7,15 +7,53 @@ Fast, handcraft, pure JavaScript badge generator. | ||
### Micro Service | ||
### Badge Service | ||
https://badgen.now.sh/badge/:subject/:status/:color | ||
`https://badgen.now.sh/badge/:subject/:status/:color` | ||
- `subject` Text | ||
- `status` Text | ||
- `color` RGB color (default '4C1') | ||
- `color` Color RGB (default '3C1') or Color Preset (`green`, `yellow`, ...see below) | ||
![](https://badgen.now.sh/badge/build/passing) | ||
![](https://badgen.now.sh/badge/style/standard/f2a) | ||
Color Presets: | ||
### As npm package | ||
![](https://badgen.now.sh/badge/color/green/green) | ||
![](https://badgen.now.sh/badge/color/yellow/yellow) | ||
![](https://badgen.now.sh/badge/color/orange/orange) | ||
![](https://badgen.now.sh/badge/color/red/red) | ||
![](https://badgen.now.sh/badge/color/pink/pink) | ||
![](https://badgen.now.sh/badge/color/purple/purple) | ||
![](https://badgen.now.sh/badge/color/blue/blue) | ||
![](https://badgen.now.sh/badge/color/grey/grey) | ||
![](https://badgen.now.sh/badge/color/black/black) | ||
```npm install badgen``` | ||
Examples: | ||
| Preview | URL | | ||
| --- | --- | | ||
|![](https://badgen.now.sh/badge/build/passing) | https://badgen.now.sh/badge/build/passing | | ||
|![](https://badgen.now.sh/badge/style/standard/f2a) | https://badgen.now.sh/badge/style/standard/f2a | | ||
|![](https://badgen.now.sh/badge/license/Apache-2.0/blue) | https://badgen.now.sh/badge/license/Apache-2.0/blue | | ||
|![](https://badgen.now.sh/list/platform/ios,macos,tvos/grey) | https://badgen.now.sh/list/platform/ios,macos,tvos/grey | | ||
### Programmatically | ||
`npm install badgen` | ||
```javascript | ||
const badgen = require('badgen') | ||
const svgString = badgen({ | ||
subject: 'npm', | ||
status: 'v1.2.3', | ||
color: 'blue' | ||
}) | ||
``` | ||
## License | ||
ISC @ Amio | ||
[npm-badge]: https://img.shields.io/npm/v/badgen.svg | ||
[npm-link]: https://www.npmjs.com/package/badgen | ||
[pp-badge]: https://packagephobia.now.sh/badge?p=badgen | ||
[pp-link]: https://packagephobia.now.sh/result?p=badgen |
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
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
7991
5
76
59
4