main.js
1
2
3
4
5
6
7
8
9
10
11
function quote(str, { char = '"', skipIfQuoted = true } = {}) {
  const length = str.length;
  if (skipIfQuoted
      && str[0] === char
      && str[length - 1] === char) {
    return str;
  }
  return char + str + char;
}
console.log(quote('Hello World', { char: '*' })); // => '*Hello World*'
console.log(quote('Sunny day'));                  // => '"Sunny day"'
Native Browser JavaScript