chalker 1.1.2 → 1.3.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.
Files changed (3) hide show
  1. package/README.md +28 -7
  2. package/lib/index.js +106 -13
  3. package/package.json +35 -25
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # chalker
5
5
 
6
- Set ansi colors in strings using XML like markers and [chalk].
6
+ Set ansi colors in strings using `<>` markers and [chalk] or [ansi-colors].
7
7
 
8
8
  # Usage
9
9
 
@@ -31,9 +31,17 @@ logger.log(chalker.remove(msg));
31
31
  # Install
32
32
 
33
33
  ```
34
- npm i --save chalker
34
+ npm i --save chalker chalk
35
35
  ```
36
36
 
37
+ or:
38
+
39
+ ```
40
+ npm i --save chalker ansi-colors
41
+ ```
42
+
43
+ `chalker` expects either [chalk] `>= 4` or [ansi-colors] `>= 4` as a peer dependency.
44
+
37
45
  # Demo
38
46
 
39
47
  ![demo][demo]
@@ -62,12 +70,15 @@ npm i --save chalker
62
70
 
63
71
  #### Advanced Chalk Colors
64
72
 
73
+ Some advanced color markers use `chalker` compatibility wrappers when Chalk does not provide
74
+ the corresponding API directly.
75
+
65
76
  [Chalk advanced colors] can be applied with:
66
77
 
67
78
  | [chalk] API | chalker marker | [chalk] API | chalker marker |
68
79
  | --------------- | --------------------------------------------- | ----------------- | ---------------------------------------------------- |
69
80
  | `chalk.rgb` | `<(255, 10, 20)>`, `<rgb(255,10,20)>` | `chalk.bgRgb` | `<bg(255, 10, 20)>`, `<bgRgb(255,10,20)>` |
70
- | `chalk.hex` | `<#FF0000>`, `<hex(#FF0000)>` | `chalk.bgHex` | `<bg#0000FF>`, `<bgHex(#0000FF)>` |
81
+ | `chalk.hex` | `<#FF0000>`, `<hex(#FF0000)>` | `chalk.bgHex` | `<bg#0000FF>`, `<bgHex(#0000FF)>` |
71
82
  | `chalk.keyword` | `<orange>`, `<(orange)>`, `<keyword(orange)>` | `chalk.bgKeyword` | `<bg-orange>`, `<bg(orange)>`, `<bgKeyword(orange)>` |
72
83
  | `chalk.hsl` | `<hsl(32,100,50)>` | `chalk.bgHsl` | `<bgHsl(32,100,50)>` |
73
84
  | `chalk.hsv` | `<hsv(32,100,100)>` | `chalk.bgHsv` | `<bgHsv(32,100,100)>` |
@@ -76,6 +87,7 @@ npm i --save chalker
76
87
  ##### More details
77
88
 
78
89
  - a marker is tried with `chalk.keyword` if:
90
+
79
91
  - it's not detected as hex value
80
92
  - it doesn't contain params enclosed in `()`
81
93
  - it's not found as a basic color that `chalk` supports
@@ -86,7 +98,7 @@ npm i --save chalker
86
98
 
87
99
  - All markers can be comined with `.` in any order as long as they work with [chalk]
88
100
 
89
- - ie: `<#FF0000.bg#0000FF.bg-orange.keyword(red)>`
101
+ - ie: `<#FF0000.bg#0000FF.bg-orange.keyword(red)>`
90
102
 
91
103
  # APIs
92
104
 
@@ -102,7 +114,16 @@ chalker(str, [chalkInstance]);
102
114
 
103
115
  **Returns:** A string with terminal/ansi color codes
104
116
 
105
- > If `chalk.supportsColor` is `false`, then it will simply remove the XML markers and decode HTML entities only.
117
+ > If `chalk.supportsColor` is `false`, then it will simply remove the `<>` markers and decode HTML entities only.
118
+
119
+ ### `chalker.CHALK`
120
+
121
+ ```js
122
+ chalker.CHALK = require("ansi-colors");
123
+ ```
124
+
125
+ Set the default colors library. By default, `chalker` loads `chalk` first and falls back to
126
+ [ansi-colors].
106
127
 
107
128
  ### `chalker.remove`
108
129
 
@@ -117,11 +138,10 @@ Simply remove all chalker markers and return the plain text string, with HTML es
117
138
 
118
139
  **Returns**: A plain text string without chalker color markers
119
140
 
120
-
121
141
  ### `chalker.decodeHtml`
122
142
 
123
143
  ```js
124
- chalker.decodeHtml(str)
144
+ chalker.decodeHtml(str);
125
145
  ```
126
146
 
127
147
  - `str` - String to decode HTML entities
@@ -137,6 +157,7 @@ Licensed under the [Apache License, Version 2.0](https://www.apache.org/licenses
137
157
  ---
138
158
 
139
159
  [demo]: ./images/demo.png
160
+ [ansi-colors]: https://www.npmjs.com/package/ansi-colors
140
161
  [chalk]: https://www.npmjs.com/package/chalk
141
162
  [chalk advanced colors]: https://github.com/chalk/chalk#256-and-truecolor-color-support
142
163
  [travis-image]: https://travis-ci.org/jchip/chalker.svg?branch=master
package/lib/index.js CHANGED
@@ -3,7 +3,9 @@
3
3
  /* eslint-disable complexity, max-statements, no-magic-numbers, prefer-template, prefer-spread */
4
4
 
5
5
  const assert = require("assert");
6
- const chalk = require("chalk");
6
+ const optionalRequire = require("optional-require")(require);
7
+ const colorConvert = require("color-convert");
8
+ const chalk = loadColors();
7
9
 
8
10
  //
9
11
  // convert color markers in a string to terminal/ansi color codes with chalk
@@ -62,8 +64,100 @@ const htmlEntities = {
62
64
  [`&reg;`]: "\xae"
63
65
  };
64
66
 
67
+ function loadColors() {
68
+ const colors = optionalRequire("chalk") || optionalRequire("ansi-colors");
69
+
70
+ if (!colors) {
71
+ throw new Error("chalker requires either chalk or ansi-colors to be installed");
72
+ }
73
+
74
+ return normalizeColors(colors);
75
+ }
76
+
77
+ function normalizeColors(colorsModule) {
78
+ const colors =
79
+ colorsModule &&
80
+ colorsModule.default &&
81
+ (typeof colorsModule.default === "function" || typeof colorsModule.default === "object")
82
+ ? colorsModule.default
83
+ : colorsModule;
84
+
85
+ return addAnsiColorsCompat(colors);
86
+ }
87
+
88
+ function addAnsiColorsCompat(colors) {
89
+ if (!colors || typeof colors.alias !== "function" || typeof colors.rgb === "function") {
90
+ return colors;
91
+ }
92
+
93
+ colors.rgb = function (r, g, b) {
94
+ return this[defineAnsiColor(colors, "rgb", [r, g, b])];
95
+ };
96
+ colors.bgRgb = function (r, g, b) {
97
+ return this[defineAnsiColor(colors, "bgRgb", [r, g, b], true)];
98
+ };
99
+ colors.hex = function (value) {
100
+ return this.rgb.apply(this, colorConvert.hex.rgb(value));
101
+ };
102
+ colors.bgHex = function (value) {
103
+ return this.bgRgb.apply(this, colorConvert.hex.rgb(value));
104
+ };
105
+
106
+ return colors;
107
+ }
108
+
109
+ function defineAnsiColor(colors, name, values, bg) {
110
+ const styleName = `chalker_${name}_${values.join("_")}`;
111
+
112
+ if (!colors.styles[styleName]) {
113
+ const prefix = bg ? 48 : 38;
114
+ const close = bg ? 49 : 39;
115
+ colors.alias(
116
+ styleName,
117
+ (text) => `\u001b[${prefix};2;${values.join(";")}m${text}\u001b[${close}m`
118
+ );
119
+ }
120
+
121
+ return styleName;
122
+ }
123
+
124
+ function convertColorModel(name, values) {
125
+ const bg = name.startsWith("bg");
126
+ const model = bg ? name[2].toLowerCase() + name.substring(3) : name;
127
+ const rgb =
128
+ model === "keyword"
129
+ ? colorConvert.keyword.rgb(String(values[0]).toLowerCase())
130
+ : colorConvert[model] && colorConvert[model].rgb.apply(colorConvert[model], values);
131
+
132
+ assert(rgb, `unknown color ${values[0]}`);
133
+
134
+ return { name: bg ? "bgRgb" : "rgb", values: rgb };
135
+ }
136
+
137
+ function applyChalkMethod(chalkInstance, name, values) {
138
+ if (typeof chalkInstance[name] === "function") {
139
+ return chalkInstance[name].apply(chalkInstance, values);
140
+ }
141
+
142
+ if (
143
+ name === "keyword" ||
144
+ name === "bgKeyword" ||
145
+ name === "hsl" ||
146
+ name === "bgHsl" ||
147
+ name === "hsv" ||
148
+ name === "bgHsv" ||
149
+ name === "hwb" ||
150
+ name === "bgHwb"
151
+ ) {
152
+ const converted = convertColorModel(name, values);
153
+ return chalkInstance[converted.name].apply(chalkInstance, converted.values);
154
+ }
155
+
156
+ throw new TypeError(`${name} is not a chalk function`);
157
+ }
158
+
65
159
  function decodeHtml(str) {
66
- return str.replace(/&[\w#]+;/g, m => {
160
+ return str.replace(/&[\w#]+;/g, (m) => {
67
161
  if (htmlEntities.hasOwnProperty(m)) return htmlEntities[m];
68
162
  if (m.startsWith("&#x")) {
69
163
  const s = m.substring(3, m.length - 1);
@@ -112,7 +206,7 @@ function applyChalkMarkers(markers, text, userChalk) {
112
206
  let values = marker.substring(openIx + 1, closeIx).trim();
113
207
  if (values.indexOf(",") >= 0) {
114
208
  // extract rgb/hsl/hsv/hwb values like (255, 10, 20)
115
- values = values.split(",").map(x => parseInt(x.trim(), 10));
209
+ values = values.split(",").map((x) => parseInt(x.trim(), 10));
116
210
 
117
211
  // default no name to rgb, and bg to bgRgb
118
212
  if (!name) name = "rgb";
@@ -127,23 +221,20 @@ function applyChalkMarkers(markers, text, userChalk) {
127
221
  }
128
222
 
129
223
  try {
130
- a = a[name].apply(a, values);
224
+ a = applyChalkMethod(a, name, values);
131
225
  } catch (err) {
132
- const msg =
133
- typeof a[name] !== "function"
134
- ? `${name} is not a chalk function`
135
- : `calling chalk.${name} failed with: ${err.message}`;
136
-
137
- throw new Error(`marker ${marker} is invalid: ${msg}`);
226
+ throw new Error(
227
+ `marker ${marker} is invalid: calling chalk.${name} failed with: ${err.message}`
228
+ );
138
229
  }
139
230
  } else {
140
231
  // if not found as a basic color, then try with chalk.keyword or chalk.bgKeyword
141
232
  try {
142
233
  const kw = deQuote(marker, marker);
143
234
  if (kw.startsWith("bg-") || kw.startsWith("bg ")) {
144
- a = a.bgKeyword(kw.substring(3));
235
+ a = applyChalkMethod(a, "bgKeyword", [kw.substring(3)]);
145
236
  } else {
146
- a = a.keyword(kw);
237
+ a = applyChalkMethod(a, "keyword", [kw]);
147
238
  }
148
239
  } catch (err) {
149
240
  throw new Error(`marker ${marker} is not found and invalid as a keyword`);
@@ -170,7 +261,7 @@ function remove(s, keepHtml) {
170
261
  }
171
262
 
172
263
  function format(s, userChalk) {
173
- userChalk = userChalk || chalk;
264
+ userChalk = normalizeColors(userChalk || chalker.CHALK);
174
265
 
175
266
  // skip applying ansi colors if chalk says color support is off
176
267
  if (userChalk.supportsColor === false) {
@@ -251,4 +342,6 @@ chalker.remove = remove;
251
342
 
252
343
  chalker.decodeHtml = decodeHtml;
253
344
 
345
+ chalker.CHALK = chalk;
346
+
254
347
  module.exports = chalker;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chalker",
3
- "version": "1.1.2",
3
+ "version": "1.3.0",
4
4
  "description": "Set ansi colors in strings using XML and chalk",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
@@ -8,8 +8,17 @@
8
8
  "url": "https://github.com/jchip/chalker.git"
9
9
  },
10
10
  "scripts": {
11
- "test": "clap test",
12
- "coverage": "clap check"
11
+ "test": "xrun --serial test:unit test:demo",
12
+ "test:unit": "vitest run",
13
+ "test:demo": "xrun --serial test:demo:legacy-cjs test:demo:ansi-colors-cjs test:demo:cjs-dynamic-import test:demo:esm-default test:demo:esm-namespace test:demo:esm-create-require test:demo:esm-named-export-probe",
14
+ "test:demo:legacy-cjs": "cd demo/legacy-cjs && fyn install && fyn test",
15
+ "test:demo:ansi-colors-cjs": "cd demo/ansi-colors-cjs && fyn install && fyn test",
16
+ "test:demo:cjs-dynamic-import": "cd demo/cjs-dynamic-import && fyn install && fyn test",
17
+ "test:demo:esm-default": "cd demo/esm-default && fyn install && fyn test",
18
+ "test:demo:esm-namespace": "cd demo/esm-namespace && fyn install && fyn test",
19
+ "test:demo:esm-create-require": "cd demo/esm-create-require && fyn install && fyn test",
20
+ "test:demo:esm-named-export-probe": "cd demo/esm-named-export-probe && fyn install && fyn test",
21
+ "coverage": "vitest run --coverage"
13
22
  },
14
23
  "files": [
15
24
  "lib"
@@ -27,30 +36,31 @@
27
36
  "author": "Joel Chen",
28
37
  "license": "Apache-2.0",
29
38
  "dependencies": {
30
- "chalk": "^2.4.2"
39
+ "color-convert": "^2.0.1",
40
+ "optional-require": "^1.1.8"
41
+ },
42
+ "peerDependencies": {
43
+ "ansi-colors": ">=4",
44
+ "chalk": ">=4"
45
+ },
46
+ "peerDependenciesMeta": {
47
+ "ansi-colors": {
48
+ "optional": true
49
+ },
50
+ "chalk": {
51
+ "optional": true
52
+ }
31
53
  },
32
54
  "devDependencies": {
33
- "electrode-archetype-njs-module-dev": "^3.0.0"
55
+ "@vitest/coverage-v8": "4.1.5",
56
+ "@xarc/run": "^2.3.0",
57
+ "ansi-colors": "^4.1.3",
58
+ "chalk": "^4.1.2",
59
+ "prettier": "^2.0.2",
60
+ "vitest": "4.1.5"
34
61
  },
35
- "nyc": {
36
- "all": true,
37
- "reporter": [
38
- "lcov",
39
- "text",
40
- "text-summary"
41
- ],
42
- "exclude": [
43
- "coverage",
44
- "*clap.js",
45
- "gulpfile.js",
46
- "dist",
47
- "test"
48
- ],
49
- "check-coverage": true,
50
- "statements": 100,
51
- "branches": 100,
52
- "functions": 100,
53
- "lines": 100,
54
- "cache": true
62
+ "prettier": {
63
+ "printWidth": 100,
64
+ "trailingComma": "none"
55
65
  }
56
66
  }