forked from gkz/LiveScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.ls
More file actions
273 lines (192 loc) · 4.36 KB
/
string.ls
File metadata and controls
273 lines (192 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
eq '({[dollars]})', '\(\{\[dollars\]\}\)'
eq 'one two three',
"one t
wo t
hree"
eq "four five",
'
four \
five
'
eq 'six seven' '
six
\ seven
'
hello = 'Hello'
world = 'World'
eq '#{hello} #{world}!', '#{hello} #{world}!'
eq "#{hello} #{world}!", 'Hello World!'
eq "#hello #world!", 'Hello World!'
eq "[#{hello}#{world}]", '[HelloWorld]'
eq "#{hello}##{world}", 'Hello#World'
eq "Hello #{ 1 + 2 } World", 'Hello 3 World'
eq "#{hello} #{ 1 + 2 } #{world}", "Hello 3 World"
eq "#{hello + world}", 'HelloWorld'
eq "#{hello + ' ' + world + '!'}", 'Hello World!'
eq helloWorld = hello + world, "#hello-world"
eq "\#{Escaping} first", '#{Escaping} first'
eq "Escaping \#{in} middle", 'Escaping #{in} middle'
eq "Escaping \#{last}", 'Escaping #{last}'
eq "\#Esc\#a\#ping", '#Esc#a#ping'
eq "##", '##'
eq "#{}", ''
eq "#{1}#{2}", '12'
eq "#{}A#{} #{} #{}B#{}", 'A B'
eq "\\\#{}", '\\#{}'
eq "#{
}", ''
eq "I won ##{20} last night.", 'I won #20 last night.'
eq "I won ##{'#20'} last night.", 'I won ##20 last night.'
list = [0 to 5]
eq "values: #{ list.join ( ) }", 'values: 0,1,2,3,4,5'
eq "values: #{ list.join ' ' }", 'values: 0 1 2 3 4 5'
obj =
name: 'Joe'
toString: -> @name
hi: -> "Hello #this."
eq obj.hi(), "Hello Joe."
eq "With #{"quotes"}", 'With quotes'
eq 'With #{"quotes"}', 'With #{"quotes"}'
eq "Where is #{obj["name"] + '?'}", 'Where is Joe?'
eq "Where is #{"the nested #{obj["name"]}"}?", 'Where is the nested Joe?'
eq "Hello #{world ? "#{hello}"}", 'Hello World'
eq "Hello #{"#{"#{obj["name"]}" + '!'}"}", 'Hello Joe!'
eq "#{"hello".replace("\"", "")}", 'hello'
a = """
Hello #{ "Joe" }
"""
eq a, "Hello Joe"
a = """
basic heredoc
on two lines
"""
eq a, "basic heredoc\non two lines"
a = '''
a
"b
c
'''
eq a, "a\n \"b\nc"
a = """
a
b
c
"""
eq a, "a\n b\n c"
eq '''one-liner''', 'one-liner'
a = """
out
here
"""
eq a, "out\nhere"
a = '''
a
b
c
'''
eq a, " a\n b\nc"
a = '''
a
b c
'''
eq a, "a\n\n\nb c"
eq '''more"than"one"quote''', 'more"than"one"quote'
# [coffee#647](https://github.com/jashkenas/coffee-script/issues/647)
eq "''Hello, World\\''", '''
'\'Hello, World\\\''
'''
eq '""Hello, World\\""', """
"\"Hello, World\\\""
"""
eq 'Hello, World\n', '''
Hello, World\
'''
a = """
basic heredoc #{10}
on two lines
"""
b = '''
basic heredoc #{10}
on two lines
'''
eq a, "basic heredoc 10\non two lines"
eq b, "basic heredoc \#{10}\non two lines"
eq '''here's an apostrophe''', "here's an apostrophe"
# Blank lines are ignored for indentation detection.
eq """
one
zero
""", " one\n\nzero\n"
# Space only lines count as indentation.
eq '" one\\n \\nzero\\n";' LiveScript.compile '''
"""
one
\
zero
\
"""
''' {+bare,-header}
eq ''' line 0
should not be relevant
to the indent level
''', ' line 0
\nshould not be relevant
\n to the indent level'
eq ''' '\\\' ''', " '\\' "
eq """ "\\\" """, ' "\\" '
eq ''' <- keep these spaces -> ''', ' <- keep these spaces -> '
eq 'multiline nested "interpolations" work', """multiline #{
"nested #{
ok true
"\"inter"
}" + """polations\""""
} work"""
compileThrows 'unterminated interpolation' 2 '"#{\n'
throws "Parse error on line 1: Unexpected ')'" -> LiveScript.compile '"(#{+})"'
compileThrows 'invalid variable interpolation \'if\'' 1 '"#if"'
compileThrows 'malformed character escape sequence' 1 '"\\x"'
compileThrows 'malformed character escape sequence' 1 '"\\u"'
hi-there = 'Hi there!'
one-two-three = 123
# Dash separated var interpolation
eq 'Hi there! How are you?' "#hi-there How are you?"
eq 'ha 123 ha' "ha #one-two-three ha"
# Character/Word Literal
eq 'word', \word
eq \c, 'c'
eq('+', \+)
eq '\\', [\\\].0
eq '$', {\$}.\$
# [coffee#923](https://github.com/jashkenas/coffee-script/issues/923)
eq "#{ "{" }", "{"
eq "#{ '#{}}' } }", '#{}} }'
# Automatic Dot Insertion
o = k: ok; k = \k
eq o.k, o\k
eq o.k, o'k'
eq o.k, o"#{k}"
o\k true
o'k' true
o"#{k}" true
o"""k""" true
# Automatic Comma Insertion
eq "#{0}" \0
eq \2 '1'.replace "#{1}" -> 2
# Safe Octals
let
'use strict'
eq '\1' '\x01'
eq '\02' '\x02'
eq '\377' '\xFF'
eq '\08\09' '\x008\x009'
# Unjoined
x = 0
y = 1
a = %"#x/#y"
eq a.0, 0
eq a.1, \/
eq a.2, 1
eq a.length, 3
# Trailing backslashes are themselves.
eq '''\''' '\\'
eq \\\\ \\ + \\