Skip to content

Commit 00072d5

Browse files
committed
Code style fixes
1 parent 80faec0 commit 00072d5

File tree

3 files changed

+48
-66
lines changed

3 files changed

+48
-66
lines changed

JavaScript/6-polyfill.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ console.log(`name = ${name}`);
88
// Polyfill
99

1010
if (!String.prototype.includes) {
11-
String.prototype.includes = function(s) {
11+
String.prototype.includes = function (s) {
1212
return this.indexOf(s) > -1;
1313
};
1414
}
@@ -17,7 +17,7 @@ console.log(name.includes('Mar'));
1717

1818
// Bad practice
1919

20-
String.prototype.includesWord = function(s) {
20+
String.prototype.includesWord = function (s) {
2121
return ` ${this} `.includes(` ${s} `);
2222
};
2323

JavaScript/a-fp.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
const words = (s) => s.split(' ');
99
const first = (s) => s.substring(0, 1);
1010

11-
const partial = (fn, x) => (...args) => fn(x, ...args);
11+
const partial =
12+
(fn, x) =>
13+
(...args) =>
14+
fn(x, ...args);
1215

1316
console.log(first('ABC'));

JavaScript/strings.js

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ const { nowDateTime } = require('./time');
88
const HTML_ESCAPE_REGEXP = new RegExp('[&<>"\'/]', 'g');
99

1010
const HTML_ESCAPE_CHARS = {
11-
'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', '\'': '&#39;'
11+
'&': '&amp;',
12+
'<': '&lt;',
13+
'>': '&gt;',
14+
'"': '&quot;',
15+
"'": '&#39;',
1216
};
1317

1418
const ALPHA_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
@@ -19,19 +23,16 @@ const ALPHA_DIGIT = ALPHA + DIGIT;
1923

2024
const htmlEscape = (
2125
// Escape html characters
22-
content // string, to escape
26+
content, // string, to escape
2327
// Returns: string
2428
// Example: htmlEscape('5>=5') = '5&lt;=5'
25-
) => (
26-
content.replace(HTML_ESCAPE_REGEXP, (char) => HTML_ESCAPE_CHARS[char])
27-
);
28-
29+
) => content.replace(HTML_ESCAPE_REGEXP, (char) => HTML_ESCAPE_CHARS[char]);
2930
const subst = (
3031
// Substitute variables
3132
tpl, // string, template body
3233
data, // hash, data structure to visualize
3334
dataPath, // string, current position in data structure
34-
escapeHtml // boolean, escape html special characters if true
35+
escapeHtml, // boolean, escape html special characters if true
3536
// Returns: string
3637
) => {
3738
let start = 0;
@@ -90,95 +91,78 @@ const subst = (
9091

9192
const fileExt = (
9293
// Extract file extension in lower case with no dot
93-
fileName // string, file name
94+
fileName, // string, file name
9495
// Returns: string
9596
// Example: fileExt('/dir/file.txt')
9697
// Result: 'txt'
97-
) => (
98-
path.extname(fileName).replace('.', '').toLowerCase()
99-
);
100-
98+
) => path.extname(fileName).replace('.', '').toLowerCase();
10199
const removeExt = (
102100
// Remove file extension from file name
103-
fileName // string, file name
101+
fileName, // string, file name
104102
// Returns: string
105103
// Example: fileExt('file.txt')
106104
// Result: 'file'
107-
) => (
108-
fileName.substr(0, fileName.lastIndexOf('.'))
109-
);
110-
105+
) => fileName.substr(0, fileName.lastIndexOf('.'));
111106
const CAPITALIZE_REGEXP = /\w+/g;
112107

113108
const capitalize = (
114109
// Capitalize string
115-
s // string
110+
s, // string
116111
// Returns: string
117-
) => (
118-
s.replace(CAPITALIZE_REGEXP, (word) => (
119-
word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()
120-
))
121-
);
122-
112+
) =>
113+
s.replace(
114+
CAPITALIZE_REGEXP,
115+
(word) => word.charAt(0).toUpperCase() + word.substr(1).toLowerCase(),
116+
);
123117
const UNDERLINE_REGEXP = /_/g;
124118

125119
const spinalToCamel = (
126120
// Convert spinal case to camel case
127-
name // string
121+
name, // string
128122
// Returns: string
129-
) => (
123+
) =>
130124
name
131125
.replace(UNDERLINE_REGEXP, '-')
132126
.split('-')
133127
.map((part, i) => (i > 0 ? capitalize(part) : part))
134-
.join('')
135-
);
136-
128+
.join('');
137129
const ESCAPE_REGEXP_SPECIALS = [
138130
// order matters for these
139131
'-', '[', ']',
140132
// order doesn't matter for any of these
141-
'/', '{', '}', '(', ')', '*', '+', '?', '.', '\\', '^', '$', '|'
133+
'/', '{', '}', '(', ')', '*', '+', '?', '.', '\\', '^', '$', '|',
142134
];
143135

144136
const ESCAPE_REGEXP = new RegExp(
145-
'[' + ESCAPE_REGEXP_SPECIALS.join('\\') + ']', 'g'
137+
'[' + ESCAPE_REGEXP_SPECIALS.join('\\') + ']', 'g',
146138
);
147139

148140
const escapeRegExp = (
149141
// Escape regular expression control characters
150-
s // string
142+
s, // string
151143
// Returns: string
152144
// Example: escapeRegExp('/path/to/res?search=this.that')
153-
) => (
154-
s.replace(ESCAPE_REGEXP, '\\$&')
155-
);
156-
145+
) => s.replace(ESCAPE_REGEXP, '\\$&');
157146
const newEscapedRegExp = (
158147
// Generate escaped regular expression
159-
s // string
148+
s, // string
160149
// Returns: RegExp, instance
161-
) => (
162-
new RegExp(escapeRegExp(s), 'g')
163-
);
164-
150+
) => new RegExp(escapeRegExp(s), 'g');
165151
const addTrailingSlash = (
166152
// Add trailing slash at the end if there isn't one
167-
s // string
153+
s, // string
168154
// Returns: string
169155
) => s + (s.endsWith('/') ? '' : '/');
170156

171157
const stripTrailingSlash = (
172158
// Remove trailing slash from string
173-
s // string
159+
s, // string
174160
// Returns: string
175-
) => (
176-
s.endsWith('/') ? s.substr(0, s.length - 1) : s
177-
);
161+
) => (s.endsWith('/') ? s.substr(0, s.length - 1) : s);
178162

179163
const dirname = (
180164
// Get directory name with trailing slash from path
181-
filePath // string
165+
filePath, // string
182166
// Returns: string
183167
) => {
184168
let dir = path.dirname(filePath);
@@ -190,7 +174,7 @@ const between = (
190174
// Extract substring between prefix and suffix
191175
s, // string, source
192176
prefix, // string, before needed fragment
193-
suffix // string, after needed fragment
177+
suffix, // string, after needed fragment
194178
// Returns: string
195179
) => {
196180
let i = s.indexOf(prefix);
@@ -208,23 +192,21 @@ const BOM_REGEXP = /^[\uBBBF\uFEFF]*/;
208192

209193
const removeBOM = (
210194
// Remove UTF-8 BOM
211-
s // string, possibly starts with BOM
195+
s, // string, possibly starts with BOM
212196
// Returns: string
213-
) => (
214-
typeof s === 'string' ? s.replace(BOM_REGEXP, '') : s
215-
);
197+
) => (typeof s === 'string' ? s.replace(BOM_REGEXP, '') : s);
216198

217199
const ITEM_ESCAPE_REGEXP = /\\\*/g;
218200

219201
const arrayRegExp = (
220202
// Generate RegExp from array with '*' wildcards
221-
items // array of strings
203+
items, // array of strings
222204
// Returns: RegExp, instance
223205
// Example: ['/css/*', '/index.html']
224206
) => {
225207
if (!items || items.length === 0) return null;
226-
items = items.map(
227-
(item) => escapeRegExp(item).replace(ITEM_ESCAPE_REGEXP, '.*')
208+
items = items.map((item) =>
209+
escapeRegExp(item).replace(ITEM_ESCAPE_REGEXP, '.*'),
228210
);
229211
const ex = items.length === 1 ? items[0] : '((' + items.join(')|(') + '))';
230212
return new RegExp('^' + ex + '$');
@@ -233,7 +215,7 @@ const arrayRegExp = (
233215
const section = (
234216
// Split string by the first occurrence of separator
235217
s, // string
236-
separator // string, or char
218+
separator, // string, or char
237219
// Returns: array of strings
238220
// Example: rsection('All you need is JavaScript', 'is')
239221
// Result: ['All you need ', ' JavaScript']
@@ -246,7 +228,7 @@ const section = (
246228
const rsection = (
247229
// Split string by the last occurrence of separator
248230
s, // string
249-
separator // string, or char
231+
separator, // string, or char
250232
// Returns: array of strings
251233
// Example: rsection('All you need is JavaScript', 'a')
252234
// Result: ['All you need is Jav', 'Script']
@@ -260,21 +242,18 @@ const split = (
260242
// Split string by multiple occurrence of separator
261243
s, // string
262244
separator = ',', // string (optional), default: ','
263-
limit = -1 // number (optional), max length of result array
245+
limit = -1, // number (optional), max length of result array
264246
// Returns: array of strings
265247
// Example: split('a,b,c,d')
266248
// Result: ['a', 'b', 'c', 'd']
267249
// Example: split('a,b,c,d', ',', 2)
268250
// Result: ['a', 'b']
269-
) => (
270-
s.split(separator, limit)
271-
);
272-
251+
) => s.split(separator, limit);
273252
const rsplit = (
274253
// Split string by multiple occurrences of separator
275254
s, // string
276255
separator = ',', // string (optional), default: ','
277-
limit = -1 // number (optional), max length of result array
256+
limit = -1, // number (optional), max length of result array
278257
// Returns: array of strings
279258
// Example: split('a,b,c,d', ',', 2)
280259
// Result: ['c', 'd']

0 commit comments

Comments
 (0)