Updating frontmater programatically

I realize the following side steps the matter of getting the API to modify the frontmatter and it’s not a generalized solution, but in my testing it does toggle the values.

<%*
const status = tp.frontmatter.status;
let change = false;
let oldStat = '';
let newStat = '';

if (status === 'active')
{
   change = true;
   oldStat = 'status: active\n';
   newStat = 'status: closed\n';
}
if (status === 'closed')
{
   change = true;
   oldStat = 'status: closed\n';
   newStat = 'status: active\n';
}
if (change)
{
   const content = tp.file.content.split(oldStat).join(newStat);
   await app.vault.modify(app.workspace.getActiveFile(),content);
}
-%>

This could have also been done more concisely with some minor code redundancy.

<%*
const status = tp.frontmatter.status;
if (status === 'active')
{
   const content = tp.file.content.split('status: active\n').join('status: closed\n');
   await app.vault.modify(app.workspace.getActiveFile(),content);
}
if (status === 'closed')
{
   const content = tp.file.content.split('status: closed\n').join('status: active\n');
   await app.vault.modify(app.workspace.getActiveFile(),content);
}
-%>