Skip to content

Commit

Permalink
Shrinked API to convert, convertFile, convertFileSync and options. Re…
Browse files Browse the repository at this point in the history
…duced tests to open API methods.
  • Loading branch information
Christian Stuff committed May 4, 2016
1 parent 86a6250 commit 18545a9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 101 deletions.
104 changes: 52 additions & 52 deletions jsontosass.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var fs = require('fs');
var repeat = require('repeat-string');

var JsonToSass = function () {
this.nestLevel = 0;
var nestLevel = 0;

this.defaultOptions = {
var defaultOptions = {
indent: 4, // or 'tabs'
prettify: true,
spaceAfterColon: 1,
Expand All @@ -25,12 +25,12 @@ var JsonToSass = function () {
this.convert = function (json, options) {
var parsedJSON;

this.mergeOptions(this.defaultOptions);
this.mergeOptions(options);
mergeOptions(defaultOptions);
mergeOptions(options);

parsedJSON = JSON.parse(json);

return this.convertObject(parsedJSON);
return convertObject(parsedJSON);
};
/**
* Converts a given JSON file represented by filename fileIn to a Sass file represented by fileOut.
Expand Down Expand Up @@ -61,127 +61,127 @@ var JsonToSass = function () {
* @param {Object} obj The JSON object.
* @return {String} The Sass map as a string.
*/
this.convertObject = function (obj) {
function convertObject (obj) {
var map = [];

for (var key in obj) {
switch (typeof obj[key]) {
case 'object':
this.nestLevel++;
nestLevel++;
if (this.options.useMaps || obj[key].hasOwnProperty('length')) {
map.push(key + this.createColon() + this.convertObject(obj[key]));
map.push(key + createColon() + convertObject(obj[key]));
} else {
map.push(key + '-' + this.convertObject(obj[key]));
map.push(key + '-' + convertObject(obj[key]));
}
this.nestLevel--;
nestLevel--;
break;
default:
if (key.match(/^[0-9]+$/)) {
map.push(obj[key]);
} else {
map.push(key + this.createColon() + obj[key]);
map.push(key + createColon() + obj[key]);
}
break;
}
}

return this.objectToString(map);
};
return objectToString(map);
}

/**
* Generates the leading spaces/tabs to indent the code
* @private
* @param {int} Indentation amount
* @return {String} The spaces
*/
this.createIndent = function (indentation) {
function createIndent (indentation) {
return this.options.indent === 'tabs'
? repeat('\t', indentation)
: repeat(' ', indentation * this.options.indent);
};
}

this.createColon = function () {
if (!this.shouldPrettify()) {
function createColon () {
if (!shouldPrettify()) {
return ':';
}
return repeat(' ', this.options.spaceBeforeColon) +
':' + repeat(' ', this.options.spaceAfterColon);
};
}

this.createLineSplit = function () {
function createLineSplit () {
var join;

if (this.nestLevel > 0) {
if (nestLevel > 0) {
join = ',';

if (this.shouldPrettifyMap()) {
join += '\n' + this.createIndent(this.nestLevel);
if (shouldPrettifyMap()) {
join += '\n' + createIndent(nestLevel);
}
} else {
join = ';';
if (this.shouldPrettify()) {
if (shouldPrettify()) {
join += '\n';
}
join += '$';
}
return join;
};
}

this.createStatementTerminator = function () {
function createStatementTerminator () {
return this.options.syntax === 'scss' ? ';' : '';
};
}

this.createObjectAfter = function (map) {
if (this.nestLevel === 0) {
return this.createStatementTerminator();
function createObjectAfter (map) {
if (nestLevel === 0) {
return createStatementTerminator();
}
var after = '';
if (this.shouldPrettifyMap()) {
after += '\n' + this.createIndent(this.nestLevel - 1);
if (shouldPrettifyMap()) {
after += '\n' + createIndent(nestLevel - 1);
}
if (this.options.useMaps || map.length > 1) {
after += ')';
}
return after;
};
}

this.createObjectBefore = function (map) {
if (this.nestLevel === 0) {
function createObjectBefore (map) {
if (nestLevel === 0) {
return '$';
}
var before = '';
if (this.options.useMaps || map.length > 1) {
before += '(';
}
if (this.shouldPrettifyMap()) {
before += '\n' + this.createIndent(this.nestLevel);
if (shouldPrettifyMap()) {
before += '\n' + createIndent(nestLevel);
}
return before;
};
}

this.objectToString = function (map) {
return this.createObjectBefore(map) + map.join(this.createLineSplit()) + this.createObjectAfter(map);
};
function objectToString (map) {
return createObjectBefore(map) + map.join(createLineSplit()) + createObjectAfter(map);
}

this.mergeOptions = function (options) {
function mergeOptions (options) {
options = options || {};

Object.keys(options).forEach(function (key) {
this.validateOption(key, options[key]);
}.bind(this));
validateOption(key, options[key]);
});

this.options = extend({}, this.defaultOptions, options);
};
this.options = extend({}, defaultOptions, options);
}

this.shouldPrettify = function () {
function shouldPrettify () {
return (this.options.prettify || this.options.syntax === 'sass');
};
}

this.shouldPrettifyMap = function () {
function shouldPrettifyMap () {
return (this.options.prettify && this.options.useMaps) || this.options.syntax === 'sass';
};
}

this.validateOption = function (key, value) {
function validateOption (key, value) {
switch (key) {
case 'syntax':
if (['sass', 'scss'].indexOf(value.toLowerCase()) === -1) {
Expand All @@ -191,9 +191,9 @@ var JsonToSass = function () {
default:
return value;
}
};
}

this.mergeOptions(this.defaultOptions);
mergeOptions(defaultOptions);
};

module.exports = new JsonToSass();
94 changes: 45 additions & 49 deletions test/jsontosass.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,39 @@ describe('jsontosass', function () {
assert.isObject(jsontosass, 'jsontosass is an object');
});
});
describe('createColon()', function () {
it('creates configured spaces before colon', function () {
jsontosass.mergeOptions({
prettify: true,
spaceBeforeColon: 4
});
assert.equal(jsontosass.createColon(), ' : ');
});
it('creates configured spaces after colon', function () {
jsontosass.mergeOptions({
prettify: true,
spaceAfterColon: 4
});
assert.equal(jsontosass.createColon(), ': ');
});
});
describe('convertObject()', function () {
beforeEach(function () {
jsontosass.mergeOptions({
prettify: false
});
});
it('is present', function () {
assert.isFunction(jsontosass.convertObject);
});
it('creates Sass variable named $key on root level', function () {
assert.equal(jsontosass.convertObject({
key: 'value'
}), '$key:value;');
});
it('creates Sass variables named by JSON key for multiple entries', function () {
assert.equal(jsontosass.convertObject({
key: 'value',
key2: 'otherValue'
}), '$key:value;$key2:otherValue;');
});
it('creates Sass map for nested object', function () {
assert.equal(jsontosass.convertObject({
key: {
key2: 'value'
}
}), '$key:(key2:value);');
});
it('creates Sass list for JSON array', function () {
assert.equal(jsontosass.convertObject({
key: [1, 2, 3]
}), '$key:(1,2,3);');
});
});
// describe('convertObject()', function () {
// beforeEach(function () {
// jsontosass.mergeOptions({
// prettify: false
// });
// });
// it('is present', function () {
// assert.isFunction(jsontosass.convertObject);
// });
// it('creates Sass variable named $key on root level', function () {
// assert.equal(jsontosass.convertObject({
// key: 'value'
// }), '$key:value;');
// });
// it('creates Sass variables named by JSON key for multiple entries', function () {
// assert.equal(jsontosass.convertObject({
// key: 'value',
// key2: 'otherValue'
// }), '$key:value;$key2:otherValue;');
// });
// it('creates Sass map for nested object', function () {
// assert.equal(jsontosass.convertObject({
// key: {
// key2: 'value'
// }
// }), '$key:(key2:value);');
// });
// it('creates Sass list for JSON array', function () {
// assert.equal(jsontosass.convertObject({
// key: [1, 2, 3]
// }), '$key:(1,2,3);');
// });
// });
describe('convertFile()', function () {
after(function () {
removeFiles();
Expand Down Expand Up @@ -143,6 +127,18 @@ describe('jsontosass', function () {
it('returns a string', function () {
assert.typeOf(jsontosass.convert('{}'), 'string');
});
it('creates configured spaces before colon', function () {
assert.equal(jsontosass.convert('{"key":"value"}', {
prettify: true,
spaceBeforeColon: 4
}), '$key : value;');
});
it('creates configured spaces after colon', function () {
assert.equal(jsontosass.convert('{"key":"value"}', {
prettify: true,
spaceAfterColon: 4
}), '$key: value;');
});
it('returns Sass variables in own line when prettify is enabled', function () {
assert.equal(jsontosass.convert('{"key":"value","key2":"otherValue"}', {
prettify: true
Expand Down

0 comments on commit 18545a9

Please sign in to comment.