Skip to content

Commit

Permalink
Merge 9a87d43 into 9650295
Browse files Browse the repository at this point in the history
  • Loading branch information
Regaddi committed Dec 27, 2019
2 parents 9650295 + 9a87d43 commit e9be941
Show file tree
Hide file tree
Showing 7 changed files with 2,759 additions and 52 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand All @@ -31,3 +32,9 @@ node_modules

# Optional REPL history
.node_repl_history

# generated files
test/*.sass

# IDE
.vscode
15 changes: 5 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
language: node_js
node_js:
- "5"
- "5.1"
- "4"
- "4.2"
- "4.1"
- "4.0"
- "0.12"
- "0.11"
- "0.10"
- "12"
- "11"
- "10"
- "8"
before_script: "npm run pretest"
script: "npm run-script test-travis"
script: "npm run-script coverage"
after_script: "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
28 changes: 16 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
[![npm version](https://badge.fury.io/js/jsontosass.svg)](https://badge.fury.io/js/jsontosass)
[![Build Status](https://travis-ci.org/Regaddi/jsontosass.svg?branch=master)](https://travis-ci.org/Regaddi/jsontosass)
[![Coverage Status](https://coveralls.io/repos/github/Regaddi/jsontosass/badge.svg?branch=master)](https://coveralls.io/github/Regaddi/jsontosass?branch=master)
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/Flet/semistandard)
[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/standard/semistandard)

# jsontosass

Oh, no! Not another JSON to Sass converter! Why did you do that?

Simple answer: there was no JSON to Sass converter out there (up to this point)
Short answer: there was no JSON to Sass converter out there (up to this point)
that was flexible enough to suite my needs to e.g. satisfy my
[scss-lint](https://github.com/brigade/scss-lint) configuration.

This one here aims to be as flexible as possible regarding

- generation of your Sass variables
- generation of Sass variables
- indentation for maps
- minification
- Syntax output (Sass and SCSS)

Additionally I want this module to be well tested, that's why I'm focussing on
[TDD](https://en.wikipedia.org/wiki/Test-driven_development) here.
Additionally this module should be well tested, that's why
[Test driven development (TDD)](https://en.wikipedia.org/wiki/Test-driven_development) has focus.

What you will get with this package is a well maintained, documented and tested
JSON to Sass converter that will definitely be the last one you'll ever need!
This package aims to be a well maintained, documented and tested
JSON to Sass converter that will be the last one you'll ever need!

# Installation

You can install this package with [npm](https://www.npmjs.com):
Install this package with [npm](https://www.npmjs.com):

npm install jsontosass

After that you should be able to access `jsontosass` by using `require`
or

yarn install jsontosass

If all went well you should be able to access `jsontosass` by using `require`

```javascript
var jsontosass = require("jsontosass");
Expand Down Expand Up @@ -72,15 +76,15 @@ jsontosass.convertFileSync(
);
```

Same as `convertFile()` but synchronously and obviously without callback parameter.
Same as `convertFile()` but synchronously and without callback parameter.

# Options

## `(Int/String)` indent

default: 4

If a number greater 0 is given, jsontosass will indent using the given number of spaces. Optionally you can set it to `'tabs'`. This will indent using `\t`. Indentation is only made, when `prettify` is set to `true`.
If a number greater 0 is given, jsontosass will indent using the given number of spaces. Optionally set it to `'tabs'`. This will indent using `\t`. Indentation is only made, when `prettify` is set to `true`.

## `Boolean` prettify

Expand Down Expand Up @@ -149,7 +153,7 @@ $key: (

# Contribution

You're free to contribute to this project by submitting [issues](https://github.com/Regaddi/jsontosass/issues) and/or [pull requests](https://github.com/Regaddi/jsontosass/pulls). This project is test-driven, so keep in mind that every change and new feature should be covered by tests.
Feel free to contribute to this project by submitting [issues](https://github.com/Regaddi/jsontosass/issues) and/or [pull requests](https://github.com/Regaddi/jsontosass/pulls). This project is test-driven, so keep in mind that every change and new feature should be covered by tests.
This project uses the [semistandard code style](https://github.com/Flet/semistandard).

# License
Expand Down
2 changes: 1 addition & 1 deletion jsontosass.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var JsonToSass = function () {
switch (typeof obj[key]) {
case 'object':
nestLevel++;
if (this.options.useMaps || obj[key].hasOwnProperty('length')) {
if (this.options.useMaps || obj[key].length) {
map.push(key + createColon() + convertObject(obj[key]));
} else {
map.push(key + '-' + convertObject(obj[key]));
Expand Down
21 changes: 13 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,28 @@
],
"main": "jsontosass.js",
"scripts": {
"coverage": "nyc npm run test",
"prerelease": "npm test",
"release": "npm run prerelease && npm-release",
"release-major": "npm run release -- major",
"release-minor": "npm run release -- minor",
"release-patch": "npm run release -- patch",
"pretest": "semistandard",
"test": "mocha",
"test-travis": "istanbul cover ./node_modules/mocha/bin/_mocha -- -R spec"
"test": "mocha"
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"glob": "^7.0.3",
"istanbul": "^0.4.3",
"mocha": "^2.4.5",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"glob": "^7.1.6",
"mocha": "^6.2.2",
"npm-release": "^1.0.0",
"semistandard": "^7.0.5"
"nyc": "^15.0.0",
"semistandard": "^14.2.0"
},
"nyc": {
"check-coverage": true,
"reporter": "lcov",
"report-dir": "coverage"
},
"semistandard": {
"globals": [
Expand Down
34 changes: 13 additions & 21 deletions test/jsontosass.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var assert = require('chai').assert;
var fs = require('fs');
var glob = require('glob');
var jsontosass = require('../jsontosass.js');
const assert = require('chai').assert;
const fs = require('fs');
const glob = require('glob');
const jsontosass = require('../jsontosass.js');

function removeFiles () {
glob('test/*.s[ac]ss', function (er, files) {
Expand All @@ -12,10 +12,6 @@ function removeFiles () {
}

describe('jsontosass', function () {
after(function () {
// delete generated test sass files
removeFiles();
});
describe('module', function () {
it('is present', function () {
assert.isDefined(jsontosass, 'jsontosass is defined');
Expand Down Expand Up @@ -65,7 +61,7 @@ describe('jsontosass', function () {
assert.isFunction(jsontosass.convertFile);
});
it('should use defaultOptions when no options are given', function (done) {
var testFile = 'test/basic.scss';
const testFile = 'test/basic.scss';
jsontosass.convertFile('test/basic.json', testFile, function () {
fs.readFile(testFile, 'utf8', function (err, sass) {
if (err) throw err;
Expand All @@ -75,7 +71,7 @@ describe('jsontosass', function () {
});
});
it('basic file conversion', function (done) {
var testFile = 'test/basic.scss';
const testFile = 'test/basic.scss';
jsontosass.convertFile('test/basic.json', testFile, { prettify: false }, function () {
fs.readFile(testFile, 'utf8', function (err, sass) {
if (err) throw err;
Expand All @@ -85,7 +81,7 @@ describe('jsontosass', function () {
});
});
it('extended file conversion', function (done) {
var testFile = 'test/extended.scss';
const testFile = 'test/extended.scss';
jsontosass.convertFile('test/extended.json', testFile, { prettify: false }, function () {
fs.readFile(testFile, 'utf8', function (err, sass) {
if (err) throw err;
Expand All @@ -103,15 +99,13 @@ describe('jsontosass', function () {
assert.isFunction(jsontosass.convertFileSync);
});
it('basic file conversion', function () {
var sass;
jsontosass.convertFileSync('test/basic.json', 'test/basic.scss', { prettify: false });
sass = fs.readFileSync('test/basic.scss', 'utf8');
const sass = fs.readFileSync('test/basic.scss', 'utf8');
assert.equal(sass, '$key:value;');
});
it('extended file conversion', function () {
var sass;
jsontosass.convertFileSync('test/extended.json', 'test/extended.scss', { prettify: false });
sass = fs.readFileSync('test/extended.scss', 'utf8');
const sass = fs.readFileSync('test/extended.scss', 'utf8');
assert.equal(sass, "$key:(inner-key:(1,2,3),some-object:(color-black:#000,font-family:'Helvetica, sans-serif'));");
});
});
Expand Down Expand Up @@ -155,15 +149,15 @@ describe('jsontosass', function () {
}), '$key: (\n key2: value\n);');
});
it('respect indent with tabs', function () {
var options = {
const options = {
indent: 'tabs',
prettify: true
};
assert.equal(jsontosass.convert('{"key":{"key2":"value"}}', options), '$key: (\n\tkey2: value\n);');
assert.equal(jsontosass.convert('{"key":{"key2":{"key3":"value"}}}', options), '$key: (\n\tkey2: (\n\t\tkey3: value\n\t)\n);');
});
it('generate dashed variables instead of maps', function () {
var options = {
const options = {
indent: 'tabs',
prettify: true,
useMaps: false
Expand Down Expand Up @@ -201,21 +195,19 @@ describe('jsontosass', function () {
}), '$key: (\n key2: value\n)');
});
it('basic file conversion', function () {
var sass;
jsontosass.convertFile('test/basic.json', 'test/basic.sass', {
syntax: 'sass'
}, function () {
sass = fs.readFileSync('test/basic.sass');
const sass = fs.readFileSync('test/basic.sass');
assert.equal(sass, '$key: value');
});
});
it('extended file conversion', function () {
var sass;
jsontosass.convertFile('test/extended.json', 'test/extended.sass', {
indent: 'tabs',
syntax: 'sass'
}, function () {
sass = fs.readFileSync('test/extended.sass');
const sass = fs.readFileSync('test/extended.sass');
assert.equal(sass, "$key: (\n\tinner-key: (\n\t\t1,\n\t\t2,\n\t\t3\n\t),\n\tsome-object: (\n\t\tcolor-black: #000,\n\t\tfont-family: 'Helvetica, sans-serif'\n\t)\n)");
});
});
Expand Down
Loading

0 comments on commit e9be941

Please sign in to comment.