MCPcopy Index your code
hub / github.com/darkreader/darkreader / getNumbersFromString

Function getNumbersFromString

src/utils/color.ts:235–282  ·  view source on GitHub ↗
(input: string, range: number[], units: {[unit: string]: number})

Source from the content-addressed store, hash-verified

233const C_PERCENT = '%'.charCodeAt(0);
234
235function getNumbersFromString(input: string, range: number[], units: {[unit: string]: number}) {
236 const numbers: number[] = [];
237 const searchStart = input.indexOf('(') + 1;
238 const searchEnd = input.length - 1;
239 let numStart = -1;
240 let unitStart = -1;
241
242 const push = (matchEnd: number) => {
243 const numEnd = unitStart > -1 ? unitStart : matchEnd;
244 const $num = input.slice(numStart, numEnd);
245 let n = parseFloat($num);
246 const r = range[numbers.length];
247 if (unitStart > -1) {
248 const unit = input.slice(unitStart, matchEnd);
249 const u = units[unit];
250 if (u != null) {
251 n *= r / u;
252 }
253 }
254 if (r > 1) {
255 n = Math.round(n);
256 }
257 numbers.push(n);
258 numStart = -1;
259 unitStart = -1;
260 };
261
262 for (let i = searchStart; i < searchEnd; i++) {
263 const c = input.charCodeAt(i);
264 const isNumChar = (c >= C_0 && c <= C_9) || c === C_DOT || c === C_PLUS || c === C_MINUS || c === C_e;
265 const isDelimiter = c === C_SPACE || c === C_COMMA || c === C_SLASH;
266 if (isNumChar) {
267 if (numStart === -1) {
268 numStart = i;
269 }
270 } else if (numStart > -1) {
271 if (isDelimiter) {
272 push(i);
273 } else if (unitStart === -1) {
274 unitStart = i;
275 }
276 }
277 }
278 if (numStart > -1) {
279 push(searchEnd);
280 }
281 return numbers;
282}
283
284const rgbRange = [255, 255, 255, 1];
285const rgbUnits = {'%': 100};

Callers 2

parseRGBFunction · 0.85
parseHSLFunction · 0.85

Calls 1

pushFunction · 0.70

Tested by

no test coverage detected