I’m trying to generate a YAML string using jsyaml in JavaScript, but I am facing an issue where strings are being serialized with triple single quotes (”’) instead of just single quotes (‘). Here’s a summary of what I am trying to achieve and the steps I have taken so far:
What I’ve tried:
- Manually Wrapping Values in Single Quotes: I tried wrapping values in single quotes using a custom function, but this resulted in an undesired output with triple quotes.
function wrapWithSingleQuotes(value) {
if (typeof value === 'string' && value !== '') {
return `'${value.replace(/'/g, "\'")}'`; // Escaping single quotes inside the value
}
return value;
}
Result: model: '''model1'''
The output here is wrapping the string in triple quotes, which I don’t want.
- Using jsyaml.dump with the styles option: I tried using the
jsyaml.dump
function with the{ styles: { '!!str': 'single' } }
option to force single quotes, but this still resulted in triple quotes in certain cases.
return jsyaml.dump(yamlObject, {
lineWidth: -1,
styles: { '!!str': 'single' },
});
Result: model: '''model1'''
The output here is wrapping the string in triple quotes, which I don’t want.
I want the output to be:
model: ‘model1’
Can anyone explain why this happens and how to avoid the triple quotes while still using jsyaml for YAML serialization?
Here is my form_preview.js
:
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('previewBtn').addEventListener('click', function() {
const formData = new FormData(document.getElementById('configForm'));
const formObject = {};
formData.forEach((value, key) => {
if (shouldIncludeField(key)) {
formObject[key] = value;
}
});
// Preview in alert popup
// const yamlData = jsyaml.dump(formObject);
// alert('YAML Preview:n' + yamlData)
// Preview in container
// const yamlString = convertToYAML(formObject);
const formattedYAML = createConfigYAML(formObject);
document.getElementById('yamlPreview').textContent = formattedYAML;
document.getElementById('previewContainer').style.display = 'block';
});
});
function shouldIncludeField(fieldName) {
// Some code here
}
function wrapWithSingleQuotes(value) {
if (typeof value === 'string' && value !== '') {
return value.replace(/'/g, "\'");
}
return value;
}
function createConfigYAML(formObject) {
const modelSelect = document.getElementById('model');
const selectedModel = modelSelect.options[modelSelect.selectedIndex].text;
const camPrefix = formObject.input === 'Camera'
? '/dev/'
: '/home/mendel/videos/';
const yamlObject = {
input: {
model: wrapWithSingleQuotes(selectedModel) || '',
cam: camPrefix + (formObject.input === 'Camera' ? formObject.cam || 'video1' : formObject.file || ''),
labels: `data/${formObject.dataLabels || ''}`,
det_class: parseInt(formObject.detClasses) || 0,
},
detector: {
threshold: parseFloat(formObject.threshold) || 0,
},
tracker:{
max_age: parseInt(formObject.maxAge) || 0,
min_hits: parseInt(formObject.minHits) || 0,
iou_threshold: parseFloat(formObject.iouThreshold) || 0,
distance_area: parseInt(formObject.distanceArea) || 0,
direction: formObject.direction || '',
},
debug: {
record: formObject.record === 'True' ? true : false,
out: formObject.output || 'outputvid.avi',
display: formObject.display === 'True' ? true : false,
},
result: {
logs: formObject.result || './logs/'
}
};
return jsyaml.dump(yamlObject, {
lineWidth: -1,
styles: { '!!str': 'single' },
});
}