@peteanderson/ansi 0.3.2 → 0.3.3

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/dist/sgr/sgr.js CHANGED
@@ -34,7 +34,7 @@ function sgr(open, close, reset = false) {
34
34
  const rxClose = new RegExp(`(?<start>\\x1b\\[(?:\\d+;)*)${closeCode}(?<end>(?:;\\d+)*m)`, "g");
35
35
  const replace = `$<start>${reopenCode}$<end>`;
36
36
  /** @type {(text: string) => string} */
37
- const func = s => openSequence + s.replace(rxClose, replace) + closeSequence;
37
+ const func = s => s ? openSequence + s.replace(rxClose, replace) + closeSequence : s;
38
38
  return Object.assign(func, {
39
39
  codes: { open, close },
40
40
  open: openSequence,
@@ -21,8 +21,17 @@ function buildIntensityAtom(attribute, codes, index, state) {
21
21
  if (code === 22)
22
22
  return { attribute, code: [code] };
23
23
  const current = state.get("intensity");
24
- if (!current || current.length == 1 && current[0] === code || current[0] === 22)
24
+ if (
25
+ // If there is no intensity currently in the state, then we can just set the new state.
26
+ !current ||
27
+ // If the current intensity is normal (22), then we can just set the new state.
28
+ // Note that while the ultimate transition may return a code sequence of `[22, 1]` or
29
+ // `[22, 2]`, in our state representation, the only two-code intensity is `[1, 2]`.
30
+ current[0] === 22 ||
31
+ // If the current intensity is equal to the new intensity, then return the same state.
32
+ current.length === 1 && current[0] === code)
25
33
  return { attribute, code: [code] };
34
+ // Otherwise, we need to set both intensity codes.
26
35
  return { attribute, code: [1, 2] };
27
36
  }
28
37
  module.exports = { buildExtendedColorAtom, buildIntensityAtom };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name" : "@peteanderson/ansi",
3
- "version" : "0.3.2",
3
+ "version" : "0.3.3",
4
4
  "description" : "ANSI escape sequence generation with automatic feature detection",
5
5
  "author" : {
6
6
  "name" : "Pete Anderson",
package/src/sgr/sgr.js CHANGED
@@ -46,7 +46,7 @@ function sgr(open, close, reset = false) {
46
46
  const replace = `$<start>${reopenCode}$<end>`;
47
47
 
48
48
  /** @type {(text: string) => string} */
49
- const func = s => openSequence + s.replace(rxClose, replace) + closeSequence;
49
+ const func = s => s ? openSequence + s.replace(rxClose, replace) + closeSequence : s;
50
50
 
51
51
  return Object.assign(func, {
52
52
  codes : {open, close},
@@ -25,9 +25,19 @@ function buildIntensityAtom(attribute, codes, index, state) {
25
25
  return {attribute, code: [code]};
26
26
 
27
27
  const current = state.get("intensity");
28
- if (!current || current.length == 1 && current[0] === code || current[0] === 22)
28
+ if (
29
+ // If there is no intensity currently in the state, then we can just set the new state.
30
+ !current ||
31
+ // If the current intensity is normal (22), then we can just set the new state.
32
+ // Note that while the ultimate transition may return a code sequence of `[22, 1]` or
33
+ // `[22, 2]`, in our state representation, the only two-code intensity is `[1, 2]`.
34
+ current[0] === 22 ||
35
+ // If the current intensity is equal to the new intensity, then return the same state.
36
+ current.length === 1 && current[0] === code
37
+ )
29
38
  return {attribute, code: [code]};
30
39
 
40
+ // Otherwise, we need to set both intensity codes.
31
41
  return {attribute, code: [1, 2]};
32
42
  }
33
43