Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fc8744

Browse files
authored
Give the NPM package some love (badges#2200)
* define a public interface for NPM package * move check-node-version to dependencies * add missing file to package * update docs * bump version * add gh-badges option to issue template * abstract text measuring from users * add a DocBlock for BadgeFactory.create()
1 parent d7a52f3 commit 6fc8744

6 files changed

Lines changed: 79 additions & 31 deletions

File tree

.github/ISSUE_TEMPLATE/1_Bug_report.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ about: Report errors and problems
44

55
---
66

7-
Are you experiencing this issue on shields.io or your own instance?
8-
- [ ] shields.io
9-
- [ ] My instance
7+
Are you experiencing an issue with...
8+
- [ ] [shields.io](https://shields.io/#/)
9+
- [ ] My own instance
10+
- [ ] [gh-badges NPM package](https://www.npmjs.com/package/gh-badges)
1011

1112
:beetle: **Description**
1213
<!-- A clear and concise description of the problem. -->

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,24 @@ badge build passed :green .png > mybadge.png
8888
```
8989

9090
```js
91-
const badge = require('gh-badges')
91+
const { BadgeFactory } = require('gh-badges')
92+
93+
const bf = new BadgeFactory({ fontPath: '/path/to/Verdana.ttf' })
9294

93-
// Optional step, to have accurate text width computation.
9495
const format = {
9596
text: ['build', 'passed'],
9697
colorscheme: 'green',
9798
template: 'flat',
9899
}
99100

100-
badge.loadFont('/path/to/Verdana.ttf', err => {
101-
badge(format, (svg, err) => {
102-
// svg is a string containing your badge
103-
})})
101+
const svg = bf.create(format)
104102
```
105103

106104
View the [documentation for gh-badges][gh-badges doc].
107105

108-
**Note:** The badge library was last released in 2016.
109-
110106
[![npm version](http://img.shields.io/npm/v/gh-badges.svg)](https://npmjs.org/package/gh-badges)
111107

112-
[gh-badges doc]: doc/gh-badges.md
108+
[gh-badges doc]: https://github.com/badges/shields/blob/master/doc/gh-badges.md
113109

114110

115111
Development

doc/gh-badges.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,30 @@ same dark grey, so you can rely on that default value by not providing a
4949
You can also use the `"colorA"` and `"colorB"` fields directly in the badges if
5050
you don't want to make a color scheme for it. In that case, remove the
5151
`"colorscheme"` field altogether.
52+
53+
Text Width Computation
54+
----------------------
55+
56+
`BadgeFactory`'s constructor takes an optional boolean
57+
`precomputeWidths` parameter which defaults to `false`.
58+
59+
Pre-computing the font width table adds some overhead to constructing the
60+
`BadgeFactory` object (so will slow down generation of a single image),
61+
but will speed up each badge generation if you are creating a lot of images.
62+
As a rule of thumb:
63+
64+
If you are generating just one image, use:
65+
66+
```js
67+
const bf = new BadgeFactory(
68+
{ fontPath: '/path/to/Verdana.ttf' }
69+
)
70+
```
71+
72+
If you are generating many images with a single instance of `BadgeFactory`:
73+
74+
```js
75+
const bf = new BadgeFactory(
76+
{ fontPath: '/path/to/Verdana.ttf', precomputeWidths: true }
77+
)
78+
```

lib/gh-badges.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const { makeBadge } = require('./make-badge')
4+
const { PDFKitTextMeasurer, QuickTextMeasurer } = require('./text-measurer')
5+
6+
class BadgeFactory {
7+
constructor({ fontPath, fallbackFontPath, precomputeWidths = false }) {
8+
this.measurer = precomputeWidths
9+
? new QuickTextMeasurer(fontPath, fallbackFontPath)
10+
: new PDFKitTextMeasurer(fontPath, fallbackFontPath)
11+
}
12+
13+
/**
14+
* Create a badge
15+
*
16+
* @param {object} format - Object specifying badge data
17+
* @param {string[]} format.text
18+
* @param {string} format.colorscheme
19+
* @param {string} format.colorA
20+
* @param {string} format.colorB
21+
* @param {string} format.format
22+
* @param {string} format.template
23+
* @return {string} Badge in SVG or JSON format
24+
* @see https://github.com/badges/shields/blob/master/doc/gh-badges.md
25+
*/
26+
create(format) {
27+
return makeBadge(this.measurer, format)
28+
}
29+
}
30+
31+
module.exports = {
32+
BadgeFactory,
33+
}

package-lock.json

Lines changed: 6 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gh-badges",
3-
"version": "1.3.0",
3+
"version": "2.0.0-beta1",
44
"description": "Official Shields.io badge library.",
55
"keywords": [
66
"GitHub",
@@ -16,14 +16,15 @@
1616
},
1717
"license": "CC0-1.0",
1818
"author": "Thaddée Tyl <[email protected]>",
19-
"main": "lib/make-badge.js",
19+
"main": "lib/gh-badges.js",
2020
"repository": {
2121
"type": "git",
2222
"url": "https://github.com/badges/shields"
2323
},
2424
"dependencies": {
2525
"camp": "~17.2.1",
2626
"chalk": "^2.4.1",
27+
"check-node-version": "^3.1.0",
2728
"chrome-web-store-item-property": "~1.1.2",
2829
"dot": "~1.1.2",
2930
"emojic": "^1.1.14",
@@ -113,6 +114,7 @@
113114
"lib/lru-cache.js",
114115
"lib/text-measurer.js",
115116
"lib/svg-to-img.js",
117+
"lib/defaults.js",
116118
"templates",
117119
"logo"
118120
],
@@ -127,7 +129,6 @@
127129
"chai-as-promised": "^7.1.1",
128130
"chai-string": "^1.4.0",
129131
"chainsmoker": "^0.1.0",
130-
"check-node-version": "^3.1.0",
131132
"child-process-promise": "^2.2.1",
132133
"classnames": "^2.2.5",
133134
"concurrently": "^4.0.1",

0 commit comments

Comments
 (0)