Skip to content

Commit 0ab1df3

Browse files
authoredMar 4, 2024··
Add useFallback option to replaceSymbols() (#101)
1 parent d0118bb commit 0ab1df3

File tree

5 files changed

+80
-28
lines changed

5 files changed

+80
-28
lines changed
 

‎index.d.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,19 @@ Symbols to use on any terminal.
246246
*/
247247
export default figureSet;
248248

249+
export type Options = {
250+
/**
251+
Whether to replace symbols with fallbacks.
252+
253+
This can be set to `true` to always use fallback symbols, whether the terminal has poor Unicode support or not.
254+
255+
@default `true` if the terminal has poor Unicode support
256+
*/
257+
readonly useFallback?: boolean;
258+
};
259+
249260
/**
250-
Replace Unicode symbols depending on the terminal.
261+
Returns the input with replaced fallback symbols if the terminal has poor Unicode support.
251262
252263
@param string - String where the Unicode symbols will be replaced with fallback symbols depending on the terminal.
253264
@returns The input with replaced fallback Unicode symbols.
@@ -260,9 +271,9 @@ console.log(replaceSymbols('✔ check'));
260271
// On terminals with Unicode symbols: ✔ check
261272
// On other terminals: √ check
262273
263-
console.log(figures.tick);
264-
// On terminals with Unicode symbols:
265-
// On other terminals: √
274+
console.log(replaceSymbols('✔ check', {useFallback: true}));
275+
// On terminals with Unicode symbols: √ check
276+
// On other terminals: √ check
266277
```
267278
*/
268-
export function replaceSymbols(string: string): string;
279+
export function replaceSymbols(string: string, options?: Options): string;

‎index.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,11 @@ export default figures;
281281
const replacements = Object.entries(specialMainSymbols);
282282

283283
// On terminals which do not support Unicode symbols, substitute them to other symbols
284-
export const replaceSymbols = string => {
285-
if (shouldUseMain) {
286-
return string;
287-
}
288-
289-
for (const [key, mainSymbol] of replacements) {
290-
string = string.replaceAll(mainSymbol, fallbackSymbols[key]);
284+
export const replaceSymbols = (string, {useFallback = !shouldUseMain} = {}) => {
285+
if (useFallback) {
286+
for (const [key, mainSymbol] of replacements) {
287+
string = string.replaceAll(mainSymbol, fallbackSymbols[key]);
288+
}
291289
}
292290

293291
return string;

‎index.test-d.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1-
import {expectType} from 'tsd';
2-
import figures, {replaceSymbols, mainSymbols, fallbackSymbols} from './index.js';
1+
import {expectType, expectError} from 'tsd';
2+
import figures, {mainSymbols, fallbackSymbols, replaceSymbols} from './index.js';
33

4-
expectType<string>(replaceSymbols('✔ check'));
54
expectType<string>(figures.tick);
5+
66
expectType<string>(mainSymbols.tick);
7+
78
expectType<string>(fallbackSymbols.tick);
9+
10+
expectType<string>(replaceSymbols('✔ check'));
11+
expectError(replaceSymbols(true));
12+
expectError(replaceSymbols());
13+
14+
replaceSymbols('', {});
15+
replaceSymbols('', {useFallback: undefined});
16+
replaceSymbols('', {useFallback: true});
17+
expectError(replaceSymbols('', {useFallback: 'other'}));
18+
expectError(replaceSymbols('', {other: true}));
19+
expectError(replaceSymbols('', ''));

‎readme.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ npm install figures
1717
## Usage
1818

1919
```js
20-
import figures, {replaceSymbols, mainSymbols, fallbackSymbols} from 'figures';
20+
import figures, {mainSymbols, fallbackSymbols, replaceSymbols} from 'figures';
2121

2222
console.log(figures.tick);
2323
// On terminals with Unicode symbols: ✔
@@ -50,9 +50,9 @@ Symbols to use when the terminal supports Unicode symbols.
5050

5151
Symbols to use when the terminal does not support Unicode symbols.
5252

53-
### replaceSymbols(string)
53+
### replaceSymbols(string, options?)
5454

55-
Returns the input with replaced fallback Unicode symbols on older terminals.
55+
Returns the input with replaced fallback symbols if the terminal has poor Unicode support.
5656

5757
All the below [figures](#figures) are attached to the default export as shown in the example above.
5858

@@ -62,6 +62,27 @@ Type: `string`
6262

6363
String where the Unicode symbols will be replaced with fallback symbols depending on the terminal.
6464

65+
#### options
66+
67+
Type: `object`
68+
69+
##### useFallback
70+
71+
Type: `boolean`\
72+
Default: `true` if the terminal has poor Unicode support
73+
74+
Whether to replace symbols with fallbacks.
75+
76+
This can be set to `true` to always use fallback symbols, whether the terminal has poor Unicode support or not.
77+
78+
```js
79+
import {replaceSymbols} from 'figures';
80+
81+
console.log(replaceSymbols('✔ check', {useFallback: true}));
82+
// On terminals with Unicode symbols: √ check
83+
// On other terminals: √ check
84+
```
85+
6586
## Figures
6687

6788
`Fallback` characters are only shown when they differ from the `Main` ones.

‎test.js

+20-10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import test from 'ava';
22
import isUnicodeSupported from 'is-unicode-supported';
33
import figures, {replaceSymbols, mainSymbols, fallbackSymbols} from './index.js';
44

5-
const result = (mainSymbols, fallbackSymbols) => isUnicodeSupported() ? mainSymbols : fallbackSymbols;
5+
const getCorrectSymbols = (mainSymbols, fallbackSymbols) => isUnicodeSupported() ? mainSymbols : fallbackSymbols;
6+
const getMainSymbols = mainSymbols => mainSymbols;
7+
const getFallbackSymbols = (mainSymbols, fallbackSymbols) => fallbackSymbols;
68

79
console.log(` ${Object.values(figures).join(' ')}\n`);
810

911
test('figures', t => {
10-
t.is(figures.tick, result('✔', '√'));
12+
t.is(figures.tick, getCorrectSymbols('✔', '√'));
1113
});
1214

1315
test('mainSymbols', t => {
@@ -18,15 +20,23 @@ test('fallbackSymbols', t => {
1820
t.is(fallbackSymbols.tick, '√');
1921
});
2022

21-
test('replaceSymbols() keep non-figures as is', t => {
22-
t.is(replaceSymbols('foo'), 'foo');
23-
});
23+
const testKeepFigures = (t, useFallback) => {
24+
t.is(replaceSymbols('foo', {useFallback}), 'foo');
25+
};
2426

25-
test('replaceSymbols() replace figures', t => {
26-
t.is(replaceSymbols('✔ ✔ ✔'), result('✔ ✔ ✔', '√ √ √'));
27-
t.is(replaceSymbols('✔ ✘\n★ ◼'), result('✔ ✘\n★ ◼', '√ ×\n✶ ■'));
28-
t.is(replaceSymbols('✔ ✘ ★ ◼'), result('✔ ✘ ★ ◼', '√ × ✶ ■'));
29-
});
27+
test('replaceSymbols() keep non-figures as is', testKeepFigures, undefined);
28+
test('"useFallback: false" keep non-figures as is', testKeepFigures, false);
29+
test('"useFallback: true" keep non-figures as is', testKeepFigures, true);
30+
31+
const testReplace = (t, useFallback, getSymbols) => {
32+
t.is(replaceSymbols('✔ ✔ ✔', {useFallback}), getSymbols('✔ ✔ ✔', '√ √ √'));
33+
t.is(replaceSymbols('✔ ✘\n★ ◼', {useFallback}), getSymbols('✔ ✘\n★ ◼', '√ ×\n✶ ■'));
34+
t.is(replaceSymbols('✔ ✘ ★ ◼', {useFallback}), getSymbols('✔ ✘ ★ ◼', '√ × ✶ ■'));
35+
};
36+
37+
test('replaceSymbols() sometimes replaces figures', testReplace, undefined, getCorrectSymbols);
38+
test('"useFallback: false" never replaces figures', testReplace, false, getMainSymbols);
39+
test('"useFallback: true" always replaces figures', testReplace, true, getFallbackSymbols);
3040

3141
test('figures are non-empty strings', t => {
3242
for (const figure of Object.values(figures)) {

0 commit comments

Comments
 (0)
Please sign in to comment.