shellwords
Version:
Manipulate strings according to the word parsing rules of the UNIX Bourne shell.
85 lines (83 loc) • 2.48 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/shellwords.ts
var shellwords_exports = {};
__export(shellwords_exports, {
escape: () => escape,
join: () => join,
split: () => split
});
module.exports = __toCommonJS(shellwords_exports);
var scan = (string, pattern, callback) => {
let result = "";
while (string.length > 0) {
const match = string.match(pattern);
if (match && match.index != null && match[0] != null) {
result += string.slice(0, match.index);
result += callback(match);
string = string.slice(match.index + match[0].length);
} else {
result += string;
string = "";
}
}
return result;
};
var split = (line = "") => {
const words = [];
let field = "";
scan(
line,
/\s*(?:([^\s\\'"]+)|'((?:[^'\\]|\\.)*)'|"((?:[^"\\]|\\.)*)"|(\\.?)|(\S))(\s|$)?/,
(match) => {
const [_raw, word, sq, dq, escape2, garbage, separator] = match;
if (garbage != null) {
throw new Error(`Unmatched quote: ${line}`);
}
if (word) {
field += word;
} else {
let addition;
if (sq) {
addition = sq;
} else if (dq) {
addition = dq;
} else if (escape2) {
addition = escape2;
}
if (addition) {
field += addition.replace(/\\(?=.)/, "");
}
}
if (separator != null) {
words.push(field);
field = "";
}
}
);
if (field) {
words.push(field);
}
return words;
};
var escape = (str = "") => {
return str.replace(/([^A-Za-z0-9_\-.,:/@\n])/g, "\\$1").replace(/\n/g, "'\n'");
};
var join = (array) => array.map((element) => escape(element)).join(" ");
//# sourceMappingURL=index.js.map