chalk/source/utilities.js

34 lines
983 B
JavaScript
Raw Normal View History

2021-11-26 16:50:15 +07:00
// TODO: When targeting Node.js 16, use `String.prototype.replaceAll`.
2021-04-16 15:23:29 +07:00
export function stringReplaceAll(string, substring, replacer) {
2019-07-12 09:40:23 +03:00
let index = string.indexOf(substring);
if (index === -1) {
return string;
}
2019-07-12 13:59:50 +07:00
const substringLength = substring.length;
let endIndex = 0;
let returnValue = '';
2019-07-12 09:40:23 +03:00
do {
returnValue += string.slice(endIndex, index) + substring + replacer;
2019-07-12 13:59:50 +07:00
endIndex = index + substringLength;
index = string.indexOf(substring, endIndex);
2019-07-12 09:40:23 +03:00
} while (index !== -1);
2021-04-16 15:23:29 +07:00
returnValue += string.slice(endIndex);
2019-07-12 13:59:50 +07:00
return returnValue;
2021-04-16 15:23:29 +07:00
}
2019-07-12 09:40:23 +03:00
2021-04-16 15:23:29 +07:00
export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
2019-07-12 13:59:50 +07:00
let endIndex = 0;
Fix security and performance issues across core, vendor, and examples Security: - S2: hexToRgb — explicit typeof branch + padStart(6) for numeric hex inputs; makes the numeric-input path intentional and preserves leading zeros - S3: FORCE_COLOR parsing — guard against NaN propagation when env value is non-numeric (e.g. FORCE_COLOR=yes now correctly falls back to level 1) - S4: _supportsColor — remove side-effecting mutation of module-level flagForceColor; effective value is now computed locally, eliminating cross-call state corruption in test environments - S5: applyOptions — change `options.level &&` to `options.level !== undefined` so null and NaN are properly rejected instead of silently stored as the level - S6: browser.js — explicit Number(brand.version) > 93 instead of implicit string-to-number coercion for Chromium UA version check Performance / correctness: - P1: rainbow.js — replace stateful global-regex test() in loop (which misclassified every other non-printable character due to lastIndex advancing) with a direct code-point comparison: character < '!' || character > '~' - P4: stringEncaseCRLFWithFirstIndex — switch from += string concatenation in loop to array-of-parts + single join(), reducing intermediate allocations for multi-line strings - P6: builder — detect tagged template literal calls via .raw property and route through String.raw(), so chalk.red`hello ${name}` now produces correct output Tests: - instance.js: new Chalk({level: null/NaN}) now throws (S5 regression test) - chalk.js: numeric hex with leading zeros (S2), template literal interpolations (P6) covered by new tests; all 35 tests pass Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:21:53 +05:30
const parts = [];
2019-07-12 09:40:23 +03:00
do {
const gotCR = string[index - 1] === '\r';
Fix security and performance issues across core, vendor, and examples Security: - S2: hexToRgb — explicit typeof branch + padStart(6) for numeric hex inputs; makes the numeric-input path intentional and preserves leading zeros - S3: FORCE_COLOR parsing — guard against NaN propagation when env value is non-numeric (e.g. FORCE_COLOR=yes now correctly falls back to level 1) - S4: _supportsColor — remove side-effecting mutation of module-level flagForceColor; effective value is now computed locally, eliminating cross-call state corruption in test environments - S5: applyOptions — change `options.level &&` to `options.level !== undefined` so null and NaN are properly rejected instead of silently stored as the level - S6: browser.js — explicit Number(brand.version) > 93 instead of implicit string-to-number coercion for Chromium UA version check Performance / correctness: - P1: rainbow.js — replace stateful global-regex test() in loop (which misclassified every other non-printable character due to lastIndex advancing) with a direct code-point comparison: character < '!' || character > '~' - P4: stringEncaseCRLFWithFirstIndex — switch from += string concatenation in loop to array-of-parts + single join(), reducing intermediate allocations for multi-line strings - P6: builder — detect tagged template literal calls via .raw property and route through String.raw(), so chalk.red`hello ${name}` now produces correct output Tests: - instance.js: new Chalk({level: null/NaN}) now throws (S5 regression test) - chalk.js: numeric hex with leading zeros (S2), template literal interpolations (P6) covered by new tests; all 35 tests pass Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:21:53 +05:30
parts.push(string.slice(endIndex, gotCR ? index - 1 : index), prefix, gotCR ? '\r\n' : '\n', postfix);
2019-07-12 13:59:50 +07:00
endIndex = index + 1;
index = string.indexOf('\n', endIndex);
2019-07-12 09:40:23 +03:00
} while (index !== -1);
Fix security and performance issues across core, vendor, and examples Security: - S2: hexToRgb — explicit typeof branch + padStart(6) for numeric hex inputs; makes the numeric-input path intentional and preserves leading zeros - S3: FORCE_COLOR parsing — guard against NaN propagation when env value is non-numeric (e.g. FORCE_COLOR=yes now correctly falls back to level 1) - S4: _supportsColor — remove side-effecting mutation of module-level flagForceColor; effective value is now computed locally, eliminating cross-call state corruption in test environments - S5: applyOptions — change `options.level &&` to `options.level !== undefined` so null and NaN are properly rejected instead of silently stored as the level - S6: browser.js — explicit Number(brand.version) > 93 instead of implicit string-to-number coercion for Chromium UA version check Performance / correctness: - P1: rainbow.js — replace stateful global-regex test() in loop (which misclassified every other non-printable character due to lastIndex advancing) with a direct code-point comparison: character < '!' || character > '~' - P4: stringEncaseCRLFWithFirstIndex — switch from += string concatenation in loop to array-of-parts + single join(), reducing intermediate allocations for multi-line strings - P6: builder — detect tagged template literal calls via .raw property and route through String.raw(), so chalk.red`hello ${name}` now produces correct output Tests: - instance.js: new Chalk({level: null/NaN}) now throws (S5 regression test) - chalk.js: numeric hex with leading zeros (S2), template literal interpolations (P6) covered by new tests; all 35 tests pass Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-21 17:21:53 +05:30
parts.push(string.slice(endIndex));
return parts.join('');
2021-04-16 15:23:29 +07:00
}