-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleRepl.js
More file actions
66 lines (52 loc) · 1.9 KB
/
Copy pathsimpleRepl.js
File metadata and controls
66 lines (52 loc) · 1.9 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
//i have parsed variables from inputString and passed their values to the repl
//to test this, run the file and type "greeting" or "name" into the repl
//to see the value that was parsed from the inputString
const repl = require("repl");
//the below variable "globalVariable" can be accessed from the repl (and anywhere)
global.globalVariable = "This can be accessed anywhere!";
//however, this is better way of doing it, declare local variables
//and then expose those to the repl context (below)
// const name = "John Doe";
// const greeting = "whats up";
const Repl = repl.start("custom-repl => ")
// // exposing local variable to repl context
// Repl.context.name = name;
// Repl.context.greeting = greeting;
//i have parsed vars below from an input str,
//and can access them now in the repl
const regexp = /(const|let|var)\s+\S+\s*=/g;
const str = 'const greeting = "hello"; const name = "john"';
const array = [...str.matchAll(regexp)];
// console.log('array', array);
// prints: [
// [
// 'greeting =',
// index: 6,
// input: 'const greeting = "hello"; const name = "john"',
// groups: undefined
// ],
// [
// 'name =',
// index: 32,
// input: 'const greeting = "hello"; const name = "john"',
// groups: undefined
// ]
// ]
const variables = array.map(variable => {
let cleaned = variable[0].replace(/(const|let|var)\s+/, "");
let collected = cleaned.slice(0, cleaned.length-2)
return collected;
})
const regexp2 = /=\s\S+/g;
const array2 = [...str.matchAll(regexp2)];
const values = array2.map(value => {
let untrimmed = value[0]
const regexp = /"\S+"/g;
let trimmed = untrimmed.match(regexp)
const final = trimmed[0].slice(1, trimmed[0].length - 1)
return final;
})
//now i have an arr of variables and a corresponding arr of values
variables.forEach((variable, index) => {
Repl.context[variable] = values[index];
})