Skip to content

Commit a06264f

Browse files
author
Antony Jones
committed
Initial commit
0 parents  commit a06264f

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
node_modules

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- 4
4+
- 5
5+
- 6
6+
- 7

README.MD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Json to Css
2+
3+
Converts JSON to Css
4+
5+
### Dependencies
6+
7+
This module has no external dependencies.
8+
9+
### API
10+
11+
* <class> `Css`
12+
* [string] <static> `of(json)`
13+
* Returns a minified version of the css rules defined in `json`
14+
15+
### Usage
16+
17+
```
18+
const Css = require('json-to-css')
19+
const json = require('./some-css.json')
20+
21+
const css = Css.of(json)
22+
console.log(css)
23+
```

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('src/css')

package.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "json-to-css",
3+
"version": "0.0.0",
4+
"description": "Converts json to CSS",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "mocha **/*.spec.js"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+ssh://git@github.com/desirable-objects/json-to-css.git"
12+
},
13+
"keywords": [
14+
"json",
15+
"css",
16+
"convert"
17+
],
18+
"author": "Antony Jones",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/desirable-objects/json-to-css/issues"
22+
},
23+
"homepage": "https://github.com/desirable-objects/json-to-css#readme",
24+
"devDependencies": {
25+
"code": "^4.0.0",
26+
"mocha": "^3.1.2"
27+
}
28+
}

src/css.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
class Css {
4+
static of (json) {
5+
const selectors = Object.keys(json)
6+
return selectors.map((selector) => {
7+
const definition = json[selector]
8+
const rules = Object.keys(definition)
9+
const result = rules.map((rule) => {
10+
return `${rule}:${definition[rule]}`
11+
}).join(';')
12+
return `${selector}{${result}}`
13+
}).join('\n')
14+
}
15+
}
16+
17+
module.exports = Css

src/css.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict'
2+
3+
const expect = require('code').expect
4+
5+
const Css = require('./css')
6+
7+
describe('Css', () => {
8+
let css
9+
const json = {
10+
h1: {
11+
'font-size': '18vw',
12+
color: 'rgb(128,128,128,255)'
13+
},
14+
p: {
15+
border: '1px 1px 1px 2px'
16+
},
17+
'a::before': {
18+
content: 'x'
19+
},
20+
'.a > .b > .c': {
21+
top: 0
22+
}
23+
}
24+
25+
context('#Of()', () => {
26+
let css
27+
before(() => {
28+
css = Css.of(json)
29+
})
30+
31+
it('Line per selector', () => {
32+
expect(css.split('\n').length).to.equal(4)
33+
})
34+
35+
it('Parses basic css', () => {
36+
expect(css).to.contain('p{border:1px 1px 1px 2px}')
37+
})
38+
39+
it('Parses multiple lines', () => {
40+
expect(css).to.contain('h1{font-size:18vw;color:rgb(128,128,128,255)}')
41+
})
42+
43+
it('Parses pseudo selector', () => {
44+
expect(css).to.contain('h1{font-size:18vw;color:rgb(128,128,128,255)}')
45+
})
46+
47+
it('Parses numbers', () => {
48+
expect(css).to.contain('.a > .b > .c{top:0}')
49+
})
50+
})
51+
})

0 commit comments

Comments
 (0)