Learning and investigating how to use functions in TiddlyWiki 5.3.0

Playing with functions;

I am learning and investigating how to use functions in TiddlyWiki 5.3.0 as they stand to allow us to make our own prepared filters and filter operators.

This simple function allows us to pass a fieldname to have it converted to a unix TIMESTAMP

\function timestamp(fieldname:"modified")
[all[current]get<fieldname>format:date[TIMESTAMP]]
\end
\define unix-days() 86400000

It is then possible to get the days between two dates as follows;

{{{ [<timestamp>subtract<timestamp created>divide<unix-days>round[]] }}}

But now I wish to write and use a function if possible which I can pass two fieldnames and return the same result as above.

  • However I cant see how to do it.

Questions

  • Can you see how to do this?,
  • am I missing something?
  • or is there a limitation to the way we can combine custom operators with parameters?

Try this (I kept your original example to compare)

<!-- original code --->
\function timestamp(fieldname:"modified")
[all[current]get<fieldname>format:date[TIMESTAMP]]
\end

\define unix-days() 86400000

<!-- new solution -->

\function timestamp2(fld01:"created", fld02:"modified")
[all[current]get<fld01>format:date[TIMESTAMP]]
:map[<..currentTiddler>get<fld02>format:date[TIMESTAMP]subtract<currentTiddler>]
:and[divide[86400000]round[]]
\end

<!-- Example -->

<$tiddler tiddler="HelloThere">

* Original: {{{ [<timestamp>subtract<timestamp created>divide<unix-days>round[]] }}}

* New: <<timestamp2 created modified>>
</$tiddler>

Returns

  • Original: 3503
  • New: 3503
1 Like

Ahh, of course, use a filter run, particularly :map and :and to continue the filter.

  • I was hoping I could reuse the timestamp function in timestamp2, but with functions we can only reference parameters as variables.
  • perhaps if they are used as filter operators?

Because hours are more obvious in a test situation I used your method to create hours-between

\function hours-between(date-field1 date-field2)
[all[current]get<date-field1>format: date[TIMESTAMP]] 
:map[<..currentTiddler>get<date-field2>format: date[TIMESTAMP]subtract<currentTiddler>]
:and[divide[3600000]round[]]
\end

between <<hours-between created modified>>
  • This works

The following works but I am trying to see if I can use .timestamp in the MAP filter run. Perhaps it “.timestamp” could be written to use …currentTiddler else currentTiddler?

\function .timestamp(fieldname:"modified")
[all[current]get<fieldname>format:date[TIMESTAMP]]
\end
\function hours-between(date-field1 date-field2)
[.timestamp<date-field1>] 
:map[<..currentTiddler>.timestamp<date-field2>subtract<currentTiddler>]
:and[divide[3600000]round[]]
\end
<<hours-between created modified>>
  • Non working example above

If it makes sense, I am trying to drive simplicity through well written and reusable functions.

  • This goal is not particulary for this example but for other use cases.
1 Like

I liked too! I also prefer simple steps (mostly because to be understandable and maintainable)

I also think ..curentTiddler and currentTiddler may be confusing for newbies!

1 Like

Yes, perhaps if currentTiddler was called title, it would read a little more flexibly, especially when it may be a string that has no tiddler. ..currentTiddler is fairly meaningful when used on its own.

  • I will keep investigating.
  • I hope in the end to have some “code patterns” that are easier to understand and use as well as demonstrate to others.
2 Likes