Skip to content

Commit

Permalink
Run prettier and build
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Brandau committed Oct 4, 2018
1 parent 1845a40 commit d63b2c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
14 changes: 8 additions & 6 deletions lib/index.js
Expand Up @@ -23,10 +23,10 @@ var CssShortener = function CssShortener(options) {
orig = capturingGroup.substr(1); // Remove dot infront of class name
// If the ignorePrefix option is set and the current class starts with the prefix, trim the prefix off and ignore the class.

if (t._options.ignorePrefix && orig.startsWith(t._options.ignorePrefix)) return t._options.trimIgnorePrefix ? ".".concat(orig.substr(t._options.ignorePrefix.length)) : ".".concat(orig);
if (t._classNameMap[orig] != null) id = t._classNameMap[orig]; // Use mapped class name
else id = t._classNameMap[orig] = t._idGenerator(); // Generate and map new class name
if (t._options.ignorePrefix && orig.startsWith(t._options.ignorePrefix)) return t._options.trimIgnorePrefix ? ".".concat(orig.substr(t._options.ignorePrefix.length)) : ".".concat(orig); // Use already mapped class name

if (t._classNameMap[orig] != null) id = t._classNameMap[orig]; // Generate and map new class name
else id = t._classNameMap[orig] = t._idGenerator();
return ".".concat(id);
};

Expand All @@ -52,7 +52,8 @@ var CssShortener = function CssShortener(options) {
this.importMap = function (map, override) {
for (var orig in map) {
if (this._classNameMap[orig] != null) {
if (override === true) this._classNameMap[orig] = map[orig]; // Override mapped class name
// Override mapped class name
if (override === true) this._classNameMap[orig] = map[orig];
} else this._classNameMap[orig] = map[orig]; // Import class name

}
Expand All @@ -62,8 +63,9 @@ var CssShortener = function CssShortener(options) {
return replaceStream(CLASS_NAME_REGEX, replaceCss);
};

this.replaceCss = function (css) {
return css.replace(CLASS_NAME_REGEX, replaceCss);
this.replaceCss = function (css, sourceMap) {
var replacedCss = css.replace(CLASS_NAME_REGEX, replaceCss);
return replacedCss;
};

this.htmlStream = function () {
Expand Down
6 changes: 3 additions & 3 deletions src/idGenerator.js
Expand Up @@ -12,8 +12,8 @@ var IdGenerator = function(alphabet) {
var res = generateId(options);
while (/^[0-9-].*$/.test(res)) res = generateId(options);
return res;
}
}
};
};

var generateId = function(options) {
var res = '';
Expand All @@ -31,6 +31,6 @@ var generateId = function(options) {
}

return res;
}
};

module.exports = IdGenerator;
29 changes: 20 additions & 9 deletions src/index.js
Expand Up @@ -7,8 +7,10 @@ const HTML_CLASS_REGEX = /class="([\w-\s]*)"/g;
const CssShortener = function(options) {
if (!options) options = {};
this._options = options;
if (!this._options.hasOwnProperty('ignorePrefix')) this._options.ignorePrefix = 'ignore-';
if (!this._options.hasOwnProperty('trimIgnorePrefix')) this._options.trimIgnorePrefix = true;
if (!this._options.hasOwnProperty('ignorePrefix'))
this._options.ignorePrefix = 'ignore-';
if (!this._options.hasOwnProperty('trimIgnorePrefix'))
this._options.trimIgnorePrefix = true;
this._idGenerator = new IdGenerator(this._options.alphabet);
this._classNameMap = {};

Expand All @@ -20,10 +22,14 @@ const CssShortener = function(options) {

// If the ignorePrefix option is set and the current class starts with the prefix, trim the prefix off and ignore the class.
if (t._options.ignorePrefix && orig.startsWith(t._options.ignorePrefix))
return t._options.trimIgnorePrefix ? `.${orig.substr(t._options.ignorePrefix.length)}` : `.${orig}`;
return t._options.trimIgnorePrefix
? `.${orig.substr(t._options.ignorePrefix.length)}`
: `.${orig}`;

if (t._classNameMap[orig] != null) id = t._classNameMap[orig]; // Use mapped class name
else id = t._classNameMap[orig] = t._idGenerator(); // Generate and map new class name
// Use already mapped class name
if (t._classNameMap[orig] != null) id = t._classNameMap[orig];
// Generate and map new class name
else id = t._classNameMap[orig] = t._idGenerator();

return `.${id}`;
};
Expand All @@ -34,7 +40,10 @@ const CssShortener = function(options) {
let result = '';
for (let i = 0; i < classCount; i++) {
// Check if class is mapped and add it to the result
result += (t._classNameMap[classes[i]] != null ? t._classNameMap[classes[i]] : classes[i]);
result +=
t._classNameMap[classes[i]] != null
? t._classNameMap[classes[i]]
: classes[i];
if (i < classCount - 1) result += ' ';
}
return `class="${result}"`;
Expand All @@ -46,16 +55,18 @@ const CssShortener = function(options) {
this.importMap = function(map, override) {
for (let orig in map) {
if (this._classNameMap[orig] != null) {
if (override === true) this._classNameMap[orig] = map[orig]; // Override mapped class name
// Override mapped class name
if (override === true) this._classNameMap[orig] = map[orig];
} else this._classNameMap[orig] = map[orig]; // Import class name
}
};

this.cssStream = function() {
return replaceStream(CLASS_NAME_REGEX, replaceCss);
};
this.replaceCss = function(css) {
return css.replace(CLASS_NAME_REGEX, replaceCss);
this.replaceCss = function(css, sourceMap) {
const replacedCss = css.replace(CLASS_NAME_REGEX, replaceCss);
return replacedCss;
};
this.htmlStream = function() {
return replaceStream(HTML_CLASS_REGEX, replaceHtml);
Expand Down

0 comments on commit d63b2c4

Please sign in to comment.