Skip to content
Closed
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
23 changes: 0 additions & 23 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Example source file `theme.json`:
"array": [1, 2, 3],
"object": {
"foo": "bar"
}
},
"null": null
}

Expand Down
18 changes: 9 additions & 9 deletions src/bin/json-sass → bin/json-sass
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ let jsonSass = require('../jsonSass');
let fs = require('fs');
let path = require('path');
let minimist = require('minimist');
import { Readable } from 'stream';
let Readable = require('stream').Readable;

let argv = minimist(process.argv.slice(2), {
alias: {
i: 'infile',
o: 'outfile',
h: 'help',
p: 'prefix',
s: 'suffix',
},
default: { i: '-', o: '-' }
alias: {
i: 'infile',
o: 'outfile',
h: 'help',
p: 'prefix',
s: 'suffix',
},
default: { i: '-', o: '-' }
});

if (argv.help) return showHelp(0);
Expand Down
27 changes: 22 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
{
"name": "json-sass",
"version": "1.3.5",
"description": "Transform JSON into scss syntax Sass.",
"description": "Transform JSON into SCSS variable syntax.",
"main": "lib/jsonSass.js",
"bin": "lib/bin/json-sass",
"bin": "bin/json-sass",
"scripts": {
"test": "make test",
"prepublish": "npm test"
"build": "rm -rf lib && babel ./src -d ./lib",
"pretest": "npm run build",
"test": "mocha --require ./lib/test-init.js lib/**/__tests__/*-test.js",
"prepublish": "in-install || npm test"
},
"keywords": [
"sass",
"JSON"
],
"author": "Andrew Clark <[email protected]>",
"license": "ISC",
"contributors": [
"Charlie Robbins <[email protected]>"
],
"repository": {
"type": "git",
"url": "https://github.com/acdlite/json-sass.git"
},
"babel": {
"presets": [
"babel-preset-es2015"
]
},
"dependencies": {
"babel": "~4.1.0",
"in-publish": "^2.0.0",
"lodash": "~2.4.1",
"lodash-node": "~2.4.1",
"minimist": "~1.1.0",
"object-assign": "~2.0.0",
"through2": "~0.6.3"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-polyfill": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"chai": "~1.9.2",
"mocha": "~2.0.1"
}
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/jsToSassString-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var foo = new Foo();

describe('JS to Sass', function() {
it('should handle strings', function() {
expect(jsToSassString('foo')).to.equal('foo');
expect(jsToSassString('foo')).to.equal('"foo"');
});

it('should handle booleans', function() {
Expand Down Expand Up @@ -49,6 +49,6 @@ describe('JS to Sass', function() {
},
};

expect(jsToSassString(obj)).to.equal('(\n foo: bar,\n bar: (\n baz: foo\n )\n)')
expect(jsToSassString(obj)).to.equal('(\n foo: "bar",\n bar: (\n baz: "foo"\n )\n)')
})
});
18 changes: 13 additions & 5 deletions src/jsToSassString.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function jsToSassString(value) {
case 'number':
return value.toString();
case 'string':
return value;
return quoteString(value);
case 'object':
if (isPlainObject(value)) {
indentLevel += 1;
Expand All @@ -39,10 +39,11 @@ function jsToSassString(value) {
return result;
}
else if (isArray(value)) {
let sassVals = [
for (v of value) if (isNotUndefined(v))
_jsToSassString(v, indentLevel)
];
const sassVals = value.map(v => {
if (isNotUndefined(v)) {
return _jsToSassString(v, indentLevel)
}
}).filter(Boolean);

return '(' + sassVals.join(', ') + ')';
}
Expand All @@ -68,4 +69,11 @@ function isNotUndefined(value) {
return typeof value !== 'undefined';
}

function quoteString(value) {
if (value.substring(0,1) === '#') {
return value;
}
return "\"" + value + "\"";
}

export default jsToSassString;
17 changes: 14 additions & 3 deletions src/jsonSass.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,29 @@ let DEFAULTS = {
suffix: ';',
};

function jsonSass(options) {
let options = assign({}, DEFAULTS, options);
function jsonSass(opts) {
const options = assign({}, DEFAULTS, opts);

return through(function(chunk, enc, callback) {
let jsValue = JSON.parse(chunk);
let jsValue;
try {
jsValue = JSON.parse(chunk);
}
catch (err) {
return callback(err);
}
let sassString = jsToSassString(jsValue);
sassString = options.prefix + sassString + options.suffix;
this.push(sassString);
callback();
});
}

//
// Cover all require use cases.
//
jsonSass.convertJs = jsToSassString;
module.exports = jsonSass;

export default jsonSass;
export { jsToSassString as convertJs };
5 changes: 2 additions & 3 deletions src/test-init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'babel/polyfill';
require('babel-polyfill');

import { expect } from 'chai';
global.expect = expect;
global.expect = require('chai').expect;