From 3eb12297c82050fdc68e58ec8ac7a38b3a9b2a98 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 27 Jun 2021 15:58:22 +0200 Subject: [PATCH] Use ES modules --- index.d.ts | 60 +++++++++++++++++++++++++------------------------ index.js | 30 ++++++++++++------------- index.test-d.ts | 3 +-- package.json | 8 ++++--- readme.md | 8 +++---- test.js | 3 ++- 6 files changed, 57 insertions(+), 55 deletions(-) diff --git a/index.d.ts b/index.d.ts index d9a285d..9470cba 100644 --- a/index.d.ts +++ b/index.d.ts @@ -227,41 +227,43 @@ declare const figureSet: { readonly lineCross: string; readonly lineBackslash: string; readonly lineSlash: string; -} +}; -type FigureSet = typeof figureSet +type FigureSet = typeof figureSet; -declare const figures: { - /** - Replace Unicode symbols depending on the OS. +/** +Symbols to use when not running on Windows. +*/ +export const mainSymbols: FigureSet; - @param string - String where the Unicode symbols will be replaced with fallback symbols depending on the OS. - @returns The input with replaced fallback Unicode symbols on Windows. +/** +Symbols to use when running on Windows. +*/ +export const windowsSymbols: FigureSet; - @example - ``` - import figures = require('figures'); +/** +Symbols to use on any OS. +*/ +export default figureSet; - console.log(figures('✔︎ check')); - // On non-Windows OSes: ✔︎ check - // On Windows: √ check +/** +Replace Unicode symbols depending on the OS. - console.log(figures.tick); - // On non-Windows OSes: ✔︎ - // On Windows: √ - ``` - */ - replaceSymbols(string: string): string; +@param string - String where the Unicode symbols will be replaced with fallback symbols depending on the OS. +@returns The input with replaced fallback Unicode symbols on Windows. - /** - Symbols to use when not running on Windows. - */ - readonly mainSymbols: FigureSet; +@example +``` +import figures, {replaceSymbols} from 'figures'; - /** - Symbols to use when running on Windows. - */ - readonly windowsSymbols: FigureSet; -} & FigureSet; +console.log(replaceSymbols('✔︎ check')); +// On non-Windows OSes: ✔︎ check +// On Windows: √ check + +console.log(figures.tick); +// On non-Windows OSes: ✔︎ +// On Windows: √ +``` +*/ +export function replaceSymbols(string: string): string; -export = figures; diff --git a/index.js b/index.js index 0c86764..e00252b 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,4 @@ -'use strict'; -const escapeStringRegexp = require('escape-string-regexp'); +import escapeStringRegexp from 'escape-string-regexp'; const {platform} = process; @@ -198,8 +197,17 @@ const common = { lineSlash: '╱' }; -const mainSymbols = { +// The main symbols for those do not look that good on Ubuntu. +const linuxMainSymbols = platform === 'linux' ? + {} : + { + circleQuestionMark: '?', + questionMarkPrefix: '?' + }; + +export const mainSymbols = { ...common, + ...linuxMainSymbols, tick: '✔', info: 'ℹ', warning: '⚠', @@ -238,7 +246,7 @@ const mainSymbols = { oneTenth: '⅒' }; -const windowsSymbols = { +export const windowsSymbols = { ...common, tick: '√', info: 'i', @@ -278,15 +286,10 @@ const windowsSymbols = { oneTenth: '1/10' }; -if (platform === 'linux') { - // The main symbols for those do not look that good on Ubuntu. - mainSymbols.circleQuestionMark = '?'; - mainSymbols.questionMarkPrefix = '?'; -} - // TODO: Use https://github.com/sindresorhus/is-unicode-supported when targeting Node.js 10. const shouldUseWindows = platform === 'win32'; const figures = shouldUseWindows ? windowsSymbols : mainSymbols; +export default figures; const isWindowsSymbol = ([key, mainSymbol]) => figures[key] !== mainSymbol; const getFigureRegExp = ([key, mainSymbol]) => [new RegExp(escapeStringRegexp(mainSymbol), 'g'), windowsSymbols[key]]; @@ -303,10 +306,8 @@ const getReplacements = () => { return replacements; }; -module.exports = figures; - // On Windows, substitute non-Windows to Windows figures -module.exports.replaceSymbols = string => { +export const replaceSymbols = string => { if (!shouldUseWindows) { return string; } @@ -317,6 +318,3 @@ module.exports.replaceSymbols = string => { return string; }; - -module.exports.mainSymbols = mainSymbols; -module.exports.windowsSymbols = windowsSymbols; diff --git a/index.test-d.ts b/index.test-d.ts index f7aff71..584c37f 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,5 @@ import {expectType} from 'tsd'; -import figures = require('.'); -import {replaceSymbols, mainSymbols, windowsSymbols} from '.'; +import figures, {replaceSymbols, mainSymbols, windowsSymbols} from './index.js'; expectType(replaceSymbols('✔︎ check')); expectType(figures.tick); diff --git a/package.json b/package.json index ee3b3df..b86e35a 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,8 @@ "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { "node": ">=8" }, @@ -34,12 +36,12 @@ "fallback" ], "dependencies": { - "escape-string-regexp": "^1.0.5" + "escape-string-regexp": "^5.0.0" }, "devDependencies": { - "ava": "^1.4.1", + "ava": "^3.15.0", "markdown-table": "^1.1.2", - "tsd": "^0.7.2", + "tsd": "^0.17.0", "xo": "^0.24.0" } } diff --git a/readme.md b/readme.md index 7e6802c..2998e83 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ $ npm install figures See the [source](index.js) for supported symbols. ```js -const figures = require('figures'); +import figures, {replaceSymbols, mainSymbols, windowsSymbols} from 'figures'; console.log(figures.replaceSymbols('✔︎ check')); // On non-Windows OSes: ✔︎ check @@ -38,7 +38,7 @@ console.log(figures.windowsSymbols.tick); ## API -### figures.replaceSymbols(string) +### replaceSymbols(string) Returns the input with replaced fallback Unicode symbols on Windows. @@ -50,11 +50,11 @@ Type: `string` String where the Unicode symbols will be replaced with fallback symbols depending on the OS. -### figures.mainSymbols +### mainSymbols Symbols to use when not running on Windows. -### figures.windowsSymbols +### windowsSymbols Symbols to use when running on Windows. diff --git a/test.js b/test.js index da13357..3f83722 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,6 @@ import test from 'ava'; -import figures, {replaceSymbols, mainSymbols, windowsSymbols} from '.'; +// eslint-disable-next-line unicorn/import-index, import/extensions +import figures, {replaceSymbols, mainSymbols, windowsSymbols} from './index.js'; const result = (mainSymbols, windowsSymbols) => process.platform === 'win32' ? windowsSymbols : mainSymbols;