saasufy-components 7.0.2 → 7.0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/utils.js +39 -23
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saasufy-components",
3
- "version": "7.0.2",
3
+ "version": "7.0.3",
4
4
  "description": "Components for Saasufy.",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utils.js CHANGED
@@ -273,14 +273,17 @@ function* selectExpressions(string) {
273
273
  let isCapturing = false;
274
274
  let isTripleBracket = false;
275
275
  let captureList = [];
276
+ let captureStartIndex = 0;
276
277
  for (let i = 1; i < charList.length; i++) {
277
278
  if (charList[i - 1] === '{' && charList[i] === '{') {
278
279
  captureList = [];
279
280
  captureList.push('{');
280
281
  isCapturing = true;
282
+ captureStartIndex = i - 1;
281
283
  isTripleBracket = charList[i - 2] === '{';
282
284
  if (isTripleBracket) {
283
285
  captureList.push('{');
286
+ captureStartIndex--;
284
287
  }
285
288
  }
286
289
  if (isCapturing) {
@@ -288,10 +291,16 @@ function* selectExpressions(string) {
288
291
  } else if (captureList.length) {
289
292
  captureList.push('}');
290
293
  captureList.push('}');
294
+ let closeBrackets = 2;
291
295
  if (isTripleBracket) {
292
296
  captureList.push('}');
297
+ closeBrackets++;
293
298
  }
294
- yield captureList.join('');
299
+ yield {
300
+ startIndex: captureStartIndex,
301
+ length: (i + closeBrackets) - captureStartIndex,
302
+ value: captureList.join('')
303
+ };
295
304
  captureList = [];
296
305
  }
297
306
  if (
@@ -303,37 +312,44 @@ function* selectExpressions(string) {
303
312
  }
304
313
  }
305
314
 
315
+ function replaceExpressions(string, replaceFn) {
316
+ let charList = string.split('');
317
+ let matches = [ ...selectExpressions(string) ];
318
+ let charSubs = matches.map(item => String(replaceFn(item.value) ?? '').split(''));
319
+ for (let i = matches.length - 1; i >= 0; i--) {
320
+ let match = matches[i];
321
+ charList.splice(match.startIndex, match.length, ...charSubs[i]);
322
+ }
323
+ return charList.join('');
324
+ }
325
+
306
326
  export function renderTemplate(templateString, data, socket) {
307
- let expressionIterator = selectExpressions(templateString);
308
327
  let options = getRenderOptions(data, socket);
309
- for (let expression of expressionIterator) {
310
- let computedValue;
311
- if (expression.match(templateTripleTagsRegExp)) {
312
- let expString = expression.slice(3, -3);
328
+ return replaceExpressions(templateString, (expression) => {
329
+ let expString;
330
+ if (expression.startsWith('{{{') && expression.endsWith('}}}')) {
331
+ expString = expression.slice(3, -3);
313
332
  try {
314
- computedValue = execExpression(
333
+ return execExpression(
315
334
  toExpression(expString),
316
335
  options
317
336
  );
318
337
  } catch (error) {
319
- computedValue = expression;
320
- }
321
- } else {
322
- let expString = expression.slice(2, -2);
323
- try {
324
- computedValue = toSafeHTML(
325
- execExpression(
326
- toExpression(expString),
327
- options
328
- )
329
- );
330
- } catch (error) {
331
- computedValue = expression;
338
+ return expression;
332
339
  }
333
340
  }
334
- templateString = templateString.replace(expression, computedValue);
335
- }
336
- return templateString;
341
+ expString = expression.slice(2, -2);
342
+ try {
343
+ return toSafeHTML(
344
+ execExpression(
345
+ toExpression(expString),
346
+ options
347
+ )
348
+ );
349
+ } catch (error) {
350
+ return expression;
351
+ }
352
+ });
337
353
  }
338
354
 
339
355
  export function updateConsumerElements(consumers, value, template, sourceElementName, outputType) {