Skip to content

Commit

Permalink
Separate replaceSymbols() method (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jul 1, 2021
1 parent 53c3c6c commit 5aa4c62
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ declare const figures: {
// On Windows: √
```
*/
(string: string): string;
replaceSymbols(string: string): string;

/**
Symbols to use when not running on Windows.
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,10 @@ const getReplacements = () => {
return replacements;
};

module.exports = figures;

// On Windows, substitute non-fallback to fallback figures
const replaceCharsToFallback = string => {
module.exports.replaceSymbols = string => {
if (figures === main) {
return string;
}
Expand All @@ -315,6 +317,5 @@ const replaceCharsToFallback = string => {
return string;
};

module.exports = Object.assign(replaceCharsToFallback, figures);
module.exports.main = main;
module.exports.windows = fallback;
5 changes: 4 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {expectType} from 'tsd';
import figures = require('.');
import {replaceSymbols, main, windows} from '.';

expectType<string>(figures('✔︎ check'));
expectType<string>(replaceSymbols('✔︎ check'));
expectType<string>(figures.tick);
expectType<string>(main.tick);
expectType<string>(windows.tick);
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ See the [source](index.js) for supported symbols.
```js
const figures = require('figures');

console.log(figures('✔︎ check'));
console.log(figures.replaceSymbols('✔︎ check'));
// On non-Windows OSes: ✔︎ check
// On Windows: √ check

Expand All @@ -38,7 +38,7 @@ console.log(figures.windows.tick);

## API

### figures(string)
### figures.replaceSymbols(string)

Returns the input with replaced fallback Unicode symbols on Windows.

Expand Down
31 changes: 15 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
import test from 'ava';
import figures from '.';
import figures, {replaceSymbols, main, windows} from '.';

const result = (main, windows) => process.platform === 'win32' ? windows : main;

console.log(` ${Object.values(figures).join(' ')}\n`);
const NON_FIGURE_KEYS = new Set(['main', 'windows', 'replaceSymbols']);
const isFigureKey = ([key]) => !NON_FIGURE_KEYS.has(key);
const getFigure = ([, figure]) => figure;
const getFigures = () => Object.entries(figures).filter(isFigureKey).map(getFigure);

console.log(` ${getFigures().join(' ')}\n`);

test('figures', t => {
t.is(figures.tick, result('✔', '√'));
});

test('fallbacks', t => {
t.is(figures('foo'), 'foo');
t.is(figures('?bar?'), '?bar?');
t.is(figures('✔ ✔ ✔'), result('✔ ✔ ✔', '√ √ √'));
t.is(figures('✔ ✖\n★ ◼'), result('✔ ✖\n★ ◼', '√ ×\n✶ ■'));
t.is(figures('✔ ✖ ★ ◼'), result('✔ ✖ ★ ◼', '√ × ✶ ■'));
t.is(replaceSymbols('foo'), 'foo');
t.is(replaceSymbols('?bar?'), '?bar?');
t.is(replaceSymbols('✔ ✔ ✔'), result('✔ ✔ ✔', '√ √ √'));
t.is(replaceSymbols('✔ ✖\n★ ◼'), result('✔ ✖\n★ ◼', '√ ×\n✶ ■'));
t.is(replaceSymbols('✔ ✖ ★ ◼'), result('✔ ✖ ★ ◼', '√ × ✶ ■'));
});

test('exported sets', t => {
t.is(figures.main.tick, '✔');
t.is(figures.windows.tick, '√');
t.is(main.tick, '✔');
t.is(windows.tick, '√');
});

const NON_FIGURE_KEYS = new Set(['main', 'windows']);

test('figures are non-empty strings', t => {
for (const [key, figure] of Object.entries(figures)) {
if (NON_FIGURE_KEYS.has(key)) {
continue;
}

for (const figure of getFigures()) {
t.true(typeof figure === 'string' && figure.trim() !== '');
}
});

0 comments on commit 5aa4c62

Please sign in to comment.