Skip to content

Commit 9a5d9c1

Browse files
committed
convert badge-maker to ESM
1 parent 21411e1 commit 9a5d9c1

File tree

15 files changed

+31
-70
lines changed

15 files changed

+31
-70
lines changed
File renamed without changes.

badge-maker/README.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ badge build passed :brightgreen > mybadge.svg
2121

2222
### As a library
2323

24-
With CommonJS in JavaScript,
25-
26-
```js
27-
const { makeBadge, ValidationError } = require('badge-maker')
28-
```
29-
30-
With ESM or TypeScript,
31-
3224
```ts
3325
import { makeBadge, ValidationError } from 'badge-maker'
3426
```

badge-maker/lib/badge-cli.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#!/usr/bin/env node
22

3-
'use strict'
4-
5-
const { namedColors } = require('./color')
6-
const { makeBadge } = require('./index')
3+
import { namedColors } from './color.js'
4+
import { makeBadge } from './index.js'
75

86
if (process.argv.length < 4) {
97
console.log('Usage: badge label message [:color] [@style]')
File renamed without changes.

badge-maker/lib/badge-renderers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const anafanafo = require('anafanafo')
4-
const { brightness } = require('./color')
5-
const { XmlElement, ElementList } = require('./xml')
3+
import anafanafo from 'anafanafo'
4+
import { brightness } from './color.js'
5+
import { XmlElement, ElementList } from './xml.js'
66

77
// https://github.com/badges/shields/pull/1132
88
const FONT_SCALE_UP_FACTOR = 10
@@ -984,7 +984,7 @@ function forTheBadge({
984984
)
985985
}
986986

987-
module.exports = {
987+
export default {
988988
plastic: params => Plastic.render(params),
989989
flat: params => Flat.render(params),
990990
'flat-square': params => FlatSquare.render(params),

badge-maker/lib/color.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3-
const { fromString } = require('css-color-converter')
3+
import { fromString } from 'css-color-converter'
44

55
// When updating these, be sure also to update the list in `badge-maker/README.md`.
6-
const namedColors = {
6+
export const namedColors = {
77
brightgreen: '#4c1',
88
green: '#97ca00',
99
yellow: '#dfb317',
@@ -33,15 +33,15 @@ Object.entries(aliases).forEach(([alias, original]) => {
3333
// This function returns false for `#ccc`. However `isCSSColor('#ccc')` is
3434
// true.
3535
const hexColorRegex = /^([\da-f]{3}){1,2}$/i
36-
function isHexColor(s = '') {
36+
export function isHexColor(s = '') {
3737
return hexColorRegex.test(s)
3838
}
3939

4040
function isCSSColor(color) {
4141
return typeof color === 'string' && fromString(color.trim())
4242
}
4343

44-
function normalizeColor(color) {
44+
export function normalizeColor(color) {
4545
if (color === undefined) {
4646
return undefined
4747
} else if (color in namedColors) {
@@ -57,7 +57,7 @@ function normalizeColor(color) {
5757
}
5858
}
5959

60-
function toSvgColor(color) {
60+
export function toSvgColor(color) {
6161
const normalized = normalizeColor(color)
6262
if (normalized in namedColors) {
6363
return namedColors[normalized]
@@ -68,7 +68,7 @@ function toSvgColor(color) {
6868
}
6969
}
7070

71-
function brightness(color) {
71+
export function brightness(color) {
7272
if (color) {
7373
const cssColor = fromString(color)
7474
if (cssColor) {
@@ -78,11 +78,3 @@ function brightness(color) {
7878
}
7979
return 0
8080
}
81-
82-
module.exports = {
83-
namedColors,
84-
isHexColor,
85-
normalizeColor,
86-
toSvgColor,
87-
brightness,
88-
}

badge-maker/lib/color.spec.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
'use strict'
22

3-
const { test, given, forCases } = require('sazerac')
4-
const {
5-
isHexColor,
6-
normalizeColor,
7-
toSvgColor,
8-
brightness,
9-
} = require('./color')
3+
import { test, given, forCases } from 'sazerac'
4+
import { isHexColor, normalizeColor, toSvgColor, brightness } from './color.js'
105

116
test(isHexColor, () => {
127
forCases([given('f00bae'), given('4c1'), given('ABC123')]).expect(true)

badge-maker/lib/constants.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
const DEFAULT_LOGO_HEIGHT = 14
2-
3-
module.exports = {
4-
DEFAULT_LOGO_HEIGHT,
5-
}
1+
export const DEFAULT_LOGO_HEIGHT = 14

badge-maker/lib/index.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* @module badge-maker
44
*/
55

6-
const _makeBadge = require('./make-badge')
6+
import _makeBadge from './make-badge.js'
77

8-
class ValidationError extends Error {}
8+
export class ValidationError extends Error {}
99

1010
function _validate(format) {
1111
if (format !== Object(format)) {
@@ -105,13 +105,8 @@ function _clean(format) {
105105
* @returns {string} Badge in SVG format
106106
* @see https://github.com/badges/shields/tree/master/badge-maker/README.md
107107
*/
108-
function makeBadge(format) {
108+
export function makeBadge(format) {
109109
_validate(format)
110110
const cleanedFormat = _clean(format)
111111
return _makeBadge(cleanedFormat)
112112
}
113-
114-
module.exports = {
115-
makeBadge,
116-
ValidationError,
117-
}
File renamed without changes.

0 commit comments

Comments
 (0)