chalk-template 1.0.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,7 +1,6 @@
1
1
  // eslint-disable-next-line unicorn/import-style
2
2
  import chalk, {chalkStderr} from 'chalk';
3
3
 
4
- // eslint-disable-next-line unicorn/better-regex
5
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;
6
5
  const STYLE_REGEX = /(?:^|\.)(?:(?:(\w+)(?:\(([^)]*)\))?)|(?:#(?=[:a-fA-F\d]{2,})([a-fA-F\d]{6})?(?::([a-fA-F\d]{6}))?))/g;
7
6
  const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
@@ -93,7 +92,7 @@ function parseStyle(style) {
93
92
  return results;
94
93
  }
95
94
 
96
- function makeTemplate(chalk) {
95
+ export function makeTemplate(chalk) {
97
96
  function buildStyle(styles) {
98
97
  const enabled = {};
99
98
 
@@ -180,6 +179,8 @@ function makeChalkTemplate(template) {
180
179
  return chalkTemplate;
181
180
  }
182
181
 
182
+ export const makeTaggedTemplate = chalkInstance => makeChalkTemplate(makeTemplate(chalkInstance));
183
+
183
184
  export const template = makeTemplate(chalk);
184
185
  export default makeChalkTemplate(template);
185
186
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chalk-template",
3
- "version": "1.0.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",
@@ -51,7 +51,7 @@
51
51
  "devDependencies": {
52
52
  "ava": "^5.2.0",
53
53
  "cross-env": "^7.0.3",
54
- "tsd": "^0.27.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