chalk-template 0.5.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type {ChalkInstance} from 'chalk';
2
+
1
3
  /**
2
4
  Terminal string styling with [tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates)
3
5
 
@@ -78,3 +80,38 @@ console.log(template('Today is {red hot}'));
78
80
  ```
79
81
  */
80
82
  export function templateStderr(text: string): string;
83
+
84
+ /**
85
+ Terminal string styling, using a custom Chalk instance.
86
+
87
+ This function can be useful if you need to create a template function using your own Chalk instance.
88
+
89
+ __Note:__ It's up to you to properly escape the input.
90
+
91
+ @example
92
+ ```
93
+ import {Chalk} from 'chalk'
94
+ import {makeTemplate} from 'chalk-template';
95
+
96
+ const template = makeTemplate(new Chalk());
97
+
98
+ console.log(template('Today is {red hot}''));
99
+ ```
100
+ */
101
+ export function makeTemplate(chalk: ChalkInstance): (text: string) => string;
102
+
103
+ /**
104
+ Terminal string styling with [tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates),
105
+ configured using a custom Chalk instance.
106
+
107
+ @example
108
+ ```
109
+ import {Chalk} from 'chalk'
110
+ import {makeTaggedTemplate} from 'chalk-template';
111
+
112
+ const chalkTemplate = makeTaggedTemplate(new Chalk());
113
+
114
+ console.log(chalkTemplate`Today is {red hot}`);
115
+ ```
116
+ */
117
+ export function makeTaggedTemplate(chalk: ChalkInstance): (text: TemplateStringsArray, ...placeholders: unknown[]) => string;
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import chalk from 'chalk';
1
+ // eslint-disable-next-line unicorn/import-style
2
+ import chalk, {chalkStderr} from 'chalk';
2
3
 
3
- // eslint-disable-next-line unicorn/better-regex
4
4
  const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|{[a-f\d]{1,6}})|x[a-f\d]{2}|.))|(?:{(~)?(#?[\w:]+(?:\([^)]*\))?(?:\.#?[\w:]+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(})|((?:.|[\r\n\f])+?)/gi;
5
5
  const STYLE_REGEX = /(?:^|\.)(?:(?:(\w+)(?:\(([^)]*)\))?)|(?:#(?=[:a-fA-F\d]{2,})([a-fA-F\d]{6})?(?::([a-fA-F\d]{6}))?))/g;
6
6
  const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
@@ -92,7 +92,7 @@ function parseStyle(style) {
92
92
  return results;
93
93
  }
94
94
 
95
- function makeTemplate(chalk) {
95
+ export function makeTemplate(chalk) {
96
96
  function buildStyle(styles) {
97
97
  const enabled = {};
98
98
 
@@ -179,8 +179,10 @@ function makeChalkTemplate(template) {
179
179
  return chalkTemplate;
180
180
  }
181
181
 
182
+ export const makeTaggedTemplate = chalkInstance => makeChalkTemplate(makeTemplate(chalkInstance));
183
+
182
184
  export const template = makeTemplate(chalk);
183
185
  export default makeChalkTemplate(template);
184
186
 
185
- export const templateStderr = makeTemplate(chalk.stderr);
187
+ export const templateStderr = makeTemplate(chalkStderr);
186
188
  export const chalkTemplateStderr = makeChalkTemplate(templateStderr);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chalk-template",
3
- "version": "0.5.0",
3
+ "version": "1.1.0",
4
4
  "description": "Terminal string styling with tagged template literals",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/chalk-template",
@@ -46,12 +46,12 @@
46
46
  "text"
47
47
  ],
48
48
  "dependencies": {
49
- "chalk": "^4.1.2"
49
+ "chalk": "^5.2.0"
50
50
  },
51
51
  "devDependencies": {
52
- "ava": "^5.1.0",
52
+ "ava": "^5.2.0",
53
53
  "cross-env": "^7.0.3",
54
- "tsd": "^0.25.0",
55
- "xo": "^0.53.1"
54
+ "tsd": "^0.28.1",
55
+ "xo": "^0.54.2"
56
56
  }
57
57
  }
package/readme.md CHANGED
@@ -87,6 +87,23 @@ import {templateStderr} from 'chalk-template';
87
87
  console.error(templateStderr('Today is {red hot}'));
88
88
  ```
89
89
 
90
+ ## Create template functions using a custom Chalk instance
91
+
92
+ The `makeTemplate` and `makeTaggedTemplate` functions are exported so functions can be created using a custom Chalk instance.
93
+
94
+ **Note:** When using a function created with `makeTemplate`, it's up to you to properly escape the input.
95
+
96
+ ```js
97
+ import {Chalk} from 'chalk'
98
+ import {makeTemplate, makeTaggedTemplate} from 'chalk-template';
99
+
100
+ const template = makeTemplate(new Chalk({level: 3}));
101
+ const chalkTemplate = makeTaggedTemplate(new Chalk({level: 3}));
102
+
103
+ console.log(template('Today is {red hot}'));
104
+ console.log(chalkTemplate`Today is {red hot}`);
105
+ ```
106
+
90
107
  ## Related
91
108
 
92
109
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right