figures
Advanced tools
Comparing version 6.0.1 to 6.1.0
@@ -249,4 +249,15 @@ declare const figureSet: { | ||
export type Options = { | ||
/** | ||
Whether to replace symbols with fallbacks. | ||
This can be set to `true` to always use fallback symbols, whether the terminal has poor Unicode support or not. | ||
@default `true` if the terminal has poor Unicode support | ||
*/ | ||
readonly useFallback?: boolean; | ||
}; | ||
/** | ||
Replace Unicode symbols depending on the terminal. | ||
Returns the input with replaced fallback symbols if the terminal has poor Unicode support. | ||
@@ -260,11 +271,11 @@ @param string - String where the Unicode symbols will be replaced with fallback symbols depending on the terminal. | ||
console.log(replaceSymbols('✔︎ check')); | ||
// On terminals with Unicode symbols: ✔︎ check | ||
console.log(replaceSymbols('✔ check')); | ||
// On terminals with Unicode symbols: ✔ check | ||
// On other terminals: √ check | ||
console.log(figures.tick); | ||
// On terminals with Unicode symbols: ✔︎ | ||
// On other terminals: √ | ||
console.log(replaceSymbols('✔ check', {useFallback: true})); | ||
// On terminals with Unicode symbols: √ check | ||
// On other terminals: √ check | ||
``` | ||
*/ | ||
export function replaceSymbols(string: string): string; | ||
export function replaceSymbols(string: string, options?: Options): string; |
24
index.js
@@ -200,4 +200,3 @@ import isUnicodeSupported from 'is-unicode-supported'; | ||
export const mainSymbols = { | ||
...common, | ||
const specialMainSymbols = { | ||
tick: '✔', | ||
@@ -239,4 +238,3 @@ info: 'ℹ', | ||
export const fallbackSymbols = { | ||
...common, | ||
const specialFallbackSymbols = { | ||
tick: '√', | ||
@@ -278,2 +276,5 @@ info: 'i', | ||
export const mainSymbols = {...common, ...specialMainSymbols}; | ||
export const fallbackSymbols = {...common, ...specialFallbackSymbols}; | ||
const shouldUseMain = isUnicodeSupported(); | ||
@@ -283,16 +284,13 @@ const figures = shouldUseMain ? mainSymbols : fallbackSymbols; | ||
const replacements = Object.entries(mainSymbols) | ||
.filter(([key, mainSymbol]) => fallbackSymbols[key] !== mainSymbol); | ||
const replacements = Object.entries(specialMainSymbols); | ||
// On terminals which do not support Unicode symbols, substitute them to other symbols | ||
export const replaceSymbols = string => { | ||
if (shouldUseMain) { | ||
return string; | ||
export const replaceSymbols = (string, {useFallback = !shouldUseMain} = {}) => { | ||
if (useFallback) { | ||
for (const [key, mainSymbol] of replacements) { | ||
string = string.replaceAll(mainSymbol, fallbackSymbols[key]); | ||
} | ||
} | ||
for (const [key, mainSymbol] of replacements) { | ||
string = string.replaceAll(mainSymbol, fallbackSymbols[key]); | ||
} | ||
return string; | ||
}; |
{ | ||
"name": "figures", | ||
"version": "6.0.1", | ||
"version": "6.1.0", | ||
"description": "Unicode symbols with fallbacks for older terminals", | ||
@@ -18,2 +18,3 @@ "license": "MIT", | ||
}, | ||
"sideEffects": false, | ||
"engines": { | ||
@@ -20,0 +21,0 @@ "node": ">=18" |
@@ -20,10 +20,10 @@ # figures | ||
```js | ||
import figures, {replaceSymbols, mainSymbols, fallbackSymbols} from 'figures'; | ||
import figures, {mainSymbols, fallbackSymbols, replaceSymbols} from 'figures'; | ||
console.log(figures.tick); | ||
// On terminals with Unicode symbols: ✔︎ | ||
// On terminals with Unicode symbols: ✔ | ||
// On other terminals: √ | ||
console.log(mainSymbols.tick); | ||
// On all terminals: ✔︎ | ||
// On all terminals: ✔ | ||
@@ -33,4 +33,4 @@ console.log(fallbackSymbols.tick); | ||
console.log(replaceSymbols('✔︎ check')); | ||
// On terminals with Unicode symbols: ✔︎ check | ||
console.log(replaceSymbols('✔ check')); | ||
// On terminals with Unicode symbols: ✔ check | ||
// On other terminals: √ check | ||
@@ -55,5 +55,5 @@ ``` | ||
### replaceSymbols(string) | ||
### replaceSymbols(string, options?) | ||
Returns the input with replaced fallback Unicode symbols on older terminals. | ||
Returns the input with replaced fallback symbols if the terminal has poor Unicode support. | ||
@@ -68,2 +68,23 @@ All the below [figures](#figures) are attached to the default export as shown in the example above. | ||
#### options | ||
Type: `object` | ||
##### useFallback | ||
Type: `boolean`\ | ||
Default: `true` if the terminal has poor Unicode support | ||
Whether to replace symbols with fallbacks. | ||
This can be set to `true` to always use fallback symbols, whether the terminal has poor Unicode support or not. | ||
```js | ||
import {replaceSymbols} from 'figures'; | ||
console.log(replaceSymbols('✔ check', {useFallback: true})); | ||
// On terminals with Unicode symbols: √ check | ||
// On other terminals: √ check | ||
``` | ||
## Figures | ||
@@ -70,0 +91,0 @@ |
36841
551
338