Skip to content

Instantly share code, notes, and snippets.

@michael
Created March 9, 2012 20:25
Show Gist options
  • Save michael/2008486 to your computer and use it in GitHub Desktop.
Save michael/2008486 to your computer and use it in GitHub Desktop.
toYAML
function toYAML(data) {
function serializeValue(val) {
function serializeArray(a) {
return a.map(function(e) { return "- "+ e}).join("\n");
}
if (_.isArray(val)) return "\n"+serializeArray(val);
return _.isNumber(val) ? val : "\""+val+"\"";
}
return Object.keys(data).map(function(key) {
return key +": "+ serializeValue(data[key]);
}).join("\n");
}
var metadata = {
"layout": "blog",
"category": "blog",
"permalink": "/blog/2012/jan/03/week-dc-tech-jan-03-2012",
"image": "http://farm7.static.flickr.com/6082/6122078434_641a01d21e.jpg",
"title": "Week in DC Tech: New Year's Edition",
"subtitle": "Python, UX, and MalColm Gladwell this week in Washington, DC",
"tags": ["dc", "Bonnie Bogle"]
};
toYAML(metadata);
// =>
// layout: blog
// category: blog
// permalink: /blog/2012/jan/03/week-dc-tech-jan-03-2012
// image: http://farm7.static.flickr.com/6082/6122078434_641a01d21e.jpg
// title: "Week in DC Tech: New Year's Edition"
// subtitle: Python, UX, and MalColm Gladwell this week in Washington, DC
// tags:
// - dc
// - Bonnie Bogle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment