1
votes

I have tried implementing a piece of simple code as a script in google docs. It's function is validating ID numbers. The problem is that the syntax used for this code is too advanced for the google docs script - it uses the arrow function which is as fas I as I understand not being supported by google docs. Trying to rewrite the code just didn't work - I am quite a beginner and while knowing how to read code adapting one is much more complicated for me. Can anyone suggest how to change back the arrow function into a seperate function?

here is the code:

function isValidIsraeliID(id) {
    var id = String(id).trim();
    if (id.length > 9 || id.length < 5 || isNaN(id)) return false;

    // Pad string with zeros up to 9 digits
    id = id.length < 9 ? ("00000000" + id).slice(-9) : id;

    return Array.from(id, Number)
            .reduce((counter, digit, i) => {
            const step = digit * ((i % 2) + 1);
            return counter + (step > 9 ? step - 9 : step);
                }) % 10 === 0;
}

I believe the line causing the problem is this one:

  .reduce((counter, digit, i) => {

But I might as well be wrong...

Thanks!

2
...Have you tried using a standard function instead of an arrow function? - CertainPerformance

2 Answers

1
votes

Edit: Arrow functions are currently supported with the V8 engine. See: https://developers.google.com/apps-script/guides/v8-runtime#arrow_functions

Google Apps Script (GAS) doesn't support ECMAScript 2015 (ES6) yet. Unfortunately, in the current stage, the functions which was added from ES6 cannot be used. So it is required to convert such functions for GAS. In your script, also such functions are used. So how about this modification?

Modification points :

  • Arrow function cannot be used at GAS.
    • This was mentioned by CertainPerformance and NielsNet.
  • Array.from() cannot be used at GAS.
    • Here, I used Array.map().

Modified script :

function isValidIsraeliID(id) {
  var id = String(id).trim();
  if (id.length > 9 || id.length < 5 || isNaN(id)) return false;

  // Pad string with zeros up to 9 digits
  id = id.length < 9 ? ("00000000" + id).slice(-9) : id;

  return Array.map(id, Number) // Modified
  .reduce(function(counter, digit, i) { // Modified
    const step = digit * ((i % 2) + 1);
    return counter + (step > 9 ? step - 9 : step);
  }) % 10 === 0;
}

References :

If this result was not what you want, please tell me. I would like to modify it.

0
votes

Just replace the arrow function with a function:

.reduce(function(counter, digit, i) {