Refactor.

This commit is contained in:
Alexandre Gigliotti
2015-12-01 09:50:47 -08:00
parent 3881565a28
commit b3d479224e
4 changed files with 157 additions and 173 deletions

View File

@@ -10,8 +10,6 @@ const url = require('url');
const collapseWhitespace = str => string(str).collapseWhitespace().toString();
const forEachIndexed = R.addIndex(R.forEach);
const format = (path) => url.format(path);
const parse = (path) => url.parse(path);
const resolve = path.resolve;
const augmentError = (error, filename, files) => {
@@ -24,13 +22,12 @@ const augmentError = (error, filename, files) => {
* Returns url path without query string and hash if present.
*
* @param path
*
* @returns path
*/
const cleanUrl = R.pipe(
parse,
url.parse,
R.pick(['protocol', 'host', 'pathname']),
format,
url.format,
decodeURI
);
/**
@@ -68,16 +65,14 @@ const inlineStyles = ($, filename) => {
try {
const styles = $('style')
.toArray();
const contents = R.map(style => {
const css = $(style).html();
const result = inlineUrl(filename, css);
files = R.concat(files, result.files);
return result.css;
}, styles);
forEachIndexed((style, index) => $(style).html(contents[index]), styles);
const replaceStyle = (style, index) => $(style).html(contents[index]);
forEachIndexed(replaceStyle, styles);
return { $, files };
}
catch (error) { throw augmentError(error, filename, files); }
@@ -95,7 +90,6 @@ const inlineStyleAttributes = ($, filename) => {
const elements = $('*')
.filter('[style]')
.toArray();
const styles = R.map(element => {
var style = $(element).attr('style');
const rule = wrap(style);
@@ -104,9 +98,8 @@ const inlineStyleAttributes = ($, filename) => {
style = R.pipe( collapseWhitespace, unwrap )(result.css);
return style;
}, elements);
forEachIndexed((element, index) => $(element).attr('style', styles[index]), elements);
const replaceElementStyle = (element, index) => $(element).attr('style', styles[index]);
forEachIndexed(replaceElementStyle, elements);
return { $, files };
}
catch (error) { throw augmentError(error, filename, files); }