Skip to content

Commit

Permalink
feat(parser): add optional argument iterator
Browse files Browse the repository at this point in the history
The iterator is useful in building a customized output.
  • Loading branch information
remarkablemark committed Nov 26, 2017
1 parent b3312d1 commit a3deea8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
30 changes: 20 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
var parse = require('css/lib/parse');

/**
* Parses inline style to object (server).
* Parses inline style.
*
* Example: 'color:red' => { color: 'red' }
*
* @param {String} style
* @return {Object|null}
* @param {String} style - The inline style.
* @param {Function} [iterator] - The iterator function.
* @return {null|Object}
*/
module.exports = function parseInlineStyleServer(style) {
module.exports = function parseInlineStyle(style, iterator) {
if (!style || typeof style !== 'string') return null;

// make sure to wrap declarations in placeholder
var declarations = parse('p{' + style + '}').stylesheet.rules[0].declarations;
var output = {};
var declaration, property, value;

declarations.forEach(function(declaration) {
var value = declaration.value;
if (value) {
output[declaration.property] = value;
var output = null;
var hasIterator = typeof iterator === 'function';

for (var i = 0, len = declarations.length; i < len; i++) {
declaration = declarations[i];
property = declaration.property;
value = declaration.value;

if (hasIterator) {
iterator(property, value, declaration);
} else if (value) {
output || (output = {});
output[property] = value;
}
});
}

return output;
};
2 changes: 1 addition & 1 deletion test/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ const styles = [
// missing value
{
style: 'z-index:',
expected: {},
expected: null,
},

// missing property
Expand Down
33 changes: 27 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
'use strict';

const assert = require('assert');
const css = require('css');
const cases = require('./cases');
const parser = require('../')

describe('parser', () => {
cases.default.forEach(({ style, expected }) => {

// error case
describe(`when style=\`${style}\``, () => {
if (expected === Error) {
it('throws error', () => {
assert.throws(() => parser(style), Error);
});

} else {
it(`returns ${JSON.stringify(expected)}`, () => {
assert.deepEqual(parser(style), expected);
});
return;
}

// normal case
it(`returns ${JSON.stringify(expected)}`, () => {
assert.deepEqual(parser(style), expected);
});
});
});

describe('when iterator is passed', () => {
it('returns null', () => {
const style = cases.styles[0];
const iterator = () => {};
assert.equal(parser(style, iterator), null);
});

it('passes the arguments: name, value, declaration', () => {
const style = 'color: #f00;';
parser(style, (name, value, declaration) => {
assert.equal(name, 'color');
assert.equal(value, '#f00');
assert.deepEqual(
declaration,
css.parse(`p{${style}}`).stylesheet.rules[0].declarations[0]
);
});
});
});
});

0 comments on commit a3deea8

Please sign in to comment.