From 8dd4c08cf030c2a2a9a83ee1432f391fbf3dc0aa Mon Sep 17 00:00:00 2001 From: Colin Date: Wed, 18 Dec 2019 10:30:16 -0800 Subject: [PATCH] Allow fallback to be true --- index.d.ts | 2 +- index.js | 2 +- readme.md | 2 +- test.js | 11 +++++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 177393f..6c2c24e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,7 +5,7 @@ declare namespace terminalLink { @default `${text} (${url})` */ - fallback?: ((text: string, url: string) => string) | false; + fallback?: ((text: string, url: string) => string) | boolean; } } diff --git a/index.js b/index.js index 8bc2574..7bbc9cb 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ const terminalLink = (text, url, {target = 'stdout', ...options} = {}) => { return text; } - return options.fallback ? options.fallback(text, url) : `${text} (\u200B${url}\u200B)`; + return typeof options.fallback === 'function' ? options.fallback(text, url) : `${text} (\u200B${url}\u200B)`; } return ansiEscapes.link(text, url); diff --git a/readme.md b/readme.md index 3f85be3..918b1ee 100644 --- a/readme.md +++ b/readme.md @@ -47,7 +47,7 @@ Type: `object` ##### fallback -Type: `Function | false` +Type: `Function | boolean` Override the default fallback. The function receives the `text` and `url` as parameters and is expected to return a string. diff --git a/test.js b/test.js index e65636d..efc01a5 100644 --- a/test.js +++ b/test.js @@ -47,6 +47,17 @@ test('disabled fallback', t => { t.is(actual, 'My Website'); }); +test('explicitly enabled fallback', t => { + process.env.FORCE_HYPERLINK = 0; + const terminalLink = require('.'); + + const actual = terminalLink('My Website', 'https://sindresorhus.com', { + fallback: true + }); + console.log(actual); + t.is(actual, 'My Website (\u200Bhttps://sindresorhus.com\u200B)'); +}); + test('stderr default fallback', t => { process.env.FORCE_HYPERLINK = 0; const terminalLink = require('.');