Created
July 22, 2014 15:54
-
-
Save rwilcox/971bc86d8b9487743e0a to your computer and use it in GitHub Desktop.
Using the CSV NPM module, version 0.2.0, turn objects into CSV
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
| # An example project showing how to write objects using the CSV npm package | |
| # | |
| # Uses the CSV 0.2.0 style syntax. For an example that shows CSV 0.4.0 | |
| # (major syntax change) see https://gist.github.com/rwilcox/e8411242ffe4f06af35c#file-objects_to_csv-coffee | |
| csv = require 'csv' | |
| util = require 'util' | |
| # Use the callback API to write this | |
| callback_generate_csv_from_list = (records_to_write, cb) -> | |
| map_reduce_it = [] | |
| for current_profile in profile_list | |
| map_reduce_it.push( [current_profile.name | |
| , current_profile.category_name | |
| , current_profile.brand?.name] ) | |
| csv().from(map_reduce_it).to(cb, {columns: ["Name", "Category", "Brand"], header: true}) | |
| profile_list = [ {name: "Macbook Pro, Retina", category_name: "Computer", brand: {name: "Apple"}}, | |
| {name: "Macbook Pro", category_name: "Computer", brand: {name: "Apple"}}, | |
| {name: "Acer 1", category_name: "Netbook", brand: {name: "Acer"}} ] | |
| callback_generate_csv_from_list profile_list, (data) -> | |
| process.stdout.write data | |
| process.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment