Skip to content

Commit

Permalink
Add functions to make a template function with a custom Chalk instance (
Browse files Browse the repository at this point in the history
#17)

Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
lukekarrys and sindresorhus committed May 23, 2023
1 parent d12eec4 commit 50b910f
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 8 deletions.
37 changes: 37 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type {ChalkInstance} from 'chalk';

/**
Terminal string styling with [tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates)
Expand Down Expand Up @@ -78,3 +80,38 @@ console.log(template('Today is {red hot}'));
```
*/
export function templateStderr(text: string): string;

/**
Terminal string styling, using a custom Chalk instance.
This function can be useful if you need to create a template function using your own Chalk instance.
__Note:__ It's up to you to properly escape the input.
@example
```
import {Chalk} from 'chalk'
import {makeTemplate} from 'chalk-template';
const template = makeTemplate(new Chalk());
console.log(template('Today is {red hot}''));
```
*/
export function makeTemplate(chalk: ChalkInstance): (text: string) => string;

/**
Terminal string styling with [tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates),
configured using a custom Chalk instance.
@example
```
import {Chalk} from 'chalk'
import {makeTaggedTemplate} from 'chalk-template';
const chalkTemplate = makeTaggedTemplate(new Chalk());
console.log(chalkTemplate`Today is {red hot}`);
```
*/
export function makeTaggedTemplate(chalk: ChalkInstance): (text: TemplateStringsArray, ...placeholders: unknown[]) => string;
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function parseStyle(style) {
return results;
}

function makeTemplate(chalk) {
export function makeTemplate(chalk) {
function buildStyle(styles) {
const enabled = {};

Expand Down Expand Up @@ -180,6 +180,8 @@ function makeChalkTemplate(template) {
return chalkTemplate;
}

export const makeTaggedTemplate = chalkInstance => makeChalkTemplate(makeTemplate(chalkInstance));

export const template = makeTemplate(chalk);
export default makeChalkTemplate(template);

Expand Down
8 changes: 6 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expectType} from 'tsd';
import chalk from 'chalk';
import chalkTemplate, {template, chalkTemplateStderr, templateStderr} from './index.js';
import chalk, {Chalk} from 'chalk';
import chalkTemplate, {template, chalkTemplateStderr, templateStderr, makeTemplate, makeTaggedTemplate} from './index.js';

// -- Template literal --
expectType<string>(chalkTemplate``);
Expand All @@ -17,3 +17,7 @@ expectType<string>(chalk.strikethrough.cyanBright.bgBlack(chalkTemplate`Works wi
// -- Stderr Types --
expectType<typeof chalkTemplate>(chalkTemplateStderr);
expectType<typeof template>(templateStderr);

// -- Make template functions --
expectType<typeof template>(makeTemplate(new Chalk()));
expectType<typeof chalkTemplate>(makeTaggedTemplate(new Chalk()));
17 changes: 17 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ import {templateStderr} from 'chalk-template';
console.error(templateStderr('Today is {red hot}'));
```

## Create template functions using a custom Chalk instance

The `makeTemplate` and `makeTaggedTemplate` functions are exported so functions can be created using a custom Chalk instance.

**Note:** When using a function created with `makeTemplate`, it's up to you to properly escape the input.

```js
import {Chalk} from 'chalk'
import {makeTemplate, makeTaggedTemplate} from 'chalk-template';

const template = makeTemplate(new Chalk({level: 3}));
const chalkTemplate = makeTaggedTemplate(new Chalk({level: 3}));

console.log(template('Today is {red hot}'));
console.log(chalkTemplate`Today is {red hot}`);
```

## Related

- [chalk](https://github.com/chalk/chalk) - Terminal string styling done right
Expand Down
7 changes: 6 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava';
import chalkTemplate, {chalkTemplateStderr} from '../index.js';
import chalk from 'chalk';
import chalkTemplate, {chalkTemplateStderr, makeTaggedTemplate} from '../index.js';

test('return an empty string for an empty literal', t => {
t.is(chalkTemplate``, '');
Expand All @@ -8,3 +9,7 @@ test('return an empty string for an empty literal', t => {
test('return an empty string for an empty literal (stderr)', t => {
t.is(chalkTemplateStderr``, '');
});

test('return an empty string for an empty literal (chalk)', t => {
t.is(makeTaggedTemplate(chalk)``, '');
});
4 changes: 2 additions & 2 deletions test/no-color.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'ava';
import chalk from 'chalk';
import chalkTemplateStdout, {chalkTemplateStderr} from '../index.js';
import chalkTemplateStdout, {chalkTemplateStderr, makeTaggedTemplate} from '../index.js';

for (const [chalkTemplate, stdio] of [[chalkTemplateStdout, 'stdout'], [chalkTemplateStderr, 'stderr']]) {
for (const [chalkTemplate, stdio] of [[chalkTemplateStdout, 'stdout'], [chalkTemplateStderr, 'stderr'], [makeTaggedTemplate(chalk), 'chalk']]) {
test(`[${stdio}] return a regular string for a literal with no templates`, t => {
t.is(chalkTemplate`hello`, 'hello');
});
Expand Down
5 changes: 3 additions & 2 deletions test/template.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'ava';
import {template as templateStdout, templateStderr} from '../index.js';
import chalk from 'chalk';
import {template as templateStdout, templateStderr, makeTemplate} from '../index.js';

for (const [template, stdio] of [[templateStdout, 'stdout'], [templateStderr, 'stderr']]) {
for (const [template, stdio] of [[templateStdout, 'stdout'], [templateStderr, 'stderr'], [makeTemplate(chalk), 'chalk']]) {
test(`[${stdio}] correctly parse and evaluate color-convert functions`, t => {
t.is(template('{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}'),
'\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, '
Expand Down

0 comments on commit 50b910f

Please sign in to comment.