String to Number with handlebar in JSReport

56 Views Asked by At

I am having a problem, and I cannot convert the strings (that correspond) into numbers with Handlebars

"this" is always String and object, even when it comes with a number (As seen in the image). What I want is that when it is an integer I convert it to a number and when it is a string I leave it original. Even when I create the report in xlsx, it doesn't do the correct conversion, because string element shows correctly but numbers doesn't see

{{#each this.properties}}
  {{#if (isNaN (convertToNumber this))}}
    <c t="inlineStr"><is><t>{{this}}</t></is></c>
  {{else}}
    <c t="n"><is><t>{{convertToNumber this}}</t></is></c>
  {{/if}}
{{/each}}

My handlebars functions:

Handlebars.registerHelper('isNumber', function(value) {
  return typeof value === 'number';
});

Handlebars.registerHelper('convertToNumber', function(value) {
  const numberString = value.replace(/'/g, '');
  const number = parseFloat(numberString);
  return isNaN(number) ? NaN : number;
});

Handlebars.registerHelper('isNaN', function(value) {
  return isNaN(value);
});

Debug example:

---Type object: ---
object
---THIS---
String {
  '0': 'T',
  '1': 'u',
  '2': 'n',
  '3': 'u',
  '4': 'y',
  '5': 'á',
  '6': 'n'
}
---Type object: ---
object
---THIS---
String {
  '0': '2',
  '1': '6',
  '2': '-',
  '3': '0',
  '4': '7',
  '5': '-',
  '6': '2',
  '7': '0',
  '8': '2',
  '9': '2',
  '10': ' ',
  '11': '1',
  '12': '7',
  '13': ':',
  '14': '2',
  '15': '9'
}

---Type object: ---
object
---THIS---
String { '0': '1', '1': '3', '2': '.', '3': '0', '4': '2' }

---Type object: ---
object
---THIS---
String { '0': '1', '1': '3', '2': '.', '3': '5', '4': '4' }
0

There are 0 best solutions below