forked from ghammock/LaTeX_Listings_JavaScript_ES6
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathes_listing.tex
142 lines (124 loc) · 3.57 KB
/
es_listing.tex
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
\documentclass[10pt]{article}
\usepackage[margin=2.5cm]{geometry} % for margins on a A4paper
\usepackage{listings}
\usepackage{color}
\usepackage{textcomp} % for upquote
\usepackage{tcolorbox}
\tcbuselibrary{listings,skins, breakable}
\usepackage{kantlipsum}
%
% ECMAScript 2015 (ES6) definition by Gary Hammock
%
\lstdefinelanguage[ECMAScript2015]{JavaScript}[]{JavaScript}{
morekeywords=[1]{await, async, case, catch, class, const, debugger, default, do,
enum, extends, finally, from, implements, instanceof,
let, static, super, switch, throw, try},
morekeywords=[2]{export, import, implements, package, private, protected, public, static
},
morestring=[b]` % Interpolation strings.
}
%
% JavaScript version 1.1 by Gary Hammock
%
% Reference:
% B. Eich and C. Rand Mckinney, "JavaScript Language Specification
% (Preliminary Draft)", JavaScript 1.1. 1996-11-18. [Online]
% http://hepunx.rl.ac.uk/~adye/jsspec11/titlepg2.htm
%
\lstdefinelanguage{JavaScript}{
morekeywords=[1]{break, continue, delete, else, for, function, if, in,
new, return, this, typeof, var, void, while, with, yield},
% Literals, primitive types, and reference types.
morekeywords=[2]{false, null, true, boolean, number, undefined,
Array, Boolean, Date, Math, Number, String, Object},
% Built-ins.
morekeywords=[3]{eval, parseInt, parseFloat, escape, unescape, console},
sensitive,
morecomment=[s]{/*}{*/},
morecomment=[l]//,
morecomment=[s]{/**}{*/}, % JavaDoc style comments
morestring=[b]',
morestring=[b]"
}[keywords, comments, strings]
\lstalias[]{ES6}[ECMAScript2015]{JavaScript}
% Requires package: color.
\definecolor{mediumgray}{rgb}{0.3, 0.4, 0.4}
\definecolor{mediumblue}{rgb}{0.0, 0.0, 0.8}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{darkviolet}{rgb}{0.58, 0.0, 0.83}
\definecolor{royalblue}{rgb}{0.25, 0.41, 0.88}
\definecolor{crimson}{rgb}{0.86, 0.8, 0.24}
\lstdefinestyle{JSES6Base}{
basicstyle=\ttfamily\small,
breakatwhitespace=true,
breaklines=true,
captionpos=b,
columns=fullflexible,
commentstyle=\color{mediumgray}\upshape,
emph={},
emphstyle=\color{crimson},
extendedchars=true, % requires inputenc
identifierstyle=\color{black},
keepspaces=true,
keywordstyle=\color{mediumblue},
keywordstyle={[2]\color{darkviolet}},
keywordstyle={[3]\color{royalblue}},
numbers=left,
numbersep=2pt,
numberstyle=\tiny\color{black},
rulecolor=\color{red},
showlines=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
stringstyle=\color{forestgreen},
tabsize=2,
title=\lstname,
upquote=true % requires textcomp
}
\lstdefinestyle{JavaScript}{
language=JavaScript,
style=JSES6Base
}
\lstdefinestyle{ES6}{
language=ES6,
style=JSES6Base
}
\begin{document}
\kant[1-3]
\begin{tcblisting}{colback=red!5!white,colframe=red!75!black,fonttitle=\bfseries,
listing only, breakable,
title=Samples for JavaScript and ES6,enhanced,drop fuzzy shadow,
listing style=ES6
}
//Option 1 - Use try catch within the function
async function doubleAndAdd(a, b) {
try {
a = await doubleAfter1Sec(a);
b = await doubleAfter1Sec(b);
} catch (e) {
return NaN; //return something
}
return a + b;
}
//New usage:
doubleAndAdd('one', 2).then(console.log); // NaN
doubleAndAdd(1, 2).then(console.log); // 6
function doubleAfter1Sec(param) {
return new Promise((resolve, reject) => {
setTimeout(function() {
let val = param * 2;
isNaN(val) ? reject(NaN) : resolve(val);
}, 1000);
});
}
// Old Syntax
function oldOne() {
console.log('Hello World..!');
}
// New Syntax in ES6
var newOne = () => {
console.log('Hello World..!');
}
\end{tcblisting}
\end{document}