Home > Software design >  JS equivalent for Golang's strconv.Quote
JS equivalent for Golang's strconv.Quote

Time:07-17

Given a YAML like this:

--- 
body: "something else"
path: site:^example\.com?"something"..."something"
title: "How to do this"

parsing it using yaml-ast-parser, it results in an invalid YAML because to he period in the regex and the quotes. I can extract the value of path and double quote it with strconv.Quote and that results in a final YAML like this:

--- 
body: "something else"
path: "site:^example\\.com?\"something\"...\"something\""
title: "How to do this"

However, strconv.Quote is a golang method and this is a javascript project. Does anyone know a way I can achieve strconv.Quote in a javascript environment.

CodePudding user response:

To get something similar to strconv.Quote, use JSON.stringify:

// Example value you got from the YAML:
const path = String.raw`site:^example\.com?"something"..."something"`;

console.log("before");
console.log(path);
console.log("after");
console.log(JSON.stringify(path));

  • Related