From 49b999f5daf118db6d691d5d82e45af7d0b11b91 Mon Sep 17 00:00:00 2001 From: Amio Date: Sat, 30 Apr 2022 16:10:24 +0800 Subject: [PATCH] chore: update tsconfig --- .github/workflows/nodejs.yml | 2 +- src/calc-text-width.ts | 4 ++-- src/color-presets.ts | 2 +- src/index.ts | 11 ++++++----- tsconfig.json | 5 ++++- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 73d550c..8d0570f 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -7,7 +7,7 @@ jobs: strategy: matrix: - node: [8, 10, 12] + node: [8, 10, 12, 14, 16, 18] os: [ubuntu-latest] runs-on: ${{ matrix.os }} diff --git a/src/calc-text-width.ts b/src/calc-text-width.ts index 6174e94..290fab0 100644 --- a/src/calc-text-width.ts +++ b/src/calc-text-width.ts @@ -1,8 +1,8 @@ // import widthsVerdana110 from './widths-verdana-110.json' // @ts-ignore -const widthsVerdana110 = require('./widths-verdana-110.json') +const widthsVerdana110: number[] = require('./widths-verdana-110.json') -const calcWidth = (charWidthTable) => { +const calcWidth = (charWidthTable: number[]) => { const fallbackWidth = charWidthTable[64] // Width as "@" for overflows return ([...text]) => { diff --git a/src/color-presets.ts b/src/color-presets.ts index 64826f0..4678124 100644 --- a/src/color-presets.ts +++ b/src/color-presets.ts @@ -10,4 +10,4 @@ export default { gray: '999', cyan: '1BC', black: '2A2A2A' -} +} as Record diff --git a/src/index.ts b/src/index.ts index 5f17901..edd2eb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,11 +3,12 @@ import { Verdana110 as calcWidth } from './calc-text-width' import colorPresets from './color-presets' type StyleOption = 'flat' | 'classic' +type ColorPreset = keyof typeof colorPresets interface BadgenOptions { status: string; subject?: string; - color?: string; + color?: ColorPreset; label?: string; labelColor?: string style?: StyleOption; @@ -38,16 +39,16 @@ export function badgen ({ labelColor = colorPresets[labelColor] || labelColor iconWidth = iconWidth * 10 - const iconSpanWidth = icon ? (label.length ? iconWidth + 30 : iconWidth - 18) : 0 + const iconSpanWidth = icon ? (label?.length ? iconWidth + 30 : iconWidth - 18) : 0 const sbTextStart = icon ? (iconSpanWidth + 50) : 50 - const sbTextWidth = calcWidth(label) + const sbTextWidth = label ? calcWidth(label) : 0 const stTextWidth = calcWidth(status) const sbRectWidth = sbTextWidth + 100 + iconSpanWidth const stRectWidth = stTextWidth + 100 const width = sbRectWidth + stRectWidth const xlink = icon ? ' xmlns:xlink="http://www.w3.org/1999/xlink"' : '' - label = sanitize(label) + label = label ? sanitize(label) : undefined status = sanitize(status) color = sanitize(color) labelColor = sanitize(labelColor) @@ -93,7 +94,7 @@ export function badgen ({ ` } -function bare ({ status, color, style, scale }) { +function bare ({ status, color = 'blue', style, scale = 1 }: BadgenOptions) { typeAssert(typeof status === 'string', ' must be string') color = colorPresets[color] || color || colorPresets.blue diff --git a/tsconfig.json b/tsconfig.json index d8f8441..1b76b5b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,7 @@ { "compilerOptions": { + "strict": true, + "target": "es2017", "module": "commonjs", "lib": ["esnext", "dom"], @@ -12,7 +14,8 @@ "resolveJsonModule": true, "experimentalDecorators": true, - "allowSyntheticDefaultImports": true + "allowSyntheticDefaultImports": true, + "forceConsistentCasingInFileNames": true, }, "include": ["src"] }