main.js
1
2
3
4
5
6
7
8
9
10
11
12
function quote(str, config) {
  const { char = '"', skipIfQuoted = true } = config;
  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('"Welcome"', { skipIfQuoted: true })); // => '"Welcome"'
Native Browser JavaScript