dss-parser-variable
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -16,4 +16,4 @@ 'use strict'; | ||
var fileVariables = {}, | ||
fileVariablesRx = /^[\$|@]([a-zA-Z0-9_]+):([^\;]+)\;/gim, | ||
lineSplitRx = /((\s|-)+)/, | ||
fileVariablesRx = /^[\$|@]([a-zA-Z0-9_-]+):([^\;]+)\;/gim, | ||
lineSplitRx = /(\s(-\s)?)/, | ||
variables = {}, | ||
@@ -23,2 +23,3 @@ match, hash, tokens, name; | ||
return function(i, line, block, css) { | ||
// Extract all defined variables in this CSS file (once per file) | ||
hash = crypto.createHash('md5').update(css).digest('hex'); | ||
@@ -32,3 +33,3 @@ if (!fileVariables[hash]) { | ||
// Extract name and any delimiter with description | ||
// Extract variable name and description from comment block | ||
tokens = line.split(lineSplitRx, 2); | ||
@@ -39,3 +40,3 @@ name = tokens[0].trim(); | ||
name: name, | ||
// Description is line with name and any delimiter replaced | ||
// Description is line with variable name and any delimiter replaced | ||
description: line.replace(tokens.join(''), ''), | ||
@@ -42,0 +43,0 @@ value: variables[name] |
{ | ||
"name": "dss-parser-variable", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A variable parser for DSS (Documented Stylesheets)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,26 +8,32 @@ dss-parser-variable [![NPM version][npm-image]][npm-url] [![Dependency Status][depstat-image]][depstat-url] [![Build Status][travis-image]][travis-url] | ||
In a file `test.scss`: | ||
```css | ||
/** | ||
* @name Colours | ||
* @variable white | ||
* @variable blue - Sky blue | ||
* @variable red | ||
* @variable dark-red Button outlines | ||
*/ | ||
$blue: #0000FF; | ||
$red: #FF0000; | ||
$dark-red: #FF0000; | ||
$black: #000000; | ||
``` | ||
In JavaScript: | ||
```javascript | ||
var dss = require('dss'); | ||
var variableParser = require('dss-parser-variable'); | ||
var fs = require('fs'), | ||
dss = require('dss'), | ||
variableParser = require('dss-parser-variable'); | ||
dss.parser('variable', variableParser()); | ||
var lines = fs.readFileSync('styles.css'), | ||
var scss = fs.readFileSync('test.scss'), | ||
options = {}, | ||
callback = function(parsed){ | ||
console.log(parsed); | ||
console.log(JSON.stringify(parsed, true, 4)); | ||
}; | ||
dss.parse(lines, options, callback); | ||
dss.parse(scss, options, callback); | ||
``` | ||
@@ -37,9 +43,9 @@ | ||
```javascript | ||
```json | ||
{ | ||
blocks: [{ | ||
name: 'Colours', | ||
variable: [ | ||
{ name: 'blue', description: 'Sky blue', value: '#0000FF' }, | ||
{ name: 'red', description: '', value: '#FF0000' } | ||
"blocks": [{ | ||
"name": "Colours", | ||
"variable": [ | ||
{ "name": "blue", "description": "Sky blue", "value": "#0000FF" }, | ||
{ "name": "dark-red", "description": "Button outlines", "value": "#FF0000" } | ||
] | ||
@@ -51,2 +57,4 @@ }] | ||
Note there is no entry for `white` because the variable does not exist in the file, and there is no entry for `black` because there is no description in the DSS block. | ||
[Documented Style Sheets]:https://github.com/darcyclarke/DSS | ||
@@ -53,0 +61,0 @@ |
'use strict'; | ||
/* globals describe, it */ | ||
var fs = require('fs'); | ||
var expect = require('chai').expect; | ||
@@ -56,2 +57,67 @@ var dssParserVariable = require('../'); | ||
}); | ||
it('should allow name and description to be space separated', function(done) { | ||
var css = '/**\n * @variable blue Sky blue\n */\n$blue: #0000FF;\n'; | ||
dss.parser('variable', dssParserVariable()); | ||
dss.parse(css, {}, function(parsedDss) { | ||
expect(parsedDss.blocks[0].variable.name).to.equal('blue'); | ||
expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); | ||
expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue'); | ||
done(); | ||
}); | ||
}); | ||
it('should allow hyphens in descriptions', function(done) { | ||
var css = '/**\n * @variable blue Sky blue - my favourite colour\n */\n$blue: #0000FF;\n'; | ||
dss.parser('variable', dssParserVariable()); | ||
dss.parse(css, {}, function(parsedDss) { | ||
expect(parsedDss.blocks[0].variable.name).to.equal('blue'); | ||
expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); | ||
expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue - my favourite colour'); | ||
done(); | ||
}); | ||
}); | ||
it('should allow space separation of name/description with hyphens in descriptions', function(done) { | ||
var css = '/**\n * @variable blue - Sky blue - my favourite colour\n */\n$blue: #0000FF;\n'; | ||
dss.parser('variable', dssParserVariable()); | ||
dss.parse(css, {}, function(parsedDss) { | ||
expect(parsedDss.blocks[0].variable.name).to.equal('blue'); | ||
expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); | ||
expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue - my favourite colour'); | ||
done(); | ||
}); | ||
}); | ||
it('should allow hyphens in variable names', function(done) { | ||
var css = '/**\n * @variable sky-blue - Sky blue\n */\n$sky-blue: #0000FF;\n'; | ||
dss.parser('variable', dssParserVariable()); | ||
dss.parse(css, {}, function(parsedDss) { | ||
expect(parsedDss.blocks[0].variable.name).to.equal('sky-blue'); | ||
expect(parsedDss.blocks[0].variable.value).to.equal('#0000FF'); | ||
expect(parsedDss.blocks[0].variable.description).to.equal('Sky blue'); | ||
done(); | ||
}); | ||
}); | ||
it('should support the example in the README', function(done) { | ||
var readme = fs.readFileSync('README.md'); | ||
var css = readme.toString().match(/```css([^`])+/)[0].split('\n').slice(1).join('\n'); | ||
var expected = JSON.parse(readme.toString().match(/```json([^`])+/)[0].split('\n').slice(1).join('\n')); | ||
dss.parse(css, {}, function(parsedDss) { | ||
expect(parsedDss.blocks.length).to.equal(expected.blocks.length); | ||
expect(parsedDss.blocks[0].variable.length).to.equal(expected.blocks[0].variable.length); | ||
expect(parsedDss.blocks[0].variable[0].name).to.equal(expected.blocks[0].variable[0].name); | ||
expect(parsedDss.blocks[0].variable[0].value).to.equal(expected.blocks[0].variable[0].value); | ||
expect(parsedDss.blocks[0].variable[0].description).to.equal(expected.blocks[0].variable[0].description); | ||
expect(parsedDss.blocks[0].variable[1].name).to.equal(expected.blocks[0].variable[1].name); | ||
expect(parsedDss.blocks[0].variable[1].value).to.equal(expected.blocks[0].variable[1].value); | ||
expect(parsedDss.blocks[0].variable[1].description).to.equal(expected.blocks[0].variable[1].description); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11060
143
69
1