UniqueWordsSigil (Unique-Words Sigil v0.1.1)
View Source~u Sigil
Unique-word strings, lists, HTML classes, checked at compile time.
Examples
~u"hello world" -> "hello world"
~u" hello world " -> "hello world" # whitespace is trimmed and "collapsed"
~u" hello world "l -> ["hello", "world"]
~u" hello world "a -> [:hello, :world]
~u" hello world "c -> [~c"hello", ~c"world"]
~u" hi hi " -> (ArgumentError) Duplicate word: hi
~u" hi-hi hi-hi "w -> "hi-hi hi-hi" Warning: Duplicate word: hi-hi
~u" hi hi "wl -> ["hi", "hi"] Warning: Duplicate word: hi
~u" hi-hi hi-hi "aw -> [:"hi-hi", :"hi-hi"] Warning: Duplicate word: hi-hi
~u" hi hi "cw -> [~c"hi", ~c"hi"] Warning: Duplicate word: hi
~~~~~~~~~~~~~~~~~~
~u is ideal for HTML classes used with templating systems such as Temple (https://github.com/mhanberg/temple):
div class: ~u"flex items-center flex" do # (CompilerError) Duplicate word: flex
p class: ~u"text-lg font-bold
text-gray text-lg" # ────── (CompilerError) Duplicate word: text-lg
do # └───── Effortless multiline classes promote readability
"Hello world" # (~u automatically strips whitespace and newlines)
end
end
~u also works well with string interpolation:
a href: ~p"/link/url"
class: ~u"flex items-center h-8 text-sm pl-8 pr-3
#{if(@active, do: ~u"bg-slate", else: ~u"hover:bg-slate")}
items-center text-blue" # Duplicate word: items-center
do
"Link text"
end
CAVEAT: Being unknown during compilation, interpolations WON'T be uniqueness-checked by default.
i
modifier may be added to check uniqueness of interpolated sections at runtime:
~u" hi hello #{"h" <> "i"}"i -> (RuntimeError) Duplicate word: hi
Interpolation uniqueness-checking is disabled unless i
modifier is set to
to avoid unintended runtime overhead.
When Mix.env() == :prod
, interpolation uniqueness-checking will ALWAYS be disabled
even when i
modifier is set (this caters to the most common use-case).