Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80522 views
1
exports.quote = function (xs) {
2
return xs.map(function (s) {
3
if (/["\s]/.test(s) && !/'/.test(s)) {
4
return "'" + s.replace(/(['\\])/g, '\\$1') + "'";
5
}
6
else if (/["'\s]/.test(s)) {
7
return '"' + s.replace(/(["\\$`(){}!#&*|])/g, '\\$1') + '"';
8
}
9
else {
10
return s.replace(/([\\$`(){}!#&*|])/g, '\\$1');
11
}
12
}).join(' ');
13
};
14
15
exports.parse = function (s) {
16
return s.match(/(['"])((\\\1|[^\1])*?)\1|(\\ |\S)+/g)
17
.map(function (s) {
18
if (/^'/.test(s)) {
19
return s
20
.replace(/^'|'$/g, '')
21
.replace(/\\(["'\\$`(){}!#&*|])/g, '$1');
22
;
23
}
24
else if (/^"/.test(s)) {
25
return s
26
.replace(/^"|"$/g, '')
27
.replace(/\\(["'\\$`(){}!#&*|])/g, '$1');
28
;
29
}
30
else return s.replace(/\\([ "'\\$`(){}!#&*|])/g, '$1');
31
})
32
;
33
};
34
35