Skip to content

Commit

Permalink
Merge pull request #4 from mbrandau/babel
Browse files Browse the repository at this point in the history
Use babel
  • Loading branch information
Maximilian Brandau committed Oct 4, 2018
2 parents ff17ce2 + 1523dcd commit 96747e4
Show file tree
Hide file tree
Showing 9 changed files with 5,173 additions and 1,780 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
68 changes: 0 additions & 68 deletions index.js

This file was deleted.

42 changes: 42 additions & 0 deletions lib/idGenerator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"use strict";

// Leaving 'd' out to avoid generating the word 'ad'
var defaultAlphabet = 'abcefghijklmnopqrstuvwxyz0123456789_-';

var IdGenerator = function IdGenerator(alphabet) {
var options = {
alphabet: alphabet || defaultAlphabet,
length: 1,
index: 0
};
return function () {
var res = generateId(options);

while (/^[0-9-].*$/.test(res)) {
res = generateId(options);
}

return res;
};
};

var generateId = function generateId(options) {
var res = '';

for (var i = options.length - 1; i >= 0; i--) {
var x = Math.pow(options.alphabet.length, i);
var n = Math.floor(options.index / x);
res += options.alphabet[n % options.alphabet.length];
}

options.index++;

if (options.index > Math.pow(options.alphabet.length, options.length) - 1) {
options.length++;
options.index = 0;
}

return res;
};

module.exports = IdGenerator;
78 changes: 78 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use strict";

var replaceStream = require('replacestream');

var IdGenerator = require('./idGenerator');

var CLASS_NAME_REGEX = /\/\*[\s\S]*?\*\/|(\.[a-z_-][\w-]*)(?=[^{}]*{)/g; // https://stackoverflow.com/a/48962872/5133130

var HTML_CLASS_REGEX = /class="([\w-\s]*)"/g;

var CssShortener = function CssShortener(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;
this._idGenerator = new IdGenerator(this._options.alphabet);
this._classNameMap = {};
var t = this;

var replaceCss = function replaceCss(match, capturingGroup) {
if (!capturingGroup) return match;
var id,
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

return ".".concat(id);
};

var replaceHtml = function replaceHtml(match, capturingGroup) {
if (!capturingGroup) return match;
var classes = capturingGroup.trim().split(' ');
var classCount = classes.length;
var result = '';

for (var 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];
if (i < classCount - 1) result += ' ';
}

return "class=\"".concat(result, "\"");
};

this.getMap = function () {
return this._classNameMap;
};

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
} 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.htmlStream = function () {
return replaceStream(HTML_CLASS_REGEX, replaceHtml);
};

this.replaceHtml = function (html) {
return html.replace(HTML_CLASS_REGEX, replaceHtml);
};
};

module.exports = CssShortener;

0 comments on commit 96747e4

Please sign in to comment.