0

The following code examines the content of inputData.body for a 32-character string match, and - to the best of my understanding - places any matches in an array.

// stores an array of any length (0 or more) with the matches
var matches = inputData.body.match(/\b[\w-]{32}\b/g)

// the .map function executes the nameless inner function once for each element of the array and returns a new array with the results
return matches.map(function (m) { return {str: m} })

I now need the code to return something in the case of no matched expressions, eg. the string "false".

I wasn't able to get this addition to work...

// stores an array of any length (0 or more) with the matches
var matches = inputData.body.match(/\b[\w-]{32}\b/g)

if (matches == null){
    return 'false'
}

// the .map function executes the nameless inner function once for each element of the array and returns a new array with the results
return matches.map(function (m) { return {str: m} })

How should I go about conditionally returning something in the event of emptiness?

Robert Andrews
  • 1,209
  • 4
  • 23
  • 47

3 Answers3

0

The caller expects an array of objects, or a single object (presumably treated like an array of that one object). So return a single object.

if (matches == null) {
    return { str: "false"; }
}
return matches.map(function (m) { return {str: m} });

or in a single statement:

return matches == null ? { str: "false"; } : matches.map(function (m) { return {str: m} });
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Noting all the above. I need some affirmative output, rather than to fill an empty anything. Where you fill `str` with `false` above seems to do that, and gives me the ability to test for the word `false` in a subsequent step. I know that's not pretty, but what I need. I imagine I need to conditionalise this, though.. ? `if (matches == null){ return {str: 'false'}; } else { return matches.map(function (m) { return {str: m} }) }` ? – Robert Andrews Oct 06 '17 at 21:39
  • You don't need `else` if the `if` performs a `return`. – Barmar Oct 06 '17 at 21:40
  • 1
    I looked in the documentation, and it seems to say that empty arrays are OK. **If Code by Zapier is the Zap's trigger and an empty array is returned, nothing happens.** – Barmar Oct 06 '17 at 21:42
  • 1
    **If Code by Zapier is the Zap's trigger and you return an empty array [], we will not trigger any actions downstream — it is as if you said "nevermind" in code.** – Barmar Oct 06 '17 at 21:42
  • Where no matching regular expressions are found, the zap's Code action is throwing "This Code step hit an error" in Task History. Support says: "Right now we don't have a great way around this, since an error message indicates to us that something is wrong. Any chance you can change the code to output something like "no" or "false" if nothing is found. That way it does succeed every time, and then your Filter can be 'does not contain whatever-word-you-chose'. I know it's not perfect [...] but it's a way to ensure that we don't get an error on that step." – Robert Andrews Oct 06 '17 at 21:53
  • To test an empty array, instead of Support's advised method, I don't understand how to conditionally output `return []` if null, but the Str array otherwise (if not with `if`/`else`). Put the positive/negative one on top of the other and everything will be okay, or... ? – Robert Andrews Oct 06 '17 at 21:55
  • The method in the answer (filling `str` with `false`) works, which I can happily mark. But this method is not the one you also later advocate, in line with the Zapier documentation you found, which suggests an empty array should work fine. That solution seems optimal, in line with the docs, which the Support operative may have missed. So, rather return `str: false`, how would I return an empty array - simply swap `return { str: "false"; }` for `return []`, right? – Robert Andrews Oct 06 '17 at 22:06
  • I don't know anything about Zapier. You said that returning an empty array doesn't work, so I discarded that answer. – Barmar Oct 06 '17 at 22:07
  • I was going partly with Zapier Support's advice. Your answer here works, I'm marking it as correct. But I now rather suspect the extra info you supplied *also* works. I have tested it in the zap and it is not producing an error when regex is not matched. I haven't tested live yet, but I will update the thread when I have both questioned this with Zapier and when I see a live test. – Robert Andrews Oct 06 '17 at 22:13
  • Actually, the answer code seems to produce "false" every time, whether an expression is there or empty. Frustrating! – Robert Andrews Oct 07 '17 at 07:11
  • Are you sure there's a match? What does `console.log(matches)` show? – Barmar Oct 07 '17 at 07:36
0

The caller probably expects an array, which should be an empty one if there were no matches. You don't need the if statement, just do this:

return (matches || []).map(function (m) { return {str: m} })
Lennholm
  • 7,205
  • 1
  • 21
  • 30
0

To test if a regEx matches a specific pattern you should use the test() method which returns true/false.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Osvaldo Maria
  • 340
  • 5
  • 14