Skip to content

Commit

Permalink
fixup! fixup! fixup! fixup! Stringify refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
skad0 committed Feb 22, 2017
1 parent 901f70a commit 46397fe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,21 +323,17 @@ const decl = [
];

bemDecl.stringify(decl, { format: 'enb', exportType: 'commonjs' });

/* output → 
module.exports = {
"format": "enb",
"decl": [
{
"block": "block"
}
]
};
*/

// → module.exports = {
// → "format": "enb",
// → "decl": [
// → {
// → "block": "block"
// → }
// → ]
// → };
```



## Contributing

Please read [CONTRIBUTING.md](https://github.com/bem-sdk/bem-sdk/blob/master/CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us.
Expand Down
40 changes: 15 additions & 25 deletions lib/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ const JSON5 = require('json5');

const format = require('./format');

const generators = {
json5: (obj, space) => JSON5.stringify(obj, null, space),
json: (obj, space) => JSON.stringify(obj, null, space),
commonjs: (obj, space) => `module.exports = ${JSON.stringify(obj, null, space)};\n`,
es2015: (obj, space) => `export default ${JSON.stringify(obj, null, space)};\n`
};
generators.es6 = generators.es2015;

/**
* Create string representation of declaration
*
* @param {BemCell[]} decl source declaration
* @param {BemCell[]} decl - source declaration
* @param {Object} opts - additional options
* @param {String} opts.format - format of declaration (v1, v2, enb)
* @param {String} [opts.exportType=json5] - defines how to wrap result (commonjs, json5, json, es6|es2015)
Expand All @@ -20,38 +28,20 @@ module.exports = function (decl, opts) {
const options = Object.assign(defaults, opts);

assert(options.format, 'You must declare target format');
assert(generators.hasOwnProperty(options.exportType), 'Specified format isn\'t supported');

Array.isArray(decl) || (decl = [decl]);

const space = options.space;
const declFieldHash = {
const fieldByFormat = {
v1: 'blocks',
enb: 'decl',
v2: 'deps'
};
const declFieldName = declFieldHash[options.format];
const formatedDecl = format(decl, { format: options.format });
const stringifiedObj = {
format: options.format
format: options.format,
[fieldByFormat[options.format]]: formatedDecl
};
stringifiedObj[declFieldName] = formatedDecl;

let exportedDecl;

switch (options.exportType) {
case 'json5':
exportedDecl = JSON5.stringify(stringifiedObj, null, space);
break;
case 'json':
exportedDecl = JSON.stringify(stringifiedObj, null, space);
break;
case 'commonjs':
exportedDecl = `module.exports = ${JSON.stringify(stringifiedObj, null, space)};\n`;
break;
case 'es6': case 'es2015':
exportedDecl = `export default ${JSON.stringify(stringifiedObj, null, space)};\n`;
break;
}

return exportedDecl;

return generators[options.exportType](stringifiedObj, options.space);
};

0 comments on commit 46397fe

Please sign in to comment.