From 0a2dd9d00ee1cd847aac4bf93ac23846f4820c38 Mon Sep 17 00:00:00 2001 From: amio Date: Thu, 3 Oct 2019 12:08:16 +0800 Subject: [PATCH] refactor: js => ts --- bench/index.js | 2 +- package-lock.json | 15 ++-- package.json | 6 +- src/bare.js | 41 ---------- ...{calc-text-width.js => calc-text-width.ts} | 6 +- src/{color-presets.js => color-presets.ts} | 2 +- src/{index.js => index.ts} | 79 +++++++++++++++++-- src/utils.js | 11 --- tsconfig.json | 8 +- 9 files changed, 98 insertions(+), 72 deletions(-) delete mode 100644 src/bare.js rename src/{calc-text-width.js => calc-text-width.ts} (77%) rename src/{color-presets.js => color-presets.ts} (90%) rename src/{index.js => index.ts} (56%) delete mode 100644 src/utils.js diff --git a/bench/index.js b/bench/index.js index abe645c..28fd47b 100644 --- a/bench/index.js +++ b/bench/index.js @@ -1,5 +1,5 @@ const { Suite } = require('benchmark') -const badgen = require('..') +const { badgen } = require('..') const icon = require('../test/assets/icon-data-uri.js') /* eslint max-len: ["error", { "code": 90 }] */ diff --git a/package-lock.json b/package-lock.json index f449b9a..545abb5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2023,7 +2023,7 @@ }, "mkdirp": { "version": "0.5.1", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "resolved": "http://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", "dev": true, "requires": { @@ -4188,6 +4188,12 @@ "bundled": true, "dev": true }, + "typescript": { + "version": "3.6.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.3.tgz", + "integrity": "sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==", + "dev": true + }, "widest-line": { "version": "2.0.1", "bundled": true, @@ -4511,10 +4517,9 @@ } }, "typescript": { - "version": "3.6.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.3.tgz", - "integrity": "sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==", - "dev": true + "version": "3.7.0-beta", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.0-beta.tgz", + "integrity": "sha512-4jyCX+IQamrPJxgkABPq9xf+hUN+GWHVxoj+oey1TadCPa4snQl1RKwUba+1dyzYCamwlCxKvZQ3TjyWLhMGBA==" }, "uglify-js": { "version": "3.6.0", diff --git a/package.json b/package.json index 01806ea..b951790 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "snaptests": "TAP_SNAPSHOT=1 npm test", "pretest": "npm run lint && npm run build", "test": "tap test/*.spec.js --reporter spec --coverage", - "build": "ncc -s -m --no-source-map-register build src/index.js && cp src/*.d.ts dist", + "prebuild": "rm -rf dist", + "build": "ncc -s -m --no-source-map-register build src/index.ts", "prepublishOnly": "npm run build" }, "devDependencies": { @@ -23,5 +24,8 @@ "serve-marked": "^2.0.2", "standard": "^14.3.1", "tap": "^14.6.9" + }, + "dependencies": { + "typescript": "^3.7.0-beta" } } diff --git a/src/bare.js b/src/bare.js deleted file mode 100644 index 9015af3..0000000 --- a/src/bare.js +++ /dev/null @@ -1,41 +0,0 @@ -const calcWidth = require('./calc-text-width.js').Verdana110 -const colorPresets = require('./color-presets.js') -const { sanitize, typeAssert } = require('./utils.js') - -module.exports = ({ status, color, style }) => { - typeAssert(typeof status === 'string', ' must be string') - color = colorPresets[color] || color || colorPresets.blue - - const stTextWidth = calcWidth(status) - const stRectWidth = stTextWidth + 115 - - status = sanitize(status) - - if (style === 'flat') { - return ` - - - - - ${status} - ${status} - -` - } - - return ` - - - - - - - - - - - ${status} - ${status} - -` -} diff --git a/src/calc-text-width.js b/src/calc-text-width.ts similarity index 77% rename from src/calc-text-width.js rename to src/calc-text-width.ts index 88c1a89..6174e94 100644 --- a/src/calc-text-width.js +++ b/src/calc-text-width.ts @@ -1,3 +1,5 @@ +// import widthsVerdana110 from './widths-verdana-110.json' +// @ts-ignore const widthsVerdana110 = require('./widths-verdana-110.json') const calcWidth = (charWidthTable) => { @@ -15,6 +17,4 @@ const calcWidth = (charWidthTable) => { } } -module.exports = { - Verdana110: calcWidth(widthsVerdana110) -} +export const Verdana110 = calcWidth(widthsVerdana110) diff --git a/src/color-presets.js b/src/color-presets.ts similarity index 90% rename from src/color-presets.js rename to src/color-presets.ts index 47ac40d..64826f0 100644 --- a/src/color-presets.js +++ b/src/color-presets.ts @@ -1,4 +1,4 @@ -module.exports = { +export default { green: '3C1', blue: '08C', red: 'E43', diff --git a/src/index.js b/src/index.ts similarity index 56% rename from src/index.js rename to src/index.ts index 33c0762..be28e28 100644 --- a/src/index.js +++ b/src/index.ts @@ -1,10 +1,21 @@ -const calcWidth = require('./calc-text-width.js').Verdana110 -const colorPresets = require('./color-presets.js') -const { sanitize, typeAssert } = require('./utils.js') +import { Verdana110 as calcWidth } from './calc-text-width' +import colorPresets from './color-presets' -const bare = require('./bare.js') +type StyleOption = 'flat' | 'classic' -module.exports = ({ +interface BadgenOptions { + status: string; + subject?: string; + color?: string; + label?: string; + labelColor?: string + style?: StyleOption; + icon?: string; + iconWidth?: number; + scale?: number +} + +export function badgen ({ label, subject, status, @@ -14,7 +25,7 @@ module.exports = ({ iconWidth = 13, labelColor = '555', scale = 1 -}) => { +}: BadgenOptions) { typeAssert(typeof status === 'string', ' must be string') label = label === undefined ? subject : label // subject is deprecated @@ -75,4 +86,58 @@ module.exports = ({ ` } -module.exports.default = module.exports +function bare ({ status, color, style }) { + typeAssert(typeof status === 'string', ' must be string') + color = colorPresets[color] || color || colorPresets.blue + + const stTextWidth = calcWidth(status) + const stRectWidth = stTextWidth + 115 + + status = sanitize(status) + + if (style === 'flat') { + return ` + + + + + ${status} + ${status} + +` + } + + return ` + + + + + + + + + + + ${status} + ${status} + +` +} + +function sanitize (str: string): string { + return str.replace(/\u0026/g, '&').replace(/\u003C/g, '<') +} + +function typeAssert (assertion: boolean, message: string): void { + if (!assertion) throw new TypeError(message) +} + +declare global { + interface Window { + badgen: typeof badgen; + } +} + +if (typeof window === 'object') { + window.badgen = badgen +} diff --git a/src/utils.js b/src/utils.js deleted file mode 100644 index 0eb6af5..0000000 --- a/src/utils.js +++ /dev/null @@ -1,11 +0,0 @@ - -const sanitize = str => str.replace(/\u0026/g, '&').replace(/\u003C/g, '<') - -const typeAssert = (assertion, message) => { - if (!assertion) throw new TypeError(message) -} - -module.exports = { - sanitize, - typeAssert -} diff --git a/tsconfig.json b/tsconfig.json index ec19181..104e91e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,16 +1,20 @@ { "compilerOptions": { "target": "es2017", - "module": "esnext", + "module": "commonjs", "lib": ["esnext", "dom"], + "rootDir": "src", "outDir": "dist", + "declaration": true, "sourceMap":true, "allowJs":true, "moduleResolution":"node", + "resolveJsonModule": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true - } + }, + "include": ["src"] }