Page 1 of 1

Learn how {expressions} work and useful Helper Functions

Posted: 13 Aug 2014, 09:50
by rednoah
If you're using format expression like {director} or {vc} you may have noticed that if a variable is not defined, it will cause the entire {expression} to fail and unwind.

The Unwind-on-Undefined Behaviour:

A common issue when writing expressions like this [{vc}, {ac}] is that it might evaluate to [, ] if the required bindings are undefined. A better way would be to write {"$vc, $ac"} (as String) or {[vc, ac]} (as List) in which case this single expression will only work if all required bindings are defined.

This expression will either evaluate successfully as a single unit, or fail as a single unit:

Code: Select all

{"A ${director} Movie"}
So if the binding is undefined, it would be evaluated like this and fail with an Exception:

Code: Select all

{"A ${throw new Exception()} Movie"}
Alternatively, if you access properties via the {self} binding you will not get this unwind-on-undefined behavior.

Code: Select all

{"A ${self.director} Movie"}
Which would evaluate like this if undefined:

Code: Select all

{"A ${null} Movie"} // A null Movie
This is useful if you want to check properties with an if-then-else statement:

Code: Select all

{"A ${self.director ?: 'No Name'} Movie"}

Helper Functions:
FileBot adds a few simple functions that can make your code much more easy to read if you need to collect the values of multiple bindings/expressions that may or may not fail and unwind.

Find and return the value of the first expression that evaluates successfully without returning null or throwing an Exception. If none of the expressions evaluates successfully null will be returned.

e.g. Take collection, or if collection is undefined take the first genre, or if no genres are defined use a fixed String value:

Code: Select all

{ any{ collection }{ genre }{ 'No collection or Genre' } }
Find and return all values of all the expressions that evaluate successfully and ignore all expressions that fail or unwind.

e.g. Take the values of multiple bindings and simply ignore those that are undefined:

Code: Select all

{ allOf{ fn.match(/Extended.Edition/) }{ vc }{ ac }{ source }{ group }.join('.')
Check if each expression yields a negative result of fails to yield a result altogether.

e.g. Return true if none of the given expressions yield a positive result:

Code: Select all

none{ ext =~ /jpg|png/ }{ fn.match(/sample|trailer/) }
@see ExpressionFormatFunctions

Real-World Examples:

e.g. combine binding variable and separator literals into a single {expression} so that you get either all or nothing (e.g. ".10bit" or ".HDR" but never just ".") and don't end up with superfluous separator literals:

Code: Select all

e.g. guess source tag based on video resolution if {vs} video source binding is undefined:

Code: Select all

{ any{ vs }{ width >= 1280 ? 'BluRay' : 'DVD' } }

Reference Documentation: