Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function convertKeyNames(obj) {
}

const propExists = prop => prop.length;
const addMedia = key => `@media ${key}`;

function mungeRules(rules) {
const rulesArr = [];
Expand All @@ -67,7 +68,10 @@ function mungeRules(rules) {
selectors.map(s => ({[s]:ruleObj})).forEach(r => rulesArr.push(r));
}
} else if(type === 'media') {
rulesArr.concat(mungeRules(rule.rules));
const key = addMedia(rule.media);
rulesArr.push({
[key]: mungeRules(rule.rules)
});
} else {
console.error('Type of ' + type + ' did not conform to media or rule \n', rule);
}
Expand All @@ -89,17 +93,25 @@ function cssToJson(string) {
}
}

const isMedia = key => /^\@/.test(key);

const camelizeArray = arr => arr.map(obj => {
const key = Object.keys(obj)[0];
if(isMedia(key)) {
console.log('obj[key]', key)
return {[key]:camelizeArray(obj[key])};
}
return {
[key]: camelize(obj[key])
}
})

function cssToCamelizedJson(string) {
if (!isString(string)) {
throw new Error('cssToCamelizedJson needs a valid css string to convert');
}
const json = cssToJson(string);
return json.map(obj => {
const key = Object.keys(obj)[0];
return {
[key]: camelize(obj[key])
}
});
return camelizeArray(json);
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "convert-css",
"version": "0.1.0",
"version": "0.1.1",
"description": "Convert css strings to JSON or JS",
"main": "index.js",
"author": "David Daniel, <david.daniel@nike.com>",
Expand Down