chalk-template 0.2.0 → 0.4.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
@@ -21,3 +21,15 @@ log(chalk.red.bgBlack(chalkTemplate`2 + 3 = {bold ${2 + 3}}`));
21
21
  ```
22
22
  */
23
23
  export default function chalkTemplate(text: TemplateStringsArray, ...placeholders: unknown[]): string;
24
+
25
+ /**
26
+ Terminal string styling. It is preferred that you use the template tag (default export) but this function is useful if you'd like to wrap the color template function.
27
+
28
+ @example
29
+ ```
30
+ import { template } from 'chalk-template';
31
+
32
+ log(template('Today is {red hot}')
33
+ ```
34
+ */
35
+ export function template(text: string): string;
package/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import chalk from 'chalk';
2
2
 
3
3
  // eslint-disable-next-line unicorn/better-regex
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
- const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
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
+ 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$/;
7
7
  const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|{[a-f\d]{1,6}})|x[a-f\d]{2}|.)|([^\\])/gi;
8
8
 
@@ -53,6 +53,18 @@ function parseArguments(name, arguments_) {
53
53
  return results;
54
54
  }
55
55
 
56
+ function parseHex(hex) {
57
+ const n = Number.parseInt(hex, 16);
58
+ return [
59
+ // eslint-disable-next-line no-bitwise
60
+ (n >> 16) & 0xFF,
61
+ // eslint-disable-next-line no-bitwise
62
+ (n >> 8) & 0xFF,
63
+ // eslint-disable-next-line no-bitwise
64
+ n & 0xFF,
65
+ ];
66
+ }
67
+
56
68
  function parseStyle(style) {
57
69
  STYLE_REGEX.lastIndex = 0;
58
70
 
@@ -64,6 +76,14 @@ function parseStyle(style) {
64
76
 
65
77
  if (matches[2]) {
66
78
  results.push([name, ...parseArguments(name, matches[2])]);
79
+ } else if (matches[3] || matches[4]) {
80
+ if (matches[3]) {
81
+ results.push(['rgb', ...parseHex(matches[3])]);
82
+ }
83
+
84
+ if (matches[4]) {
85
+ results.push(['bgRgb', ...parseHex(matches[4])]);
86
+ }
67
87
  } else {
68
88
  results.push([name]);
69
89
  }
@@ -97,7 +117,7 @@ function buildStyle(styles) {
97
117
  return current;
98
118
  }
99
119
 
100
- function template(string) {
120
+ export function template(string) {
101
121
  const styles = [];
102
122
  const chunks = [];
103
123
  let chunk = [];
@@ -141,7 +161,7 @@ export default function chalkTemplate(firstString, ...arguments_) {
141
161
 
142
162
  const parts = [firstString.raw[0]];
143
163
 
144
- for (let index = 1; index < firstString.length; index++) {
164
+ for (let index = 1; index < firstString.raw.length; index++) {
145
165
  parts.push(
146
166
  String(arguments_[index - 1]).replace(/[{}\\]/g, '\\$&'),
147
167
  String(firstString.raw[index]),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chalk-template",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Terminal string styling with tagged template literals",
5
5
  "license": "MIT",
6
6
  "repository": "chalk/chalk-template",
@@ -11,7 +11,7 @@
11
11
  "node": ">=12"
12
12
  },
13
13
  "scripts": {
14
- "test": "xo && ava test/index.js && cross-env FORCE_COLOR=0 ava test/no-color.js && cross-env FORCE_COLOR=3 TERM=dumb ava test/full-color.js && tsd"
14
+ "test": "xo && ava test/index.js && cross-env FORCE_COLOR=0 ava test/no-color.js && cross-env FORCE_COLOR=3 TERM=dumb ava test/full-color.js && cross-env FORCE_COLOR=3 TERM=dumb ava test/template.js && tsd"
15
15
  },
16
16
  "files": [
17
17
  "index.js",
@@ -49,6 +49,7 @@
49
49
  "ava": "^3.15.0",
50
50
  "cross-env": "^7.0.3",
51
51
  "tsd": "^0.18.0",
52
+ "typescript": "^4.6.2",
52
53
  "xo": "^0.45.0"
53
54
  }
54
55
  }
package/readme.md CHANGED
@@ -31,6 +31,13 @@ console.log(chalk`
31
31
  There are {bold 5280 feet} in a mile.
32
32
  In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
33
33
  `);
34
+
35
+
36
+ console.log(chalk`
37
+ There are also {#FF0000 shorthand hex styles} for
38
+ both the {#ABCDEF foreground}, {#:123456 background},
39
+ or {#ABCDEF:123456 both}.
40
+ `);
34
41
  ```
35
42
 
36
43
  ## API
@@ -47,10 +54,20 @@ console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
47
54
  console.log(chalkTemplate`{bold.rgb(10,100,200) Hello!}`);
48
55
  ```
49
56
 
50
- Note that function styles (`rgb()`, `hex()`, etc.) may not contain spaces between parameters.
57
+ Note that function styles (`rgb()`, etc.) may not contain spaces between parameters.
51
58
 
52
59
  All interpolated values (`` chalkTemplate`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
53
60
 
61
+ ## Template function
62
+
63
+ You may also use the template function as an alternative to the tagged template function.
64
+
65
+ ```js
66
+ import {template} from 'chalk-template';
67
+
68
+ console.log(template('Today is {red hot}'));
69
+ ```
70
+
54
71
  ## Related
55
72
 
56
73
  - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right