quote
Version:
Add quotes to given string unless it already has them
43 lines (35 loc) • 801 B
JavaScript
function emptyQuotes(options) {
return options.quotes + options.quotes;
}
function quoteString(options, str) {
if (str === '') {
return emptyQuotes(options);
}
if (str[0] !== options.quotes) {
str = options.quotes + str;
}
if (str[str.length - 1] !== options.quotes) {
str += options.quotes;
}
return str;
}
function quoteOptions(options, str) {
if (arguments.length === 1) {
str = options;
options = { quotes: '"' };
}
if (typeof str === 'string') {
return quoteString(options, str);
}
if (typeof str === 'undefined' || str === null) {
return emptyQuotes(options);
}
return str;
}
function quote(str) {
if (typeof str === 'object') {
return quoteOptions.bind(null, str);
}
return quoteOptions(str);
}
module.exports = quote;