Skip to content

Commit

Permalink
Added ToYAML formula
Browse files Browse the repository at this point in the history
  • Loading branch information
erickoledadevrel committed Sep 26, 2024
1 parent ac196c5 commit cb15a87
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions yaml/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,42 @@ pack.addFormula({
},
});

pack.addFormula({
name: "ToYAML",
description: "Converts a JSON string to YAML.",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
name: "json",
description: "The JSON contents as a string.",
}),
],
resultType: coda.ValueType.String,
cacheTtlSecs: OneDaySecs,
examples: [
{
params: [`{"a":[1,2]}`],
result: trimIndent(`
a:
- 1
- 2
`),
},
],
execute: async function (args, context) {
let [json] = args;
try {
let value = JSON.parse(json);
return YAML.stringify(value);
} catch (e) {
if (e.toString().startsWith("SyntaxError")) {
throw new coda.UserVisibleError(e);
}
throw e;
}
},
});

function parse(yaml: string) {
try {
return YAML.parse(yaml);
Expand Down

0 comments on commit cb15a87

Please sign in to comment.