| 1 | <h2>Comments</h2> |
| 2 | <pre><code># This is a single line comment |
| 3 | /* This is a |
| 4 | multi line comment */</code></pre> |
| 5 | |
| 6 | <h2>Numbers</h2> |
| 7 | <pre><code>42 |
| 8 | 42km |
| 9 | 3.754km_2 |
| 10 | 16~BadFace |
| 11 | 36~azertyuiop0123456789</code></pre> |
| 12 | |
| 13 | <h2>Strings and interpolation</h2> |
| 14 | <pre><code>'' |
| 15 | '''''' |
| 16 | "" |
| 17 | """""" |
| 18 | 'Foo \' bar |
| 19 | baz' |
| 20 | '''Foo \''' bar |
| 21 | bar''' |
| 22 | "Foo #bar \" |
| 23 | #{2 + 2}\"" |
| 24 | """#foobar \""" #{ if /test/ == 'test' then 3 else 4} |
| 25 | baz"""</code></pre> |
| 26 | |
| 27 | <h2>Regex</h2> |
| 28 | <pre><code>/foobar/ig |
| 29 | // |
| 30 | ^foo # foo |
| 31 | [bar]*bA?z # barbaz |
| 32 | //m</code></pre> |
| 33 | |
| 34 | <h2>Full example</h2> |
| 35 | <pre><code># example from Str.ls |
| 36 | |
| 37 | split = (sep, str) --> |
| 38 | str.split sep |
| 39 | |
| 40 | join = (sep, xs) --> |
| 41 | xs.join sep |
| 42 | |
| 43 | lines = (str) -> |
| 44 | return [] unless str.length |
| 45 | str.split '\n' |
| 46 | |
| 47 | unlines = (.join '\n') |
| 48 | |
| 49 | words = (str) -> |
| 50 | return [] unless str.length |
| 51 | str.split /[ ]+/ |
| 52 | |
| 53 | unwords = (.join ' ') |
| 54 | |
| 55 | chars = (.split '') |
| 56 | |
| 57 | unchars = (.join '') |
| 58 | |
| 59 | reverse = (str) -> |
| 60 | str.split '' .reverse!.join '' |
| 61 | |
| 62 | repeat = (n, str) --> |
| 63 | result = '' |
| 64 | for til n |
| 65 | result += str |
| 66 | result |
| 67 | |
| 68 | capitalize = (str) -> |
| 69 | (str.char-at 0).to-upper-case! + str.slice 1 |
| 70 | |
| 71 | camelize = (.replace /[-_]+(.)?/g, (, c) -> (c ? '').to-upper-case!) |
| 72 | |
| 73 | # convert camelCase to camel-case, and setJSON to set-JSON |
| 74 | dasherize = (str) -> |
| 75 | str |
| 76 | .replace /([^-A-Z])([A-Z]+)/g, (, lower, upper) -> |
| 77 | "#{lower}-#{if upper.length > 1 then upper else upper.to-lower-case!}" |
| 78 | .replace /^([A-Z]+)/, (, upper) -> |
| 79 | if upper.length > 1 then "#upper-" else upper.to-lower-case! |
| 80 | |
| 81 | module.exports = { |
| 82 | split, join, lines, unlines, words, unwords, chars, unchars, reverse, |
| 83 | repeat, capitalize, camelize, dasherize, |
| 84 | }</code></pre> |