Last active
August 29, 2015 13:56
-
-
Save KittyGiraudel/9327085 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
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
// ---- | |
// Sass (v3.3.0.rc.5) | |
// Compass (v1.0.0.alpha.18) | |
// ---- | |
// Sass to CSV converter | |
// Just for the sake of it. | |
// -------------------------------------------- | |
// @param [list|map] $value: value to convert | |
// @param [bool] $labels: enable first line as labels | |
// @param [bool] $quotes: enable quotes | |
// -------------------------------------------- | |
// @return [string] CSV | |
@function to-csv($input, $labels: true, $quotes: true) { | |
$string: ''; | |
$keys: (); | |
$break: ' | |
'; // Smart line break is smart | |
// Looping through list entries | |
@for $i from 1 through length($input) { | |
$values: (); | |
$item: nth($input, $i); | |
$keys: if(length($keys) == 0, map-keys($item, $quotes), $keys); | |
// I am the key master. | |
@if $i == 1 and $labels { | |
$string: $string + $keys; | |
} | |
// Looping through the keys! | |
@each $key in $keys { | |
$value: map-get($item, $key); | |
$value: if($quotes, if(type-of($value) == 'string', quote($value), $value), unquote($value)); | |
$values: append($values, $value, comma); | |
} | |
$string: $string + if($string != '', $break, '') + $values; | |
} | |
@return $string; | |
} | |
// Helper function to get map-keys | |
// Built in function is not enough | |
// -------------------------------------------- | |
// @param [map] $map: map to get keys from | |
// @param [bool] $quotes: enable quotes | |
// -------------------------------------------- | |
// @return [list] keys | |
@function map-keys($map, $quotes: true) { | |
$keys: (); | |
@each $key, $value in $map { | |
$key: if($quotes, quote($key), unquote($key)); | |
$keys: append($keys, $key, comma); | |
} | |
@return $keys; | |
} | |
$list: ( | |
('id': 1, 'first': "Stuart", 'last': "Robson", 'twitter': '@sturobson'), | |
('id': 2, 'first': "Maxime", 'last': Thirouin, 'twitter': '@moox'), | |
('id': 3, 'first': Fabrice, 'last': "Weinberg", 'twitter': '@fweinb'), | |
); | |
/*! Quick and dirty Sass to CSV converter | |
## Sass list (of maps) | |
#{inspect($list)} | |
## Generated CSV | |
#{to-csv($list)} | |
## With $labels set to `false` | |
#{to-csv($list, false)} | |
## With $quotes set to `false` | |
#{to-csv($list, $quotes: false)} | |
*/ |
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
/*! Quick and dirty Sass to CSV converter | |
## Sass list (of maps) | |
("id": 1, "first": "Stuart", "last": "Robson", "twitter": "@sturobson"), ("id": 2, "first": "Maxime", "last": Thirouin, "twitter": "@moox"), ("id": 3, "first": Fabrice, "last": "Weinberg", "twitter": "@fweinb") | |
## Generated CSV | |
"id", "first", "last", "twitter" | |
1, "Stuart", "Robson", "@sturobson" | |
2, "Maxime", "Thirouin", "@moox" | |
3, "Fabrice", "Weinberg", "@fweinb" | |
## With $labels set to `false` | |
1, "Stuart", "Robson", "@sturobson" | |
2, "Maxime", "Thirouin", "@moox" | |
3, "Fabrice", "Weinberg", "@fweinb" | |
## With $quotes set to `false` | |
id, first, last, twitter | |
1, Stuart, Robson, @sturobson | |
2, Maxime, Thirouin, @moox | |
3, Fabrice, Weinberg, @fweinb | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment