MCPcopy Create free account
hub / github.com/callstack/linaria / substitutePlaceholders

Function substitutePlaceholders

packages/postcss-linaria/src/stringify.ts:16–54  ·  view source on GitHub ↗
(
  stringWithPlaceholders: string,
  expressions: string[]
)

Source from the content-addressed store, hash-verified

14import { placeholderText } from './util';
15
16const substitutePlaceholders = (
17 stringWithPlaceholders: string,
18 expressions: string[]
19) => {
20 if (!stringWithPlaceholders.includes(placeholderText) || !expressions) {
21 return stringWithPlaceholders;
22 }
23
24 const values = stringWithPlaceholders.split(' ');
25 const temp: string[] = [];
26 values.forEach((val) => {
27 let [prefix, expressionIndexString] = val.split(placeholderText);
28 prefix = prefix.replace(/(\.|--|\/\*)$/, '');
29 // if the val is 'pcss-lin10px', need to remove the px to get the placeholder number
30 let suffix = '';
31 while (
32 Number.isNaN(Number(expressionIndexString)) &&
33 expressionIndexString &&
34 expressionIndexString.length > 0
35 ) {
36 suffix = expressionIndexString[expressionIndexString.length - 1] + suffix;
37 expressionIndexString = expressionIndexString.slice(
38 0,
39 expressionIndexString.length - 1
40 );
41 }
42 const expressionIndex = Number(expressionIndexString);
43 const expression =
44 expressions &&
45 !Number.isNaN(expressionIndex) &&
46 expressions[expressionIndex];
47 if (expression) {
48 temp.push(prefix + expression + suffix);
49 } else {
50 temp.push(val);
51 }
52 });
53 return temp.join(' ');
54};
55
56/**
57 * Stringifies PostCSS nodes while taking interpolated expressions

Callers 3

atruleMethod · 0.85
declMethod · 0.85
ruleMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…