Skip to content

Commit 3f9e1a7

Browse files
committed
Add forceFallbackSymbols() method
1 parent b10ba98 commit 3f9e1a7

File tree

5 files changed

+76
-28
lines changed

5 files changed

+76
-28
lines changed

index.d.ts

Lines changed: 16 additions & 5 deletions
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+
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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 15 additions & 3 deletions
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

Lines changed: 20 additions & 3 deletions
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, forceFallbackSymbols} from 'figures';
2121

2222
console.log(figures.tick);
2323
// On terminals with Unicode symbols: ✔︎
@@ -32,6 +32,10 @@ console.log(fallbackSymbols.tick);
3232
console.log(replaceSymbols('✔︎ check'));
3333
// On terminals with Unicode symbols: ✔︎ check
3434
// On other terminals: √ check
35+
36+
console.log(replaceSymbols('✔︎ check', {useFallback: true}));
37+
// On terminals with Unicode symbols: √︎ check
38+
// On other terminals: √ check
3539
```
3640

3741
## API
@@ -50,9 +54,9 @@ Symbols to use when the terminal supports Unicode symbols.
5054

5155
Symbols to use when the terminal does not support Unicode symbols.
5256

53-
### replaceSymbols(string)
57+
### replaceSymbols(string, options?)
5458

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

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

@@ -62,6 +66,19 @@ Type: `string`
6266

6367
String where the Unicode symbols will be replaced with fallback symbols depending on the terminal.
6468

69+
#### options
70+
71+
Type: `object`
72+
73+
##### useFallback
74+
75+
Type: `boolean`\
76+
Default: `true` if the terminal has poor Unicode support
77+
78+
Whether to replace symbols with fallbacks.
79+
80+
This can be set to `true` to always use fallback symbols, whether the terminal has poor Unicode support or not.
81+
6582
## Figures
6683

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

test.js

Lines changed: 20 additions & 10 deletions
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 replace 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)