Skip to content

String Literals

Shane Brinkman-Davis Delamore edited this page Apr 5, 2017 · 3 revisions

CaffeineScript has lots of nice, streamlined ways to express strings:

# CaffeineScript
a = "" To end of line string.
b = :word-string

c = ""
  This is
  my multiline
  string without newlines.

d = """
  This is
  my multiline
  string with newlines.
// JavaScript
a = "To end of line string."; 
b = "word-string"; 
c = "This is my multiline string without newlines."; 
d = "This is\nmy multiline\nstring with newlines.";

Unquoted Strings

There are several forms of strings which do not require quotes:

:WordStrings

Start with a : and every character after it up to the first space, newline or delimiter character (/:[^ ,\]\)\n]+/) will be part of the string. The initial ':' is not included in the string.

These are inspired by Ruby's symbol syntax. Even though JavaScript ES6 has 'symbols,' they are so broken it makes more sense for the colon-syntax to just resolve to strings. Word-strings allow many common strings to be expressed without the need for matching quotes: urls, files with paths, dependency-versions, npm package-names and much more.

:word                 == "word"
:caffeine-script      == "caffeine-script"
:80%                  == "80%"
:^1.2.3               == "^1.2.3
:www.foo.com          == "www.foo.com"
:../bar/foo.jpg       == "../bar/foo.jpg"
:http://cafscript.com == "http://cafscript.com"
:'hi'                 == "'hi'"
:"hi"                 == '"hi"'

#HashStrings

The # symbol followed by 1 or more legal identifier characters. The '#' character is included in the string. Though this clearly allows #hashtags, the primary motivation is to express hex-colors: #fff, #ab45ef, #0007.

"#hashTAG" == #hashTAG
"#ff0"     == #ff0

NOTE: Comments start with a #, too, but they must be followed with a non-identifier character or EOF. Generally they will be followed by a -, another #, a space or be the last character on the line.

10UnitStrings

Any decimal number-literal, including signs, decimal-points and exponents which is immediately followed by one or more non-digit-identifier-characters is converted as-is into a string.

NOT: ES6-style octal, hex and binary literals are interpreted as numbers, not strings: 0o123, 0x123 and 0b101 are 83, 291 and 5, respectively.

Motivation: With these you can express most CSS properties without quotes. The main exception is percentages (i.e. 10%), for which you'll need to use the word-string method shown above (:10%) or the 10vh, 10vw and 10vmin css units.

10pt       == "10pt"
0.8percent == "0.8percent"
10times10  == "10times10"
Clone this wiki locally