forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
typescript
209 lines (165 loc) · 3.83 KB
/
typescript
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
// vim: ft=typescript
var myFloat = 0.123;
var myOctal = 0x123;
var myRegex = /asdf/;
var myComplicatedRegex = /.[.](?=x\h\[[)])[^.{}abc]{foo}{3,}x{2,3}/
var myObject = { a: 1, b: 2 }
var ternary = 0 ? 1 : {object_key : 2 ? variable : 3 };
var ternary2 = a ? { object_key : c } + variable : e;
var ternary3 = a ? {
key1 : var1,
key2: var2 ? function() {
label1: // break label, not an object key!
while(true) {
break label1;
}
label2: // also a break label
while (true) {
break label2;
}
} + var3: var4, // ternary, not object key
key3: 'foo'
} : 2
var a = {
num: 0,
func: function() {},
str: '',
bool: true
}
var obj =
{
// comment
key: val
}
/*!
* multiline comment
*/
var quotedKeys = {
"one": 1,
"two": 2,
"three": 3,
};
Blag = {};
jQuery.noConflict();
if (cond)
{
label1:
while (cond) break label1;
}
Blag.ReadMore = (function($) {
function getFold(button) {
return $(button).siblings('.fold');
}
function expand(e) {
e.preventDefault();
var self = $(this);
getFold(self).show();
self.html('« less');
}
function contract(e) {
e.preventDefault();
var self = $(this);
getFold(self).hide();
self.html('more »')
}
function init() {
$('a.read-more').toggle(expand, contract);
}
$(document).ready(init);
})(jQuery);
// evil regexes, from pygments
/regexp/.test(foo) || x = [/regexp/,/regexp/, /regexp/, // comment
// comment
/regexp/];
if (/regexp/.test(string))
{/regexp/.test(string);};
x =/regexp/;
x = /regexp/;
if (0</regexp/.exec(string) || 1>/regexp/.exec(string))
x = { u:/regexp/, v: /regexp/ };
foo();/regexp/.test(string); /regexp/.test(string);
if (!/regexp/) foobar();
x = u %/regexp/.exec(string) */regexp/.exec(string) / /regexp/.exec(string);
x = u?/regexp/.exec(string) : v +/regexp/.exec(string) -/regexp/.exec(string);
a = u^/regexp/.exec(string) &/regexp/.exec(string) |/regexp/.exec(string) +~/regexp/.exec(string);
x = /regexp/ /* a comment */ ;
x = /[reg/exp]/;
x = 4/2/i;
x = (a == b) ?/* this is a comment */ c : d;
/// a comment //
a = /regex//2/1; //syntactically correct, returns NaN
// bad regexen - should have an error token at the last char and recover
// on the next line
var a = /foo
var b = /[foo
var c = /valid-regex/
var template = "{{current}} of beer on the wall";
var stanza = template.replace(/{{current}}/g, "99 bottles");
/* original examples */
// regex
blah(/abc/);
x = /abc/;
x = /abc/.match;
// math
blah(1/2); //comment
x = 1 / 2 / 3;
x = 1/1/.1;
x=/1/; // regex
x=1/a/g; // division
x=a/a/g; // division
// real-world
var x = 1/(1+Math.sqrt(sum)); // convert to number between 1-0
return Math.round((num / den) * 100)/100;
// generator
function* range(from, to)
{
to++;
while (from > to) {
yield from++;
}
}
/* TypeScript examples */
declare namespace Foo {
interface FooStatic {
someFunc(): string;
}
}
declare var foo: Foo;
export = foo;
export { foo as default }
declare module "foo" {
export interface IFoo {
theObject: any;
aWord: string;
aNumber?: number;
}
}
export default abstract class FooClass extends Bar<T> {
constructor() {
super();
}
readonly foo: number = 42;
}
async function fooAsync() {
let result = await barAsync();
}
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>My First Angular App</h1>'
})
export class AppComponent { }
// Mapped types
declare module "foo" {
export type IFooPartial = Partial<IFoo>
export interface IFooPartial2 {
[K in keyof IFoo]?: IFoo[K]
}
}
// Template strings
const x = `Hello world ${a.b}`;
const y = `Hello world ${a?.b}`;
const z = `Hello world`;
// Nullish coalescing in template strings
const content = `Value is ${value ?? 'not set'}`;
const description = "Highlighting is not broken now";