Created
March 9, 2012 20:25
-
-
Save michael/2008486 to your computer and use it in GitHub Desktop.
toYAML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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