Permalink
Browse files

Adding plain function support

  • Loading branch information...
davidkpiano committed Jul 4, 2015
1 parent 4907b55 commit 36c0b4a0f06b5bf87a8cd4122bd4ee4fdb54b447
Showing with 366 additions and 2 deletions.
  1. +1 −0 .gitignore
  2. +189 −0 dist/index.js
  3. +15 −0 index.js
  4. +9 −2 package.json
  5. +148 −0 src/index.js
  6. +4 −0 test.scss
View
@@ -0,0 +1 @@
node_modules/
View
@@ -0,0 +1,189 @@
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var _nodeSass = require('node-sass');
var _nodeSass2 = _interopRequireDefault(_nodeSass);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
var sassport = function sassport(imports) {
if (!Array.isArray(imports)) {
imports = [imports];
}
return new Renderer(imports);
};
sassport.functions = function (funcMap) {
var sassportInstance = new Sassport();
return sassportInstance.functions(funcMap);
};
sassport.plain = function (pureFunc) {
return function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
for (var i = 0; i < args.length; i++) {
args[i] = convertSassValue(args[i]);
}
var result = pureFunc.apply(undefined, args);
return inferPlainValue(result);
};
};
var inferPlainValue = function inferPlainValue(value) {
if (value === null || value === undefined) {
return _nodeSass2['default'].types.Null.NULL;
}
if (_lodash2['default'].isString(value)) {
return _nodeSass2['default'].types.String(value);
}
if (_lodash2['default'].isNumber(value)) {
return _nodeSass2['default'].types.Number(value);
}
if (_lodash2['default'].isBoolean(value)) {
return value ? _nodeSass2['default'].types.Boolean.TRUE : _nodeSass2['default'].types.boolean.FALSE;
}
if (_lodash2['default'].isArray(value)) {
var _length = value.length;
var result = _nodeSass2['default'].types.List(_length);
for (var i = 0; i < _length; i++) {
result = result.setValue(i, inferPlainValue(value[i]));
}
return result;
}
if (_lodash2['default'].isObject(value)) {
if (value instanceof Map) {
var result = _nodeSass2['default'].types.Map(value.size());
var keys = value.keys();
for (var i = 0; i < keys.length; i++) {
var key = inferPlainValue(key);
var val = value.getValue(key);
result.setValue(key, val);
}
return result;
}
}
};
var convertSassValue = function convertSassValue(value) {
if (!value.getR && !value.getValue) {
return null;
}
if (value.getKey) {
var _length2 = value.getLength();
var result = new Map();
for (var i = 0; i < _length2; i++) {
var key = value.getKey(i);
var val = value.getValue(i);
result.set(convertSassValue(key), convertSassValue(val));
}
return result;
}
if (value.getLength) {
var _length3 = value.getLength();
var result = [];
for (var i = 0; i < _length3; i++) {
result.push(value.getValue(i));
}
return result.map(function (item) {
return convertSassValue(item);
});
}
if (value.getR) {
return {
r: value.getR(),
g: value.getG(),
b: value.getB(),
a: value.getA()
};
}
return value.getValue();
};
var Sassport = (function () {
function Sassport() {
_classCallCheck(this, Sassport);
this.options = {};
}
_createClass(Sassport, [{
key: 'functions',
value: function functions(funcMap) {
_lodash2['default'].extend(this.options, funcMap);
return this.options;
}
}]);
return Sassport;
})();
var Renderer = (function () {
function Renderer() {
var sassports = arguments[0] === undefined ? [] : arguments[0];
_classCallCheck(this, Renderer);
this.options = {};
this.functions = {};
this._includeSassports(sassports);
}
_createClass(Renderer, [{
key: '_includeSassports',
value: function _includeSassports(sassports) {
var _this = this;
sassports.forEach(function (sassport) {
var functions = sassport.functions;
_lodash2['default'].extend(_this, functions);
}, this);
}
}]);
return Renderer;
})();
exports['default'] = sassport;
module.exports = exports['default'];
View
@@ -0,0 +1,15 @@
var sass = require('node-sass');
var sassport = require('./dist/index.js');
var sassOptions = {
file: './test.scss',
functions: {
'saypure($message)': sassport.plain(function(message) {
return 'Hi, '+message+'!';
})
}
};
sass.render(sassOptions, function(err, result) {
console.log(result.css.toString());
});
View
@@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "babel -w ./src -d ./dist"
},
"repository": {
"type": "git",
@@ -15,5 +15,12 @@
"bugs": {
"url": "https://github.com/davidkpiano/sassport/issues"
},
"homepage": "https://github.com/davidkpiano/sassport#readme"
"homepage": "https://github.com/davidkpiano/sassport#readme",
"devDependencies": {
"babel": "^5.6.14",
"lodash": "^3.10.0"
},
"dependencies": {
"node-sass": "^3.2.0"
}
}
View
@@ -0,0 +1,148 @@
import sass from 'node-sass';
import _ from 'lodash';
let sassport = function(imports) {
if (!Array.isArray(imports)) {
imports = [imports];
}
return new Renderer(imports);
};
sassport.functions = function(funcMap) {
let sassportInstance = new Sassport();
return sassportInstance.functions(funcMap);
};
sassport.plain = function(pureFunc) {
return function(...args) {
for (let i = 0; i < args.length; i++) {
args[i] = convertSassValue(args[i]);
}
let result = pureFunc(...args);
return inferPlainValue(result);
}
}
let inferPlainValue = function(value) {
if (value === null || value === undefined) {
return sass.types.Null.NULL;
}
if (_.isString(value)) {
return sass.types.String(value);
}
if (_.isNumber(value)) {
return sass.types.Number(value);
}
if (_.isBoolean(value)) {
return value ? sass.types.Boolean.TRUE : sass.types.boolean.FALSE;
}
if (_.isArray(value)) {
let length = value.length;
let result = sass.types.List(length);
for (let i = 0; i < length; i++) {
result = result.setValue(i, inferPlainValue(value[i]));
}
return result;
}
if (_.isObject(value)) {
if (value instanceof Map) {
let result = sass.types.Map(value.size());
let keys = value.keys();
for (let i = 0; i < keys.length; i++) {
let key = inferPlainValue(key);
let val = value.getValue(key);
result.setValue(key, val);
}
return result;
}
}
}
let convertSassValue = function(value) {
if (!value.getR && !value.getValue) {
return null;
}
if (value.getKey) {
let length = value.getLength();
let result = new Map();
for (let i = 0; i < length; i++) {
let key = value.getKey(i);
let val = value.getValue(i);
result.set(convertSassValue(key), convertSassValue(val));
}
return result;
}
if (value.getLength) {
let length = value.getLength();
let result = [];
for (let i = 0; i < length; i++) {
result.push(value.getValue(i));
}
return result.map(item => convertSassValue(item));
}
if (value.getR) {
return {
r: value.getR(),
g: value.getG(),
b: value.getB(),
a: value.getA()
};
}
return value.getValue();
}
class Sassport {
constructor() {
this.options = {};
}
functions(funcMap) {
_.extend(this.options, funcMap);
return this.options;
}
}
class Renderer {
constructor(sassports = []) {
this.options = {};
this.functions = {};
this._includeSassports(sassports);
}
_includeSassports(sassports) {
sassports.forEach(sassport => {
let {functions} = sassport;
_.extend(this, functions);
}, this);
}
}
export default sassport;
View
@@ -0,0 +1,4 @@
.foo {
bar: baz;
message: saypure('billybob');
}

0 comments on commit 36c0b4a

Please sign in to comment.