Creating an is-integer function?

I’m afraid I am stumped. I tried to create a function that filters non-integer tiddlers, but it did not work. What did I do wrong?

\function is-integer()
[regexp[^\d+$]]
\end

The name for a custom function used like this inside a filter must include a period (.).

So this should work:

\function is.integer()
[regexp[^\d+$]]
\end

<<list-links filter:"[[42]] [[abc]] [[99]] [[3.141592]] :filter[is.integer[]]">>

Yields 42 and 99

is-integer.json (239 Bytes)

Note the bit I’ve highlighted in Functions:

  • Directly invoke functions whose names contain a period as custom filter operators with the syntax [my.fn[value]] or [.myfn[value]]
1 Like

Might not apply in your case, but if we want to handle integers that could have plus or minus signs:

\define isInteger() ^[+-]?[\d]+$

<<list-links filter:"[[42]] [[abc]] [[99]] [[-4]] [[+2]] [[3.141592]] +[regexp<isInteger>]">>

BTW, don’t mind me, I’ve stayed with tw5.2.3, so I still do things via macros. My sample code is just to sanity test the regular expression.

1 Like