You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.2 KiB
JavaScript

"use strict";
const utf7 = require('utf7').imap;
const RE_BACKSLASH = /\\/g;
const RE_DBLQUOTE = /"/g;
function escape(str) {
return str.replace(RE_BACKSLASH, '\\\\').replace(RE_DBLQUOTE, '\\"');
}
module.exports = {
command: function (strings, ... interpolations) {
return {
toCommandString: function () {
let processedInterpolations = interpolations.map((interpolation) => {
let isObject = interpolation != null && typeof interpolation === "object";
if (isObject && interpolation._isRaw) {
return interpolation.string;
} else if (isObject && interpolation._is7Bit) {
return escape(interpolation.string);
} else if (typeof interpolation === "string") {
return escape(utf7.encode(interpolation));
} else {
throw new Error(`Invalid input into command string: ${interpolation}`);
}
});
let combined = "";
strings.slice(0, -1).forEach((string, i) => {
combined += string;
combined += processedInterpolations[i];
});
combined += strings[strings.length - 1];
return combined;
}
};
},
unsafeRaw: function (string) {
return {
_isRaw: true,
string: string
};
},
already7Bit: function (string) {
return {
_is7Bit: true,
string: string
};
}
};