chalker 1.2.0 → 1.3.1

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 +23 -2
  2. package/lib/index.js +108 -13
  3. package/package.json +33 -28
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  # chalker
5
5
 
6
- Set ansi colors in strings using `<>` 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,6 +70,9 @@ 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 |
@@ -105,6 +116,15 @@ chalker(str, [chalkInstance]);
105
116
 
106
117
  > If `chalk.supportsColor` is `false`, then it will simply remove the `<>` markers and decode HTML entities only.
107
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].
127
+
108
128
  ### `chalker.remove`
109
129
 
110
130
  ```js
@@ -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,102 @@ const htmlEntities = {
62
64
  [`&reg;`]: "\xae"
63
65
  };
64
66
 
67
+ function loadColors() {
68
+ let colors = optionalRequire("chalk") || optionalRequire("ansi-colors");
69
+
70
+ if (!colors) {
71
+ // just go for chalk and let require take care of errors
72
+ colors = require("chalk");
73
+ // throw new Error("chalker requires either chalk or ansi-colors to be installed");
74
+ }
75
+
76
+ return normalizeColors(colors);
77
+ }
78
+
79
+ function normalizeColors(colorsModule) {
80
+ const colors =
81
+ colorsModule &&
82
+ colorsModule.default &&
83
+ (typeof colorsModule.default === "function" || typeof colorsModule.default === "object")
84
+ ? colorsModule.default
85
+ : colorsModule;
86
+
87
+ return addAnsiColorsCompat(colors);
88
+ }
89
+
90
+ function addAnsiColorsCompat(colors) {
91
+ if (!colors || typeof colors.alias !== "function" || typeof colors.rgb === "function") {
92
+ return colors;
93
+ }
94
+
95
+ colors.rgb = function (r, g, b) {
96
+ return this[defineAnsiColor(colors, "rgb", [r, g, b])];
97
+ };
98
+ colors.bgRgb = function (r, g, b) {
99
+ return this[defineAnsiColor(colors, "bgRgb", [r, g, b], true)];
100
+ };
101
+ colors.hex = function (value) {
102
+ return this.rgb.apply(this, colorConvert.hex.rgb(value));
103
+ };
104
+ colors.bgHex = function (value) {
105
+ return this.bgRgb.apply(this, colorConvert.hex.rgb(value));
106
+ };
107
+
108
+ return colors;
109
+ }
110
+
111
+ function defineAnsiColor(colors, name, values, bg) {
112
+ const styleName = `chalker_${name}_${values.join("_")}`;
113
+
114
+ if (!colors.styles[styleName]) {
115
+ const prefix = bg ? 48 : 38;
116
+ const close = bg ? 49 : 39;
117
+ colors.alias(
118
+ styleName,
119
+ (text) => `\u001b[${prefix};2;${values.join(";")}m${text}\u001b[${close}m`
120
+ );
121
+ }
122
+
123
+ return styleName;
124
+ }
125
+
126
+ function convertColorModel(name, values) {
127
+ const bg = name.startsWith("bg");
128
+ const model = bg ? name[2].toLowerCase() + name.substring(3) : name;
129
+ const rgb =
130
+ model === "keyword"
131
+ ? colorConvert.keyword.rgb(String(values[0]).toLowerCase())
132
+ : colorConvert[model] && colorConvert[model].rgb.apply(colorConvert[model], values);
133
+
134
+ assert(rgb, `unknown color ${values[0]}`);
135
+
136
+ return { name: bg ? "bgRgb" : "rgb", values: rgb };
137
+ }
138
+
139
+ function applyChalkMethod(chalkInstance, name, values) {
140
+ if (typeof chalkInstance[name] === "function") {
141
+ return chalkInstance[name].apply(chalkInstance, values);
142
+ }
143
+
144
+ if (
145
+ name === "keyword" ||
146
+ name === "bgKeyword" ||
147
+ name === "hsl" ||
148
+ name === "bgHsl" ||
149
+ name === "hsv" ||
150
+ name === "bgHsv" ||
151
+ name === "hwb" ||
152
+ name === "bgHwb"
153
+ ) {
154
+ const converted = convertColorModel(name, values);
155
+ return chalkInstance[converted.name].apply(chalkInstance, converted.values);
156
+ }
157
+
158
+ throw new TypeError(`${name} is not a chalk function`);
159
+ }
160
+
65
161
  function decodeHtml(str) {
66
- return str.replace(/&[\w#]+;/g, m => {
162
+ return str.replace(/&[\w#]+;/g, (m) => {
67
163
  if (htmlEntities.hasOwnProperty(m)) return htmlEntities[m];
68
164
  if (m.startsWith("&#x")) {
69
165
  const s = m.substring(3, m.length - 1);
@@ -112,7 +208,7 @@ function applyChalkMarkers(markers, text, userChalk) {
112
208
  let values = marker.substring(openIx + 1, closeIx).trim();
113
209
  if (values.indexOf(",") >= 0) {
114
210
  // extract rgb/hsl/hsv/hwb values like (255, 10, 20)
115
- values = values.split(",").map(x => parseInt(x.trim(), 10));
211
+ values = values.split(",").map((x) => parseInt(x.trim(), 10));
116
212
 
117
213
  // default no name to rgb, and bg to bgRgb
118
214
  if (!name) name = "rgb";
@@ -127,23 +223,20 @@ function applyChalkMarkers(markers, text, userChalk) {
127
223
  }
128
224
 
129
225
  try {
130
- a = a[name].apply(a, values);
226
+ a = applyChalkMethod(a, name, values);
131
227
  } 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}`);
228
+ throw new Error(
229
+ `marker ${marker} is invalid: calling chalk.${name} failed with: ${err.message}`
230
+ );
138
231
  }
139
232
  } else {
140
233
  // if not found as a basic color, then try with chalk.keyword or chalk.bgKeyword
141
234
  try {
142
235
  const kw = deQuote(marker, marker);
143
236
  if (kw.startsWith("bg-") || kw.startsWith("bg ")) {
144
- a = a.bgKeyword(kw.substring(3));
237
+ a = applyChalkMethod(a, "bgKeyword", [kw.substring(3)]);
145
238
  } else {
146
- a = a.keyword(kw);
239
+ a = applyChalkMethod(a, "keyword", [kw]);
147
240
  }
148
241
  } catch (err) {
149
242
  throw new Error(`marker ${marker} is not found and invalid as a keyword`);
@@ -170,7 +263,7 @@ function remove(s, keepHtml) {
170
263
  }
171
264
 
172
265
  function format(s, userChalk) {
173
- userChalk = userChalk || chalk;
266
+ userChalk = normalizeColors(userChalk || chalker.CHALK);
174
267
 
175
268
  // skip applying ansi colors if chalk says color support is off
176
269
  if (userChalk.supportsColor === false) {
@@ -251,4 +344,6 @@ chalker.remove = remove;
251
344
 
252
345
  chalker.decodeHtml = decodeHtml;
253
346
 
347
+ chalker.CHALK = chalk;
348
+
254
349
  module.exports = chalker;
package/package.json CHANGED
@@ -1,15 +1,24 @@
1
1
  {
2
2
  "name": "chalker",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Set ansi colors in strings using XML and chalk",
5
5
  "main": "lib/index.js",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "https://github.com/jchip/chalker.git"
8
+ "url": "git+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,32 +36,28 @@
27
36
  "author": "Joel Chen",
28
37
  "license": "Apache-2.0",
29
38
  "dependencies": {
30
- "chalk": "^4.0.0"
39
+ "color-convert": "^2.0.1",
40
+ "optional-require": "^1.1.8"
31
41
  },
32
- "devDependencies": {
33
- "electrode-archetype-njs-module-dev": "^3.0.0",
34
- "prettier": "^2.0.2"
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
+ }
35
53
  },
36
- "nyc": {
37
- "all": true,
38
- "reporter": [
39
- "lcov",
40
- "text",
41
- "text-summary"
42
- ],
43
- "exclude": [
44
- "coverage",
45
- "*clap.js",
46
- "gulpfile.js",
47
- "dist",
48
- "test"
49
- ],
50
- "check-coverage": true,
51
- "statements": 100,
52
- "branches": 100,
53
- "functions": 100,
54
- "lines": 100,
55
- "cache": true
54
+ "devDependencies": {
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"
56
61
  },
57
62
  "prettier": {
58
63
  "printWidth": 100,