Filters

July 29, 2022 ยท View on GitHub

This is a list of filters that are added by StencilSwiftKit on top of the filters already provided by Stencil (which you can find here).

Filter: basename

Get the last component of a path, essentially the filename (and extension).

InputOutput
test.jpgtest.jpg
some/folder/test.jpgtest.jpg
file.txt.pngfile.txt.png

Filter: camelToSnakeCase

Transforms text from camelCase to snake_case. The filter accepts an optional boolean parameter:

  • true (default): Lowercase each component.
  • false: Keep the casing for each component.
InputOutput (true)Output (false)
SomeCapStringsome_cap_stringSome_Cap_String
someCapStringsome_cap_stringsome_Cap_String
String_With_WoRdsstring_with_wordsString_With_Wo_Rds
string_wiTH_WOrdsst_ring_with_wordsstring_wi_TH_W_Ords
URLChooserurl_chooserURL_Chooser
PLEASE_STOP_SCREAMINGplease_stop_screamingPLEASE_STOP_SCREAMING

Filter: contains

Checks if the string contains given substring - works the same as Swift's String.contains.

InputOutput
Hello eltrue
Hi mates! yofalse

Filter: dirname

Remove the last component of a path, essentially returning the path without the filename (and extension).

InputOutput
test.jpg``
some/folder/test.jpgsome/folder
file.txt.png``

Filter: escapeReservedKeywords

Checks if the given string matches a reserved Swift keyword. If it does, wrap the string in escape characters (backticks).

InputOutput
hellohello
self`self`
Any`Any`

Filter: hasPrefix

Checks if the string has the given prefix - works the same as Swift's String.hasPrefix.

InputOutput
Hello Hifalse
Hi mates! Htrue

Filter: hasSuffix

Checks if the string has the given suffix - works the same as Swift's String.hasSuffix.

InputOutput
Hello llotrue
Hi mates! ?false

Filter: lowerFirstLetter

Simply lowercases the first character, leaving the other characters untouched.

InputOutput
Hellohello
PeopleChooserpeopleChooser
Hi There!hi There!

Filter: lowerFirstWord

Transforms an arbitrary string so that only the first "word" is lowercased.

  • If the string starts with only one uppercase character, lowercase that first character.
  • If the string starts with multiple uppercase character, lowercase those first characters up to the one before the last uppercase one, but only if the last one is followed by a lowercase character. This allows to support strings beginnng with an acronym, like URL.
InputOutput
PeoplePickerpeoplePicker
URLChooserurlChooser

Filter: replace

This filter receives at least 2 parameters: a search parameter and a replacement. This filter has a couple of modes that you can specify using an optional mode argument:

  • normal (default): Simple find and replace.
  • regex: Enables the use of regular expressions in the search parameter.
Input (search, replacement)Output (normal)Output (regex)
Hello (l, k)HekkoHekko
Europe (e, a)EuropaEuropa
Hey1World2! (\d, )Hey1World2!Hey World !

Filter: snakeToCamelCase

Transforms a string in "snake_case" format into one in "camelCase" format, following the steps below:

  • Separate the string in components using the _ as separator.
  • For each component, uppercase the first character and do not touch the other characters in the component.
  • Join the components again into one string.

If the whole starting "snake_case" string only contained uppercase characters, then each component will be capitalized: uppercase the first character and lowercase the other characters.

This filter accepts an optional boolean parameter that controls the prefixing behaviour:

  • false (default): don't remove any empty components.
  • true: trim empty components from the beginning of the string
InputOutput (false)Output (true)
snake_caseSnakeCaseSnakeCase
snAke_caseSnAkeCaseSnAkeCase
SNAKE_CASESnakeCaseSnakeCase
__snake_case__SnakeCaseSnakeCase

Filter: swiftIdentifier

Transforms an arbitrary string into a valid Swift identifier (using only valid characters for a Swift identifier as defined in the Swift language reference). It will apply the following rules:

  • Uppercase the first character.
  • Prefix with an underscore if the first character is a number.
  • Replace invalid characters by an underscore (_).

The list of allowed characters can be found here: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/LexicalStructure.html

This filter has a couple of modes that you can specify using an optional mode argument:

  • valid: Only do the bare minimum for a valid identifier, i.e. the steps mentioned above except uppercasing characters.
  • normal (default): apply the steps mentioned above (uppercase first, prefix if needed, replace invalid characters with _).
  • pretty: Same as normal, but afterwards it will apply the snakeToCamelCase filter, and other manipulations, for a prettier (but still valid) identifier.
InputOutput (valid)Output (normal)Output (pretty)
hellohelloHelloHello
42hello_42hello_42hello_42hello
some$URLsome_URLSome_URLSomeURL
25 Ultra Light_25_Ultra_Light_25_Ultra_Light_25UltraLight
26_extra_ultra_light_26_extra_ultra_light_26_extra_ultra_light_26ExtraUltraLight
apples.countapples_countApples_CountApplesCount
foo_bar.baz.qux-yayfoo_bar_baz_qux_yayFoo_bar_Baz_Qux_YayFooBarBazQuxYay

Filter: titlecase

Deprecated in favor of upperFirstLetter.

Filter: upperFirstLetter

Simply uppercases the first character, leaving the other characters untouched.

Note that even if very similar, this filter differs from the capitalized filter, which uppercases the first character but also lowercases the remaining characters.

InputOutput
helloHello
peopleChooserPeopleChooser