-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstlet.js
63 lines (52 loc) · 1.91 KB
/
constlet.js
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
const name = 'Jane';
let title = 'Software Engineer';
let hourlyWage = 40;
//some time later
title = 'Senior Software Engineer';
hourlyWage = 45;
function count(targetString) {
const characters = [ 'a', 'e', 'i', 'o', 'u'];
let number = 0;
for(let i = 0; i < targetString.length; i++) {
if(characters.includes(targetString[i])) {
number++;
}
}
return number;
}
count('aeiobzxceaisipbiox');
//below is the code used to parse input on stephengrider.github.io/JSPlaygrounds/
function buildExpressions(code) {
const transformedCode = JSXTransformer.transform(code).code;
const codeByLine = transformedCode.split('\n');
const tokenized = esprima.tokenize(transformedCode, { loc: true });
const parens = { '(': 0, '{': 0, '[': 0 };
let wasOpen = false;
var exp = _.reduce(tokenized, (expressions, { value, loc: { end } }, index) => {
const lineNumber = end.line;
const lineContents = codeByLine[lineNumber - 1];
const lineHasMoreDelimiters = this.lineHasMoreDelimiters(end, lineContents);
const endOfLine = end.column === lineContents.length;
if (expressions[lineNumber]) {
return expressions;
}
if (OPEN_DELIMITERS.includes(value)) {
parens[value] += 1;
wasOpen = true;
}
if (CLOSE_DELIMITERS.includes(value)) {
parens[DELIMITER_MAP[value]] -= 1;
}
if (!lineHasMoreDelimiters && wasOpen && _.every(parens, count => count === 0)) {
wasOpen = false;
expressions[lineNumber] = _.take(codeByLine, lineNumber).join('\n');
return expressions;
}
if (!lineHasMoreDelimiters && _.every(parens, count => count === 0)) {
expressions[lineNumber] = _.take(codeByLine, lineNumber).join('\n');
return expressions;
}
return expressions;
}, {});
return exp;
}