{iftype:haystack:test1:text1:...:else_text}

Description

Classifies a string by testing its type and returns the text for the first type that matches. The first parameter is the value to classify (the haystack); after it come type:text pairs tested left to right - the first matching type wins and its text is returned. A final unpaired parameter is the else text, returned when no type matched (omit it to return nothing). Supported types: longid (a 32-character hexadecimal item id), shortid (a positive whole number under 12 digits, used as a short item id), mail (a valid e-mail address), signed (any whole number, negatives allowed), number (a whole number of zero or more, no sign), positive (a whole number greater than zero). An unknown type name never matches. Inside any returned text _#1 is replaced by the tested value and _#2 by the name of the type that matched. Because 0 is a number but neither shortid nor positive, and a negative like -5 is signed but not number, list the most specific type first.

Parameters

haystack required

The value to classify. Its type is what the command tests.

test1 required

A type name to test the haystack against. The first type that matches wins. One of: longid, shortid, mail, signed, number, positive. An unknown name never matches.

text1 required

The text returned when test1 matches the haystack. Inside it 6ca347b888a4f0f6d7e24630c02b84af-6a71913645b5c3e0fffc3c2c18410777-6683747624dab8ed126108b7ddfa298b-014c3ec32f1026ba7ffc307db053f63a-a50994eed499d59c91056f08f4b9754f stands for the tested value and for the matched type name.

... optional

More type:text pairs (test2:text2, test3:text3, ...), tested left to right until one matches. Optional.

else_text optional

Text returned when no type matched. Optional - omit it to return an empty string.

Examples

test{iftype:d41d8cd98f00b204e9800998ecf8427e:longid:valid long id:not a long id}
Expectedvalid long id
Actualvalid long id
longid succeeds only for a 32-character hexadecimal id.
test{iftype:42:longid:valid long id:not a long id}
Expectednot a long id
Actualnot a long id
A plain small number is not a long id, so the else text is returned.
test{iftype:editor@example.org:mail:looks like an email:not an email}
Expectedlooks like an email
Actuallooks like an email
mail uses PHP e-mail validation.
test{iftype:42:positive:positive integer:not positive}
Expectedpositive integer
Actualpositive integer
positive matches a whole number greater than zero.
test{iftype:0:positive:positive integer:not positive}
Expectednot positive
Actualnot positive
Zero is not positive, so the else text is returned. (number would accept zero - see the next example.)
test{iftype:0:number:non-negative integer:positive:positive integer:other}
Expectednon-negative integer
Actualnon-negative integer
number matches zero or any whole number with no sign; the first matching type (number) wins.
test{iftype:-5:number:non-negative:signed:any integer:neither}
Expectedany integer
Actualany integer
A negative value is not number, but it is signed, so the second pair matches.
test{iftype:-5:signed:value _#1 is _#2:number:non-negative:}
Expectedvalue -5 is signed
Actualvalue -5 is signed
Inside the returned text _#1 is replaced by the tested value (-5) and _#2 by the name of the type that matched (signed). Avoid putting a literal colon in the text - colons separate parameters.
test{iftype:42:shortid:short id _#1:number:plain number:}
Expectedshort id 42
Actualshort id 42
42 matches shortid, number and positive, but tests run in order so the first one (shortid) is returned.
test{iftype:hello:longid:is long id:shortid:is short id:unknown}
Expectedunknown
Actualunknown
A word is neither a long id nor a short id, so the final unpaired parameter (the else text) is returned.
test[{iftype:hello:longid:is long id}]
Expected[]
Actual[]
With no else text and no match, the result is empty. The square brackets are literal, shown only to make the empty result visible.